Files
orca/src/cli/browser/browser-env.ts
T
Neil 920a83889c refactor: move browser contracts and publication to defining owners (#40209)
* refactor: move atomic publication to its filesystem owner

* refactor: group shared browser definitions by feature

* refactor: bind proxy process and session contracts to defining owners

* refactor: consolidate direct session contract type imports

* refactor: retain atomic publication test timing under moved owner

* fix(benchmark): resolve historical tunnel framing source before loading
2026-11-09 00:45:45 -08:00

121 lines
5.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type {
BrowserGeolocationResult,
BrowserViewportResult
} from '../../shared/browser/runtime/runtime-browser-contracts'
import type { CommandHandler } from '../dispatch'
import { printResult } from '../format'
import {
getOptionalStringFlag,
getRequiredFiniteNumber,
getRequiredPositiveNumber,
getRequiredStringFlag,
getRequiredStringFlagAllowingEmpty
} from '../flags'
import { RuntimeClientError } from '../runtime/types'
import { getBrowserCommandTarget } from './browser-command-target'
export const BROWSER_ENV_HANDLERS: Record<string, CommandHandler> = {
viewport: async ({ flags, client, cwd, json }) => {
const width = getRequiredPositiveNumber(flags, 'width')
const height = getRequiredPositiveNumber(flags, 'height')
const params: Record<string, unknown> = { width, height }
const scale = getOptionalStringFlag(flags, 'scale')
if (scale) {
const n = Number(scale)
if (!Number.isFinite(n) || n <= 0) {
throw new RuntimeClientError('invalid_argument', '--scale must be a positive number')
}
params.deviceScaleFactor = n
}
if (flags.has('mobile')) {
params.mobile = true
}
Object.assign(params, await getBrowserCommandTarget(flags, cwd, client))
const result = await client.call<BrowserViewportResult>('browser.viewport', params)
printResult(
result,
json,
(v) => `Viewport set to ${v.width}×${v.height}${v.mobile ? ' (mobile)' : ''}`
)
},
geolocation: async ({ flags, client, cwd, json }) => {
const latitude = getRequiredFiniteNumber(flags, 'latitude')
const longitude = getRequiredFiniteNumber(flags, 'longitude')
const params: Record<string, unknown> = { latitude, longitude }
const accuracy = getOptionalStringFlag(flags, 'accuracy')
if (accuracy) {
const n = Number(accuracy)
if (!Number.isFinite(n) || n <= 0) {
throw new RuntimeClientError('invalid_argument', '--accuracy must be a positive number')
}
params.accuracy = n
}
Object.assign(params, await getBrowserCommandTarget(flags, cwd, client))
const result = await client.call<BrowserGeolocationResult>('browser.geolocation', params)
printResult(result, json, (v) => `Geolocation set to ${v.latitude}, ${v.longitude}`)
},
'set device': async ({ flags, client, cwd, json }) => {
const name = getRequiredStringFlag(flags, 'name')
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<unknown>('browser.setDevice', { name, ...target })
printResult(result, json, () => `Device emulation set to ${name}`)
},
'set offline': async ({ flags, client, cwd, json }) => {
const state = getOptionalStringFlag(flags, 'state')
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<unknown>('browser.setOffline', { state, ...target })
printResult(result, json, () => `Offline mode ${state ?? 'toggled'}`)
},
'set headers': async ({ flags, client, cwd, json }) => {
const headers = getRequiredStringFlag(flags, 'headers')
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<unknown>('browser.setHeaders', { headers, ...target })
printResult(result, json, () => 'Extra HTTP headers set')
},
'set credentials': async ({ flags, client, cwd, json }) => {
const user = getRequiredStringFlag(flags, 'user')
// HTTP basic auth permits empty passwords.
const pass = getRequiredStringFlagAllowingEmpty(flags, 'pass')
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<unknown>('browser.setCredentials', {
user,
pass,
...target
})
printResult(result, json, () => `HTTP auth credentials set for ${user}`)
},
'set media': async ({ flags, client, cwd, json }) => {
const colorScheme = getOptionalStringFlag(flags, 'color-scheme')
const reducedMotion = getOptionalStringFlag(flags, 'reduced-motion')
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<unknown>('browser.setMedia', {
colorScheme,
reducedMotion,
...target
})
printResult(result, json, () => 'Media preferences set')
},
'clipboard read': async ({ flags, client, cwd, json }) => {
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<unknown>('browser.clipboardRead', target)
printResult(result, json, (v) => JSON.stringify(v, null, 2))
},
'clipboard write': async ({ flags, client, cwd, json }) => {
const text = getRequiredStringFlag(flags, 'text')
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<unknown>('browser.clipboardWrite', { text, ...target })
printResult(result, json, () => 'Clipboard updated')
},
'dialog accept': async ({ flags, client, cwd, json }) => {
const text = getOptionalStringFlag(flags, 'text')
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<unknown>('browser.dialogAccept', { text, ...target })
printResult(result, json, () => 'Dialog accepted')
},
'dialog dismiss': async ({ flags, client, cwd, json }) => {
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<unknown>('browser.dialogDismiss', target)
printResult(result, json, () => 'Dialog dismissed')
}
}