@rezkam/browser-tools 1.1.0 → 1.1.1

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @rezkam/browser-tools
2
2
 
3
+ ## 1.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 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.
8
+
3
9
  ## 1.1.0
4
10
 
5
11
  ### Minor Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rezkam/browser-tools",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
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 settled = false;
406
+ let state = { phase: 'connecting' };
407
407
  const finish = (occupied) => {
408
- if (settled) return;
409
- settled = true;
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 settled = false;
456
+ let probeState = { phase: 'connecting' };
457
457
  const finish = (ok) => {
458
- if (settled) return;
459
- settled = true;
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', () => socket.send(JSON.stringify({ id: 1, method: 'Browser.getBrowserCommandLine' })));
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;
@@ -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
- writeEvent({
110
- timestamp: new Date().toISOString(),
111
- elapsed_ms: Number((performance.now() - startedMonotonic).toFixed(3)),
112
- method,
113
- params: sanitizeCdpEvent(method, params, options),
114
- });
115
- eventCount += 1;
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
- for (const domain of options.domains) {
120
- if (options.skippedDomainEnables.includes(domain)) continue;
121
- try {
122
- await session.send(`${domain}.enable`);
123
- } catch (error) {
124
- 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.`);
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
- for (const command of options.setupCommands) {
128
- await session.send(command.method, command.params);
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
- const deadline = Date.now() + options.maxDurationSeconds * 1000;
133
- while (!shouldStop() && Date.now() < deadline && eventCount < options.maxEvents) await sleep(100);
134
- const reason = shouldStop() ? 'requested' : (eventCount >= options.maxEvents ? 'max-events' : 'max-duration');
135
- await onPhase('post-wait', reason);
136
- await sleep(options.postWaitMs);
137
- session.off('*', onAnyEvent);
138
- return { reason, eventCount };
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
  }
@@ -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 spawnError = null;
224
+ let state = { phase: 'encoding' };
224
225
  let stderr = '';
225
- child.once('error', (error) => {
226
- spawnError = error;
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) => resolve({ 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 (spawnError) throw spawnError;
238
- if (child.exitCode !== null) {
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 (!child.stdin.destroyed) child.stdin.end();
247
- const result = await closed;
248
- if (spawnError) throw spawnError;
249
- if (result.code !== 0) {
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 onPhase('pre-roll');
362
+ await phase('pre-roll');
338
363
  await capturePeriod(preRollMs);
339
- await onPhase('recording');
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 onPhase('post-roll', reason);
373
+ await phase('post-roll', reason);
349
374
  await capturePeriod(postRollMs);
350
- await encoder.stop();
375
+ await finalize();
376
+ lifecycle.transition('completed');
351
377
  return { reason, frameCount };
352
378
  } catch (error) {
353
- await encoder.stop().catch(() => {});
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 baseState = {
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, baseState);
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 = null) => updateState({
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
- updateState({
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();
@@ -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
- this.activeRequestIds.delete(event.requestId);
288
- if (!entry) return;
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
- this.activeRequestIds.delete(event.requestId);
303
- if (!entry) return;
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
- if (this.activeRequestIds.size === 0 && Date.now() - this.lastActivityAt >= idleMs) return true;
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 track = (promise) => {
455
- pending.add(promise);
456
- promise.finally(() => pending.delete(promise));
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
- session.on('Network.requestWillBeSent', (event) => collector.requestWillBeSent(event));
460
- session.on('Network.requestWillBeSentExtraInfo', (event) => collector.requestWillBeSentExtraInfo(event));
461
- session.on('Network.responseReceived', (event) => collector.responseReceived(event));
462
- session.on('Network.responseReceivedExtraInfo', (event) => collector.responseReceivedExtraInfo(event));
463
- session.on('Network.requestServedFromCache', (event) => collector.requestServedFromCache(event));
464
- session.on('Network.loadingFinished', (event) => track(collector.loadingFinished(event, session)));
465
- session.on('Network.loadingFailed', (event) => collector.loadingFailed(event));
466
-
467
- await session.send('Network.enable', {
468
- maxTotalBufferSize: Math.max(options.maxBodyBytes * 4, 4 * 1024 * 1024),
469
- maxResourceBufferSize: options.maxBodyBytes,
470
- maxPostDataSize: options.maxBodyBytes,
471
- });
472
- await onPhase('recording');
473
-
474
- const deadline = Date.now() + options.maxDurationSeconds * 1000;
475
- while (!shouldStop() && Date.now() < deadline) await sleep(100);
476
- const reason = shouldStop() ? 'requested' : 'max-duration';
477
- await onPhase('draining', reason);
478
- await collector.waitForDrain();
479
- await Promise.allSettled([...pending]);
480
- return { reason, har: collector.build(), eventCount: collector.entries.length };
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 settled = false;
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 (settled) return;
134
- settled = true;
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 (settled) return;
141
- settled = true;
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(selections.length > 0 ? selections : info(el));
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' && selections.length > 0) {
190
+ if (e.key === 'Enter' && state === 'multi-selecting') {
187
191
  e.preventDefault();
188
192
  finish(selections);
189
193
  }
@@ -20,6 +20,7 @@ import {
20
20
  requiredOptionValue,
21
21
  } from './browser-control.mjs';
22
22
  import { openPrivateFile, privateOutputPath, withPageCdpSession } from './cdp-common.mjs';
23
+ import { createRecordingLifecycle } from './recording-lifecycle.mjs';
23
24
  import {
24
25
  CDP_CAPTURE_STOP_TIMEOUT_MS,
25
26
  assertCdpRecordingOwner,
@@ -173,23 +174,18 @@ async function runWorker(args) {
173
174
  const output = requiredOptionValue(args, '--output', null);
174
175
  const options = decodeOptions(requiredOptionValue(args, '--worker-options', null));
175
176
  const startedAt = new Date().toISOString();
176
- let state = {
177
+ const metadata = {
177
178
  managedBy: 'browser-tools-cdp-recorder',
178
179
  kind: CAPTURE_KIND,
179
180
  pid: process.pid,
180
181
  port,
181
182
  output,
182
183
  ownerTokenHash: ownerTokenHash(ownerToken),
183
- status: 'starting',
184
- phase: 'starting',
185
184
  startedAt,
186
185
  options,
187
186
  };
188
- const update = (changes) => {
189
- state = { ...state, ...changes };
190
- updateCdpRecordingState(CAPTURE_KIND, port, state);
191
- };
192
- update({});
187
+ const lifecycle = createRecordingLifecycle(CAPTURE_KIND, metadata,
188
+ (state) => updateCdpRecordingState(CAPTURE_KIND, port, state));
193
189
 
194
190
  let outputFd = null;
195
191
  try {
@@ -200,31 +196,15 @@ async function runWorker(args) {
200
196
  options,
201
197
  shouldStop: () => existsSync(cdpRecordingStopFile(CAPTURE_KIND, port)),
202
198
  writeEvent: (event) => writeSync(outputFd, `${JSON.stringify(event)}\n`),
203
- onPhase: (phase, reason = null) => update({
204
- status: phase === 'recording' ? 'recording' : state.status,
205
- phase,
206
- reason: reason ?? state.reason,
207
- readyAt: phase === 'recording' ? new Date().toISOString() : state.readyAt,
208
- }),
199
+ onPhase: (phase, reason) => lifecycle.transition(phase, reason ? { reason } : {}),
209
200
  });
210
201
  });
211
202
  closeSync(outputFd);
212
203
  outputFd = null;
213
- update({
214
- status: 'completed',
215
- phase: 'completed',
216
- reason: result.reason,
217
- eventCount: result.eventCount,
218
- completedAt: new Date().toISOString(),
219
- });
204
+ lifecycle.transition('completed', { reason: result.reason, eventCount: result.eventCount });
220
205
  } catch (error) {
221
206
  if (outputFd !== null) closeSync(outputFd);
222
- update({
223
- status: 'failed',
224
- phase: 'failed',
225
- error: error.message,
226
- completedAt: new Date().toISOString(),
227
- });
207
+ lifecycle.transition('failed', { error: error.message });
228
208
  throw error;
229
209
  }
230
210
  }