@rezkam/browser-tools 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/package.json +1 -1
- package/scripts/browser-control.mjs +15 -8
- package/scripts/cdp-event-capture.mjs +45 -26
- package/scripts/gif-recorder.mjs +51 -46
- package/scripts/har-capture.mjs +75 -35
- package/scripts/pick.mjs +12 -8
- package/scripts/record-cdp.mjs +7 -27
- package/scripts/record-har.mjs +7 -27
- package/scripts/recording-lifecycle.mjs +33 -0
- package/scripts/resource-helper.mjs +8 -10
- package/scripts/serve.mjs +6 -6
- package/scripts/tab-broker.mjs +96 -163
- package/scripts/tab-pool.mjs +195 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @rezkam/browser-tools
|
|
2
2
|
|
|
3
|
+
## 1.1.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- b8a738f: Headless launches no longer report automation or Chrome's default 800x600 screen. They disable the `AutomationControlled` Blink feature so `navigator.webdriver` is false, and present a 1920x1080 screen with a 1920x1040 window, so bot defenses that reject those headless signals treat the browser like a windowed one.
|
|
8
|
+
|
|
9
|
+
## 1.1.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 0ba174d: Fixed tab allocation, retry and shutdown races with locally owned lifecycle states. Capture failures now release their listeners without waiting for stalled body reads, GIF finalization runs once, and terminal lease messages cannot grant an empty tab. Failed disposal can retry or recover capacity after verified browser exit without overwriting a replacement browser.
|
|
14
|
+
|
|
3
15
|
## 1.1.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rezkam/browser-tools",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Sandboxed, owner-token-protected Chrome automation for AI agents: control pages, record and review GIFs, capture HAR and raw CDP events, send CDP calls, and manage Chrome profiles.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -403,10 +403,10 @@ export async function browserWSEndpoint(port = DEFAULT_PORT, timeoutMs = CONNECT
|
|
|
403
403
|
function tcpListenerOnHost(port, host, timeoutMs) {
|
|
404
404
|
return new Promise((resolvePromise) => {
|
|
405
405
|
const socket = createConnection({ host, port });
|
|
406
|
-
let
|
|
406
|
+
let state = { phase: 'connecting' };
|
|
407
407
|
const finish = (occupied) => {
|
|
408
|
-
if (settled) return;
|
|
409
|
-
|
|
408
|
+
if (state.phase === 'settled') return;
|
|
409
|
+
state = { phase: 'settled', occupied };
|
|
410
410
|
socket.destroy();
|
|
411
411
|
resolvePromise(occupied);
|
|
412
412
|
};
|
|
@@ -453,10 +453,10 @@ export async function launchedEndpointMatches({ port, endpoint, state, timeoutMs
|
|
|
453
453
|
let socket;
|
|
454
454
|
try { socket = new WebSocket(endpoint, { followRedirects: false, handshakeTimeout: timeoutMs, maxPayload: 1024 * 1024 }); }
|
|
455
455
|
catch { resolve(false); return; }
|
|
456
|
-
let
|
|
456
|
+
let probeState = { phase: 'connecting' };
|
|
457
457
|
const finish = (ok) => {
|
|
458
|
-
if (settled) return;
|
|
459
|
-
|
|
458
|
+
if (probeState.phase === 'settled') return;
|
|
459
|
+
probeState = { phase: 'settled', ok };
|
|
460
460
|
clearTimeout(timer);
|
|
461
461
|
socket.terminate();
|
|
462
462
|
resolve(ok);
|
|
@@ -464,8 +464,13 @@ export async function launchedEndpointMatches({ port, endpoint, state, timeoutMs
|
|
|
464
464
|
const timer = setTimeout(() => finish(false), timeoutMs);
|
|
465
465
|
socket.on('error', () => finish(false));
|
|
466
466
|
socket.on('close', () => finish(false));
|
|
467
|
-
socket.on('open', () =>
|
|
467
|
+
socket.on('open', () => {
|
|
468
|
+
if (probeState.phase !== 'connecting') return;
|
|
469
|
+
probeState = { phase: 'checking' };
|
|
470
|
+
socket.send(JSON.stringify({ id: 1, method: 'Browser.getBrowserCommandLine' }));
|
|
471
|
+
});
|
|
468
472
|
socket.on('message', (data) => {
|
|
473
|
+
if (probeState.phase !== 'checking') return;
|
|
469
474
|
try {
|
|
470
475
|
const message = JSON.parse(data.toString());
|
|
471
476
|
if (message.id !== 1) return;
|
|
@@ -1022,7 +1027,9 @@ export function chromeLaunchArgs({ port = DEFAULT_PORT, profileName = null, mana
|
|
|
1022
1027
|
];
|
|
1023
1028
|
// New headless runs the full browser (extensions and the copied profile still load) without opening
|
|
1024
1029
|
// a window. Old --headless is a separate lightweight engine that ignores extensions, so keep =new.
|
|
1025
|
-
|
|
1030
|
+
// Bot defenses reject navigator.webdriver and headless Chrome's default 800x600 screen, so a
|
|
1031
|
+
// headless launch hides the automation flag and reports a common desktop display.
|
|
1032
|
+
if (headless) args.push('--headless=new', '--disable-blink-features=AutomationControlled', '--window-size=1920,1040', '--screen-info={1920x1080}');
|
|
1026
1033
|
// A headless launch otherwise advertises a "HeadlessChrome" User-Agent. When the copied profile is
|
|
1027
1034
|
// signed in to Google, that headless fingerprint trips Google's session-theft protection and logs the
|
|
1028
1035
|
// source profile out, while a windowed launch (normal Chrome UA) does not. Present the normal UA so a
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { hasFlag, parsePositiveIntegerOption, sleep } from './browser-control.mjs';
|
|
2
|
+
import { createRecordingLifecycle } from './recording-lifecycle.mjs';
|
|
2
3
|
import {
|
|
3
4
|
matchesPatterns,
|
|
4
5
|
optionValues,
|
|
@@ -100,40 +101,58 @@ export async function runCdpEventCapture({
|
|
|
100
101
|
writeEvent,
|
|
101
102
|
onPhase = () => {},
|
|
102
103
|
}) {
|
|
104
|
+
const lifecycle = createRecordingLifecycle('cdp');
|
|
103
105
|
let eventCount = 0;
|
|
104
106
|
const startedMonotonic = performance.now();
|
|
105
107
|
const onAnyEvent = (method, params) => {
|
|
106
|
-
if (eventCount >= options.maxEvents) return;
|
|
108
|
+
if (['completed', 'failed'].includes(lifecycle.phase) || eventCount >= options.maxEvents) return;
|
|
107
109
|
if (typeof method !== 'string') return;
|
|
108
110
|
if (!matchesPatterns(method, options.eventPatterns, options.excludedEventPatterns)) return;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
111
|
+
try {
|
|
112
|
+
writeEvent({
|
|
113
|
+
timestamp: new Date().toISOString(),
|
|
114
|
+
elapsed_ms: Number((performance.now() - startedMonotonic).toFixed(3)),
|
|
115
|
+
method,
|
|
116
|
+
params: sanitizeCdpEvent(method, params, options),
|
|
117
|
+
});
|
|
118
|
+
eventCount += 1;
|
|
119
|
+
} catch (error) {
|
|
120
|
+
lifecycle.transition('failed', { error });
|
|
121
|
+
}
|
|
116
122
|
};
|
|
117
123
|
session.on('*', onAnyEvent);
|
|
118
124
|
|
|
119
|
-
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
+
const assertHealthy = () => {
|
|
126
|
+
if (lifecycle.phase === 'failed') throw lifecycle.snapshot().error;
|
|
127
|
+
};
|
|
128
|
+
try {
|
|
129
|
+
for (const domain of options.domains) {
|
|
130
|
+
if (options.skippedDomainEnables.includes(domain)) continue;
|
|
131
|
+
try {
|
|
132
|
+
await session.send(`${domain}.enable`);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
throw new Error(`Could not enable CDP domain ${domain}: ${error.message}. Use --skip-enable ${domain} and --setup when the domain needs a different activation method.`);
|
|
135
|
+
}
|
|
125
136
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
await onPhase('recording');
|
|
137
|
+
for (const command of options.setupCommands) await session.send(command.method, command.params);
|
|
138
|
+
assertHealthy();
|
|
139
|
+
lifecycle.transition('recording');
|
|
140
|
+
await onPhase('recording');
|
|
131
141
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
142
|
+
const deadline = Date.now() + options.maxDurationSeconds * 1000;
|
|
143
|
+
while (lifecycle.phase === 'recording' && !shouldStop() && Date.now() < deadline && eventCount < options.maxEvents) await sleep(100);
|
|
144
|
+
assertHealthy();
|
|
145
|
+
const reason = shouldStop() ? 'requested' : (eventCount >= options.maxEvents ? 'max-events' : 'max-duration');
|
|
146
|
+
lifecycle.transition('post-wait', { reason });
|
|
147
|
+
await onPhase('post-wait', reason);
|
|
148
|
+
await sleep(options.postWaitMs);
|
|
149
|
+
assertHealthy();
|
|
150
|
+
lifecycle.transition('completed');
|
|
151
|
+
return { reason, eventCount };
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if (lifecycle.phase !== 'failed') lifecycle.transition('failed', { error });
|
|
154
|
+
throw error;
|
|
155
|
+
} finally {
|
|
156
|
+
session.off('*', onAnyEvent);
|
|
157
|
+
}
|
|
139
158
|
}
|
package/scripts/gif-recorder.mjs
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
sleep,
|
|
23
23
|
} from './browser-control.mjs';
|
|
24
24
|
import { openPrivateFile } from './cdp-common.mjs';
|
|
25
|
+
import { createRecordingLifecycle } from './recording-lifecycle.mjs';
|
|
25
26
|
|
|
26
27
|
export const DEFAULT_GIF_FPS = 10;
|
|
27
28
|
export const DEFAULT_GIF_COLORS = 128;
|
|
@@ -220,22 +221,31 @@ export function createFfmpegGifEncoder({
|
|
|
220
221
|
output,
|
|
221
222
|
], { stdio: ['pipe', 'ignore', 'pipe'] });
|
|
222
223
|
|
|
223
|
-
let
|
|
224
|
+
let state = { phase: 'encoding' };
|
|
224
225
|
let stderr = '';
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
226
|
+
const fail = (error) => { if (state.phase !== 'failed') state = { phase: 'failed', error }; };
|
|
227
|
+
child.once('error', fail);
|
|
228
|
+
child.stdin.on('error', fail);
|
|
228
229
|
child.stderr.on('data', (chunk) => {
|
|
229
230
|
stderr += chunk.toString('utf-8');
|
|
230
231
|
});
|
|
231
232
|
const closed = new Promise((resolve) => {
|
|
232
|
-
child.once('close', (code, signal) =>
|
|
233
|
+
child.once('close', (code, signal) => {
|
|
234
|
+
if (state.phase !== 'failed') {
|
|
235
|
+
if (code === 0) state = { phase: 'closed' };
|
|
236
|
+
else {
|
|
237
|
+
const termination = signal ? `signal ${signal}` : `exit status ${code}`;
|
|
238
|
+
fail(new Error(`ffmpeg failed to encode GIF (${termination})${stderr.trim() ? `: ${stderr.trim()}` : ''}`));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
resolve();
|
|
242
|
+
});
|
|
233
243
|
});
|
|
234
244
|
|
|
235
245
|
return {
|
|
236
246
|
async writeFrame(frame) {
|
|
237
|
-
if (
|
|
238
|
-
if (
|
|
247
|
+
if (state.phase === 'failed') throw state.error;
|
|
248
|
+
if (state.phase !== 'encoding') {
|
|
239
249
|
throw new Error(`ffmpeg exited before GIF recording finished${stderr.trim() ? `: ${stderr.trim()}` : ''}`);
|
|
240
250
|
}
|
|
241
251
|
await new Promise((resolveWrite, rejectWrite) => {
|
|
@@ -243,13 +253,10 @@ export function createFfmpegGifEncoder({
|
|
|
243
253
|
});
|
|
244
254
|
},
|
|
245
255
|
async stop() {
|
|
246
|
-
if (
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
if (
|
|
250
|
-
const termination = result.signal ? `signal ${result.signal}` : `exit status ${result.code}`;
|
|
251
|
-
throw new Error(`ffmpeg failed to encode GIF (${termination})${stderr.trim() ? `: ${stderr.trim()}` : ''}`);
|
|
252
|
-
}
|
|
256
|
+
if (state.phase === 'encoding') state = { phase: 'finishing' };
|
|
257
|
+
if (!child.stdin.destroyed && !child.stdin.writableEnded) child.stdin.end();
|
|
258
|
+
await closed;
|
|
259
|
+
if (state.phase === 'failed') throw state.error;
|
|
253
260
|
chmodSync(output, 0o600);
|
|
254
261
|
},
|
|
255
262
|
};
|
|
@@ -306,6 +313,24 @@ export async function recordGifSession({
|
|
|
306
313
|
colors,
|
|
307
314
|
scale,
|
|
308
315
|
});
|
|
316
|
+
const lifecycle = createRecordingLifecycle('gif');
|
|
317
|
+
// Finalization belongs to this session, including when finalization itself fails.
|
|
318
|
+
let encoderState = { phase: 'open' };
|
|
319
|
+
const finalize = () => {
|
|
320
|
+
if (encoderState.phase === 'open') {
|
|
321
|
+
const result = Promise.resolve().then(() => encoder.stop());
|
|
322
|
+
encoderState = { phase: 'closing', result };
|
|
323
|
+
result.then(
|
|
324
|
+
() => { encoderState = { phase: 'closed', result }; },
|
|
325
|
+
(error) => { encoderState = { phase: 'failed', result, error }; },
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
return encoderState.result;
|
|
329
|
+
};
|
|
330
|
+
const phase = async (next, reason) => {
|
|
331
|
+
lifecycle.transition(next, reason ? { reason } : {});
|
|
332
|
+
await onPhase(next, reason);
|
|
333
|
+
};
|
|
309
334
|
const frameIntervalMs = 1000 / fps;
|
|
310
335
|
let nextFrameAt = now();
|
|
311
336
|
let frameCount = 0;
|
|
@@ -334,9 +359,9 @@ export async function recordGifSession({
|
|
|
334
359
|
};
|
|
335
360
|
|
|
336
361
|
try {
|
|
337
|
-
await
|
|
362
|
+
await phase('pre-roll');
|
|
338
363
|
await capturePeriod(preRollMs);
|
|
339
|
-
await
|
|
364
|
+
await phase('recording');
|
|
340
365
|
|
|
341
366
|
const deadline = now() + maxDurationSeconds * 1000;
|
|
342
367
|
while (!shouldStop() && now() < deadline) {
|
|
@@ -345,12 +370,14 @@ export async function recordGifSession({
|
|
|
345
370
|
|
|
346
371
|
const stopRequested = shouldStop();
|
|
347
372
|
const reason = stopRequested ? 'requested' : 'max-duration';
|
|
348
|
-
await
|
|
373
|
+
await phase('post-roll', reason);
|
|
349
374
|
await capturePeriod(postRollMs);
|
|
350
|
-
await
|
|
375
|
+
await finalize();
|
|
376
|
+
lifecycle.transition('completed');
|
|
351
377
|
return { reason, frameCount };
|
|
352
378
|
} catch (error) {
|
|
353
|
-
|
|
379
|
+
lifecycle.transition('failed', { error });
|
|
380
|
+
await finalize().catch(() => {});
|
|
354
381
|
throw error;
|
|
355
382
|
}
|
|
356
383
|
}
|
|
@@ -369,25 +396,18 @@ export async function runGifRecordingWorker({
|
|
|
369
396
|
const normalizedPort = normalizePort(port);
|
|
370
397
|
const stateFile = gifRecordingStateFile(normalizedPort);
|
|
371
398
|
const startedAt = new Date().toISOString();
|
|
372
|
-
const
|
|
399
|
+
const metadata = {
|
|
373
400
|
managedBy: 'browser-tools-gif-recorder',
|
|
374
401
|
pid: process.pid,
|
|
375
402
|
port: normalizedPort,
|
|
376
403
|
output,
|
|
377
404
|
ownerTokenHash: ownerTokenHash(ownerToken),
|
|
378
|
-
status: 'starting',
|
|
379
|
-
phase: 'starting',
|
|
380
405
|
startedAt,
|
|
381
406
|
options: { fps, colors, scale, preRollMs, postRollMs, maxDurationSeconds },
|
|
382
407
|
};
|
|
383
|
-
writePrivateJson(stateFile,
|
|
408
|
+
const lifecycle = createRecordingLifecycle('gif', metadata, (state) => writePrivateJson(stateFile, state));
|
|
384
409
|
|
|
385
410
|
let browser;
|
|
386
|
-
let currentState = baseState;
|
|
387
|
-
const updateState = (changes) => {
|
|
388
|
-
currentState = { ...currentState, ...changes };
|
|
389
|
-
writePrivateJson(stateFile, currentState);
|
|
390
|
-
};
|
|
391
411
|
|
|
392
412
|
try {
|
|
393
413
|
browser = await connectBrowser(normalizedPort, { ownerToken });
|
|
@@ -402,26 +422,11 @@ export async function runGifRecordingWorker({
|
|
|
402
422
|
postRollMs,
|
|
403
423
|
maxDurationSeconds,
|
|
404
424
|
shouldStop: () => existsSync(gifRecordingStopFile(normalizedPort)),
|
|
405
|
-
onPhase: (phase, reason
|
|
406
|
-
status: phase === 'recording' ? 'recording' : currentState.status,
|
|
407
|
-
phase,
|
|
408
|
-
readyAt: phase === 'recording' ? new Date().toISOString() : currentState.readyAt,
|
|
409
|
-
reason: reason ?? currentState.reason,
|
|
410
|
-
}),
|
|
411
|
-
});
|
|
412
|
-
updateState({
|
|
413
|
-
status: 'completed',
|
|
414
|
-
phase: 'completed',
|
|
415
|
-
reason: result.reason,
|
|
416
|
-
completedAt: new Date().toISOString(),
|
|
425
|
+
onPhase: (phase, reason) => lifecycle.transition(phase, reason ? { reason } : {}),
|
|
417
426
|
});
|
|
427
|
+
lifecycle.transition('completed', { reason: result.reason });
|
|
418
428
|
} catch (error) {
|
|
419
|
-
|
|
420
|
-
status: 'failed',
|
|
421
|
-
phase: 'failed',
|
|
422
|
-
error: error.message,
|
|
423
|
-
completedAt: new Date().toISOString(),
|
|
424
|
-
});
|
|
429
|
+
lifecycle.transition('failed', { error: error.message });
|
|
425
430
|
throw error;
|
|
426
431
|
} finally {
|
|
427
432
|
browser?.disconnect();
|
package/scripts/har-capture.mjs
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
writePrivateJson,
|
|
22
22
|
} from './cdp-common.mjs';
|
|
23
23
|
import { parseBoundedNumber } from './gif-recorder.mjs';
|
|
24
|
+
import { createRecordingLifecycle } from './recording-lifecycle.mjs';
|
|
24
25
|
|
|
25
26
|
const { version: BROWSER_TOOLS_VERSION } = createRequire(import.meta.url)('../package.json');
|
|
26
27
|
|
|
@@ -161,6 +162,10 @@ export function parseHarCaptureOptions(args) {
|
|
|
161
162
|
};
|
|
162
163
|
}
|
|
163
164
|
|
|
165
|
+
function requestInFlight(entry) {
|
|
166
|
+
return entry && ['requesting', 'responding'].includes(entry.state);
|
|
167
|
+
}
|
|
168
|
+
|
|
164
169
|
export class HarCollector {
|
|
165
170
|
constructor(options, { pageId = 'page_0', pageUrl = '', pageTitle = '' } = {}) {
|
|
166
171
|
this.options = options;
|
|
@@ -175,7 +180,6 @@ export class HarCollector {
|
|
|
175
180
|
this.responseExtraIndexes = new Map();
|
|
176
181
|
this.pendingRequestExtra = new Map();
|
|
177
182
|
this.pendingResponseExtra = new Map();
|
|
178
|
-
this.activeRequestIds = new Set();
|
|
179
183
|
this.lastActivityAt = Date.now();
|
|
180
184
|
}
|
|
181
185
|
|
|
@@ -204,7 +208,8 @@ export class HarCollector {
|
|
|
204
208
|
this.requestIndexes.set(event.requestId, entryIndex + 1);
|
|
205
209
|
if (event.redirectResponse) {
|
|
206
210
|
const redirected = this.currentByRequestId.get(event.requestId);
|
|
207
|
-
if (redirected) {
|
|
211
|
+
if (requestInFlight(redirected)) {
|
|
212
|
+
redirected.state = 'redirected';
|
|
208
213
|
redirected.response = event.redirectResponse;
|
|
209
214
|
redirected.responseTimestamp = event.timestamp;
|
|
210
215
|
redirected.finishedTimestamp = event.timestamp;
|
|
@@ -214,12 +219,12 @@ export class HarCollector {
|
|
|
214
219
|
|
|
215
220
|
if (!this.requestMatches(event)) {
|
|
216
221
|
this.currentByRequestId.delete(event.requestId);
|
|
217
|
-
this.activeRequestIds.delete(event.requestId);
|
|
218
222
|
return;
|
|
219
223
|
}
|
|
220
224
|
|
|
221
225
|
const requestEntries = this.entriesByRequestId.get(event.requestId) || [];
|
|
222
226
|
const entry = {
|
|
227
|
+
state: 'requesting',
|
|
223
228
|
requestId: event.requestId,
|
|
224
229
|
request: event.request,
|
|
225
230
|
resourceType: event.type || 'Other',
|
|
@@ -240,7 +245,6 @@ export class HarCollector {
|
|
|
240
245
|
this.entriesByRequestId.set(event.requestId, requestEntries);
|
|
241
246
|
this.entries.push(entry);
|
|
242
247
|
this.currentByRequestId.set(event.requestId, entry);
|
|
243
|
-
this.activeRequestIds.add(event.requestId);
|
|
244
248
|
}
|
|
245
249
|
|
|
246
250
|
requestWillBeSentExtraInfo(event) {
|
|
@@ -270,7 +274,8 @@ export class HarCollector {
|
|
|
270
274
|
responseReceived(event) {
|
|
271
275
|
this.lastActivityAt = Date.now();
|
|
272
276
|
const entry = this.currentByRequestId.get(event.requestId);
|
|
273
|
-
if (!entry) return;
|
|
277
|
+
if (!requestInFlight(entry)) return;
|
|
278
|
+
entry.state = 'responding';
|
|
274
279
|
entry.response = event.response;
|
|
275
280
|
entry.responseTimestamp = event.timestamp;
|
|
276
281
|
if (event.type) entry.resourceType = event.type;
|
|
@@ -284,8 +289,8 @@ export class HarCollector {
|
|
|
284
289
|
async loadingFinished(event, session) {
|
|
285
290
|
this.lastActivityAt = Date.now();
|
|
286
291
|
const entry = this.currentByRequestId.get(event.requestId);
|
|
287
|
-
|
|
288
|
-
|
|
292
|
+
if (!requestInFlight(entry)) return;
|
|
293
|
+
entry.state = 'completed';
|
|
289
294
|
entry.finishedTimestamp = event.timestamp;
|
|
290
295
|
entry.encodedDataLength = Number(event.encodedDataLength || 0);
|
|
291
296
|
if (!this.options.capture.includes('bodies') || !this.responseMatches(entry)) return;
|
|
@@ -299,8 +304,8 @@ export class HarCollector {
|
|
|
299
304
|
loadingFailed(event) {
|
|
300
305
|
this.lastActivityAt = Date.now();
|
|
301
306
|
const entry = this.currentByRequestId.get(event.requestId);
|
|
302
|
-
|
|
303
|
-
|
|
307
|
+
if (!requestInFlight(entry)) return;
|
|
308
|
+
entry.state = 'failed';
|
|
304
309
|
entry.finishedTimestamp = event.timestamp;
|
|
305
310
|
entry.failed = {
|
|
306
311
|
errorText: event.errorText,
|
|
@@ -312,7 +317,8 @@ export class HarCollector {
|
|
|
312
317
|
async waitForDrain({ idleMs = this.options.idleMs, timeoutMs = this.options.drainTimeoutMs } = {}) {
|
|
313
318
|
const deadline = Date.now() + timeoutMs;
|
|
314
319
|
while (Date.now() < deadline) {
|
|
315
|
-
|
|
320
|
+
const active = [...this.currentByRequestId.values()].some(requestInFlight);
|
|
321
|
+
if (!active && Date.now() - this.lastActivityAt >= idleMs) return true;
|
|
316
322
|
await sleep(50);
|
|
317
323
|
}
|
|
318
324
|
return false;
|
|
@@ -450,34 +456,68 @@ export async function runHarCapture({
|
|
|
450
456
|
pageUrl: page.url(),
|
|
451
457
|
pageTitle: await page.title().catch(() => ''),
|
|
452
458
|
});
|
|
459
|
+
const lifecycle = createRecordingLifecycle('har');
|
|
453
460
|
const pending = new Set();
|
|
454
|
-
const
|
|
455
|
-
|
|
456
|
-
|
|
461
|
+
const fail = (error) => {
|
|
462
|
+
if (lifecycle.phase !== 'failed') lifecycle.transition('failed', { error });
|
|
463
|
+
};
|
|
464
|
+
const assertHealthy = () => {
|
|
465
|
+
if (lifecycle.phase === 'failed') throw lifecycle.snapshot().error;
|
|
457
466
|
};
|
|
467
|
+
const handlers = new Map();
|
|
468
|
+
const subscribe = (method, handle) => {
|
|
469
|
+
const listener = (event) => {
|
|
470
|
+
if (['completed', 'failed'].includes(lifecycle.phase)) return;
|
|
471
|
+
try {
|
|
472
|
+
const task = Promise.resolve(handle(event));
|
|
473
|
+
pending.add(task);
|
|
474
|
+
task.then(() => pending.delete(task), (error) => { pending.delete(task); fail(error); });
|
|
475
|
+
} catch (error) {
|
|
476
|
+
fail(error);
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
handlers.set(method, listener);
|
|
480
|
+
session.on(method, listener);
|
|
481
|
+
};
|
|
482
|
+
subscribe('Network.requestWillBeSent', (event) => collector.requestWillBeSent(event));
|
|
483
|
+
subscribe('Network.requestWillBeSentExtraInfo', (event) => collector.requestWillBeSentExtraInfo(event));
|
|
484
|
+
subscribe('Network.responseReceived', (event) => collector.responseReceived(event));
|
|
485
|
+
subscribe('Network.responseReceivedExtraInfo', (event) => collector.responseReceivedExtraInfo(event));
|
|
486
|
+
subscribe('Network.requestServedFromCache', (event) => collector.requestServedFromCache(event));
|
|
487
|
+
subscribe('Network.loadingFinished', (event) => collector.loadingFinished(event, session));
|
|
488
|
+
subscribe('Network.loadingFailed', (event) => collector.loadingFailed(event));
|
|
458
489
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
490
|
+
try {
|
|
491
|
+
await session.send('Network.enable', {
|
|
492
|
+
maxTotalBufferSize: Math.max(options.maxBodyBytes * 4, 4 * 1024 * 1024),
|
|
493
|
+
maxResourceBufferSize: options.maxBodyBytes,
|
|
494
|
+
maxPostDataSize: options.maxBodyBytes,
|
|
495
|
+
});
|
|
496
|
+
assertHealthy();
|
|
497
|
+
lifecycle.transition('recording');
|
|
498
|
+
await onPhase('recording');
|
|
499
|
+
|
|
500
|
+
const deadline = Date.now() + options.maxDurationSeconds * 1000;
|
|
501
|
+
while (lifecycle.phase === 'recording' && !shouldStop() && Date.now() < deadline) await sleep(100);
|
|
502
|
+
assertHealthy();
|
|
503
|
+
const reason = shouldStop() ? 'requested' : 'max-duration';
|
|
504
|
+
lifecycle.transition('draining', { reason });
|
|
505
|
+
await onPhase('draining', reason);
|
|
506
|
+
await collector.waitForDrain();
|
|
507
|
+
// Freeze the event stream before awaiting body reads, so the final set cannot grow.
|
|
508
|
+
for (const [method, handler] of handlers) session.off(method, handler);
|
|
509
|
+
await Promise.allSettled([...pending]);
|
|
510
|
+
assertHealthy();
|
|
511
|
+
const har = collector.build();
|
|
512
|
+
lifecycle.transition('completed');
|
|
513
|
+
return { reason, har, eventCount: collector.entries.length };
|
|
514
|
+
} catch (error) {
|
|
515
|
+
fail(error);
|
|
516
|
+
throw error;
|
|
517
|
+
} finally {
|
|
518
|
+
for (const [method, handler] of handlers) session.off(method, handler);
|
|
519
|
+
// On failure, let the session owner detach before waiting for stalled body reads.
|
|
520
|
+
}
|
|
481
521
|
}
|
|
482
522
|
|
|
483
523
|
export function writeHar(file, har) {
|
package/scripts/pick.mjs
CHANGED
|
@@ -97,7 +97,8 @@ export function installPicker() {
|
|
|
97
97
|
new Promise((resolve, reject) => {
|
|
98
98
|
const selections = [];
|
|
99
99
|
const selectedEls = new Set();
|
|
100
|
-
let
|
|
100
|
+
let state = 'selecting';
|
|
101
|
+
const active = () => state === 'selecting' || state === 'multi-selecting';
|
|
101
102
|
|
|
102
103
|
const overlay = document.createElement('div');
|
|
103
104
|
overlay.style.cssText =
|
|
@@ -130,20 +131,21 @@ export function installPicker() {
|
|
|
130
131
|
};
|
|
131
132
|
|
|
132
133
|
const finish = (result) => {
|
|
133
|
-
if (
|
|
134
|
-
|
|
134
|
+
if (!active()) return;
|
|
135
|
+
state = result === null ? 'cancelled' : 'completed';
|
|
135
136
|
cleanup();
|
|
136
137
|
resolve(result);
|
|
137
138
|
};
|
|
138
139
|
|
|
139
140
|
const fail = (error) => {
|
|
140
|
-
if (
|
|
141
|
-
|
|
141
|
+
if (!active()) return;
|
|
142
|
+
state = 'failed';
|
|
142
143
|
cleanup();
|
|
143
144
|
reject(new Error(`Picker failed to extract element info: ${errorMessage(error)}`));
|
|
144
145
|
};
|
|
145
146
|
|
|
146
147
|
const onMove = (e) => {
|
|
148
|
+
if (!active()) return;
|
|
147
149
|
const el = document.elementFromPoint(e.clientX, e.clientY);
|
|
148
150
|
if (!el || overlay.contains(el) || banner.contains(el)) return;
|
|
149
151
|
const r = el.getBoundingClientRect();
|
|
@@ -156,7 +158,7 @@ export function installPicker() {
|
|
|
156
158
|
};
|
|
157
159
|
|
|
158
160
|
const onClick = (e) => {
|
|
159
|
-
if (banner.contains(e.target)) return;
|
|
161
|
+
if (!active() || banner.contains(e.target)) return;
|
|
160
162
|
e.preventDefault();
|
|
161
163
|
e.stopPropagation();
|
|
162
164
|
const el = document.elementFromPoint(e.clientX, e.clientY);
|
|
@@ -168,10 +170,11 @@ export function installPicker() {
|
|
|
168
170
|
selectedEls.add(el);
|
|
169
171
|
el.style.outline = '3px solid #10b981';
|
|
170
172
|
selections.push(info(el));
|
|
173
|
+
state = 'multi-selecting';
|
|
171
174
|
refresh();
|
|
172
175
|
}
|
|
173
176
|
} else {
|
|
174
|
-
finish(
|
|
177
|
+
finish(state === 'multi-selecting' ? selections : info(el));
|
|
175
178
|
}
|
|
176
179
|
} catch (error) {
|
|
177
180
|
fail(error);
|
|
@@ -179,11 +182,12 @@ export function installPicker() {
|
|
|
179
182
|
};
|
|
180
183
|
|
|
181
184
|
const onKey = (e) => {
|
|
185
|
+
if (!active()) return;
|
|
182
186
|
if (e.key === 'Escape') {
|
|
183
187
|
e.preventDefault();
|
|
184
188
|
finish(null);
|
|
185
189
|
}
|
|
186
|
-
if (e.key === 'Enter' &&
|
|
190
|
+
if (e.key === 'Enter' && state === 'multi-selecting') {
|
|
187
191
|
e.preventDefault();
|
|
188
192
|
finish(selections);
|
|
189
193
|
}
|