@rezkam/browser-tools 0.2.1 → 1.0.0
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/README.md +33 -1
- package/bin/browser-tools.mjs +12 -0
- package/package.json +4 -3
- package/scripts/browser-control.mjs +774 -74
- package/scripts/cdp-common.mjs +309 -0
- package/scripts/cdp-event-capture.mjs +139 -0
- package/scripts/cdp-recording-state.mjs +160 -0
- package/scripts/cdp.mjs +111 -0
- package/scripts/extract-har.mjs +227 -0
- package/scripts/gif-recorder.mjs +451 -0
- package/scripts/har-capture.mjs +485 -0
- package/scripts/record-cdp.mjs +261 -0
- package/scripts/record-gif.mjs +234 -0
- package/scripts/record-har.mjs +270 -0
- package/scripts/review-gif.mjs +203 -0
- package/scripts/start.mjs +2 -2
- package/scripts/stop.mjs +60 -5
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Record selected active-tab CDP events as private JSON Lines.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* scripts/record-cdp.mjs start --output ./checkout_network_events.jsonl --domain Network
|
|
7
|
+
* scripts/record-cdp.mjs status --json
|
|
8
|
+
* scripts/record-cdp.mjs stop
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { closeSync, existsSync, openSync, realpathSync, writeSync } from 'node:fs';
|
|
12
|
+
import { spawn } from 'node:child_process';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
import {
|
|
15
|
+
OWNER_TOKEN_ENV,
|
|
16
|
+
hasFlag,
|
|
17
|
+
ownerTokenHash,
|
|
18
|
+
parseOwnerToken,
|
|
19
|
+
parsePort,
|
|
20
|
+
requiredOptionValue,
|
|
21
|
+
} from './browser-control.mjs';
|
|
22
|
+
import { openPrivateFile, privateOutputPath, withPageCdpSession } from './cdp-common.mjs';
|
|
23
|
+
import {
|
|
24
|
+
CDP_CAPTURE_STOP_TIMEOUT_MS,
|
|
25
|
+
assertCdpRecordingOwner,
|
|
26
|
+
cdpRecordingLogFile,
|
|
27
|
+
cdpRecordingReport,
|
|
28
|
+
cdpRecordingStopFile,
|
|
29
|
+
claimCdpRecording,
|
|
30
|
+
readCdpRecordingState,
|
|
31
|
+
removeCdpRecordingState,
|
|
32
|
+
requestCdpRecordingStop,
|
|
33
|
+
updateCdpRecordingState,
|
|
34
|
+
waitForCdpRecordingState,
|
|
35
|
+
} from './cdp-recording-state.mjs';
|
|
36
|
+
import {
|
|
37
|
+
DEFAULT_CDP_POST_WAIT_MS,
|
|
38
|
+
parseCdpEventCaptureOptions,
|
|
39
|
+
runCdpEventCapture,
|
|
40
|
+
} from './cdp-event-capture.mjs';
|
|
41
|
+
|
|
42
|
+
const SCRIPT_FILE = fileURLToPath(import.meta.url);
|
|
43
|
+
const CAPTURE_KIND = 'cdp';
|
|
44
|
+
|
|
45
|
+
function usage() {
|
|
46
|
+
console.error(`Usage: browser-tools record-cdp <command> [options]
|
|
47
|
+
|
|
48
|
+
Commands:
|
|
49
|
+
start --output <meaningful.jsonl> Start raw active-tab CDP event capture
|
|
50
|
+
status [--json] Report CDP capture status
|
|
51
|
+
stop Capture the post-wait period and finalize
|
|
52
|
+
|
|
53
|
+
Selection:
|
|
54
|
+
--domain <name> Enable a CDP domain, repeatable
|
|
55
|
+
--event <pattern> Include Domain.event or Domain.*, repeatable
|
|
56
|
+
--exclude-event <pattern> Exclude event pattern, repeatable
|
|
57
|
+
--skip-enable <domain> Subscribe without calling Domain.enable
|
|
58
|
+
--setup <json|Domain.method> Session setup command, repeatable
|
|
59
|
+
|
|
60
|
+
Lifecycle:
|
|
61
|
+
--post-wait-ms <n> Capture after stop request (default: 500)
|
|
62
|
+
--max-duration <seconds> Safety limit (default: 300)
|
|
63
|
+
--max-events <n> Event safety limit (default: 100000)
|
|
64
|
+
--redact Redact auth, cookies, bodies, and frame payloads
|
|
65
|
+
--overwrite Replace existing output
|
|
66
|
+
--port <n> Managed browser port (default: 9222)
|
|
67
|
+
--owner-token <token> Prefer ${OWNER_TOKEN_ENV} instead
|
|
68
|
+
|
|
69
|
+
Each JSONL record contains an ISO timestamp, elapsed milliseconds, CDP method, and
|
|
70
|
+
parameters. Outputs are private (0600). The Browser Tools owner token is never stored.`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function encodeOptions(options) {
|
|
74
|
+
return Buffer.from(JSON.stringify(options), 'utf-8').toString('base64url');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function decodeOptions(value) {
|
|
78
|
+
if (!value) throw new Error('Missing internal CDP worker options');
|
|
79
|
+
return JSON.parse(Buffer.from(value, 'base64url').toString('utf-8'));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function startCapture(args) {
|
|
83
|
+
const port = parsePort(args);
|
|
84
|
+
const ownerToken = parseOwnerToken(args);
|
|
85
|
+
if (!ownerToken) {
|
|
86
|
+
throw new Error(`Missing browser owner token. Export ${OWNER_TOKEN_ENV} with the token printed by browser-tools start`);
|
|
87
|
+
}
|
|
88
|
+
const overwrite = hasFlag(args, '--overwrite');
|
|
89
|
+
const output = privateOutputPath(requiredOptionValue(args, '--output', null), 'jsonl', { overwrite });
|
|
90
|
+
const options = parseCdpEventCaptureOptions(args);
|
|
91
|
+
claimCdpRecording(CAPTURE_KIND, port);
|
|
92
|
+
|
|
93
|
+
const logFile = cdpRecordingLogFile(CAPTURE_KIND, port);
|
|
94
|
+
let logFd = null;
|
|
95
|
+
try {
|
|
96
|
+
logFd = openSync(logFile, 'a', 0o600);
|
|
97
|
+
const child = spawn(process.execPath, [
|
|
98
|
+
SCRIPT_FILE,
|
|
99
|
+
'worker',
|
|
100
|
+
'--port', String(port),
|
|
101
|
+
'--output', output,
|
|
102
|
+
'--worker-options', encodeOptions(options),
|
|
103
|
+
], {
|
|
104
|
+
detached: true,
|
|
105
|
+
stdio: ['ignore', logFd, logFd],
|
|
106
|
+
env: { ...process.env, [OWNER_TOKEN_ENV]: ownerToken },
|
|
107
|
+
});
|
|
108
|
+
child.unref();
|
|
109
|
+
} catch (error) {
|
|
110
|
+
removeCdpRecordingState(CAPTURE_KIND, port);
|
|
111
|
+
throw error;
|
|
112
|
+
} finally {
|
|
113
|
+
if (logFd !== null) closeSync(logFd);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const state = await waitForCdpRecordingState(
|
|
117
|
+
CAPTURE_KIND,
|
|
118
|
+
port,
|
|
119
|
+
(candidate) => candidate.status === 'recording' || candidate.status === 'failed',
|
|
120
|
+
);
|
|
121
|
+
if (!state) throw new Error(`CDP recorder did not start within 30 seconds. See ${logFile}`);
|
|
122
|
+
if (state.status === 'failed') throw new Error(`CDP recorder failed: ${state.error || 'unknown failure'}. See ${logFile}`);
|
|
123
|
+
console.log(`✓ CDP capture active on :${port}: ${output}`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function showStatus(args) {
|
|
127
|
+
const port = parsePort(args);
|
|
128
|
+
const report = cdpRecordingReport(CAPTURE_KIND, port);
|
|
129
|
+
if (hasFlag(args, '--json')) console.log(JSON.stringify(report, null, 2));
|
|
130
|
+
else if (report.recording) console.log(`✓ CDP capture ${report.phase} on :${port}: ${report.output}`);
|
|
131
|
+
else if (report.status) console.log(`CDP capture ${report.status} on :${port}: ${report.output}`);
|
|
132
|
+
else console.log(`No CDP capture found for port ${port}`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function stopCapture(args) {
|
|
136
|
+
const port = parsePort(args);
|
|
137
|
+
const ownerToken = parseOwnerToken(args);
|
|
138
|
+
const initialState = readCdpRecordingState(CAPTURE_KIND, port);
|
|
139
|
+
if (!initialState) throw new Error(`No CDP capture found on port ${port}`);
|
|
140
|
+
assertCdpRecordingOwner(initialState, ownerToken);
|
|
141
|
+
if (initialState.status === 'completed') {
|
|
142
|
+
console.log(initialState.output);
|
|
143
|
+
removeCdpRecordingState(CAPTURE_KIND, port);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (initialState.status === 'failed') {
|
|
147
|
+
throw new Error(`CDP recorder failed: ${initialState.error || 'unknown failure'}. See ${cdpRecordingLogFile(CAPTURE_KIND, port)}`);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
requestCdpRecordingStop(CAPTURE_KIND, port, ownerToken);
|
|
151
|
+
console.error('⟳ Capturing final CDP events and closing JSONL...');
|
|
152
|
+
const timeoutMs = CDP_CAPTURE_STOP_TIMEOUT_MS
|
|
153
|
+
+ (initialState.options?.postWaitMs || DEFAULT_CDP_POST_WAIT_MS);
|
|
154
|
+
const state = await waitForCdpRecordingState(
|
|
155
|
+
CAPTURE_KIND,
|
|
156
|
+
port,
|
|
157
|
+
(candidate) => ['completed', 'failed'].includes(candidate.status),
|
|
158
|
+
{ timeoutMs },
|
|
159
|
+
);
|
|
160
|
+
if (!state || !['completed', 'failed'].includes(state.status)) {
|
|
161
|
+
throw new Error(`CDP recorder did not stop within ${Math.ceil(timeoutMs / 1000)} seconds`);
|
|
162
|
+
}
|
|
163
|
+
if (state.status === 'failed') {
|
|
164
|
+
throw new Error(`CDP recorder failed: ${state.error || 'unknown failure'}. See ${cdpRecordingLogFile(CAPTURE_KIND, port)}`);
|
|
165
|
+
}
|
|
166
|
+
console.log(state.output);
|
|
167
|
+
removeCdpRecordingState(CAPTURE_KIND, port);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function runWorker(args) {
|
|
171
|
+
const port = parsePort(args);
|
|
172
|
+
const ownerToken = parseOwnerToken(args);
|
|
173
|
+
const output = requiredOptionValue(args, '--output', null);
|
|
174
|
+
const options = decodeOptions(requiredOptionValue(args, '--worker-options', null));
|
|
175
|
+
const startedAt = new Date().toISOString();
|
|
176
|
+
let state = {
|
|
177
|
+
managedBy: 'browser-tools-cdp-recorder',
|
|
178
|
+
kind: CAPTURE_KIND,
|
|
179
|
+
pid: process.pid,
|
|
180
|
+
port,
|
|
181
|
+
output,
|
|
182
|
+
ownerTokenHash: ownerTokenHash(ownerToken),
|
|
183
|
+
status: 'starting',
|
|
184
|
+
phase: 'starting',
|
|
185
|
+
startedAt,
|
|
186
|
+
options,
|
|
187
|
+
};
|
|
188
|
+
const update = (changes) => {
|
|
189
|
+
state = { ...state, ...changes };
|
|
190
|
+
updateCdpRecordingState(CAPTURE_KIND, port, state);
|
|
191
|
+
};
|
|
192
|
+
update({});
|
|
193
|
+
|
|
194
|
+
let outputFd = null;
|
|
195
|
+
try {
|
|
196
|
+
const result = await withPageCdpSession(port, ownerToken, async ({ session }) => {
|
|
197
|
+
outputFd = openPrivateFile(output, 'w');
|
|
198
|
+
return runCdpEventCapture({
|
|
199
|
+
session,
|
|
200
|
+
options,
|
|
201
|
+
shouldStop: () => existsSync(cdpRecordingStopFile(CAPTURE_KIND, port)),
|
|
202
|
+
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
|
+
}),
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
closeSync(outputFd);
|
|
212
|
+
outputFd = null;
|
|
213
|
+
update({
|
|
214
|
+
status: 'completed',
|
|
215
|
+
phase: 'completed',
|
|
216
|
+
reason: result.reason,
|
|
217
|
+
eventCount: result.eventCount,
|
|
218
|
+
completedAt: new Date().toISOString(),
|
|
219
|
+
});
|
|
220
|
+
} catch (error) {
|
|
221
|
+
if (outputFd !== null) closeSync(outputFd);
|
|
222
|
+
update({
|
|
223
|
+
status: 'failed',
|
|
224
|
+
phase: 'failed',
|
|
225
|
+
error: error.message,
|
|
226
|
+
completedAt: new Date().toISOString(),
|
|
227
|
+
});
|
|
228
|
+
throw error;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export async function main(args = process.argv.slice(2)) {
|
|
233
|
+
const [command] = args;
|
|
234
|
+
if (!command || command === '--help' || command === '-h') {
|
|
235
|
+
usage();
|
|
236
|
+
return command ? 0 : 1;
|
|
237
|
+
}
|
|
238
|
+
if (command === 'start') await startCapture(args.slice(1));
|
|
239
|
+
else if (command === 'status') showStatus(args.slice(1));
|
|
240
|
+
else if (command === 'stop') await stopCapture(args.slice(1));
|
|
241
|
+
else if (command === 'worker') await runWorker(args.slice(1));
|
|
242
|
+
else {
|
|
243
|
+
usage();
|
|
244
|
+
throw new Error(`Unknown record-cdp command: ${command}`);
|
|
245
|
+
}
|
|
246
|
+
return 0;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function isDirectExecution() {
|
|
250
|
+
if (!process.argv[1]) return false;
|
|
251
|
+
return realpathSync(process.argv[1]) === realpathSync(SCRIPT_FILE);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (isDirectExecution()) {
|
|
255
|
+
try {
|
|
256
|
+
process.exitCode = await main();
|
|
257
|
+
} catch (error) {
|
|
258
|
+
console.error(`✗ ${error.message}`);
|
|
259
|
+
process.exitCode = 1;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Record the active browser tab as a GIF around a multi-step interaction.
|
|
4
|
+
* Start waits for pre-action frames; stop captures post-action frames before finalizing.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* scripts/record-gif.mjs start --output ./login_process.gif [--port 9223]
|
|
8
|
+
* scripts/record-gif.mjs status [--port 9223] [--json]
|
|
9
|
+
* scripts/record-gif.mjs stop [--port 9223]
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { closeSync, openSync, realpathSync } from 'node:fs';
|
|
13
|
+
import { spawn } from 'node:child_process';
|
|
14
|
+
import { fileURLToPath } from 'node:url';
|
|
15
|
+
import {
|
|
16
|
+
OWNER_TOKEN_ENV,
|
|
17
|
+
hasFlag,
|
|
18
|
+
optionValue,
|
|
19
|
+
parseOwnerToken,
|
|
20
|
+
parsePort,
|
|
21
|
+
requiredOptionValue,
|
|
22
|
+
} from './browser-control.mjs';
|
|
23
|
+
import {
|
|
24
|
+
DEFAULT_GIF_COLORS,
|
|
25
|
+
DEFAULT_GIF_FPS,
|
|
26
|
+
DEFAULT_GIF_MAX_DURATION_SECONDS,
|
|
27
|
+
DEFAULT_GIF_POST_ROLL_MS,
|
|
28
|
+
DEFAULT_GIF_PRE_ROLL_MS,
|
|
29
|
+
DEFAULT_GIF_SCALE,
|
|
30
|
+
GIF_STOP_TIMEOUT_MS,
|
|
31
|
+
assertFfmpegAvailable,
|
|
32
|
+
assertGifRecordingOwner,
|
|
33
|
+
claimAndPrepareGifOutput,
|
|
34
|
+
gifRecordingLogFile,
|
|
35
|
+
gifRecordingReport,
|
|
36
|
+
normalizeGifOutputPath,
|
|
37
|
+
parseBoundedNumber,
|
|
38
|
+
readGifRecordingState,
|
|
39
|
+
removeGifRecordingState,
|
|
40
|
+
requestGifRecordingStop,
|
|
41
|
+
runGifRecordingWorker,
|
|
42
|
+
waitForGifRecordingState,
|
|
43
|
+
} from './gif-recorder.mjs';
|
|
44
|
+
|
|
45
|
+
const SCRIPT_FILE = fileURLToPath(import.meta.url);
|
|
46
|
+
|
|
47
|
+
function usage() {
|
|
48
|
+
console.error(`Usage: browser-tools record-gif <command> [options]
|
|
49
|
+
|
|
50
|
+
Commands:
|
|
51
|
+
start --output <meaningful.gif> Start recording and capture pre-action frames
|
|
52
|
+
status [--json] Report recording status
|
|
53
|
+
stop Capture post-action frames and finish the GIF
|
|
54
|
+
|
|
55
|
+
Options:
|
|
56
|
+
--port <n> Managed browser port (default: 9222)
|
|
57
|
+
--owner-token <token> Prefer ${OWNER_TOKEN_ENV} instead
|
|
58
|
+
--fps <1-30> GIF frame rate (default: ${DEFAULT_GIF_FPS})
|
|
59
|
+
--colors <2-256> GIF palette size (default: ${DEFAULT_GIF_COLORS})
|
|
60
|
+
--scale <0.1-2> Output scale (default: ${DEFAULT_GIF_SCALE})
|
|
61
|
+
--pre-roll-ms <100-10000> Frames before actions (default: ${DEFAULT_GIF_PRE_ROLL_MS})
|
|
62
|
+
--post-roll-ms <100-10000> Frames after actions (default: ${DEFAULT_GIF_POST_ROLL_MS})
|
|
63
|
+
--max-duration <1-3600> Safety limit in seconds (default: ${DEFAULT_GIF_MAX_DURATION_SECONDS})
|
|
64
|
+
--overwrite Replace an existing output file
|
|
65
|
+
|
|
66
|
+
Use an action-specific filename such as login_process.gif, not recording.gif.`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function startOptions(args) {
|
|
70
|
+
const value = (name) => optionValue(args, name, null);
|
|
71
|
+
return {
|
|
72
|
+
fps: parseBoundedNumber(value('--fps'), '--fps', { fallback: DEFAULT_GIF_FPS, min: 1, max: 30, integer: true }),
|
|
73
|
+
colors: parseBoundedNumber(value('--colors'), '--colors', { fallback: DEFAULT_GIF_COLORS, min: 2, max: 256, integer: true }),
|
|
74
|
+
scale: parseBoundedNumber(value('--scale'), '--scale', { fallback: DEFAULT_GIF_SCALE, min: 0.1, max: 2 }),
|
|
75
|
+
preRollMs: parseBoundedNumber(value('--pre-roll-ms'), '--pre-roll-ms', { fallback: DEFAULT_GIF_PRE_ROLL_MS, min: 100, max: 10000, integer: true }),
|
|
76
|
+
postRollMs: parseBoundedNumber(value('--post-roll-ms'), '--post-roll-ms', { fallback: DEFAULT_GIF_POST_ROLL_MS, min: 100, max: 10000, integer: true }),
|
|
77
|
+
maxDurationSeconds: parseBoundedNumber(value('--max-duration'), '--max-duration', { fallback: DEFAULT_GIF_MAX_DURATION_SECONDS, min: 1, max: 3600, integer: true }),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function startRecording(args) {
|
|
82
|
+
const port = parsePort(args);
|
|
83
|
+
const ownerToken = parseOwnerToken(args);
|
|
84
|
+
if (!ownerToken) {
|
|
85
|
+
throw new Error(`Missing browser owner token. Export ${OWNER_TOKEN_ENV} with the token printed by browser-tools start`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const output = normalizeGifOutputPath(requiredOptionValue(args, '--output', null));
|
|
89
|
+
const overwrite = hasFlag(args, '--overwrite');
|
|
90
|
+
const options = startOptions(args);
|
|
91
|
+
assertFfmpegAvailable();
|
|
92
|
+
claimAndPrepareGifOutput(port, output, { overwrite });
|
|
93
|
+
|
|
94
|
+
const logFile = gifRecordingLogFile(port);
|
|
95
|
+
let logFd = null;
|
|
96
|
+
try {
|
|
97
|
+
logFd = openSync(logFile, 'a', 0o600);
|
|
98
|
+
const workerArgs = [
|
|
99
|
+
SCRIPT_FILE,
|
|
100
|
+
'worker',
|
|
101
|
+
'--port', String(port),
|
|
102
|
+
'--output', output,
|
|
103
|
+
'--fps', String(options.fps),
|
|
104
|
+
'--colors', String(options.colors),
|
|
105
|
+
'--scale', String(options.scale),
|
|
106
|
+
'--pre-roll-ms', String(options.preRollMs),
|
|
107
|
+
'--post-roll-ms', String(options.postRollMs),
|
|
108
|
+
'--max-duration', String(options.maxDurationSeconds),
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
const child = spawn(process.execPath, workerArgs, {
|
|
112
|
+
detached: true,
|
|
113
|
+
stdio: ['ignore', logFd, logFd],
|
|
114
|
+
env: { ...process.env, [OWNER_TOKEN_ENV]: ownerToken },
|
|
115
|
+
});
|
|
116
|
+
child.unref();
|
|
117
|
+
} catch (error) {
|
|
118
|
+
removeGifRecordingState(port);
|
|
119
|
+
throw error;
|
|
120
|
+
} finally {
|
|
121
|
+
if (logFd !== null) closeSync(logFd);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const state = await waitForGifRecordingState(
|
|
125
|
+
port,
|
|
126
|
+
(candidate) => candidate.status === 'recording' || candidate.status === 'failed',
|
|
127
|
+
);
|
|
128
|
+
if (!state) {
|
|
129
|
+
throw new Error(`GIF recorder did not start within 30 seconds. See ${logFile}`);
|
|
130
|
+
}
|
|
131
|
+
if (state.status === 'failed') {
|
|
132
|
+
const message = state.error || 'unknown recorder failure';
|
|
133
|
+
throw new Error(`GIF recorder failed: ${message}. See ${logFile}`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
console.log(`✓ Recording active tab to ${output}`);
|
|
137
|
+
console.log(`✓ Captured ${options.preRollMs}ms of pre-action frames; perform browser actions now`);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function statusRecording(args) {
|
|
141
|
+
const port = parsePort(args);
|
|
142
|
+
const report = gifRecordingReport(port);
|
|
143
|
+
if (hasFlag(args, '--json')) {
|
|
144
|
+
console.log(JSON.stringify(report, null, 2));
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (!report.status) {
|
|
149
|
+
console.log(`No GIF recording found for port ${port}`);
|
|
150
|
+
} else if (report.recording) {
|
|
151
|
+
console.log(`✓ GIF recording ${report.phase} on :${port}: ${report.output}`);
|
|
152
|
+
} else {
|
|
153
|
+
console.log(`GIF recording ${report.status} on :${port}: ${report.output}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function stopRecording(args) {
|
|
158
|
+
const port = parsePort(args);
|
|
159
|
+
const ownerToken = parseOwnerToken(args);
|
|
160
|
+
const initialState = readGifRecordingState(port);
|
|
161
|
+
if (!initialState) throw new Error(`No GIF recording found on port ${port}`);
|
|
162
|
+
assertGifRecordingOwner(initialState, ownerToken);
|
|
163
|
+
|
|
164
|
+
if (initialState.status === 'completed') {
|
|
165
|
+
console.log(initialState.output);
|
|
166
|
+
removeGifRecordingState(port);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (initialState.status === 'failed') {
|
|
170
|
+
throw new Error(`GIF recorder failed: ${initialState.error || 'unknown failure'}. See ${gifRecordingLogFile(port)}`);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
requestGifRecordingStop(port, ownerToken);
|
|
174
|
+
console.error('⟳ Capturing post-action frames and finalizing GIF...');
|
|
175
|
+
const timeoutMs = GIF_STOP_TIMEOUT_MS + (initialState.options?.postRollMs || DEFAULT_GIF_POST_ROLL_MS);
|
|
176
|
+
const state = await waitForGifRecordingState(
|
|
177
|
+
port,
|
|
178
|
+
(candidate) => candidate.status === 'completed' || candidate.status === 'failed',
|
|
179
|
+
{ timeoutMs },
|
|
180
|
+
);
|
|
181
|
+
if (!state || !['completed', 'failed'].includes(state.status)) {
|
|
182
|
+
throw new Error(`GIF recorder did not stop within ${Math.ceil(timeoutMs / 1000)} seconds. Check browser-tools record-gif status --port ${port}`);
|
|
183
|
+
}
|
|
184
|
+
if (state.status === 'failed') {
|
|
185
|
+
throw new Error(`GIF recorder failed: ${state.error || 'unknown failure'}. See ${gifRecordingLogFile(port)}`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
console.log(state.output);
|
|
189
|
+
removeGifRecordingState(port);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function runWorker(args) {
|
|
193
|
+
const port = parsePort(args);
|
|
194
|
+
const ownerToken = parseOwnerToken(args);
|
|
195
|
+
const output = normalizeGifOutputPath(requiredOptionValue(args, '--output', null));
|
|
196
|
+
await runGifRecordingWorker({
|
|
197
|
+
port,
|
|
198
|
+
output,
|
|
199
|
+
ownerToken,
|
|
200
|
+
...startOptions(args),
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export async function main(args = process.argv.slice(2)) {
|
|
205
|
+
const [command] = args;
|
|
206
|
+
if (!command || command === '--help' || command === '-h') {
|
|
207
|
+
usage();
|
|
208
|
+
return command ? 0 : 1;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (command === 'start') await startRecording(args.slice(1));
|
|
212
|
+
else if (command === 'status') statusRecording(args.slice(1));
|
|
213
|
+
else if (command === 'stop') await stopRecording(args.slice(1));
|
|
214
|
+
else if (command === 'worker') await runWorker(args.slice(1));
|
|
215
|
+
else {
|
|
216
|
+
usage();
|
|
217
|
+
throw new Error(`Unknown record-gif command: ${command}`);
|
|
218
|
+
}
|
|
219
|
+
return 0;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function isDirectExecution() {
|
|
223
|
+
if (!process.argv[1]) return false;
|
|
224
|
+
return realpathSync(process.argv[1]) === realpathSync(SCRIPT_FILE);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (isDirectExecution()) {
|
|
228
|
+
try {
|
|
229
|
+
process.exitCode = await main();
|
|
230
|
+
} catch (error) {
|
|
231
|
+
console.error(`✗ ${error.message}`);
|
|
232
|
+
process.exitCode = 1;
|
|
233
|
+
}
|
|
234
|
+
}
|