-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathruntime.ts
More file actions
86 lines (79 loc) · 2.93 KB
/
runtime.ts
File metadata and controls
86 lines (79 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { BrowserBridge, CDPBridge } from './browser/index.js';
import type { IPage } from './types.js';
import { TimeoutError } from './errors.js';
import { isElectronApp } from './electron-apps.js';
/**
* Returns the appropriate browser factory based on site type.
* Uses CDPBridge for registered Electron apps, otherwise BrowserBridge.
*/
export function getBrowserFactory(site?: string): new () => IBrowserFactory {
if (site && isElectronApp(site)) return CDPBridge;
return BrowserBridge;
}
function parseEnvTimeout(envVar: string, fallback: number): number {
const raw = process.env[envVar];
if (raw === undefined) return fallback;
const parsed = parseInt(raw, 10);
if (Number.isNaN(parsed) || parsed <= 0) {
console.error(`[runtime] Invalid ${envVar}="${raw}", using default ${fallback}s`);
return fallback;
}
return parsed;
}
export const DEFAULT_BROWSER_CONNECT_TIMEOUT = parseEnvTimeout('OPENCLI_BROWSER_CONNECT_TIMEOUT', 30);
export const DEFAULT_BROWSER_COMMAND_TIMEOUT = parseEnvTimeout('OPENCLI_BROWSER_COMMAND_TIMEOUT', 60);
export const DEFAULT_BROWSER_EXPLORE_TIMEOUT = parseEnvTimeout('OPENCLI_BROWSER_EXPLORE_TIMEOUT', 120);
/**
* Timeout with seconds unit. Used for high-level command timeouts.
*/
export async function runWithTimeout<T>(
promise: Promise<T>,
opts: { timeout: number; label?: string; hint?: string },
): Promise<T> {
const label = opts.label ?? 'Operation';
return withTimeoutMs(promise, opts.timeout * 1000,
() => new TimeoutError(label, opts.timeout, opts.hint));
}
/**
* Timeout with milliseconds unit. Used for low-level internal timeouts.
* Accepts a factory function to create the rejection error, keeping this
* utility decoupled from specific error types.
*/
export function withTimeoutMs<T>(
promise: Promise<T>,
timeoutMs: number,
makeError: string | (() => Error) = 'Operation timed out',
): Promise<T> {
const reject_ = typeof makeError === 'string'
? () => new Error(makeError)
: makeError;
return new Promise<T>((resolve, reject) => {
const timer = setTimeout(() => reject(reject_()), timeoutMs);
promise.then(
(value) => { clearTimeout(timer); resolve(value); },
(error) => { clearTimeout(timer); reject(error); },
);
});
}
/** Interface for browser factory (BrowserBridge or test mocks) */
export interface IBrowserFactory {
connect(opts?: { timeout?: number; workspace?: string; cdpEndpoint?: string }): Promise<IPage>;
close(): Promise<void>;
}
export async function browserSession<T>(
BrowserFactory: new () => IBrowserFactory,
fn: (page: IPage) => Promise<T>,
opts: { workspace?: string; cdpEndpoint?: string } = {},
): Promise<T> {
const browser = new BrowserFactory();
try {
const page = await browser.connect({
timeout: DEFAULT_BROWSER_CONNECT_TIMEOUT,
workspace: opts.workspace,
cdpEndpoint: opts.cdpEndpoint,
});
return await fn(page);
} finally {
await browser.close().catch(() => {});
}
}