@@ -14,6 +14,17 @@ function parseOnOff(raw: string): boolean | null {
|
14 | 14 | return parsed === undefined ? null : parsed; |
15 | 15 | } |
16 | 16 | |
| 17 | +function parsePositiveInteger(value: unknown, label: string): number | undefined { |
| 18 | +const raw = typeof value === "string" ? value.trim() : String(value); |
| 19 | +const parsed = /^\d+$/.test(raw) ? Number(raw) : Number.NaN; |
| 20 | +if (!Number.isSafeInteger(parsed) || parsed < 1) { |
| 21 | +defaultRuntime.error(danger(`Invalid ${label}: must be a positive integer`)); |
| 22 | +defaultRuntime.exit(1); |
| 23 | +return undefined; |
| 24 | +} |
| 25 | +return parsed; |
| 26 | +} |
| 27 | + |
17 | 28 | function runBrowserCommand(action: () => Promise<void>) { |
18 | 29 | return runCommandWithRuntime(defaultRuntime, action, (err) => { |
19 | 30 | defaultRuntime.error(danger(String(err))); |
@@ -58,10 +69,15 @@ export function registerBrowserStateCommands(
|
58 | 69 | set |
59 | 70 | .command("viewport") |
60 | 71 | .description("Set viewport size (alias for resize)") |
61 | | -.argument("<width>", "Viewport width", (v: string) => Number(v)) |
62 | | -.argument("<height>", "Viewport height", (v: string) => Number(v)) |
| 72 | +.argument("<width>", "Viewport width") |
| 73 | +.argument("<height>", "Viewport height") |
63 | 74 | .option("--target-id <id>", "CDP target id (or unique prefix)") |
64 | | -.action(async (width: number, height: number, opts, cmd) => { |
| 75 | +.action(async (widthRaw: string, heightRaw: string, opts, cmd) => { |
| 76 | +const width = parsePositiveInteger(widthRaw, "width"); |
| 77 | +const height = parsePositiveInteger(heightRaw, "height"); |
| 78 | +if (width === undefined || height === undefined) { |
| 79 | +return; |
| 80 | +} |
65 | 81 | const parent = parentOpts(cmd); |
66 | 82 | const profile = parent?.browserProfile; |
67 | 83 | await runBrowserCommand(async () => { |
|