-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-model.js
More file actions
80 lines (65 loc) · 2.17 KB
/
session-model.js
File metadata and controls
80 lines (65 loc) · 2.17 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
import { CliError } from "./errors.js";
export const SUPPORTED_TRANSPORTS = new Set(["open", "connect", "attach"]);
export const SUPPORTED_BROWSERS = new Set(["chromium", "firefox", "webkit"]);
export const SUPPORTED_ENGINES = new Set(["auto", "custom", "devtools"]);
export function normalizeTransport(value) {
const transport = value ? String(value) : "open";
if (!SUPPORTED_TRANSPORTS.has(transport)) {
throw new CliError(`Unsupported transport: ${transport}`, {
code: "unsupported-transport",
});
}
return transport;
}
export function normalizeBrowserName(value, transport = "open") {
const browserName = value ? String(value) : "chromium";
if (!SUPPORTED_BROWSERS.has(browserName)) {
throw new CliError(`Unsupported browser: ${browserName}`, {
code: "unsupported-browser",
});
}
if (transport === "attach" && browserName !== "chromium") {
throw new CliError("CDP attach mode only supports Chromium.", {
code: "unsupported-browser-for-cdp",
});
}
return browserName;
}
export function normalizeEngine(value) {
const engine = value ? String(value) : "auto";
if (!SUPPORTED_ENGINES.has(engine)) {
throw new CliError(`Unsupported engine: ${engine}`, {
code: "unsupported-engine",
});
}
return engine;
}
export function resolveTimeoutMs(options = {}) {
if (typeof options.timeout === "number") {
return options.timeout;
}
if (typeof options.timeoutMs === "number") {
return options.timeoutMs;
}
return undefined;
}
export function buildSessionCapabilities({ transport, persistent }) {
return {
supportsReconnect: transport === "connect" || transport === "attach",
supportsPersistentContext: transport === "open",
supportsHighFidelityProtocol: transport !== "attach",
isChromiumOnly: transport === "attach",
ownsBrowserLifecycle: transport === "open",
requiresReloadForHookInjection: transport !== "open",
persistent: Boolean(persistent),
};
}
export function getSessionEndpoint(transport, options = {}) {
if (transport === "connect") {
return options.wsEndpoint ?? null;
}
if (transport === "attach") {
return options.cdpUrl ?? null;
}
return null;
}