-
Notifications
You must be signed in to change notification settings - Fork 226
browser-trace: fix bodies-snapshot dir + add helper #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
skyzer
wants to merge
1
commit into
browserbase:main
Choose a base branch
from
skyzer:fix/snapshot-bodies-dir
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| #!/usr/bin/env node | ||
| // Snapshot the `browse network` bodies dir into a run before the next capture | ||
| // overwrites it. | ||
| // | ||
| // Usage: | ||
| // node scripts/snapshot-bodies.mjs <run-id> [--no-off] [--bodies <path>] | ||
| // | ||
| // What it does: | ||
| // 1. Resolves the `browse network` capture dir (live, via `browse network path`, | ||
| // or an explicit `--bodies <path>` override). | ||
| // 2. Copies its contents into `<run>/cdp/network/bodies/`. | ||
| // 3. Calls `browse network off` so a future `browse network on` starts clean | ||
| // (skip with `--no-off`). | ||
| // | ||
| // The manual `cp -r "$(browse network path | jq -r .path)" <run>/cdp/network/bodies` | ||
| // pattern is fragile across BSD vs GNU `cp` (trailing-slash semantics differ and | ||
| // the parent dir must already exist). This script encodes the safe path so the | ||
| // docs can refer to one command. | ||
|
|
||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { spawnSync } from 'node:child_process'; | ||
|
|
||
| import { runDir, ensureDir } from './lib.mjs'; | ||
|
|
||
| const args = process.argv.slice(2); | ||
| if (args.length === 0 || args[0].startsWith('--')) { | ||
| console.error('usage: snapshot-bodies.mjs <run-id> [--no-off] [--bodies <path>]'); | ||
| process.exit(2); | ||
| } | ||
| const runId = args[0]; | ||
| let bodiesOverride = null; | ||
| let runOff = true; | ||
| for (let i = 1; i < args.length; i++) { | ||
| if (args[i] === '--no-off') { runOff = false; continue; } | ||
| if (args[i] === '--bodies') { bodiesOverride = args[++i]; continue; } | ||
| console.error(`unknown arg: ${args[i]}`); | ||
| process.exit(2); | ||
| } | ||
|
|
||
| const RD = runDir(runId); | ||
| if (!fs.existsSync(RD)) { | ||
| console.error(`run dir not found: ${RD}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| let src = bodiesOverride; | ||
| if (!src) { | ||
| const out = spawnSync('browse', ['network', 'path', '--json'], { encoding: 'utf8' }); | ||
| if (out.status !== 0) { | ||
| console.error('failed to resolve `browse network path`:'); | ||
| console.error(out.stderr || out.stdout); | ||
| process.exit(1); | ||
| } | ||
| try { | ||
| src = JSON.parse(out.stdout).path; | ||
| } catch { | ||
| // older `browse` versions don't take --json; fall back to plain stdout. | ||
| src = (spawnSync('browse', ['network', 'path'], { encoding: 'utf8' }).stdout || '').trim(); | ||
| } | ||
| } | ||
| if (!src || !fs.existsSync(src)) { | ||
| console.error(`bodies source dir not found: ${src ?? '(unresolved)'}`); | ||
| console.error('Did you run `browse network on` before capturing?'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const dest = path.join(RD, 'cdp', 'network', 'bodies'); | ||
| ensureDir(path.dirname(dest)); | ||
| // fs.cpSync avoids the BSD-vs-GNU cp portability footgun: cp's trailing-slash | ||
| // and missing-parent semantics differ across macOS and Linux. Node's recursive | ||
| // copy is the same everywhere. | ||
| fs.cpSync(src, dest, { recursive: true }); | ||
| const fileCount = fs.readdirSync(dest).length; | ||
|
|
||
| if (runOff) { | ||
| const off = spawnSync('browse', ['network', 'off'], { encoding: 'utf8' }); | ||
| if (off.status !== 0) { | ||
| console.error('warning: `browse network off` failed (continuing):'); | ||
| console.error(off.stderr || off.stdout); | ||
| } | ||
| } | ||
|
|
||
| console.log(JSON.stringify({ | ||
| run_id: runId, | ||
| bodies_src: src, | ||
| bodies_dest: dest, | ||
| files: fileCount, | ||
| ran_off: runOff, | ||
| })); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
browse network path --jsonexits non-zeroThe script exits immediately when
browse network path --jsonreturns a non-zero status, so the documented compatibility fallback to plainbrowse network pathnever runs in the common “unknown flag” case on olderbrowseversions. In those environments,snapshot-bodies.mjsfails even though the non-JSON command could still provide a valid path, blocking body snapshotting for the whole workflow.Useful? React with 👍 / 👎.