-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.js
More file actions
executable file
·408 lines (340 loc) · 12.6 KB
/
daemon.js
File metadata and controls
executable file
·408 lines (340 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env bun
const { chromium } = require('playwright');
const net = require('net');
const fs = require('fs');
const {
SOCKET_PATH,
PROFILE_PATH,
BROWSER_PATH,
IDLE_TIMEOUT,
NAV_TIMEOUT,
CACHE_MAX,
MAX_CONCURRENT,
CMD_STOP,
CMD_STATUS,
BLOCKED_RESOURCE_RE,
TRACKER_RE
} = require('./lib/constants');
const DEBUG = process.env.DEBUG === '1';
// Headless resolution:
// HEADLESS=false → headed
// HEADLESS=true → headless
// (unset) + DEBUG → headed (backward compat)
// (unset) → headless (default)
const IS_HEADLESS = process.env.HEADLESS !== undefined
? process.env.HEADLESS !== 'false'
: !DEBUG;
// ═══════════════════════════════════════════════════════════════
// STATE
// ═══════════════════════════════════════════════════════════════
let browserContext = null;
let browserLaunching = null;
let idleTimer = null;
let server = null;
const cache = new Map();
const startTime = Date.now();
// Concurrency & Pooling
let activeTabs = 0;
const waitQueue = [];
const pagePool = [];
const MAX_POOL_SIZE = MAX_CONCURRENT;
function log(msg) {
if (DEBUG) console.log(`[${new Date().toISOString().slice(11, 23)}] ${msg}`);
}
function resetIdleTimer() {
if (idleTimer) clearTimeout(idleTimer);
idleTimer = setTimeout(shutdown, IDLE_TIMEOUT);
}
// ═══════════════════════════════════════════════════════════════
// CONCURRENCY CONTROL
// ═══════════════════════════════════════════════════════════════
function acquireSlot() {
return new Promise(resolve => {
if (activeTabs < MAX_CONCURRENT) {
activeTabs++;
resolve();
} else {
waitQueue.push(resolve);
}
});
}
function releaseSlot() {
if (waitQueue.length > 0) {
// Transfer slot directly to next waiter
const next = waitQueue.shift();
next();
} else {
activeTabs--;
}
}
// ═══════════════════════════════════════════════════════════════
// BROWSER
// ═══════════════════════════════════════════════════════════════
async function initContext() {
if (browserContext) return browserContext;
if (browserLaunching) return browserLaunching;
browserLaunching = (async () => {
if (!fs.existsSync(PROFILE_PATH)) {
throw new Error('Profile not found. Run: bun run setup');
}
log('Launching browser...');
const args = [
'--disable-blink-features=AutomationControlled',
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-extensions',
'--disable-component-extensions-with-background-pages',
'--disable-default-apps',
'--disable-sync',
'--disable-translate',
'--disable-background-networking',
'--disable-backgrounding-occluded-windows',
'--no-first-run',
'--disable-notifications',
'--lang=en-US'
];
try {
browserContext = await chromium.launchPersistentContext(PROFILE_PATH, {
executablePath: BROWSER_PATH,
headless: IS_HEADLESS,
args,
viewport: { width: 1280, height: 800 },
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
});
log(`Browser launched (headless: ${IS_HEADLESS})`);
// Block resources at context level (applies to all pages)
await browserContext.route(BLOCKED_RESOURCE_RE, r => r.abort());
await browserContext.route(TRACKER_RE, r => r.abort());
// Handle user manually closing the browser context
browserContext.on('close', () => {
log('Browser context closed');
browserContext = null;
browserLaunching = null;
pagePool.length = 0;
});
// Grant clipboard permissions for Google
await browserContext.grantPermissions(
['clipboard-read', 'clipboard-write'],
{ origin: 'https://www.google.com' }
);
// Stealth + clipboard intercept at context level
await browserContext.addInitScript(() => {
Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
window.chrome = { runtime: {} };
// Intercept clipboard writes to capture copy button output
window.__copiedText = null;
if (navigator.clipboard) {
const origWriteText = navigator.clipboard.writeText?.bind(navigator.clipboard);
const origWrite = navigator.clipboard.write?.bind(navigator.clipboard);
if (origWriteText) {
navigator.clipboard.writeText = async (text) => {
window.__copiedText = text;
return origWriteText(text).catch(() => {});
};
}
if (origWrite) {
navigator.clipboard.write = async (items) => {
try {
for (const item of items) {
if (item.types.includes('text/plain')) {
const blob = await item.getType('text/plain');
window.__copiedText = await blob.text();
}
}
} catch {}
return origWrite(items).catch(() => {});
};
}
}
});
// Close old tabs from previous sessions to ensure a fresh window
const pages = browserContext.pages();
// Keep only one page. Close the rest.
while (pages.length > 1) {
const p = pages.pop();
await p.close().catch(() => {});
}
// Put the single remaining initial page into the pool so it gets reused
for (const p of browserContext.pages()) {
if (pagePool.length < MAX_POOL_SIZE) {
pagePool.push(p);
} else {
await p.close().catch(() => {});
}
}
} catch (e) {
browserLaunching = null;
if (e.message.includes('SingletonLock')) {
throw new Error('Profile locked. Run: ask --stop or pkill -f chromium');
}
throw e;
}
return browserContext;
})();
return browserLaunching;
}
async function getPage() {
while (pagePool.length > 0) {
const page = pagePool.pop();
if (!page.isClosed()) {
log(`Reusing page from pool (size: ${pagePool.length})`);
return page;
}
}
const ctx = await initContext();
// Try to use a pre-existing page from context creation if available
if (pagePool.length > 0) {
const page = pagePool.pop();
if (!page.isClosed()) {
log(`Reusing default page from context (pool size: ${pagePool.length})`);
return page;
}
}
return await ctx.newPage();
}
// ═══════════════════════════════════════════════════════════════
// SEARCH
// ═══════════════════════════════════════════════════════════════
async function search(query) {
const cacheKey = query.toLowerCase().trim();
const t0 = Date.now();
// Cache check (before acquiring a tab slot)
if (cache.has(cacheKey)) {
log(`Cache hit: "${query}"`);
const cached = cache.get(cacheKey);
// LRU refresh
cache.delete(cacheKey);
cache.set(cacheKey, cached);
return { markdown: cached, fromCache: true, timeMs: Date.now() - t0 };
}
await acquireSlot();
let page = null;
try {
page = await getPage();
// Navigate
await page.goto(`https://www.google.com/search?udm=50&q=${encodeURIComponent(query)}`, {
waitUntil: 'domcontentloaded',
timeout: NAV_TIMEOUT
});
// CAPTCHA check
if (await page.$('form[action*="Captcha"], #captcha-form, #recaptcha')) {
throw new Error("CAPTCHA detected. Run 'bun run setup' to solve.");
}
// Wait for AI generation to complete (copy button appears when done)
await page.waitForSelector('button[aria-label="Copy text"]', { timeout: NAV_TIMEOUT });
// Reset intercepted text, click copy, read captured text
await page.evaluate(() => { window.__copiedText = null; });
await page.click('button[aria-label="Copy text"]');
await page.waitForFunction(() => window.__copiedText !== null, { timeout: 5000 });
const markdown = await page.evaluate(() => window.__copiedText);
if (!markdown) {
throw new Error('No content copied from page.');
}
// Cache (LRU eviction)
cache.set(cacheKey, markdown);
if (cache.size > CACHE_MAX) {
const firstKey = cache.keys().next().value;
cache.delete(firstKey);
}
const timeMs = Date.now() - t0;
log(`"${query}" done in ${timeMs}ms`);
return { markdown, fromCache: false, timeMs };
} catch (err) {
log(`Error: ${err.message}`);
return { error: err.message, timeMs: Date.now() - t0 };
} finally {
if (page) {
if (pagePool.length < MAX_POOL_SIZE) {
log('Returning page to pool');
pagePool.push(page);
} else {
await page.close().catch(() => { });
}
}
releaseSlot();
}
}
// ═══════════════════════════════════════════════════════════════
// REQUEST HANDLER
// ═══════════════════════════════════════════════════════════════
async function handleRequest(data) {
resetIdleTimer();
if (!data || typeof data.query !== 'string') {
return { error: 'Invalid request' };
}
const q = data.query;
if (q === CMD_STOP) {
setImmediate(shutdown);
return { stopped: true };
}
if (q === CMD_STATUS) {
return {
status: 'running',
uptime: Math.floor((Date.now() - startTime) / 1000),
cacheSize: cache.size,
browser: browserContext ? 'connected' : 'initializing',
headless: IS_HEADLESS
};
}
return await search(q);
}
// ═══════════════════════════════════════════════════════════════
// SERVER
// ═══════════════════════════════════════════════════════════════
const cleanupSocket = () => { try { fs.unlinkSync(SOCKET_PATH); } catch { } };
function startServer() {
cleanupSocket();
server = net.createServer(socket => {
let buffer = '';
let processing = false;
socket.on('data', async chunk => {
buffer += chunk;
if (processing) return;
const idx = buffer.indexOf('\n');
if (idx === -1) return;
processing = true;
const msg = buffer.slice(0, idx);
buffer = '';
try {
const req = JSON.parse(msg);
const res = await handleRequest(req);
socket.write(JSON.stringify(res) + '\n');
} catch {
socket.write(JSON.stringify({ error: 'Internal error' }) + '\n');
}
socket.end();
});
socket.on('error', () => { });
});
server.listen(SOCKET_PATH, () => {
log(`Listening on ${SOCKET_PATH} (headless: ${IS_HEADLESS})`);
resetIdleTimer();
initContext().then(() => {
log('Page pre-warmed');
}).catch(e => log(`Warmup: ${e.message}`));
});
server.on('error', e => {
console.error(`Server error: ${e.message}`);
process.exit(1);
});
}
async function shutdown() {
log('Shutting down...');
if (idleTimer) clearTimeout(idleTimer);
if (browserContext) await browserContext.close().catch(() => { });
if (server) server.close();
cleanupSocket();
process.exit(0);
}
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
process.on('uncaughtException', async e => {
console.error('Fatal:', e);
try { if (browserContext) await browserContext.close().catch(() => { }); } catch { }
try { if (server) server.close(); } catch { }
cleanupSocket();
process.exit(1);
});
startServer();