cdp-toolkit 3.0.3 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/CONTRACT.md +2 -2
- package/README.md +122 -1
- package/dist/bidi/mux-daemon.js +675 -130
- package/dist/cli.js +1432 -635
- package/dist/index.js +1110 -116
- package/dist/mcp.js +1474 -658
- package/package.json +3 -1
- package/src/bidi/snapshot.ts +123 -113
- package/src/cdp/driver.ts +55 -41
- package/src/cdp/endpoint.ts +200 -0
- package/src/cdp/session.ts +246 -0
- package/src/cli.ts +17 -4
- package/src/client.ts +140 -16
- package/src/endpoints.ts +428 -0
- package/src/leases-tools.ts +6 -3
- package/src/leases.ts +28 -4
- package/src/manifest.ts +54 -42
- package/src/mcp.ts +52 -8
- package/src/origins.ts +5 -2
- package/src/policy.ts +163 -0
- package/src/reap.ts +14 -0
- package/src/shared-tools.ts +164 -25
- package/src/toolDocs.ts +41 -38
- package/src/tools/browser-session.ts +26 -12
- package/src/tools/dispatch-mouse.ts +2 -1
- package/src/tools/evaluate.ts +3 -3
- package/src/tools/focus-emulation.ts +5 -3
- package/src/tools/lighthouse.ts +4 -2
- package/src/tools/navigation.ts +3 -3
- package/src/tools/network_mock.ts +2 -2
- package/src/tools/performance.ts +5 -5
- package/src/tools/recorder.ts +3 -3
- package/src/tools/screencast.ts +46 -6
- package/src/tools/snapshot.ts +2 -2
- package/src/version.ts +1 -1
package/dist/cli.js
CHANGED
|
@@ -11,11 +11,308 @@ var __esm = (fn, res, err) => () => {
|
|
|
11
11
|
return res;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
// src/
|
|
14
|
+
// src/endpoints.ts
|
|
15
15
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
16
|
+
import { readFileSync } from "node:fs";
|
|
17
|
+
function checkName(name, where) {
|
|
18
|
+
if (!NAME_RE.test(name)) {
|
|
19
|
+
throw new EndpointConfigError(`${where}: endpoint name '${name}' is invalid; names are letters, digits and '_' only (no '-', '/', ':' or spaces)`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function sameBase(a, b) {
|
|
23
|
+
return a.replace(/\/+$/, "") === b.replace(/\/+$/, "");
|
|
24
|
+
}
|
|
25
|
+
function parseBase(raw, where) {
|
|
26
|
+
let url;
|
|
27
|
+
try {
|
|
28
|
+
url = new URL(raw);
|
|
29
|
+
} catch {
|
|
30
|
+
throw new EndpointConfigError(`${where}: '${raw}' is not a valid URL (expected e.g. http://127.0.0.1:9222)`);
|
|
31
|
+
}
|
|
32
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
33
|
+
throw new EndpointConfigError(`${where}: '${raw}' must be an http:// or https:// DevTools origin, not ${url.protocol}`);
|
|
34
|
+
}
|
|
35
|
+
if (url.hash)
|
|
36
|
+
throw new EndpointConfigError(`${where}: '${raw}' must not carry a #fragment`);
|
|
37
|
+
let mode;
|
|
38
|
+
for (const [key, value] of url.searchParams) {
|
|
39
|
+
if (key !== "mode") {
|
|
40
|
+
throw new EndpointConfigError(`${where}: unknown query parameter '${key}' in '${raw}' (only ?mode=read or ?mode=write is understood)`);
|
|
41
|
+
}
|
|
42
|
+
if (value !== "read" && value !== "write") {
|
|
43
|
+
throw new EndpointConfigError(`${where}: unknown mode '${value}' in '${raw}' (use mode=read or mode=write)`);
|
|
44
|
+
}
|
|
45
|
+
mode = value;
|
|
46
|
+
}
|
|
47
|
+
const path = url.pathname.replace(/\/+$/, "");
|
|
48
|
+
return { base: `${url.protocol}//${url.host}${path}`, mode };
|
|
49
|
+
}
|
|
50
|
+
function parseEnvString(spec) {
|
|
51
|
+
const out = [];
|
|
52
|
+
const seen = new Set;
|
|
53
|
+
for (const part of spec.split(",")) {
|
|
54
|
+
const entry = part.trim();
|
|
55
|
+
if (!entry)
|
|
56
|
+
continue;
|
|
57
|
+
const eq = entry.indexOf("=");
|
|
58
|
+
if (eq <= 0) {
|
|
59
|
+
throw new EndpointConfigError(`CDP_ENDPOINTS: entry '${entry}' is not name=url (e.g. teams=http://127.0.0.1:9333)`);
|
|
60
|
+
}
|
|
61
|
+
const name = entry.slice(0, eq).trim();
|
|
62
|
+
checkName(name, "CDP_ENDPOINTS");
|
|
63
|
+
if (seen.has(name))
|
|
64
|
+
throw new EndpointConfigError(`CDP_ENDPOINTS: duplicate endpoint name '${name}'`);
|
|
65
|
+
seen.add(name);
|
|
66
|
+
const { base, mode } = parseBase(entry.slice(eq + 1).trim(), `CDP_ENDPOINTS endpoint '${name}'`);
|
|
67
|
+
out.push({ name, base, readOnly: mode === "read" });
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
function parseFile(path) {
|
|
72
|
+
let text;
|
|
73
|
+
try {
|
|
74
|
+
text = readFileSync(path, "utf8");
|
|
75
|
+
} catch (err) {
|
|
76
|
+
const code = err.code ?? String(err);
|
|
77
|
+
throw new EndpointConfigError(`CDP_ENDPOINTS_FILE: cannot read '${path}' (${code})`);
|
|
78
|
+
}
|
|
79
|
+
let json;
|
|
80
|
+
try {
|
|
81
|
+
json = JSON.parse(text);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
throw new EndpointConfigError(`CDP_ENDPOINTS_FILE: '${path}' is not valid JSON (${err instanceof Error ? err.message : String(err)})`);
|
|
84
|
+
}
|
|
85
|
+
const where = `CDP_ENDPOINTS_FILE '${path}'`;
|
|
86
|
+
if (!json || typeof json !== "object" || Array.isArray(json)) {
|
|
87
|
+
throw new EndpointConfigError(`${where}: top level must be an object {"default"?, "endpoints": {...}}`);
|
|
88
|
+
}
|
|
89
|
+
const top = json;
|
|
90
|
+
for (const key of Object.keys(top)) {
|
|
91
|
+
if (key !== "default" && key !== "endpoints")
|
|
92
|
+
throw new EndpointConfigError(`${where}: unknown top-level key '${key}'`);
|
|
93
|
+
}
|
|
94
|
+
if (top.default !== undefined && typeof top.default !== "string") {
|
|
95
|
+
throw new EndpointConfigError(`${where}: "default" must be an endpoint name string`);
|
|
96
|
+
}
|
|
97
|
+
const raw = top.endpoints ?? {};
|
|
98
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
|
|
99
|
+
throw new EndpointConfigError(`${where}: "endpoints" must be an object keyed by endpoint name`);
|
|
100
|
+
}
|
|
101
|
+
const endpoints = [];
|
|
102
|
+
for (const [name, value] of Object.entries(raw)) {
|
|
103
|
+
checkName(name, where);
|
|
104
|
+
const at = `${where} endpoint '${name}'`;
|
|
105
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
106
|
+
throw new EndpointConfigError(`${at}: must be an object like {"base":"http://127.0.0.1:9333"}`);
|
|
107
|
+
}
|
|
108
|
+
const v = value;
|
|
109
|
+
for (const key of Object.keys(v)) {
|
|
110
|
+
if (key !== "base" && key !== "readOnly" && key !== "mode") {
|
|
111
|
+
throw new EndpointConfigError(`${at}: unknown key '${key}' (known: base, readOnly, mode)`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (typeof v.base !== "string")
|
|
115
|
+
throw new EndpointConfigError(`${at}: "base" is required and must be a URL string`);
|
|
116
|
+
if (v.readOnly !== undefined && typeof v.readOnly !== "boolean")
|
|
117
|
+
throw new EndpointConfigError(`${at}: "readOnly" must be a boolean`);
|
|
118
|
+
if (v.mode !== undefined && v.mode !== "read" && v.mode !== "write") {
|
|
119
|
+
throw new EndpointConfigError(`${at}: unknown mode '${String(v.mode)}' (use "read" or "write")`);
|
|
120
|
+
}
|
|
121
|
+
const parsed = parseBase(v.base, at);
|
|
122
|
+
const votes = [parsed.mode, v.mode, v.readOnly === undefined ? undefined : v.readOnly ? "read" : "write"].filter((x) => x !== undefined);
|
|
123
|
+
if (new Set(votes).size > 1)
|
|
124
|
+
throw new EndpointConfigError(`${at}: readOnly / mode settings contradict each other`);
|
|
125
|
+
endpoints.push({ name, base: parsed.base, readOnly: votes[0] === "read" });
|
|
126
|
+
}
|
|
127
|
+
return { endpoints, defaultName: top.default };
|
|
128
|
+
}
|
|
129
|
+
function loadEndpoints(env = process.env) {
|
|
130
|
+
const fromEnv = env.CDP_ENDPOINTS !== undefined ? parseEnvString(env.CDP_ENDPOINTS) : [];
|
|
131
|
+
const file = env.CDP_ENDPOINTS_FILE ? parseFile(env.CDP_ENDPOINTS_FILE) : { endpoints: [], defaultName: undefined };
|
|
132
|
+
const merged = new Map;
|
|
133
|
+
for (const ep of fromEnv)
|
|
134
|
+
merged.set(ep.name, ep);
|
|
135
|
+
for (const ep of file.endpoints) {
|
|
136
|
+
const prior = merged.get(ep.name);
|
|
137
|
+
if (prior && (prior.base !== ep.base || prior.readOnly !== ep.readOnly)) {
|
|
138
|
+
throw new EndpointConfigError(`endpoint '${ep.name}' is defined differently in CDP_ENDPOINTS (${prior.base}${prior.readOnly ? " read-only" : ""}) and CDP_ENDPOINTS_FILE (${ep.base}${ep.readOnly ? " read-only" : ""})`);
|
|
139
|
+
}
|
|
140
|
+
if (!prior)
|
|
141
|
+
merged.set(ep.name, ep);
|
|
142
|
+
}
|
|
143
|
+
const cdpBase = env.CDP_BASE;
|
|
144
|
+
if (cdpBase !== undefined) {
|
|
145
|
+
const configured = merged.get("default");
|
|
146
|
+
if (configured && !sameBase(configured.base, cdpBase)) {
|
|
147
|
+
throw new EndpointConfigError(`CDP_BASE (${cdpBase}) and the configured endpoint 'default' (${configured.base}) disagree; drop one or make them match`);
|
|
148
|
+
}
|
|
149
|
+
merged.set("default", { name: "default", base: cdpBase, readOnly: configured?.readOnly ?? false });
|
|
150
|
+
}
|
|
151
|
+
const defaultName = file.defaultName ?? "default";
|
|
152
|
+
if (!merged.has(defaultName)) {
|
|
153
|
+
if (file.defaultName !== undefined) {
|
|
154
|
+
throw new EndpointConfigError(`CDP_ENDPOINTS_FILE names default endpoint '${file.defaultName}', which is not defined (known: ${[...merged.keys()].join(", ") || "none"})`);
|
|
155
|
+
}
|
|
156
|
+
merged.set("default", { name: "default", base: DEFAULT_CDP_BASE, readOnly: false });
|
|
157
|
+
}
|
|
158
|
+
const def = merged.get(defaultName);
|
|
159
|
+
const byName = new Map([[def.name, def]]);
|
|
160
|
+
for (const [name, ep] of merged)
|
|
161
|
+
if (name !== def.name)
|
|
162
|
+
byName.set(name, ep);
|
|
163
|
+
return { default: def, byName };
|
|
164
|
+
}
|
|
165
|
+
function signature(env) {
|
|
166
|
+
return JSON.stringify([env.CDP_BASE ?? null, env.CDP_ENDPOINTS ?? null, env.CDP_ENDPOINTS_FILE ?? null]);
|
|
167
|
+
}
|
|
168
|
+
function endpointConfig() {
|
|
169
|
+
const sig = signature(process.env);
|
|
170
|
+
if (cache?.sig === sig)
|
|
171
|
+
return cache.config;
|
|
172
|
+
const config = loadEndpoints(process.env);
|
|
173
|
+
cache = { sig, config };
|
|
174
|
+
return config;
|
|
175
|
+
}
|
|
176
|
+
function defaultEndpoint() {
|
|
177
|
+
return endpointConfig().default;
|
|
178
|
+
}
|
|
179
|
+
function allEndpoints() {
|
|
180
|
+
return [...endpointConfig().byName.values()];
|
|
181
|
+
}
|
|
182
|
+
function getEndpoint(name) {
|
|
183
|
+
const { byName } = endpointConfig();
|
|
184
|
+
const ep = byName.get(name);
|
|
185
|
+
if (!ep) {
|
|
186
|
+
throw new EndpointConfigError(`unknown endpoint '${name}' (known: ${[...byName.keys()].join(", ")}). Configure it with CDP_ENDPOINTS or CDP_ENDPOINTS_FILE.`);
|
|
187
|
+
}
|
|
188
|
+
return ep;
|
|
189
|
+
}
|
|
190
|
+
function leaseNamespace(ep) {
|
|
191
|
+
const { default: def, byName } = endpointConfig();
|
|
192
|
+
if (sameBase(ep.base, def.base))
|
|
193
|
+
return;
|
|
194
|
+
for (const other of byName.values())
|
|
195
|
+
if (sameBase(other.base, ep.base))
|
|
196
|
+
return other.name;
|
|
197
|
+
return ep.name;
|
|
198
|
+
}
|
|
199
|
+
function hasEndpointPrefix(target) {
|
|
200
|
+
return typeof target === "string" && target.startsWith(ENDPOINT_PREFIX);
|
|
201
|
+
}
|
|
202
|
+
function splitEndpointSelector(target) {
|
|
203
|
+
if (!hasEndpointPrefix(target))
|
|
204
|
+
return { endpoint: defaultEndpoint(), rest: target };
|
|
205
|
+
const body = target.slice(ENDPOINT_PREFIX.length);
|
|
206
|
+
const slash = body.indexOf("/");
|
|
207
|
+
const name = slash < 0 ? body : body.slice(0, slash);
|
|
208
|
+
if (!name)
|
|
209
|
+
throw new EndpointConfigError(`selector '${target}' has an empty endpoint name; use endpoint:<name>/<selector>`);
|
|
210
|
+
const rest = slash < 0 ? undefined : body.slice(slash + 1);
|
|
211
|
+
return { endpoint: getEndpoint(name), rest: rest === "" ? undefined : rest };
|
|
212
|
+
}
|
|
213
|
+
function usesEndpointArgs(args) {
|
|
214
|
+
if (!args || typeof args !== "object")
|
|
215
|
+
return false;
|
|
216
|
+
const a = args;
|
|
217
|
+
return SELECTOR_KEYS.some((k) => hasEndpointPrefix(a[k])) || a.endpoint !== undefined || a.allEndpoints === true;
|
|
218
|
+
}
|
|
219
|
+
function stripEndpointArgs(args) {
|
|
220
|
+
if (!args || typeof args !== "object" || Array.isArray(args))
|
|
221
|
+
return { endpoint: defaultEndpoint(), args };
|
|
222
|
+
const a = args;
|
|
223
|
+
const picks = [];
|
|
224
|
+
let out;
|
|
225
|
+
for (const key of SELECTOR_KEYS) {
|
|
226
|
+
const v = a[key];
|
|
227
|
+
if (!hasEndpointPrefix(v))
|
|
228
|
+
continue;
|
|
229
|
+
const { endpoint, rest } = splitEndpointSelector(v);
|
|
230
|
+
picks.push({ from: `${key} '${v}'`, endpoint });
|
|
231
|
+
out ??= { ...a };
|
|
232
|
+
if (rest === undefined)
|
|
233
|
+
delete out[key];
|
|
234
|
+
else
|
|
235
|
+
out[key] = rest;
|
|
236
|
+
}
|
|
237
|
+
if (a.endpoint !== undefined) {
|
|
238
|
+
if (typeof a.endpoint !== "string" || a.endpoint === "") {
|
|
239
|
+
throw new EndpointConfigError("`endpoint` must be a non-empty endpoint name string");
|
|
240
|
+
}
|
|
241
|
+
picks.push({ from: `endpoint '${a.endpoint}'`, endpoint: getEndpoint(a.endpoint) });
|
|
242
|
+
out ??= { ...a };
|
|
243
|
+
delete out.endpoint;
|
|
244
|
+
}
|
|
245
|
+
const token = leaseTokenNamespace(a.lease);
|
|
246
|
+
if (!picks.length) {
|
|
247
|
+
if (token?.ns === undefined)
|
|
248
|
+
return { endpoint: defaultEndpoint(), args };
|
|
249
|
+
return { endpoint: endpointForNamespace(token.ns), args };
|
|
250
|
+
}
|
|
251
|
+
const first = picks[0];
|
|
252
|
+
for (const p of picks.slice(1)) {
|
|
253
|
+
if (p.endpoint.name !== first.endpoint.name) {
|
|
254
|
+
throw new EndpointConfigError(`conflicting endpoints: ${first.from} vs ${p.from}; name one endpoint per call`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (token && leaseNamespace(first.endpoint) !== token.ns) {
|
|
258
|
+
throw new EndpointConfigError(`conflicting endpoints: ${first.from} vs the lease token, which belongs to ${token.ns === undefined ? "the default endpoint" : `endpoint '${token.ns}'`}; name one endpoint per call`);
|
|
259
|
+
}
|
|
260
|
+
return { endpoint: first.endpoint, args: out ?? a };
|
|
261
|
+
}
|
|
262
|
+
function leaseTokenNamespace(lease) {
|
|
263
|
+
if (typeof lease !== "string")
|
|
264
|
+
return null;
|
|
265
|
+
const colon = lease.indexOf(":");
|
|
266
|
+
if (colon <= 0)
|
|
267
|
+
return null;
|
|
268
|
+
const head = lease.slice(0, colon);
|
|
269
|
+
if (head === "chrome")
|
|
270
|
+
return { ns: undefined };
|
|
271
|
+
if (!head.startsWith("chrome@"))
|
|
272
|
+
return null;
|
|
273
|
+
const ns = head.slice("chrome@".length);
|
|
274
|
+
return NAME_RE.test(ns) ? { ns } : null;
|
|
275
|
+
}
|
|
276
|
+
function endpointForNamespace(ns) {
|
|
277
|
+
for (const ep of endpointConfig().byName.values())
|
|
278
|
+
if (leaseNamespace(ep) === ns)
|
|
279
|
+
return ep;
|
|
280
|
+
throw new EndpointConfigError(`the lease token belongs to endpoint '${ns}', which is not configured (known: ${[...endpointConfig().byName.keys()].join(", ")})`);
|
|
281
|
+
}
|
|
282
|
+
function currentEndpoint() {
|
|
283
|
+
return endpointScope.getStore() ?? defaultEndpoint();
|
|
284
|
+
}
|
|
285
|
+
function isDefaultEndpoint(ep) {
|
|
286
|
+
return ep.name === defaultEndpoint().name;
|
|
287
|
+
}
|
|
288
|
+
function withEndpointScope(ep, fn) {
|
|
289
|
+
return endpointScope.run(ep, fn);
|
|
290
|
+
}
|
|
291
|
+
var EndpointConfigError, DEFAULT_CDP_BASE = "http://127.0.0.1:9222", ENDPOINT_PREFIX = "endpoint:", NAME_RE, cache, SELECTOR_KEYS, ENDPOINTS_FIREFOX_MESSAGE = "named CDP endpoints (an 'endpoint:<name>/' selector prefix, the endpoint arg, list_pages allEndpoints) need the Chrome backend; the Firefox BiDi backend has one session and no endpoints", endpointScope;
|
|
292
|
+
var init_endpoints = __esm(() => {
|
|
293
|
+
EndpointConfigError = class EndpointConfigError extends Error {
|
|
294
|
+
constructor(message) {
|
|
295
|
+
super(message);
|
|
296
|
+
this.name = "EndpointConfigError";
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
NAME_RE = /^[A-Za-z0-9_]+$/;
|
|
300
|
+
SELECTOR_KEYS = ["target", "targetId"];
|
|
301
|
+
endpointScope = new AsyncLocalStorage;
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// src/leases.ts
|
|
305
|
+
import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
|
|
16
306
|
import { randomBytes } from "node:crypto";
|
|
17
307
|
import { mkdir, readFile, readdir, unlink, writeFile } from "node:fs/promises";
|
|
18
308
|
import { join } from "node:path";
|
|
309
|
+
function chromeLeaseBackend(ep = currentEndpoint()) {
|
|
310
|
+
const ns = leaseNamespace(ep);
|
|
311
|
+
return ns === undefined ? "chrome" : `chrome@${ns}`;
|
|
312
|
+
}
|
|
313
|
+
function isLeaseBackend(value) {
|
|
314
|
+
return BACKEND_KEY_RE.test(value);
|
|
315
|
+
}
|
|
19
316
|
function leaseDir() {
|
|
20
317
|
return process.env.CDP_ARTIFACT_DIR ?? "/tmp/cdp-toolkit";
|
|
21
318
|
}
|
|
@@ -52,7 +349,7 @@ function tokenParts(token) {
|
|
|
52
349
|
if (first <= 0 || last <= first)
|
|
53
350
|
return;
|
|
54
351
|
const backend = token.slice(0, first);
|
|
55
|
-
if (backend
|
|
352
|
+
if (!isLeaseBackend(backend))
|
|
56
353
|
return;
|
|
57
354
|
const targetId = token.slice(first + 1, last);
|
|
58
355
|
const nonce = token.slice(last + 1);
|
|
@@ -273,8 +570,10 @@ async function listLeases(opts = {}) {
|
|
|
273
570
|
}
|
|
274
571
|
return out;
|
|
275
572
|
}
|
|
276
|
-
var DEFAULT_LEASE_TTL_MS = 900000, longLived = false, LeaseConflictError, leaseScope, LEASE_FILE_RE;
|
|
573
|
+
var BACKEND_KEY_SOURCE = "chrome@[A-Za-z0-9_]+|chrome|firefox", BACKEND_KEY_RE, DEFAULT_LEASE_TTL_MS = 900000, longLived = false, LeaseConflictError, leaseScope, LEASE_FILE_RE;
|
|
277
574
|
var init_leases = __esm(() => {
|
|
575
|
+
init_endpoints();
|
|
576
|
+
BACKEND_KEY_RE = new RegExp(`^(?:${BACKEND_KEY_SOURCE})$`);
|
|
278
577
|
LeaseConflictError = class LeaseConflictError extends Error {
|
|
279
578
|
targetId;
|
|
280
579
|
holder;
|
|
@@ -285,8 +584,8 @@ var init_leases = __esm(() => {
|
|
|
285
584
|
this.name = "LeaseConflictError";
|
|
286
585
|
}
|
|
287
586
|
};
|
|
288
|
-
leaseScope = new
|
|
289
|
-
LEASE_FILE_RE =
|
|
587
|
+
leaseScope = new AsyncLocalStorage2;
|
|
588
|
+
LEASE_FILE_RE = new RegExp(`^lease-(${BACKEND_KEY_SOURCE})-(.+)\\.json$`);
|
|
290
589
|
});
|
|
291
590
|
|
|
292
591
|
// src/origins.ts
|
|
@@ -416,7 +715,7 @@ async function resolveLiveLabel(backend, label, liveIds) {
|
|
|
416
715
|
var ORIGIN_FILE_RE;
|
|
417
716
|
var init_origins = __esm(() => {
|
|
418
717
|
init_leases();
|
|
419
|
-
ORIGIN_FILE_RE =
|
|
718
|
+
ORIGIN_FILE_RE = new RegExp(`^origin-(${BACKEND_KEY_SOURCE})-(.+)\\.json$`);
|
|
420
719
|
});
|
|
421
720
|
|
|
422
721
|
// src/workers.ts
|
|
@@ -504,6 +803,204 @@ var init_frames = __esm(() => {
|
|
|
504
803
|
FRAME_SELECTOR_PAGE_ONLY_MESSAGE = "target 'frame:<substring>' selects an out-of-process iframe, and this tool is page-only " + "(closing, selecting, releasing or claiming an iframe is meaningless; these act on the embedder tab). " + `The page-only tools are ${FRAME_PAGE_ONLY_TOOLS.join(", ")}; every other tool that takes a 'target' ` + "accepts a frame: selector.";
|
|
505
804
|
});
|
|
506
805
|
|
|
806
|
+
// src/cdp/endpoint.ts
|
|
807
|
+
import { readFile as readFile3 } from "node:fs/promises";
|
|
808
|
+
import { join as join3 } from "node:path";
|
|
809
|
+
async function browserWsFromPortFile(dir, base) {
|
|
810
|
+
const raw = await readFile3(join3(dir, "DevToolsActivePort"), "utf8").catch(() => {
|
|
811
|
+
return;
|
|
812
|
+
});
|
|
813
|
+
if (!raw)
|
|
814
|
+
return;
|
|
815
|
+
const [port, path] = raw.split(`
|
|
816
|
+
`);
|
|
817
|
+
if (!port?.trim() || !path?.trim())
|
|
818
|
+
return;
|
|
819
|
+
const host = new URL(base).hostname;
|
|
820
|
+
return `ws://${host}:${port.trim()}${path.trim()}`;
|
|
821
|
+
}
|
|
822
|
+
async function discoverBrowserWs(base) {
|
|
823
|
+
const dir = process.env.CDP_USER_DATA_DIR;
|
|
824
|
+
return dir ? browserWsFromPortFile(dir, base) : undefined;
|
|
825
|
+
}
|
|
826
|
+
async function handshake(url, timeoutMs) {
|
|
827
|
+
const conn = new CdpConnection(url, { timeoutMs });
|
|
828
|
+
try {
|
|
829
|
+
await conn.connect();
|
|
830
|
+
return true;
|
|
831
|
+
} catch {
|
|
832
|
+
return false;
|
|
833
|
+
} finally {
|
|
834
|
+
conn.close();
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
function detectEndpoint() {
|
|
838
|
+
const endpoint = currentEndpoint();
|
|
839
|
+
const base = endpoint.base;
|
|
840
|
+
const isDefault = isDefaultEndpoint(endpoint);
|
|
841
|
+
const hit = cached.get(base);
|
|
842
|
+
if (hit)
|
|
843
|
+
return hit;
|
|
844
|
+
const detection = (async () => {
|
|
845
|
+
const forced = isDefault ? process.env.CDP_BROWSER_WS : undefined;
|
|
846
|
+
if (forced)
|
|
847
|
+
return { transport: "browser-ws", browserWsUrl: forced };
|
|
848
|
+
const discovered = isDefault ? await discoverBrowserWs(base) : undefined;
|
|
849
|
+
if (discovered && await handshake(discovered, 5000)) {
|
|
850
|
+
return { transport: "browser-ws", browserWsUrl: discovered };
|
|
851
|
+
}
|
|
852
|
+
const version = await fetch(`${base}/json/version`, { signal: AbortSignal.timeout(5000) }).then((r) => r.ok ? r.json() : undefined).catch(() => {
|
|
853
|
+
return;
|
|
854
|
+
});
|
|
855
|
+
if (version?.webSocketDebuggerUrl) {
|
|
856
|
+
return { transport: "http", browserWsUrl: version.webSocketDebuggerUrl };
|
|
857
|
+
}
|
|
858
|
+
const dir = isDefault ? process.env.CDP_USER_DATA_DIR : undefined;
|
|
859
|
+
throw new Error(`no DevTools endpoint at ${base}: GET /json/version did not answer and ` + (dir ? `no live browser socket was named by ${dir}/DevToolsActivePort. ` : `no profile was searched — CDP_USER_DATA_DIR is unset, and no profile is ever scanned unless you name one. `) + `Set CDP_BROWSER_WS to the ws://.../devtools/browser/<uuid> URL, or CDP_USER_DATA_DIR to the profile in use.`);
|
|
860
|
+
})().then((info) => {
|
|
861
|
+
if (info.transport === "http")
|
|
862
|
+
cached.delete(base);
|
|
863
|
+
return info;
|
|
864
|
+
}, (error) => {
|
|
865
|
+
cached.delete(base);
|
|
866
|
+
throw error;
|
|
867
|
+
});
|
|
868
|
+
cached.set(base, detection);
|
|
869
|
+
return detection;
|
|
870
|
+
}
|
|
871
|
+
function forgetEndpoint() {
|
|
872
|
+
cached.delete(currentEndpoint().base);
|
|
873
|
+
}
|
|
874
|
+
var cached;
|
|
875
|
+
var init_endpoint = __esm(() => {
|
|
876
|
+
init_endpoints();
|
|
877
|
+
init_client();
|
|
878
|
+
cached = new Map;
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
// src/cdp/session.ts
|
|
882
|
+
async function acquireBrowserConn() {
|
|
883
|
+
if (shared) {
|
|
884
|
+
shared.refs++;
|
|
885
|
+
return shared.conn;
|
|
886
|
+
}
|
|
887
|
+
if (!opening) {
|
|
888
|
+
opening = (async () => {
|
|
889
|
+
let { browserWsUrl } = await detectEndpoint();
|
|
890
|
+
let conn;
|
|
891
|
+
try {
|
|
892
|
+
conn = await new CdpConnection(browserWsUrl).connect();
|
|
893
|
+
} catch (error) {
|
|
894
|
+
forgetEndpoint();
|
|
895
|
+
const fresh = (await detectEndpoint().catch(() => {
|
|
896
|
+
return;
|
|
897
|
+
}))?.browserWsUrl;
|
|
898
|
+
if (!fresh || fresh === browserWsUrl)
|
|
899
|
+
throw error;
|
|
900
|
+
browserWsUrl = fresh;
|
|
901
|
+
conn = await new CdpConnection(browserWsUrl).connect();
|
|
902
|
+
}
|
|
903
|
+
shared = { conn, refs: 0, url: browserWsUrl };
|
|
904
|
+
return conn;
|
|
905
|
+
})().finally(() => {
|
|
906
|
+
opening = undefined;
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
const conn = await opening;
|
|
910
|
+
shared.refs++;
|
|
911
|
+
return conn;
|
|
912
|
+
}
|
|
913
|
+
function releaseBrowserConn() {
|
|
914
|
+
if (!shared)
|
|
915
|
+
return;
|
|
916
|
+
shared.refs--;
|
|
917
|
+
if (shared.refs <= 0) {
|
|
918
|
+
const { conn } = shared;
|
|
919
|
+
shared = undefined;
|
|
920
|
+
conn.close();
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
async function withBrowserSocket(fn) {
|
|
924
|
+
const conn = await acquireBrowserConn();
|
|
925
|
+
try {
|
|
926
|
+
return await fn(conn);
|
|
927
|
+
} finally {
|
|
928
|
+
releaseBrowserConn();
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
class SessionConnection {
|
|
933
|
+
browser;
|
|
934
|
+
sessionId;
|
|
935
|
+
opts;
|
|
936
|
+
detached = false;
|
|
937
|
+
offs = [];
|
|
938
|
+
constructor(browser, sessionId, opts = {}) {
|
|
939
|
+
this.browser = browser;
|
|
940
|
+
this.sessionId = sessionId;
|
|
941
|
+
this.opts = opts;
|
|
942
|
+
}
|
|
943
|
+
send(method, params = {}, opts = {}) {
|
|
944
|
+
if (this.detached)
|
|
945
|
+
return Promise.reject(new CdpError("connection not open"));
|
|
946
|
+
return this.browser.send(method, params, {
|
|
947
|
+
timeoutMs: opts.timeoutMs ?? this.opts.timeoutMs,
|
|
948
|
+
sessionId: opts.sessionId ?? this.sessionId
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
on(method, handler) {
|
|
952
|
+
const off = this.browser.on(method, (params, sessionId) => {
|
|
953
|
+
if (sessionId && sessionId !== this.sessionId)
|
|
954
|
+
return;
|
|
955
|
+
handler(params, sessionId);
|
|
956
|
+
});
|
|
957
|
+
this.offs.push(off);
|
|
958
|
+
return off;
|
|
959
|
+
}
|
|
960
|
+
waitFor(method, predicate, timeoutMs = this.opts.timeoutMs ?? DEFAULT_TIMEOUT_MS) {
|
|
961
|
+
return new Promise((resolve, reject) => {
|
|
962
|
+
const timer = setTimeout(() => {
|
|
963
|
+
off();
|
|
964
|
+
reject(new CdpError(`waitFor('${method}') timed out after ${timeoutMs}ms`));
|
|
965
|
+
}, timeoutMs);
|
|
966
|
+
const off = this.on(method, (params) => {
|
|
967
|
+
if (!predicate || predicate(params)) {
|
|
968
|
+
clearTimeout(timer);
|
|
969
|
+
off();
|
|
970
|
+
resolve(params);
|
|
971
|
+
}
|
|
972
|
+
});
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
close() {
|
|
976
|
+
if (this.detached)
|
|
977
|
+
return;
|
|
978
|
+
this.detached = true;
|
|
979
|
+
for (const off of this.offs)
|
|
980
|
+
off();
|
|
981
|
+
this.offs.length = 0;
|
|
982
|
+
this.browser.send("Target.detachFromTarget", { sessionId: this.sessionId }).catch(() => {});
|
|
983
|
+
releaseBrowserConn();
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
async function openSession(targetId, opts = {}) {
|
|
987
|
+
const browser = await acquireBrowserConn();
|
|
988
|
+
try {
|
|
989
|
+
const { sessionId } = await browser.send("Target.attachToTarget", { targetId, flatten: true }, { timeoutMs: opts.timeoutMs });
|
|
990
|
+
if (!sessionId)
|
|
991
|
+
throw new CdpError(`Target.attachToTarget returned no sessionId for ${targetId}`);
|
|
992
|
+
return new SessionConnection(browser, sessionId, opts);
|
|
993
|
+
} catch (e) {
|
|
994
|
+
releaseBrowserConn();
|
|
995
|
+
throw e;
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
var shared, opening;
|
|
999
|
+
var init_session = __esm(() => {
|
|
1000
|
+
init_client();
|
|
1001
|
+
init_endpoint();
|
|
1002
|
+
});
|
|
1003
|
+
|
|
507
1004
|
// src/client.ts
|
|
508
1005
|
class CdpConnection {
|
|
509
1006
|
wsUrl;
|
|
@@ -631,24 +1128,59 @@ class CdpConnection {
|
|
|
631
1128
|
}
|
|
632
1129
|
}
|
|
633
1130
|
async function httpJson(path) {
|
|
634
|
-
const res = await fetch(`${
|
|
1131
|
+
const res = await fetch(`${currentEndpoint().base}${path}`);
|
|
635
1132
|
if (!res.ok)
|
|
636
1133
|
throw new CdpError(`${path} -> HTTP ${res.status}`);
|
|
637
1134
|
return await res.json();
|
|
638
1135
|
}
|
|
639
|
-
function
|
|
640
|
-
|
|
1136
|
+
async function getTargetsOverBrowserWs() {
|
|
1137
|
+
const { targetInfos } = await withBrowserSocket((conn) => conn.send("Target.getTargets", { filter: [{}] }));
|
|
1138
|
+
return targetInfos.map((t) => ({
|
|
1139
|
+
id: t.targetId,
|
|
1140
|
+
type: t.type,
|
|
1141
|
+
title: t.title,
|
|
1142
|
+
url: t.url,
|
|
1143
|
+
webSocketDebuggerUrl: "",
|
|
1144
|
+
...t.openerId ? { parentId: t.openerId } : {}
|
|
1145
|
+
}));
|
|
1146
|
+
}
|
|
1147
|
+
async function listTargets() {
|
|
1148
|
+
const base = currentEndpoint().base;
|
|
1149
|
+
if (knownTransports.get(base) === "browser-ws")
|
|
1150
|
+
return getTargetsOverBrowserWs();
|
|
1151
|
+
try {
|
|
1152
|
+
const listing = await httpJson("/json/list");
|
|
1153
|
+
knownTransports.set(base, "http");
|
|
1154
|
+
return listing;
|
|
1155
|
+
} catch (httpError) {
|
|
1156
|
+
const detected = await detectEndpoint().catch(() => {
|
|
1157
|
+
return;
|
|
1158
|
+
});
|
|
1159
|
+
if (detected?.transport !== "browser-ws")
|
|
1160
|
+
throw httpError;
|
|
1161
|
+
const targets = await getTargetsOverBrowserWs().catch(() => {
|
|
1162
|
+
throw httpError;
|
|
1163
|
+
});
|
|
1164
|
+
knownTransports.set(base, "browser-ws");
|
|
1165
|
+
return targets;
|
|
1166
|
+
}
|
|
641
1167
|
}
|
|
642
1168
|
async function browserWsUrl() {
|
|
643
|
-
|
|
644
|
-
|
|
1169
|
+
if (knownTransports.get(currentEndpoint().base) !== "browser-ws") {
|
|
1170
|
+
const version = await httpJson("/json/version").catch(() => {
|
|
1171
|
+
return;
|
|
1172
|
+
});
|
|
1173
|
+
if (version?.webSocketDebuggerUrl)
|
|
1174
|
+
return version.webSocketDebuggerUrl;
|
|
1175
|
+
}
|
|
1176
|
+
return (await detectEndpoint()).browserWsUrl;
|
|
645
1177
|
}
|
|
646
1178
|
async function resolveTarget(selector, opts = {}) {
|
|
647
1179
|
const targets = await listTargets();
|
|
648
1180
|
const hit = await pickTarget(targets, selector);
|
|
649
1181
|
if (isWorkerTargetType(hit.type))
|
|
650
1182
|
return hit;
|
|
651
|
-
await assertLeaseOk(
|
|
1183
|
+
await assertLeaseOk(chromeLeaseBackend(), hit.id, { lease: opts.lease, url: hit.url, title: hit.title });
|
|
652
1184
|
return hit;
|
|
653
1185
|
}
|
|
654
1186
|
async function pickTarget(targets, selector) {
|
|
@@ -680,7 +1212,7 @@ async function pickTarget(targets, selector) {
|
|
|
680
1212
|
}
|
|
681
1213
|
if (selector.startsWith("label:")) {
|
|
682
1214
|
const label = selector.slice(6);
|
|
683
|
-
const { matchIds, knownLabels } = await resolveLiveLabel(
|
|
1215
|
+
const { matchIds, knownLabels } = await resolveLiveLabel(chromeLeaseBackend(), label, pages.map((t) => t.id));
|
|
684
1216
|
if (matchIds.length > 1) {
|
|
685
1217
|
throw new CdpError(`label 'label:${label}' matches more than one live target: ${matchIds.join(", ")}. Resolve by target id instead.`);
|
|
686
1218
|
}
|
|
@@ -723,7 +1255,7 @@ async function openBrowser(opts = {}) {
|
|
|
723
1255
|
}
|
|
724
1256
|
async function openPage(selector, opts = {}) {
|
|
725
1257
|
const target = await resolveTarget(selector, { lease: opts.lease });
|
|
726
|
-
const conn = await new CdpConnection(target.webSocketDebuggerUrl, opts).connect();
|
|
1258
|
+
const conn = target.webSocketDebuggerUrl ? await new CdpConnection(target.webSocketDebuggerUrl, opts).connect() : await openSession(target.id, opts);
|
|
727
1259
|
return { conn, target };
|
|
728
1260
|
}
|
|
729
1261
|
async function withPage(selector, fn, opts = {}) {
|
|
@@ -734,13 +1266,23 @@ async function withPage(selector, fn, opts = {}) {
|
|
|
734
1266
|
conn.close();
|
|
735
1267
|
}
|
|
736
1268
|
}
|
|
737
|
-
var BASE, DEFAULT_TIMEOUT_MS, CdpError;
|
|
1269
|
+
var BASE, DEFAULT_TIMEOUT_MS, CdpError, knownTransports;
|
|
738
1270
|
var init_client = __esm(() => {
|
|
739
1271
|
init_leases();
|
|
740
1272
|
init_origins();
|
|
1273
|
+
init_endpoints();
|
|
741
1274
|
init_workers();
|
|
742
1275
|
init_frames();
|
|
743
|
-
|
|
1276
|
+
init_endpoint();
|
|
1277
|
+
init_session();
|
|
1278
|
+
init_session();
|
|
1279
|
+
BASE = (() => {
|
|
1280
|
+
try {
|
|
1281
|
+
return defaultEndpoint().base;
|
|
1282
|
+
} catch {
|
|
1283
|
+
return process.env.CDP_BASE ?? DEFAULT_CDP_BASE;
|
|
1284
|
+
}
|
|
1285
|
+
})();
|
|
744
1286
|
DEFAULT_TIMEOUT_MS = Number(process.env.CDP_TIMEOUT_MS ?? 15000);
|
|
745
1287
|
CdpError = class CdpError extends Error {
|
|
746
1288
|
code;
|
|
@@ -752,6 +1294,7 @@ var init_client = __esm(() => {
|
|
|
752
1294
|
this.name = "CdpError";
|
|
753
1295
|
}
|
|
754
1296
|
};
|
|
1297
|
+
knownTransports = new Map;
|
|
755
1298
|
});
|
|
756
1299
|
|
|
757
1300
|
// src/activity.ts
|
|
@@ -1068,8 +1611,8 @@ var init_network_mock = __esm(() => {
|
|
|
1068
1611
|
});
|
|
1069
1612
|
|
|
1070
1613
|
// src/driver.ts
|
|
1071
|
-
import { mkdir as mkdir3, readFile as
|
|
1072
|
-
import { join as
|
|
1614
|
+
import { mkdir as mkdir3, readFile as readFile4, unlink as unlink3, writeFile as writeFile3 } from "node:fs/promises";
|
|
1615
|
+
import { join as join4 } from "node:path";
|
|
1073
1616
|
function isDriverError(e) {
|
|
1074
1617
|
return e instanceof Error && typeof e.code === "string";
|
|
1075
1618
|
}
|
|
@@ -1085,11 +1628,11 @@ function isTiledCapture(delivery) {
|
|
|
1085
1628
|
return delivery.tiled === true;
|
|
1086
1629
|
}
|
|
1087
1630
|
function viewportRecordFile(scheme, targetId) {
|
|
1088
|
-
return
|
|
1631
|
+
return join4(leaseDir(), `viewport-${scheme}-${targetId.replace(/[^A-Za-z0-9._-]/g, "_")}.json`);
|
|
1089
1632
|
}
|
|
1090
1633
|
async function readViewportRecord(scheme, targetId) {
|
|
1091
1634
|
try {
|
|
1092
|
-
const raw = JSON.parse(await
|
|
1635
|
+
const raw = JSON.parse(await readFile4(viewportRecordFile(scheme, targetId), "utf8"));
|
|
1093
1636
|
if (typeof raw?.width !== "number" || typeof raw?.height !== "number")
|
|
1094
1637
|
return;
|
|
1095
1638
|
return {
|
|
@@ -1140,81 +1683,396 @@ var init_driver = __esm(() => {
|
|
|
1140
1683
|
};
|
|
1141
1684
|
});
|
|
1142
1685
|
|
|
1143
|
-
// src/bidi/
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
closed = false;
|
|
1152
|
-
refCount = 0;
|
|
1153
|
-
pending = new Map;
|
|
1154
|
-
listeners = new Map;
|
|
1155
|
-
constructor(wsUrl, opts = {}) {
|
|
1156
|
-
this.wsUrl = wsUrl;
|
|
1157
|
-
this.opts = opts;
|
|
1158
|
-
}
|
|
1159
|
-
get sessionId() {
|
|
1160
|
-
return this.sessId;
|
|
1161
|
-
}
|
|
1162
|
-
get capabilities() {
|
|
1163
|
-
return this.caps;
|
|
1164
|
-
}
|
|
1165
|
-
get holders() {
|
|
1166
|
-
return this.refCount;
|
|
1167
|
-
}
|
|
1168
|
-
get isOpen() {
|
|
1169
|
-
return !this.closed && this.ws !== undefined;
|
|
1170
|
-
}
|
|
1171
|
-
connect() {
|
|
1172
|
-
return new Promise((resolve, reject) => {
|
|
1173
|
-
const ws = new WebSocket(this.wsUrl);
|
|
1174
|
-
this.ws = ws;
|
|
1175
|
-
const openTimer = setTimeout(() => reject(new BidiError(`connect timeout: ${this.wsUrl}`)), this.opts.timeoutMs ?? DEFAULT_TIMEOUT_MS3);
|
|
1176
|
-
ws.onopen = () => {
|
|
1177
|
-
clearTimeout(openTimer);
|
|
1178
|
-
resolve(this);
|
|
1179
|
-
};
|
|
1180
|
-
ws.onerror = (e) => {
|
|
1181
|
-
clearTimeout(openTimer);
|
|
1182
|
-
const msg = e?.message ?? "websocket error";
|
|
1183
|
-
if (this.pending.size === 0)
|
|
1184
|
-
reject(new BidiError(msg));
|
|
1185
|
-
this.rejectAll(new BidiError(msg));
|
|
1186
|
-
};
|
|
1187
|
-
ws.onclose = () => {
|
|
1188
|
-
this.closed = true;
|
|
1189
|
-
this.rejectAll(new BidiError("connection closed"));
|
|
1190
|
-
};
|
|
1191
|
-
ws.onmessage = (ev) => this.onMessage(String(ev.data));
|
|
1192
|
-
});
|
|
1193
|
-
}
|
|
1194
|
-
async newSession(capabilities = {}) {
|
|
1195
|
-
const result = await this.send("session.new", { capabilities });
|
|
1196
|
-
this.sessId = result.sessionId;
|
|
1197
|
-
this.caps = result.capabilities;
|
|
1198
|
-
return result.sessionId;
|
|
1686
|
+
// src/bidi/snapshot.ts
|
|
1687
|
+
async function takeStampedSnapshot(evaluate) {
|
|
1688
|
+
const raw = await evaluate(buildSnapshotFunctionSource());
|
|
1689
|
+
return coerceSnapshotResult(raw);
|
|
1690
|
+
}
|
|
1691
|
+
function coerceSnapshotResult(raw) {
|
|
1692
|
+
if (typeof raw !== "object" || raw === null || typeof raw.snapshot !== "string" || typeof raw.nodeCount !== "number") {
|
|
1693
|
+
throw new Error("takeStampedSnapshot: evaluate() returned an unexpected shape");
|
|
1199
1694
|
}
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1695
|
+
return { snapshot: raw.snapshot, nodeCount: raw.nodeCount };
|
|
1696
|
+
}
|
|
1697
|
+
function buildSnapshotFunctionSource() {
|
|
1698
|
+
return `function() {
|
|
1699
|
+
var STAMP_ATTR = ${JSON.stringify(STAMP_ATTR)};
|
|
1700
|
+
var MAX_NAME_LENGTH = ${MAX_NAME_LENGTH};
|
|
1701
|
+
var lines = [];
|
|
1702
|
+
var nodeCount = 0;
|
|
1703
|
+
|
|
1704
|
+
// THE UID CODEC (driver.ts) documents a bidi payload as exactly 12 lowercase hex chars,
|
|
1705
|
+
// /^[0-9a-f]{12}$/. Build it from two padded random blocks so the length is exact regardless
|
|
1706
|
+
// of the random value drawn, never from string concatenation that could come up short.
|
|
1707
|
+
function nextStamp() {
|
|
1708
|
+
var hi = Math.floor(Math.random() * 0x100000000).toString(16).padStart(8, "0");
|
|
1709
|
+
var lo = Math.floor(Math.random() * 0x10000).toString(16).padStart(4, "0");
|
|
1710
|
+
return hi + lo;
|
|
1206
1711
|
}
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
p.resolve(msg);
|
|
1215
|
-
return;
|
|
1712
|
+
|
|
1713
|
+
function stampOf(el) {
|
|
1714
|
+
var existing = el.getAttribute(STAMP_ATTR);
|
|
1715
|
+
if (existing) return existing;
|
|
1716
|
+
var stamp = nextStamp();
|
|
1717
|
+
el.setAttribute(STAMP_ATTR, stamp);
|
|
1718
|
+
return stamp;
|
|
1216
1719
|
}
|
|
1217
|
-
|
|
1720
|
+
|
|
1721
|
+
// skipSizeCheck exempts <option>/<optgroup> from the zero-size prune: a
|
|
1722
|
+
// closed select's popup is unpainted (0x0) though its options are still
|
|
1723
|
+
// present and pickable. Other visibility rules still apply to them.
|
|
1724
|
+
function isVisible(el, skipSizeCheck) {
|
|
1725
|
+
if (el.hidden) return false;
|
|
1726
|
+
if (el.getAttribute("aria-hidden") === "true") return false;
|
|
1727
|
+
var style;
|
|
1728
|
+
try {
|
|
1729
|
+
style = el.ownerDocument.defaultView.getComputedStyle(el);
|
|
1730
|
+
} catch (e) {
|
|
1731
|
+
style = null;
|
|
1732
|
+
}
|
|
1733
|
+
if (style) {
|
|
1734
|
+
if (style.display === "none" || style.visibility === "hidden") return false;
|
|
1735
|
+
}
|
|
1736
|
+
if (!skipSizeCheck) {
|
|
1737
|
+
var rect = el.getBoundingClientRect();
|
|
1738
|
+
if (rect.width === 0 && rect.height === 0) return false;
|
|
1739
|
+
}
|
|
1740
|
+
return true;
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
${ACCNAME_HELPERS_SOURCE}
|
|
1744
|
+
|
|
1745
|
+
function headingLevel(el) {
|
|
1746
|
+
var m = /^H([1-6])$/.exec(el.tagName);
|
|
1747
|
+
return m ? m[1] : null;
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
function currentValue(el) {
|
|
1751
|
+
var tag = el.tagName;
|
|
1752
|
+
if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT") {
|
|
1753
|
+
return el.value;
|
|
1754
|
+
}
|
|
1755
|
+
if (el.isContentEditable) return textOf(el);
|
|
1756
|
+
return undefined;
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
function isChecked(el) {
|
|
1760
|
+
var tag = el.tagName;
|
|
1761
|
+
if (tag === "INPUT") {
|
|
1762
|
+
var type = (el.getAttribute("type") || "").toLowerCase();
|
|
1763
|
+
if (type === "checkbox" || type === "radio") return !!el.checked;
|
|
1764
|
+
}
|
|
1765
|
+
var aria = el.getAttribute("aria-checked");
|
|
1766
|
+
if (aria === "true") return true;
|
|
1767
|
+
return false;
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
function describeNode(el, role) {
|
|
1771
|
+
var contentNamed = !!CONTENT_NAMED_ROLES[role];
|
|
1772
|
+
var name = accessibleName(el, contentNamed);
|
|
1773
|
+
var extras = [];
|
|
1774
|
+
var value = currentValue(el);
|
|
1775
|
+
if (value !== undefined && value !== "") extras.push("value=" + JSON.stringify(String(value)));
|
|
1776
|
+
if (isChecked(el)) extras.push("checked=true");
|
|
1777
|
+
if (el.tagName === "OPTION" && el.selected) extras.push("selected");
|
|
1778
|
+
var href = el.getAttribute("href");
|
|
1779
|
+
if (href) extras.push("url=" + href);
|
|
1780
|
+
if (el.disabled || el.getAttribute("aria-disabled") === "true") extras.push("disabled");
|
|
1781
|
+
var level = headingLevel(el);
|
|
1782
|
+
if (level) extras.push("level=" + level);
|
|
1783
|
+
var label = name ? " " + JSON.stringify(name) : "";
|
|
1784
|
+
var extra = extras.length ? " [" + extras.join(" ") + "]" : "";
|
|
1785
|
+
return { role: role, label: label, extra: extra, name: name };
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
// Traverses light DOM, open shadow roots, and same-origin iframe documents.
|
|
1789
|
+
// A cross-origin iframe throws on contentDocument access; caught and
|
|
1790
|
+
// skipped rather than aborting the whole walk, and noted in a comment
|
|
1791
|
+
// here rather than silently vanishing from the tree without a trace.
|
|
1792
|
+
function walk(root, depth) {
|
|
1793
|
+
var children = root.children ? Array.prototype.slice.call(root.children) : [];
|
|
1794
|
+
for (var i = 0; i < children.length; i++) {
|
|
1795
|
+
var el = children[i];
|
|
1796
|
+
var isSelectChild = el.tagName === "OPTION" || el.tagName === "OPTGROUP";
|
|
1797
|
+
if (!isVisible(el, isSelectChild)) continue;
|
|
1798
|
+
|
|
1799
|
+
var role = implicitRole(el);
|
|
1800
|
+
var isLandmarkOrControl = !!role;
|
|
1801
|
+
var described = null;
|
|
1802
|
+
if (isLandmarkOrControl) {
|
|
1803
|
+
described = describeNode(el, role);
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
var hasContent = described && (described.name || described.extra);
|
|
1807
|
+
var isStructural = isLandmarkOrControl && (hasContent || isInteractiveRole(role) || isLandmarkRole(role));
|
|
1808
|
+
|
|
1809
|
+
// Role-less visible leaf text (measured vs Chrome's take_snapshot,
|
|
1810
|
+
// which keeps a StaticText under a generic wrapper for this case,
|
|
1811
|
+
// e.g. a plain status <div>). Only a leaf (no element children, so
|
|
1812
|
+
// its text is not already captured by a nested control/label).
|
|
1813
|
+
var hasElementChildren = !!(el.children && el.children.length > 0);
|
|
1814
|
+
var leafName = !isStructural && !hasElementChildren ? truncate(textOf(el)) : "";
|
|
1815
|
+
var isLeafText = !!leafName;
|
|
1816
|
+
|
|
1817
|
+
var nextDepth = depth;
|
|
1818
|
+
if (isStructural || isLeafText) {
|
|
1819
|
+
var stamp = stampOf(el);
|
|
1820
|
+
var indent = new Array(depth + 1).join(" ");
|
|
1821
|
+
var lineTail = isStructural
|
|
1822
|
+
? described.role + described.label + described.extra
|
|
1823
|
+
: "text " + JSON.stringify(leafName);
|
|
1824
|
+
lines.push(indent + "[" + stamp + "] " + lineTail);
|
|
1825
|
+
nodeCount += 1;
|
|
1826
|
+
nextDepth = depth + 1;
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
if (el.shadowRoot) {
|
|
1830
|
+
walk(el.shadowRoot, nextDepth);
|
|
1831
|
+
} else if (el.tagName === "IFRAME") {
|
|
1832
|
+
try {
|
|
1833
|
+
var doc = el.contentDocument;
|
|
1834
|
+
if (doc && doc.body) walk(doc.body, nextDepth);
|
|
1835
|
+
} catch (e) {
|
|
1836
|
+
// cross-origin iframe: intentionally skipped, not silently merged
|
|
1837
|
+
// into the parent tree as though it were empty.
|
|
1838
|
+
}
|
|
1839
|
+
} else {
|
|
1840
|
+
walk(el, nextDepth);
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
|
|
1845
|
+
function isInteractiveRole(role) {
|
|
1846
|
+
return (
|
|
1847
|
+
role === "button" ||
|
|
1848
|
+
role === "link" ||
|
|
1849
|
+
role === "textbox" ||
|
|
1850
|
+
role === "searchbox" ||
|
|
1851
|
+
role === "checkbox" ||
|
|
1852
|
+
role === "radio" ||
|
|
1853
|
+
role === "combobox" ||
|
|
1854
|
+
role === "listbox" ||
|
|
1855
|
+
role === "option" ||
|
|
1856
|
+
role === "slider" ||
|
|
1857
|
+
role === "spinbutton" ||
|
|
1858
|
+
role === "progressbar" ||
|
|
1859
|
+
role === "meter"
|
|
1860
|
+
);
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
function isLandmarkRole(role) {
|
|
1864
|
+
return (
|
|
1865
|
+
role === "navigation" ||
|
|
1866
|
+
role === "main" ||
|
|
1867
|
+
role === "form" ||
|
|
1868
|
+
role === "dialog" ||
|
|
1869
|
+
role === "region" ||
|
|
1870
|
+
role === "heading" ||
|
|
1871
|
+
role === "list" ||
|
|
1872
|
+
role === "listitem" ||
|
|
1873
|
+
role === "table" ||
|
|
1874
|
+
role === "row" ||
|
|
1875
|
+
role === "cell" ||
|
|
1876
|
+
role === "columnheader" ||
|
|
1877
|
+
role === "img"
|
|
1878
|
+
);
|
|
1879
|
+
}
|
|
1880
|
+
|
|
1881
|
+
walk(document.body, 0);
|
|
1882
|
+
return { snapshot: lines.join("\\n"), nodeCount: nodeCount };
|
|
1883
|
+
}`;
|
|
1884
|
+
}
|
|
1885
|
+
var STAMP_ATTR = "data-cdp-uid", ACCNAME_HELPERS_SOURCE = `
|
|
1886
|
+
function truncate(s) {
|
|
1887
|
+
s = s.trim().replace(/\\s+/g, " ");
|
|
1888
|
+
if (s.length > MAX_NAME_LENGTH) return s.slice(0, MAX_NAME_LENGTH - 1) + "\\u2026";
|
|
1889
|
+
return s;
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1892
|
+
function textOf(el) {
|
|
1893
|
+
return el.textContent || "";
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
function idRefsText(doc, ids) {
|
|
1897
|
+
var parts = [];
|
|
1898
|
+
var list = ids.trim().split(/\\s+/);
|
|
1899
|
+
for (var i = 0; i < list.length; i++) {
|
|
1900
|
+
var ref = doc.getElementById(list[i]);
|
|
1901
|
+
if (ref) parts.push(textOf(ref));
|
|
1902
|
+
}
|
|
1903
|
+
return parts.join(" ").trim();
|
|
1904
|
+
}
|
|
1905
|
+
|
|
1906
|
+
// Text of a <label>, minus any nested form control's own content: a
|
|
1907
|
+
// label that WRAPS its control ("<label>Color <select>...</select></label>")
|
|
1908
|
+
// would otherwise include every option's text in the control's own name.
|
|
1909
|
+
function labelOwnText(label) {
|
|
1910
|
+
var clone = label.cloneNode(true);
|
|
1911
|
+
var controls = clone.querySelectorAll("input, select, textarea, button");
|
|
1912
|
+
for (var i = 0; i < controls.length; i++) {
|
|
1913
|
+
controls[i].parentNode.removeChild(controls[i]);
|
|
1914
|
+
}
|
|
1915
|
+
return textOf(clone);
|
|
1916
|
+
}
|
|
1917
|
+
|
|
1918
|
+
// Guard: without this, an <option> inside a labelled <select> would
|
|
1919
|
+
// inherit the select's own label via closest("label"), not its text.
|
|
1920
|
+
var LABELABLE_TAGS = { INPUT: 1, TEXTAREA: 1, SELECT: 1, BUTTON: 1, METER: 1, OUTPUT: 1, PROGRESS: 1 };
|
|
1921
|
+
|
|
1922
|
+
function nativeLabelText(el) {
|
|
1923
|
+
if (!LABELABLE_TAGS[el.tagName]) return "";
|
|
1924
|
+
var doc = el.ownerDocument;
|
|
1925
|
+
if (el.id) {
|
|
1926
|
+
var forLabel = doc.querySelector('label[for="' + el.id + '"]');
|
|
1927
|
+
if (forLabel) return labelOwnText(forLabel);
|
|
1928
|
+
}
|
|
1929
|
+
var ancestor = el.closest ? el.closest("label") : null;
|
|
1930
|
+
if (ancestor) return labelOwnText(ancestor);
|
|
1931
|
+
return "";
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1934
|
+
// Reduced accname: aria-labelledby, then aria-label, then native label,
|
|
1935
|
+
// then alt/title/placeholder, then trimmed own text for content-named
|
|
1936
|
+
// elements. Not the full W3C accname algorithm (no recursive traversal
|
|
1937
|
+
// rules for nested presentational content) but covers the common cases.
|
|
1938
|
+
function accessibleName(el, contentNamed) {
|
|
1939
|
+
var doc = el.ownerDocument;
|
|
1940
|
+
var labelledby = el.getAttribute("aria-labelledby");
|
|
1941
|
+
if (labelledby) {
|
|
1942
|
+
var viaIds = idRefsText(doc, labelledby);
|
|
1943
|
+
if (viaIds) return truncate(viaIds);
|
|
1944
|
+
}
|
|
1945
|
+
var ariaLabel = el.getAttribute("aria-label");
|
|
1946
|
+
if (ariaLabel && ariaLabel.trim()) return truncate(ariaLabel);
|
|
1947
|
+
var native = nativeLabelText(el);
|
|
1948
|
+
if (native && native.trim()) return truncate(native);
|
|
1949
|
+
var alt = el.getAttribute("alt");
|
|
1950
|
+
if (alt && alt.trim()) return truncate(alt);
|
|
1951
|
+
var title = el.getAttribute("title");
|
|
1952
|
+
if (title && title.trim()) return truncate(title);
|
|
1953
|
+
var placeholder = el.getAttribute("placeholder");
|
|
1954
|
+
if (placeholder && placeholder.trim()) return truncate(placeholder);
|
|
1955
|
+
if (contentNamed) {
|
|
1956
|
+
var text = textOf(el);
|
|
1957
|
+
if (text && text.trim()) return truncate(text);
|
|
1958
|
+
}
|
|
1959
|
+
return "";
|
|
1960
|
+
}
|
|
1961
|
+
|
|
1962
|
+
var IMPLICIT_ROLE_BY_TAG = {
|
|
1963
|
+
A: "link", BUTTON: "button", NAV: "navigation", MAIN: "main", FORM: "form", DIALOG: "dialog",
|
|
1964
|
+
UL: "list", OL: "list", LI: "listitem", TABLE: "table", TR: "row", TD: "cell", TH: "columnheader",
|
|
1965
|
+
SELECT: "combobox", OPTION: "option", TEXTAREA: "textbox", IMG: "img",
|
|
1966
|
+
H1: "heading", H2: "heading", H3: "heading", H4: "heading", H5: "heading", H6: "heading",
|
|
1967
|
+
PROGRESS: "progressbar", METER: "meter"
|
|
1968
|
+
};
|
|
1969
|
+
|
|
1970
|
+
var INPUT_TYPE_ROLE = {
|
|
1971
|
+
button: "button", submit: "button", reset: "button", checkbox: "checkbox", radio: "radio",
|
|
1972
|
+
range: "slider", number: "spinbutton", search: "searchbox", email: "textbox", tel: "textbox",
|
|
1973
|
+
url: "textbox", password: "textbox", text: "textbox", image: "button"
|
|
1974
|
+
};
|
|
1975
|
+
|
|
1976
|
+
var CONTENT_NAMED_ROLES = {
|
|
1977
|
+
button: true, link: true, heading: true, option: true, cell: true,
|
|
1978
|
+
columnheader: true, listitem: true, menuitem: true, tab: true
|
|
1979
|
+
};
|
|
1980
|
+
|
|
1981
|
+
function implicitRole(el) {
|
|
1982
|
+
var explicit = el.getAttribute("role");
|
|
1983
|
+
if (explicit) return explicit;
|
|
1984
|
+
var tag = el.tagName;
|
|
1985
|
+
if (tag === "INPUT") {
|
|
1986
|
+
var type = (el.getAttribute("type") || "text").toLowerCase();
|
|
1987
|
+
return INPUT_TYPE_ROLE[type] || "textbox";
|
|
1988
|
+
}
|
|
1989
|
+
if (tag === "A" && !el.getAttribute("href")) return "";
|
|
1990
|
+
if (Object.prototype.hasOwnProperty.call(IMPLICIT_ROLE_BY_TAG, tag)) {
|
|
1991
|
+
return IMPLICIT_ROLE_BY_TAG[tag];
|
|
1992
|
+
}
|
|
1993
|
+
// Deliberate divergence: Chrome's real AX tree says "generic" here,
|
|
1994
|
+
// but "textbox" is the useful signal an agent needs. Chrome is out of
|
|
1995
|
+
// step, do not "fix" this toward parity.
|
|
1996
|
+
if (el.isContentEditable) return "textbox";
|
|
1997
|
+
return "";
|
|
1998
|
+
}
|
|
1999
|
+
`, MAX_NAME_LENGTH = 120;
|
|
2000
|
+
|
|
2001
|
+
// src/bidi/client.ts
|
|
2002
|
+
class BidiConnection {
|
|
2003
|
+
wsUrl;
|
|
2004
|
+
opts;
|
|
2005
|
+
ws;
|
|
2006
|
+
nextId = 0;
|
|
2007
|
+
sessId;
|
|
2008
|
+
caps;
|
|
2009
|
+
closed = false;
|
|
2010
|
+
refCount = 0;
|
|
2011
|
+
pending = new Map;
|
|
2012
|
+
listeners = new Map;
|
|
2013
|
+
constructor(wsUrl, opts = {}) {
|
|
2014
|
+
this.wsUrl = wsUrl;
|
|
2015
|
+
this.opts = opts;
|
|
2016
|
+
}
|
|
2017
|
+
get sessionId() {
|
|
2018
|
+
return this.sessId;
|
|
2019
|
+
}
|
|
2020
|
+
get capabilities() {
|
|
2021
|
+
return this.caps;
|
|
2022
|
+
}
|
|
2023
|
+
get holders() {
|
|
2024
|
+
return this.refCount;
|
|
2025
|
+
}
|
|
2026
|
+
get isOpen() {
|
|
2027
|
+
return !this.closed && this.ws !== undefined;
|
|
2028
|
+
}
|
|
2029
|
+
connect() {
|
|
2030
|
+
return new Promise((resolve, reject) => {
|
|
2031
|
+
const ws = new WebSocket(this.wsUrl);
|
|
2032
|
+
this.ws = ws;
|
|
2033
|
+
const openTimer = setTimeout(() => reject(new BidiError(`connect timeout: ${this.wsUrl}`)), this.opts.timeoutMs ?? DEFAULT_TIMEOUT_MS3);
|
|
2034
|
+
ws.onopen = () => {
|
|
2035
|
+
clearTimeout(openTimer);
|
|
2036
|
+
resolve(this);
|
|
2037
|
+
};
|
|
2038
|
+
ws.onerror = (e) => {
|
|
2039
|
+
clearTimeout(openTimer);
|
|
2040
|
+
const msg = e?.message ?? "websocket error";
|
|
2041
|
+
if (this.pending.size === 0)
|
|
2042
|
+
reject(new BidiError(msg));
|
|
2043
|
+
this.rejectAll(new BidiError(msg));
|
|
2044
|
+
};
|
|
2045
|
+
ws.onclose = () => {
|
|
2046
|
+
this.closed = true;
|
|
2047
|
+
this.rejectAll(new BidiError("connection closed"));
|
|
2048
|
+
};
|
|
2049
|
+
ws.onmessage = (ev) => this.onMessage(String(ev.data));
|
|
2050
|
+
});
|
|
2051
|
+
}
|
|
2052
|
+
async newSession(capabilities = {}) {
|
|
2053
|
+
const result = await this.send("session.new", { capabilities });
|
|
2054
|
+
this.sessId = result.sessionId;
|
|
2055
|
+
this.caps = result.capabilities;
|
|
2056
|
+
return result.sessionId;
|
|
2057
|
+
}
|
|
2058
|
+
onMessage(raw) {
|
|
2059
|
+
let msg;
|
|
2060
|
+
try {
|
|
2061
|
+
msg = JSON.parse(raw);
|
|
2062
|
+
} catch {
|
|
2063
|
+
return;
|
|
2064
|
+
}
|
|
2065
|
+
if (msg.type === "success" || msg.type === "error") {
|
|
2066
|
+
const id = msg.id;
|
|
2067
|
+
if (typeof id !== "number" || !this.pending.has(id))
|
|
2068
|
+
return;
|
|
2069
|
+
const p = this.pending.get(id);
|
|
2070
|
+
clearTimeout(p.timer);
|
|
2071
|
+
this.pending.delete(id);
|
|
2072
|
+
p.resolve(msg);
|
|
2073
|
+
return;
|
|
2074
|
+
}
|
|
2075
|
+
if (msg.type === "event") {
|
|
1218
2076
|
const set = this.listeners.get(msg.method);
|
|
1219
2077
|
if (set)
|
|
1220
2078
|
for (const h of set)
|
|
@@ -1339,16 +2197,16 @@ var init_client2 = __esm(() => {
|
|
|
1339
2197
|
});
|
|
1340
2198
|
|
|
1341
2199
|
// src/bidi/session-lease.ts
|
|
1342
|
-
import { mkdir as mkdir13, readFile as
|
|
1343
|
-
import { join as
|
|
2200
|
+
import { mkdir as mkdir13, readFile as readFile9, rename as rename3, unlink as unlink5, writeFile as writeFile10 } from "node:fs/promises";
|
|
2201
|
+
import { join as join12 } from "node:path";
|
|
1344
2202
|
function safeEndpoint(endpoint) {
|
|
1345
2203
|
return endpoint.replace(/[^A-Za-z0-9._-]/g, "_");
|
|
1346
2204
|
}
|
|
1347
2205
|
function sessionSlotFile(endpoint) {
|
|
1348
|
-
return
|
|
2206
|
+
return join12(leaseDir(), `ff-session-${safeEndpoint(endpoint)}.json`);
|
|
1349
2207
|
}
|
|
1350
2208
|
function muxDaemonLogFile(endpoint) {
|
|
1351
|
-
return
|
|
2209
|
+
return join12(leaseDir(), `ff-mux-${safeEndpoint(endpoint)}.log`);
|
|
1352
2210
|
}
|
|
1353
2211
|
function sessionWaitMs() {
|
|
1354
2212
|
const raw = Number(process.env.CDP_FIREFOX_SESSION_WAIT_MS);
|
|
@@ -1357,7 +2215,7 @@ function sessionWaitMs() {
|
|
|
1357
2215
|
async function readSessionSlot(endpoint) {
|
|
1358
2216
|
let raw;
|
|
1359
2217
|
try {
|
|
1360
|
-
raw = await
|
|
2218
|
+
raw = await readFile9(sessionSlotFile(endpoint), "utf8");
|
|
1361
2219
|
} catch (err) {
|
|
1362
2220
|
if (err.code === "ENOENT")
|
|
1363
2221
|
return;
|
|
@@ -2006,391 +2864,78 @@ function isSlotFreedReply(frame) {
|
|
|
2006
2864
|
const err = frame[2];
|
|
2007
2865
|
if (err === null)
|
|
2008
2866
|
return true;
|
|
2009
|
-
if (typeof err === "object" && err !== null && err.error === "invalid session id") {
|
|
2010
|
-
return true;
|
|
2011
|
-
}
|
|
2012
|
-
return false;
|
|
2013
|
-
}
|
|
2014
|
-
function forceClearOrphanSession(marionettePort, opts = {}) {
|
|
2015
|
-
const timeoutMs = opts.timeoutMs !== undefined && Number.isFinite(opts.timeoutMs) && opts.timeoutMs > 0 ? opts.timeoutMs : DEFAULT_TIMEOUT_MS4;
|
|
2016
|
-
return new Promise((resolve) => {
|
|
2017
|
-
if (!Number.isFinite(marionettePort) || marionettePort <= 0 || marionettePort > 65535) {
|
|
2018
|
-
resolve(false);
|
|
2019
|
-
return;
|
|
2020
|
-
}
|
|
2021
|
-
const sock = new Socket;
|
|
2022
|
-
const decoder = new FrameDecoder;
|
|
2023
|
-
let settled = false;
|
|
2024
|
-
let handshakeSeen = false;
|
|
2025
|
-
let commandSent = false;
|
|
2026
|
-
const settle = (value) => {
|
|
2027
|
-
if (settled)
|
|
2028
|
-
return;
|
|
2029
|
-
settled = true;
|
|
2030
|
-
clearTimeout(timer);
|
|
2031
|
-
sock.removeAllListeners();
|
|
2032
|
-
sock.destroy();
|
|
2033
|
-
resolve(value);
|
|
2034
|
-
};
|
|
2035
|
-
const timer = setTimeout(() => settle(false), timeoutMs);
|
|
2036
|
-
if (typeof timer.unref === "function")
|
|
2037
|
-
timer.unref();
|
|
2038
|
-
sock.on("error", () => settle(false));
|
|
2039
|
-
sock.on("close", () => settle(false));
|
|
2040
|
-
sock.on("data", (chunk) => {
|
|
2041
|
-
if (settled)
|
|
2042
|
-
return;
|
|
2043
|
-
try {
|
|
2044
|
-
decoder.push(chunk);
|
|
2045
|
-
for (;; ) {
|
|
2046
|
-
const frame = decoder.next();
|
|
2047
|
-
if (frame === undefined)
|
|
2048
|
-
break;
|
|
2049
|
-
if (!handshakeSeen) {
|
|
2050
|
-
handshakeSeen = true;
|
|
2051
|
-
continue;
|
|
2052
|
-
}
|
|
2053
|
-
settle(isSlotFreedReply(frame));
|
|
2054
|
-
return;
|
|
2055
|
-
}
|
|
2056
|
-
if (handshakeSeen && !commandSent) {
|
|
2057
|
-
commandSent = true;
|
|
2058
|
-
sock.write(DELETE_SESSION_FRAME);
|
|
2059
|
-
}
|
|
2060
|
-
} catch {
|
|
2061
|
-
settle(false);
|
|
2062
|
-
}
|
|
2063
|
-
});
|
|
2064
|
-
try {
|
|
2065
|
-
sock.connect(marionettePort, HOST);
|
|
2066
|
-
} catch {
|
|
2067
|
-
settle(false);
|
|
2068
|
-
}
|
|
2069
|
-
});
|
|
2070
|
-
}
|
|
2071
|
-
var DEFAULT_MARIONETTE_PORT = 2828, DEFAULT_TIMEOUT_MS4 = 4000, HOST = "127.0.0.1", COLON = 58, DELETE_SESSION_FRAME, MarionetteProtocolError;
|
|
2072
|
-
var init_marionette = __esm(() => {
|
|
2073
|
-
DELETE_SESSION_FRAME = encodeFrame2([0, 1, "WebDriver:DeleteSession", {}]);
|
|
2074
|
-
MarionetteProtocolError = class MarionetteProtocolError extends Error {
|
|
2075
|
-
constructor(message) {
|
|
2076
|
-
super(message);
|
|
2077
|
-
this.name = "MarionetteProtocolError";
|
|
2078
|
-
}
|
|
2079
|
-
};
|
|
2080
|
-
});
|
|
2081
|
-
|
|
2082
|
-
// src/bidi/snapshot.ts
|
|
2083
|
-
async function takeStampedSnapshot(evaluate) {
|
|
2084
|
-
const raw = await evaluate(buildSnapshotFunctionSource());
|
|
2085
|
-
return coerceSnapshotResult(raw);
|
|
2086
|
-
}
|
|
2087
|
-
function coerceSnapshotResult(raw) {
|
|
2088
|
-
if (typeof raw !== "object" || raw === null || typeof raw.snapshot !== "string" || typeof raw.nodeCount !== "number") {
|
|
2089
|
-
throw new Error("takeStampedSnapshot: evaluate() returned an unexpected shape");
|
|
2090
|
-
}
|
|
2091
|
-
return { snapshot: raw.snapshot, nodeCount: raw.nodeCount };
|
|
2092
|
-
}
|
|
2093
|
-
function buildSnapshotFunctionSource() {
|
|
2094
|
-
return `function() {
|
|
2095
|
-
var STAMP_ATTR = ${JSON.stringify(STAMP_ATTR)};
|
|
2096
|
-
var MAX_NAME_LENGTH = ${MAX_NAME_LENGTH};
|
|
2097
|
-
var lines = [];
|
|
2098
|
-
var nodeCount = 0;
|
|
2099
|
-
|
|
2100
|
-
// THE UID CODEC (driver.ts) documents a bidi payload as exactly 12 lowercase hex chars,
|
|
2101
|
-
// /^[0-9a-f]{12}$/. Build it from two padded random blocks so the length is exact regardless
|
|
2102
|
-
// of the random value drawn, never from string concatenation that could come up short.
|
|
2103
|
-
function nextStamp() {
|
|
2104
|
-
var hi = Math.floor(Math.random() * 0x100000000).toString(16).padStart(8, "0");
|
|
2105
|
-
var lo = Math.floor(Math.random() * 0x10000).toString(16).padStart(4, "0");
|
|
2106
|
-
return hi + lo;
|
|
2107
|
-
}
|
|
2108
|
-
|
|
2109
|
-
function stampOf(el) {
|
|
2110
|
-
var existing = el.getAttribute(STAMP_ATTR);
|
|
2111
|
-
if (existing) return existing;
|
|
2112
|
-
var stamp = nextStamp();
|
|
2113
|
-
el.setAttribute(STAMP_ATTR, stamp);
|
|
2114
|
-
return stamp;
|
|
2115
|
-
}
|
|
2116
|
-
|
|
2117
|
-
// skipSizeCheck exempts <option>/<optgroup> from the zero-size prune: a
|
|
2118
|
-
// closed select's popup is unpainted (0x0) though its options are still
|
|
2119
|
-
// present and pickable. Other visibility rules still apply to them.
|
|
2120
|
-
function isVisible(el, skipSizeCheck) {
|
|
2121
|
-
if (el.hidden) return false;
|
|
2122
|
-
if (el.getAttribute("aria-hidden") === "true") return false;
|
|
2123
|
-
var style;
|
|
2124
|
-
try {
|
|
2125
|
-
style = el.ownerDocument.defaultView.getComputedStyle(el);
|
|
2126
|
-
} catch (e) {
|
|
2127
|
-
style = null;
|
|
2128
|
-
}
|
|
2129
|
-
if (style) {
|
|
2130
|
-
if (style.display === "none" || style.visibility === "hidden") return false;
|
|
2131
|
-
}
|
|
2132
|
-
if (!skipSizeCheck) {
|
|
2133
|
-
var rect = el.getBoundingClientRect();
|
|
2134
|
-
if (rect.width === 0 && rect.height === 0) return false;
|
|
2135
|
-
}
|
|
2136
|
-
return true;
|
|
2137
|
-
}
|
|
2138
|
-
|
|
2139
|
-
function truncate(s) {
|
|
2140
|
-
s = s.trim().replace(/\\s+/g, " ");
|
|
2141
|
-
if (s.length > MAX_NAME_LENGTH) return s.slice(0, MAX_NAME_LENGTH - 1) + "\\u2026";
|
|
2142
|
-
return s;
|
|
2143
|
-
}
|
|
2144
|
-
|
|
2145
|
-
function textOf(el) {
|
|
2146
|
-
return el.textContent || "";
|
|
2147
|
-
}
|
|
2148
|
-
|
|
2149
|
-
function idRefsText(doc, ids) {
|
|
2150
|
-
var parts = [];
|
|
2151
|
-
var list = ids.trim().split(/\\s+/);
|
|
2152
|
-
for (var i = 0; i < list.length; i++) {
|
|
2153
|
-
var ref = doc.getElementById(list[i]);
|
|
2154
|
-
if (ref) parts.push(textOf(ref));
|
|
2155
|
-
}
|
|
2156
|
-
return parts.join(" ").trim();
|
|
2157
|
-
}
|
|
2158
|
-
|
|
2159
|
-
// Text of a <label>, minus any nested form control's own content: a
|
|
2160
|
-
// label that WRAPS its control ("<label>Color <select>...</select></label>")
|
|
2161
|
-
// would otherwise include every option's text in the control's own name.
|
|
2162
|
-
function labelOwnText(label) {
|
|
2163
|
-
var clone = label.cloneNode(true);
|
|
2164
|
-
var controls = clone.querySelectorAll("input, select, textarea, button");
|
|
2165
|
-
for (var i = 0; i < controls.length; i++) {
|
|
2166
|
-
controls[i].parentNode.removeChild(controls[i]);
|
|
2167
|
-
}
|
|
2168
|
-
return textOf(clone);
|
|
2169
|
-
}
|
|
2170
|
-
|
|
2171
|
-
// Guard: without this, an <option> inside a labelled <select> would
|
|
2172
|
-
// inherit the select's own label via closest("label"), not its text.
|
|
2173
|
-
var LABELABLE_TAGS = { INPUT: 1, TEXTAREA: 1, SELECT: 1, BUTTON: 1, METER: 1, OUTPUT: 1, PROGRESS: 1 };
|
|
2174
|
-
|
|
2175
|
-
function nativeLabelText(el) {
|
|
2176
|
-
if (!LABELABLE_TAGS[el.tagName]) return "";
|
|
2177
|
-
var doc = el.ownerDocument;
|
|
2178
|
-
if (el.id) {
|
|
2179
|
-
var forLabel = doc.querySelector('label[for="' + el.id + '"]');
|
|
2180
|
-
if (forLabel) return labelOwnText(forLabel);
|
|
2181
|
-
}
|
|
2182
|
-
var ancestor = el.closest ? el.closest("label") : null;
|
|
2183
|
-
if (ancestor) return labelOwnText(ancestor);
|
|
2184
|
-
return "";
|
|
2185
|
-
}
|
|
2186
|
-
|
|
2187
|
-
// Reduced accname: aria-labelledby, then aria-label, then native label,
|
|
2188
|
-
// then alt/title/placeholder, then trimmed own text for content-named
|
|
2189
|
-
// elements. Not the full W3C accname algorithm (no recursive traversal
|
|
2190
|
-
// rules for nested presentational content) but covers the common cases.
|
|
2191
|
-
function accessibleName(el, contentNamed) {
|
|
2192
|
-
var doc = el.ownerDocument;
|
|
2193
|
-
var labelledby = el.getAttribute("aria-labelledby");
|
|
2194
|
-
if (labelledby) {
|
|
2195
|
-
var viaIds = idRefsText(doc, labelledby);
|
|
2196
|
-
if (viaIds) return truncate(viaIds);
|
|
2197
|
-
}
|
|
2198
|
-
var ariaLabel = el.getAttribute("aria-label");
|
|
2199
|
-
if (ariaLabel && ariaLabel.trim()) return truncate(ariaLabel);
|
|
2200
|
-
var native = nativeLabelText(el);
|
|
2201
|
-
if (native && native.trim()) return truncate(native);
|
|
2202
|
-
var alt = el.getAttribute("alt");
|
|
2203
|
-
if (alt && alt.trim()) return truncate(alt);
|
|
2204
|
-
var title = el.getAttribute("title");
|
|
2205
|
-
if (title && title.trim()) return truncate(title);
|
|
2206
|
-
var placeholder = el.getAttribute("placeholder");
|
|
2207
|
-
if (placeholder && placeholder.trim()) return truncate(placeholder);
|
|
2208
|
-
if (contentNamed) {
|
|
2209
|
-
var text = textOf(el);
|
|
2210
|
-
if (text && text.trim()) return truncate(text);
|
|
2211
|
-
}
|
|
2212
|
-
return "";
|
|
2213
|
-
}
|
|
2214
|
-
|
|
2215
|
-
var IMPLICIT_ROLE_BY_TAG = {
|
|
2216
|
-
A: "link", BUTTON: "button", NAV: "navigation", MAIN: "main", FORM: "form", DIALOG: "dialog",
|
|
2217
|
-
UL: "list", OL: "list", LI: "listitem", TABLE: "table", TR: "row", TD: "cell", TH: "columnheader",
|
|
2218
|
-
SELECT: "combobox", OPTION: "option", TEXTAREA: "textbox", IMG: "img",
|
|
2219
|
-
H1: "heading", H2: "heading", H3: "heading", H4: "heading", H5: "heading", H6: "heading",
|
|
2220
|
-
PROGRESS: "progressbar", METER: "meter"
|
|
2221
|
-
};
|
|
2222
|
-
|
|
2223
|
-
var INPUT_TYPE_ROLE = {
|
|
2224
|
-
button: "button", submit: "button", reset: "button", checkbox: "checkbox", radio: "radio",
|
|
2225
|
-
range: "slider", number: "spinbutton", search: "searchbox", email: "textbox", tel: "textbox",
|
|
2226
|
-
url: "textbox", password: "textbox", text: "textbox", image: "button"
|
|
2227
|
-
};
|
|
2228
|
-
|
|
2229
|
-
var CONTENT_NAMED_ROLES = {
|
|
2230
|
-
button: true, link: true, heading: true, option: true, cell: true,
|
|
2231
|
-
columnheader: true, listitem: true, menuitem: true, tab: true
|
|
2232
|
-
};
|
|
2233
|
-
|
|
2234
|
-
function implicitRole(el) {
|
|
2235
|
-
var explicit = el.getAttribute("role");
|
|
2236
|
-
if (explicit) return explicit;
|
|
2237
|
-
var tag = el.tagName;
|
|
2238
|
-
if (tag === "INPUT") {
|
|
2239
|
-
var type = (el.getAttribute("type") || "text").toLowerCase();
|
|
2240
|
-
return INPUT_TYPE_ROLE[type] || "textbox";
|
|
2241
|
-
}
|
|
2242
|
-
if (tag === "A" && !el.getAttribute("href")) return "";
|
|
2243
|
-
if (Object.prototype.hasOwnProperty.call(IMPLICIT_ROLE_BY_TAG, tag)) {
|
|
2244
|
-
return IMPLICIT_ROLE_BY_TAG[tag];
|
|
2245
|
-
}
|
|
2246
|
-
// Deliberate divergence: Chrome's real AX tree says "generic" here,
|
|
2247
|
-
// but "textbox" is the useful signal an agent needs. Chrome is out of
|
|
2248
|
-
// step, do not "fix" this toward parity.
|
|
2249
|
-
if (el.isContentEditable) return "textbox";
|
|
2250
|
-
return "";
|
|
2251
|
-
}
|
|
2252
|
-
|
|
2253
|
-
function headingLevel(el) {
|
|
2254
|
-
var m = /^H([1-6])$/.exec(el.tagName);
|
|
2255
|
-
return m ? m[1] : null;
|
|
2256
|
-
}
|
|
2257
|
-
|
|
2258
|
-
function currentValue(el) {
|
|
2259
|
-
var tag = el.tagName;
|
|
2260
|
-
if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT") {
|
|
2261
|
-
return el.value;
|
|
2262
|
-
}
|
|
2263
|
-
if (el.isContentEditable) return textOf(el);
|
|
2264
|
-
return undefined;
|
|
2265
|
-
}
|
|
2266
|
-
|
|
2267
|
-
function isChecked(el) {
|
|
2268
|
-
var tag = el.tagName;
|
|
2269
|
-
if (tag === "INPUT") {
|
|
2270
|
-
var type = (el.getAttribute("type") || "").toLowerCase();
|
|
2271
|
-
if (type === "checkbox" || type === "radio") return !!el.checked;
|
|
2272
|
-
}
|
|
2273
|
-
var aria = el.getAttribute("aria-checked");
|
|
2274
|
-
if (aria === "true") return true;
|
|
2275
|
-
return false;
|
|
2276
|
-
}
|
|
2277
|
-
|
|
2278
|
-
function describeNode(el, role) {
|
|
2279
|
-
var contentNamed = !!CONTENT_NAMED_ROLES[role];
|
|
2280
|
-
var name = accessibleName(el, contentNamed);
|
|
2281
|
-
var extras = [];
|
|
2282
|
-
var value = currentValue(el);
|
|
2283
|
-
if (value !== undefined && value !== "") extras.push("value=" + JSON.stringify(String(value)));
|
|
2284
|
-
if (isChecked(el)) extras.push("checked=true");
|
|
2285
|
-
if (el.tagName === "OPTION" && el.selected) extras.push("selected");
|
|
2286
|
-
var href = el.getAttribute("href");
|
|
2287
|
-
if (href) extras.push("url=" + href);
|
|
2288
|
-
if (el.disabled || el.getAttribute("aria-disabled") === "true") extras.push("disabled");
|
|
2289
|
-
var level = headingLevel(el);
|
|
2290
|
-
if (level) extras.push("level=" + level);
|
|
2291
|
-
var label = name ? " " + JSON.stringify(name) : "";
|
|
2292
|
-
var extra = extras.length ? " [" + extras.join(" ") + "]" : "";
|
|
2293
|
-
return { role: role, label: label, extra: extra, name: name };
|
|
2867
|
+
if (typeof err === "object" && err !== null && err.error === "invalid session id") {
|
|
2868
|
+
return true;
|
|
2869
|
+
}
|
|
2870
|
+
return false;
|
|
2871
|
+
}
|
|
2872
|
+
function forceClearOrphanSession(marionettePort, opts = {}) {
|
|
2873
|
+
const timeoutMs = opts.timeoutMs !== undefined && Number.isFinite(opts.timeoutMs) && opts.timeoutMs > 0 ? opts.timeoutMs : DEFAULT_TIMEOUT_MS4;
|
|
2874
|
+
return new Promise((resolve) => {
|
|
2875
|
+
if (!Number.isFinite(marionettePort) || marionettePort <= 0 || marionettePort > 65535) {
|
|
2876
|
+
resolve(false);
|
|
2877
|
+
return;
|
|
2294
2878
|
}
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
if (isStructural || isLeafText) {
|
|
2327
|
-
var stamp = stampOf(el);
|
|
2328
|
-
var indent = new Array(depth + 1).join(" ");
|
|
2329
|
-
var lineTail = isStructural
|
|
2330
|
-
? described.role + described.label + described.extra
|
|
2331
|
-
: "text " + JSON.stringify(leafName);
|
|
2332
|
-
lines.push(indent + "[" + stamp + "] " + lineTail);
|
|
2333
|
-
nodeCount += 1;
|
|
2334
|
-
nextDepth = depth + 1;
|
|
2335
|
-
}
|
|
2336
|
-
|
|
2337
|
-
if (el.shadowRoot) {
|
|
2338
|
-
walk(el.shadowRoot, nextDepth);
|
|
2339
|
-
} else if (el.tagName === "IFRAME") {
|
|
2340
|
-
try {
|
|
2341
|
-
var doc = el.contentDocument;
|
|
2342
|
-
if (doc && doc.body) walk(doc.body, nextDepth);
|
|
2343
|
-
} catch (e) {
|
|
2344
|
-
// cross-origin iframe: intentionally skipped, not silently merged
|
|
2345
|
-
// into the parent tree as though it were empty.
|
|
2879
|
+
const sock = new Socket;
|
|
2880
|
+
const decoder = new FrameDecoder;
|
|
2881
|
+
let settled = false;
|
|
2882
|
+
let handshakeSeen = false;
|
|
2883
|
+
let commandSent = false;
|
|
2884
|
+
const settle = (value) => {
|
|
2885
|
+
if (settled)
|
|
2886
|
+
return;
|
|
2887
|
+
settled = true;
|
|
2888
|
+
clearTimeout(timer);
|
|
2889
|
+
sock.removeAllListeners();
|
|
2890
|
+
sock.destroy();
|
|
2891
|
+
resolve(value);
|
|
2892
|
+
};
|
|
2893
|
+
const timer = setTimeout(() => settle(false), timeoutMs);
|
|
2894
|
+
if (typeof timer.unref === "function")
|
|
2895
|
+
timer.unref();
|
|
2896
|
+
sock.on("error", () => settle(false));
|
|
2897
|
+
sock.on("close", () => settle(false));
|
|
2898
|
+
sock.on("data", (chunk) => {
|
|
2899
|
+
if (settled)
|
|
2900
|
+
return;
|
|
2901
|
+
try {
|
|
2902
|
+
decoder.push(chunk);
|
|
2903
|
+
for (;; ) {
|
|
2904
|
+
const frame = decoder.next();
|
|
2905
|
+
if (frame === undefined)
|
|
2906
|
+
break;
|
|
2907
|
+
if (!handshakeSeen) {
|
|
2908
|
+
handshakeSeen = true;
|
|
2909
|
+
continue;
|
|
2346
2910
|
}
|
|
2347
|
-
|
|
2348
|
-
|
|
2911
|
+
settle(isSlotFreedReply(frame));
|
|
2912
|
+
return;
|
|
2913
|
+
}
|
|
2914
|
+
if (handshakeSeen && !commandSent) {
|
|
2915
|
+
commandSent = true;
|
|
2916
|
+
sock.write(DELETE_SESSION_FRAME);
|
|
2349
2917
|
}
|
|
2918
|
+
} catch {
|
|
2919
|
+
settle(false);
|
|
2350
2920
|
}
|
|
2921
|
+
});
|
|
2922
|
+
try {
|
|
2923
|
+
sock.connect(marionettePort, HOST);
|
|
2924
|
+
} catch {
|
|
2925
|
+
settle(false);
|
|
2351
2926
|
}
|
|
2352
|
-
|
|
2353
|
-
function isInteractiveRole(role) {
|
|
2354
|
-
return (
|
|
2355
|
-
role === "button" ||
|
|
2356
|
-
role === "link" ||
|
|
2357
|
-
role === "textbox" ||
|
|
2358
|
-
role === "searchbox" ||
|
|
2359
|
-
role === "checkbox" ||
|
|
2360
|
-
role === "radio" ||
|
|
2361
|
-
role === "combobox" ||
|
|
2362
|
-
role === "listbox" ||
|
|
2363
|
-
role === "option" ||
|
|
2364
|
-
role === "slider" ||
|
|
2365
|
-
role === "spinbutton" ||
|
|
2366
|
-
role === "progressbar" ||
|
|
2367
|
-
role === "meter"
|
|
2368
|
-
);
|
|
2369
|
-
}
|
|
2370
|
-
|
|
2371
|
-
function isLandmarkRole(role) {
|
|
2372
|
-
return (
|
|
2373
|
-
role === "navigation" ||
|
|
2374
|
-
role === "main" ||
|
|
2375
|
-
role === "form" ||
|
|
2376
|
-
role === "dialog" ||
|
|
2377
|
-
role === "region" ||
|
|
2378
|
-
role === "heading" ||
|
|
2379
|
-
role === "list" ||
|
|
2380
|
-
role === "listitem" ||
|
|
2381
|
-
role === "table" ||
|
|
2382
|
-
role === "row" ||
|
|
2383
|
-
role === "cell" ||
|
|
2384
|
-
role === "columnheader" ||
|
|
2385
|
-
role === "img"
|
|
2386
|
-
);
|
|
2387
|
-
}
|
|
2388
|
-
|
|
2389
|
-
walk(document.body, 0);
|
|
2390
|
-
return { snapshot: lines.join("\\n"), nodeCount: nodeCount };
|
|
2391
|
-
}`;
|
|
2927
|
+
});
|
|
2392
2928
|
}
|
|
2393
|
-
var
|
|
2929
|
+
var DEFAULT_MARIONETTE_PORT = 2828, DEFAULT_TIMEOUT_MS4 = 4000, HOST = "127.0.0.1", COLON = 58, DELETE_SESSION_FRAME, MarionetteProtocolError;
|
|
2930
|
+
var init_marionette = __esm(() => {
|
|
2931
|
+
DELETE_SESSION_FRAME = encodeFrame2([0, 1, "WebDriver:DeleteSession", {}]);
|
|
2932
|
+
MarionetteProtocolError = class MarionetteProtocolError extends Error {
|
|
2933
|
+
constructor(message) {
|
|
2934
|
+
super(message);
|
|
2935
|
+
this.name = "MarionetteProtocolError";
|
|
2936
|
+
}
|
|
2937
|
+
};
|
|
2938
|
+
});
|
|
2394
2939
|
|
|
2395
2940
|
// src/bidi/driver.ts
|
|
2396
2941
|
import { spawn as spawn3 } from "node:child_process";
|
|
@@ -3743,7 +4288,9 @@ var init_driver2 = __esm(() => {
|
|
|
3743
4288
|
|
|
3744
4289
|
// src/cdp/driver.ts
|
|
3745
4290
|
init_client();
|
|
4291
|
+
init_session();
|
|
3746
4292
|
init_leases();
|
|
4293
|
+
init_endpoints();
|
|
3747
4294
|
init_activity();
|
|
3748
4295
|
|
|
3749
4296
|
// src/tools/snapshot.ts
|
|
@@ -4231,7 +4778,7 @@ class CdpPageDriver {
|
|
|
4231
4778
|
await this.conn.send(`${name}.enable`);
|
|
4232
4779
|
}
|
|
4233
4780
|
async dispatchInput(method, params = {}) {
|
|
4234
|
-
return sendInput(this.conn,
|
|
4781
|
+
return sendInput(this.conn, chromeLeaseBackend(), this.info.id, method, params);
|
|
4235
4782
|
}
|
|
4236
4783
|
async ensureDomainSoft(name) {
|
|
4237
4784
|
if (this.enabledDomains.has(name))
|
|
@@ -4825,9 +5372,21 @@ class CdpPageDriver {
|
|
|
4825
5372
|
}
|
|
4826
5373
|
}
|
|
4827
5374
|
var beaconSessions = new BeaconSessions((conn) => conn.close());
|
|
4828
|
-
|
|
5375
|
+
function beaconKey(targetId) {
|
|
5376
|
+
const backend = chromeLeaseBackend(currentEndpoint());
|
|
5377
|
+
return backend === "chrome" ? targetId : `${backend}\x00${targetId}`;
|
|
5378
|
+
}
|
|
5379
|
+
async function openTargetConn(targetId, opts = {}) {
|
|
4829
5380
|
const hit = (await listTargets()).find((t) => t.id === targetId);
|
|
4830
|
-
|
|
5381
|
+
if (!hit)
|
|
5382
|
+
return;
|
|
5383
|
+
if (!hit.webSocketDebuggerUrl)
|
|
5384
|
+
return openSession(targetId, opts).catch(() => {
|
|
5385
|
+
return;
|
|
5386
|
+
});
|
|
5387
|
+
return new CdpConnection(hit.webSocketDebuggerUrl, opts).connect().catch(() => {
|
|
5388
|
+
return;
|
|
5389
|
+
});
|
|
4831
5390
|
}
|
|
4832
5391
|
async function readBeaconOver(conn) {
|
|
4833
5392
|
try {
|
|
@@ -4926,7 +5485,7 @@ class CdpBrowserDriver {
|
|
|
4926
5485
|
return withBrowserConn(async (conn) => {
|
|
4927
5486
|
const res = await conn.send("Target.closeTarget", { targetId: id });
|
|
4928
5487
|
if (res.success !== false)
|
|
4929
|
-
beaconSessions.drop(id);
|
|
5488
|
+
beaconSessions.drop(beaconKey(id));
|
|
4930
5489
|
return { success: res.success ?? true };
|
|
4931
5490
|
});
|
|
4932
5491
|
}
|
|
@@ -4944,19 +5503,15 @@ class CdpBrowserDriver {
|
|
|
4944
5503
|
return new CdpPageDriver(conn, target, this);
|
|
4945
5504
|
}
|
|
4946
5505
|
async installActivityBeacon(targetId) {
|
|
4947
|
-
const
|
|
5506
|
+
const key = beaconKey(targetId);
|
|
5507
|
+
const held = beaconSessions.get(key);
|
|
4948
5508
|
if (held) {
|
|
4949
5509
|
const rearmed = await held.send("Runtime.evaluate", { expression: BEACON_SOURCE, returnByValue: true }).then(() => true).catch(() => false);
|
|
4950
5510
|
if (rearmed)
|
|
4951
5511
|
return true;
|
|
4952
|
-
beaconSessions.drop(
|
|
5512
|
+
beaconSessions.drop(key);
|
|
4953
5513
|
}
|
|
4954
|
-
const
|
|
4955
|
-
return;
|
|
4956
|
-
});
|
|
4957
|
-
if (!wsUrl)
|
|
4958
|
-
return false;
|
|
4959
|
-
const conn = await new CdpConnection(wsUrl).connect().catch(() => {
|
|
5514
|
+
const conn = await openTargetConn(targetId).catch(() => {
|
|
4960
5515
|
return;
|
|
4961
5516
|
});
|
|
4962
5517
|
if (!conn)
|
|
@@ -4969,23 +5524,19 @@ class CdpBrowserDriver {
|
|
|
4969
5524
|
conn.close();
|
|
4970
5525
|
return false;
|
|
4971
5526
|
}
|
|
4972
|
-
beaconSessions.set(
|
|
5527
|
+
beaconSessions.set(key, conn);
|
|
4973
5528
|
return true;
|
|
4974
5529
|
}
|
|
4975
5530
|
async readActivityBeacon(targetId) {
|
|
4976
|
-
const
|
|
5531
|
+
const key = beaconKey(targetId);
|
|
5532
|
+
const held = beaconSessions.get(key);
|
|
4977
5533
|
if (held) {
|
|
4978
5534
|
const value = await readBeaconOver(held);
|
|
4979
5535
|
if (value !== undefined)
|
|
4980
5536
|
return value;
|
|
4981
|
-
beaconSessions.drop(
|
|
5537
|
+
beaconSessions.drop(key);
|
|
4982
5538
|
}
|
|
4983
|
-
const
|
|
4984
|
-
return;
|
|
4985
|
-
});
|
|
4986
|
-
if (!wsUrl)
|
|
4987
|
-
return null;
|
|
4988
|
-
const conn = await new CdpConnection(wsUrl).connect().catch(() => {
|
|
5539
|
+
const conn = await openTargetConn(targetId).catch(() => {
|
|
4989
5540
|
return;
|
|
4990
5541
|
});
|
|
4991
5542
|
if (!conn)
|
|
@@ -4997,20 +5548,14 @@ class CdpBrowserDriver {
|
|
|
4997
5548
|
}
|
|
4998
5549
|
}
|
|
4999
5550
|
async probeRenderer(targetId, timeoutMs = RENDERER_PROBE_TIMEOUT_MS) {
|
|
5000
|
-
const held = beaconSessions.get(targetId);
|
|
5551
|
+
const held = beaconSessions.get(beaconKey(targetId));
|
|
5001
5552
|
if (held)
|
|
5002
5553
|
return probeOver(held, timeoutMs);
|
|
5003
|
-
const
|
|
5554
|
+
const conn = await openTargetConn(targetId, { timeoutMs }).catch(() => {
|
|
5004
5555
|
return;
|
|
5005
5556
|
});
|
|
5006
|
-
if (!
|
|
5007
|
-
return { responsive: false, beaconTs: null };
|
|
5008
|
-
const conn = new CdpConnection(wsUrl, { timeoutMs });
|
|
5009
|
-
try {
|
|
5010
|
-
await conn.connect();
|
|
5011
|
-
} catch {
|
|
5557
|
+
if (!conn)
|
|
5012
5558
|
return { responsive: false, beaconTs: null };
|
|
5013
|
-
}
|
|
5014
5559
|
try {
|
|
5015
5560
|
return await probeOver(conn, timeoutMs);
|
|
5016
5561
|
} finally {
|
|
@@ -5030,6 +5575,7 @@ init_activity();
|
|
|
5030
5575
|
init_origins();
|
|
5031
5576
|
|
|
5032
5577
|
// src/reap.ts
|
|
5578
|
+
init_endpoints();
|
|
5033
5579
|
init_leases();
|
|
5034
5580
|
init_origins();
|
|
5035
5581
|
function staleAgentTabs(input) {
|
|
@@ -5058,6 +5604,11 @@ function reapGraceMs() {
|
|
|
5058
5604
|
const raw = Number(process.env.CDP_REAP_GRACE_MS);
|
|
5059
5605
|
return Number.isFinite(raw) && raw >= 0 ? raw : DEFAULT_REAP_GRACE_MS;
|
|
5060
5606
|
}
|
|
5607
|
+
function reapAllowed(driver) {
|
|
5608
|
+
if (driver.scheme === "bidi")
|
|
5609
|
+
return true;
|
|
5610
|
+
return !currentEndpoint().readOnly;
|
|
5611
|
+
}
|
|
5061
5612
|
async function reapStaleAgentTabs(driver, backend) {
|
|
5062
5613
|
const now = Date.now();
|
|
5063
5614
|
const livePageIds = (await driver.listPages()).map((p) => p.id);
|
|
@@ -5080,7 +5631,7 @@ async function reapStaleAgentTabs(driver, backend) {
|
|
|
5080
5631
|
// src/shared-tools.ts
|
|
5081
5632
|
init_driver();
|
|
5082
5633
|
import { mkdir as mkdir5, writeFile as writeFile4 } from "node:fs/promises";
|
|
5083
|
-
import { dirname as dirname2, join as
|
|
5634
|
+
import { dirname as dirname2, join as join5 } from "node:path";
|
|
5084
5635
|
|
|
5085
5636
|
// src/png.ts
|
|
5086
5637
|
import { once } from "node:events";
|
|
@@ -5600,6 +6151,7 @@ init_activity();
|
|
|
5600
6151
|
init_origins();
|
|
5601
6152
|
init_workers();
|
|
5602
6153
|
init_frames();
|
|
6154
|
+
init_endpoints();
|
|
5603
6155
|
var ARTIFACT_DIR = process.env.CDP_ARTIFACT_DIR ?? "/tmp/cdp-toolkit";
|
|
5604
6156
|
var STATE_DIR = process.env.CDP_STATE_DIR ?? "/tmp/cdp-toolkit";
|
|
5605
6157
|
|
|
@@ -5609,7 +6161,7 @@ function target3(p) {
|
|
|
5609
6161
|
return { id: p.id, url: p.url, title: p.title };
|
|
5610
6162
|
}
|
|
5611
6163
|
function backendOf(driver) {
|
|
5612
|
-
return driver.scheme === "bidi" ? "firefox" :
|
|
6164
|
+
return driver.scheme === "bidi" ? "firefox" : chromeLeaseBackend();
|
|
5613
6165
|
}
|
|
5614
6166
|
async function withPage2(driver, target, fn) {
|
|
5615
6167
|
const page = await driver.page(target);
|
|
@@ -5697,8 +6249,44 @@ async function reapSet(driver, pages, all) {
|
|
|
5697
6249
|
}
|
|
5698
6250
|
}
|
|
5699
6251
|
async function listPages(driver, args = {}) {
|
|
6252
|
+
const one = { all: args.all, probe: args.probe };
|
|
6253
|
+
if (args.allEndpoints === true) {
|
|
6254
|
+
if (driver.scheme === "bidi")
|
|
6255
|
+
throw new SharedToolError("list_pages: allEndpoints needs the Chrome backend; named endpoints are CDP-only");
|
|
6256
|
+
const results = await Promise.all(allEndpoints().map((ep) => withEndpointScope(ep, () => listPagesOne(driver, one)).then((r) => ({ ep, r }), (e) => ({ ep, error: e instanceof Error ? e.message : String(e) }))));
|
|
6257
|
+
const pages = [];
|
|
6258
|
+
const reaped = [];
|
|
6259
|
+
const endpoints = [];
|
|
6260
|
+
for (const res of results) {
|
|
6261
|
+
const { ep } = res;
|
|
6262
|
+
if ("error" in res) {
|
|
6263
|
+
endpoints.push({ endpoint: ep.name, base: ep.base, readOnly: ep.readOnly, down: true, error: res.error });
|
|
6264
|
+
continue;
|
|
6265
|
+
}
|
|
6266
|
+
for (const p of res.r.pages)
|
|
6267
|
+
pages.push({ ...p, endpoint: ep.name });
|
|
6268
|
+
for (const t of res.r.reaped ?? [])
|
|
6269
|
+
reaped.push({ ...t, endpoint: ep.name });
|
|
6270
|
+
endpoints.push({ endpoint: ep.name, base: ep.base, readOnly: ep.readOnly, count: res.r.count });
|
|
6271
|
+
}
|
|
6272
|
+
return { pages, count: pages.length, ...reaped.length ? { reaped } : {}, endpoints };
|
|
6273
|
+
}
|
|
6274
|
+
const res = await listPagesOne(driver, one);
|
|
6275
|
+
if (driver.scheme !== "bidi") {
|
|
6276
|
+
const ep = currentEndpoint();
|
|
6277
|
+
if (!isDefaultEndpoint(ep)) {
|
|
6278
|
+
return {
|
|
6279
|
+
...res,
|
|
6280
|
+
pages: res.pages.map((p) => ({ ...p, endpoint: ep.name })),
|
|
6281
|
+
...res.reaped ? { reaped: res.reaped.map((t) => ({ ...t, endpoint: ep.name })) } : {}
|
|
6282
|
+
};
|
|
6283
|
+
}
|
|
6284
|
+
}
|
|
6285
|
+
return res;
|
|
6286
|
+
}
|
|
6287
|
+
async function listPagesOne(driver, args = {}) {
|
|
5700
6288
|
const backend = backendOf(driver);
|
|
5701
|
-
const reaped = requireLease() ? await reapStaleAgentTabs(driver, backend) : [];
|
|
6289
|
+
const reaped = requireLease() && reapAllowed(driver) ? await reapStaleAgentTabs(driver, backend) : [];
|
|
5702
6290
|
const reapedIds = new Set(reaped.map((r) => r.targetId));
|
|
5703
6291
|
const pages = (await driver.listPages({ all: args.all })).filter((p) => !reapedIds.has(p.id));
|
|
5704
6292
|
const ledger = await originIndex(backend, await reapSet(driver, pages, args.all));
|
|
@@ -5748,7 +6336,7 @@ async function closePage(driver, args) {
|
|
|
5748
6336
|
}
|
|
5749
6337
|
async function writeSelectedFile(id) {
|
|
5750
6338
|
await mkdir5(STATE_DIR, { recursive: true });
|
|
5751
|
-
await writeFile4(
|
|
6339
|
+
await writeFile4(join5(STATE_DIR, "selected"), id, "utf8");
|
|
5752
6340
|
}
|
|
5753
6341
|
async function selectPage(driver, args) {
|
|
5754
6342
|
if (args.target === undefined || args.target === "")
|
|
@@ -5812,7 +6400,7 @@ function serializeForSink(value) {
|
|
|
5812
6400
|
return json ?? "null";
|
|
5813
6401
|
}
|
|
5814
6402
|
async function writeJsonSink(savePath, value) {
|
|
5815
|
-
const path = savePath.startsWith("/") ? savePath :
|
|
6403
|
+
const path = savePath.startsWith("/") ? savePath : join5(ARTIFACT_DIR, savePath);
|
|
5816
6404
|
await mkdir5(dirname2(path), { recursive: true });
|
|
5817
6405
|
const json = serializeForSink(value);
|
|
5818
6406
|
await writeFile4(path, json, "utf8");
|
|
@@ -5956,10 +6544,45 @@ async function takeSnapshot(driver, args = {}) {
|
|
|
5956
6544
|
`), target: target3(page.info), nodeCount: nodes.length };
|
|
5957
6545
|
});
|
|
5958
6546
|
}
|
|
6547
|
+
function describeElementSource() {
|
|
6548
|
+
return `function() {
|
|
6549
|
+
var MAX_NAME_LENGTH = ${MAX_NAME_LENGTH};
|
|
6550
|
+
${ACCNAME_HELPERS_SOURCE}
|
|
6551
|
+
var el = this;
|
|
6552
|
+
if (!el || el.nodeType !== 1) return null;
|
|
6553
|
+
var role = implicitRole(el);
|
|
6554
|
+
var name = accessibleName(el, !!CONTENT_NAMED_ROLES[role]);
|
|
6555
|
+
if (!name) name = truncate(el.innerText || el.textContent || "");
|
|
6556
|
+
return (role || el.tagName.toLowerCase()) + (name ? " " + JSON.stringify(name) : "");
|
|
6557
|
+
}`;
|
|
6558
|
+
}
|
|
6559
|
+
var DESCRIBE_TIMEOUT_MS = 2000;
|
|
6560
|
+
async function describeElement(page, loc) {
|
|
6561
|
+
let timer;
|
|
6562
|
+
try {
|
|
6563
|
+
const timeout = new Promise((r) => {
|
|
6564
|
+
timer = setTimeout(() => r(undefined), DESCRIBE_TIMEOUT_MS);
|
|
6565
|
+
});
|
|
6566
|
+
const desc = await Promise.race([page.callOnElement(loc, describeElementSource()), timeout]);
|
|
6567
|
+
return typeof desc === "string" && desc.length > 0 ? desc : undefined;
|
|
6568
|
+
} catch {
|
|
6569
|
+
return;
|
|
6570
|
+
} finally {
|
|
6571
|
+
clearTimeout(timer);
|
|
6572
|
+
}
|
|
6573
|
+
}
|
|
6574
|
+
function isElementLocator(d) {
|
|
6575
|
+
return !("x" in d) && !("dx" in d);
|
|
6576
|
+
}
|
|
6577
|
+
function elementField(element) {
|
|
6578
|
+
return element === undefined ? {} : { element };
|
|
6579
|
+
}
|
|
5959
6580
|
async function click(driver, args) {
|
|
6581
|
+
const loc = locatorOf(args);
|
|
5960
6582
|
return withPage2(driver, args.target, async (page) => {
|
|
5961
|
-
const
|
|
5962
|
-
|
|
6583
|
+
const element = await describeElement(page, loc);
|
|
6584
|
+
const { x, y } = await page.click(loc, { button: args.button ?? "left", clickCount: args.clickCount ?? 1, modifiers: args.modifiers });
|
|
6585
|
+
return { clicked: true, ...elementField(element), x, y };
|
|
5963
6586
|
});
|
|
5964
6587
|
}
|
|
5965
6588
|
function resolveScrollAnchor(args) {
|
|
@@ -5995,9 +6618,11 @@ async function scroll(driver, args) {
|
|
|
5995
6618
|
});
|
|
5996
6619
|
}
|
|
5997
6620
|
async function hover(driver, args) {
|
|
6621
|
+
const loc = locatorOf(args);
|
|
5998
6622
|
return withPage2(driver, args.target, async (page) => {
|
|
5999
|
-
const
|
|
6000
|
-
|
|
6623
|
+
const element = await describeElement(page, loc);
|
|
6624
|
+
const { x, y } = await page.hover(loc);
|
|
6625
|
+
return { hovered: true, ...elementField(element), x, y };
|
|
6001
6626
|
});
|
|
6002
6627
|
}
|
|
6003
6628
|
function resolveDragTo(to) {
|
|
@@ -6063,16 +6688,29 @@ async function drag(driver, args) {
|
|
|
6063
6688
|
const steps = resolveDragSteps(args.steps);
|
|
6064
6689
|
const mode = resolveDragMode(args.mode, driver.capabilities);
|
|
6065
6690
|
return withPage2(driver, args.target, async (page) => {
|
|
6066
|
-
const
|
|
6067
|
-
|
|
6691
|
+
const fromLoc = locatorOf(args.from);
|
|
6692
|
+
const element = await describeElement(page, fromLoc);
|
|
6693
|
+
const targetElement = isElementLocator(destination) ? await describeElement(page, destination) : undefined;
|
|
6694
|
+
const { from, to } = await page.drag(fromLoc, destination, { mode, steps });
|
|
6695
|
+
return {
|
|
6696
|
+
dragged: true,
|
|
6697
|
+
...elementField(element),
|
|
6698
|
+
...targetElement === undefined ? {} : { target_element: targetElement },
|
|
6699
|
+
mode,
|
|
6700
|
+
steps,
|
|
6701
|
+
from,
|
|
6702
|
+
to
|
|
6703
|
+
};
|
|
6068
6704
|
});
|
|
6069
6705
|
}
|
|
6070
6706
|
async function fill(driver, args) {
|
|
6071
6707
|
if (typeof args.value !== "string")
|
|
6072
6708
|
throw new SharedToolError("fill requires a string { value }");
|
|
6709
|
+
const loc = locatorOf(args);
|
|
6073
6710
|
return withPage2(driver, args.target, async (page) => {
|
|
6074
|
-
await page
|
|
6075
|
-
|
|
6711
|
+
const element = await describeElement(page, loc);
|
|
6712
|
+
await page.setValue(loc, args.value);
|
|
6713
|
+
return { filled: true, ...elementField(element), value: args.value };
|
|
6076
6714
|
});
|
|
6077
6715
|
}
|
|
6078
6716
|
async function fillForm(driver, args) {
|
|
@@ -6080,21 +6718,26 @@ async function fillForm(driver, args) {
|
|
|
6080
6718
|
throw new SharedToolError("fill_form requires a non-empty { fields } array");
|
|
6081
6719
|
return withPage2(driver, args.target, async (page) => {
|
|
6082
6720
|
let filled = 0;
|
|
6721
|
+
const elements = [];
|
|
6083
6722
|
for (const f of args.fields) {
|
|
6084
6723
|
if (typeof f.value !== "string")
|
|
6085
6724
|
throw new SharedToolError("each fill_form field requires a string value");
|
|
6086
|
-
|
|
6725
|
+
const loc = locatorOf(f);
|
|
6726
|
+
elements.push(await describeElement(page, loc) ?? null);
|
|
6727
|
+
await page.setValue(loc, f.value);
|
|
6087
6728
|
filled++;
|
|
6088
6729
|
}
|
|
6089
|
-
return { filled, fields: args.fields.length };
|
|
6730
|
+
return { filled, fields: args.fields.length, elements };
|
|
6090
6731
|
});
|
|
6091
6732
|
}
|
|
6092
6733
|
async function typeText(driver, args) {
|
|
6093
6734
|
if (typeof args.text !== "string")
|
|
6094
6735
|
throw new SharedToolError("type_text requires a string { text }");
|
|
6736
|
+
const loc = locatorOf(args);
|
|
6095
6737
|
return withPage2(driver, args.target, async (page) => {
|
|
6096
|
-
await page
|
|
6097
|
-
|
|
6738
|
+
const element = await describeElement(page, loc);
|
|
6739
|
+
await page.typeText(loc, args.text);
|
|
6740
|
+
return { typed: true, ...elementField(element), text: args.text };
|
|
6098
6741
|
});
|
|
6099
6742
|
}
|
|
6100
6743
|
async function pressKey(driver, args) {
|
|
@@ -6112,8 +6755,10 @@ async function uploadFile(driver, args) {
|
|
|
6112
6755
|
throw new SharedToolError("upload_file requires { files } as a non-empty path or array of paths");
|
|
6113
6756
|
}
|
|
6114
6757
|
return withPage2(driver, args.target, async (page) => {
|
|
6115
|
-
|
|
6116
|
-
|
|
6758
|
+
const loc = locatorOf(args);
|
|
6759
|
+
const element = await describeElement(page, loc);
|
|
6760
|
+
await page.setFiles(loc, files);
|
|
6761
|
+
return { uploaded: files, ...elementField(element) };
|
|
6117
6762
|
});
|
|
6118
6763
|
}
|
|
6119
6764
|
function stamp() {
|
|
@@ -6227,7 +6872,7 @@ async function takeScreenshot(driver, args = {}) {
|
|
|
6227
6872
|
return withPage2(driver, args.target, async (page) => {
|
|
6228
6873
|
const clip = args.uid !== undefined || args.selector ? locatorOf(args) : undefined;
|
|
6229
6874
|
const ext = format === "jpeg" ? "jpg" : "png";
|
|
6230
|
-
const path = args.savePath ??
|
|
6875
|
+
const path = args.savePath ?? join5(ARTIFACT_DIR, `screenshot-${page.info.id.slice(0, 8)}-${stamp()}.${ext}`);
|
|
6231
6876
|
const stitch = async (banded) => {
|
|
6232
6877
|
if (format !== "png")
|
|
6233
6878
|
throw tiledCaptureRefusal("format", banded);
|
|
@@ -6339,7 +6984,7 @@ init_leases();
|
|
|
6339
6984
|
class LeaseToolError extends Error {
|
|
6340
6985
|
}
|
|
6341
6986
|
function backendOf2(driver) {
|
|
6342
|
-
return driver.scheme === "bidi" ? "firefox" :
|
|
6987
|
+
return driver.scheme === "bidi" ? "firefox" : chromeLeaseBackend();
|
|
6343
6988
|
}
|
|
6344
6989
|
async function claimPage(driver, args = {}) {
|
|
6345
6990
|
const hasTarget = typeof args.target === "string" && args.target.length > 0;
|
|
@@ -6424,7 +7069,7 @@ async function releasePage(driver, args = {}) {
|
|
|
6424
7069
|
}
|
|
6425
7070
|
async function listLeasesTool(driver, _args = {}) {
|
|
6426
7071
|
const backend = backendOf2(driver);
|
|
6427
|
-
const reaped = requireLease() ? await reapStaleAgentTabs(driver, backend) : [];
|
|
7072
|
+
const reaped = requireLease() && reapAllowed(driver) ? await reapStaleAgentTabs(driver, backend) : [];
|
|
6428
7073
|
const reapedIds = new Set(reaped.map((r) => r.targetId));
|
|
6429
7074
|
const livePages = await driver.listPages();
|
|
6430
7075
|
const liveIds = livePages.map((p) => p.id);
|
|
@@ -6452,7 +7097,7 @@ var LEASE_TOOLS = {
|
|
|
6452
7097
|
|
|
6453
7098
|
// src/tools/console.ts
|
|
6454
7099
|
init_client();
|
|
6455
|
-
import { readFile as
|
|
7100
|
+
import { readFile as readFile5 } from "node:fs/promises";
|
|
6456
7101
|
|
|
6457
7102
|
// src/tools/recorder.ts
|
|
6458
7103
|
init_client();
|
|
@@ -6655,7 +7300,7 @@ function lineToEntry(line, index) {
|
|
|
6655
7300
|
async function readConsoleEntries(file) {
|
|
6656
7301
|
let raw;
|
|
6657
7302
|
try {
|
|
6658
|
-
raw = await
|
|
7303
|
+
raw = await readFile5(file, "utf8");
|
|
6659
7304
|
} catch {
|
|
6660
7305
|
return [];
|
|
6661
7306
|
}
|
|
@@ -6713,7 +7358,7 @@ async function getConsoleMessage(args = {}) {
|
|
|
6713
7358
|
|
|
6714
7359
|
// src/tools/network.ts
|
|
6715
7360
|
init_client();
|
|
6716
|
-
import { readFile as
|
|
7361
|
+
import { readFile as readFile6 } from "node:fs/promises";
|
|
6717
7362
|
var DEFAULT_CAPTURE_MS2 = 2500;
|
|
6718
7363
|
function asHeaders(v) {
|
|
6719
7364
|
if (!v || typeof v !== "object")
|
|
@@ -6780,7 +7425,7 @@ function correlate(lines) {
|
|
|
6780
7425
|
async function readNetworkRequests(file) {
|
|
6781
7426
|
let raw;
|
|
6782
7427
|
try {
|
|
6783
|
-
raw = await
|
|
7428
|
+
raw = await readFile6(file, "utf8");
|
|
6784
7429
|
} catch {
|
|
6785
7430
|
return [];
|
|
6786
7431
|
}
|
|
@@ -6865,8 +7510,8 @@ async function getNetworkRequest(args = {}) {
|
|
|
6865
7510
|
|
|
6866
7511
|
// src/tools/performance.ts
|
|
6867
7512
|
init_client();
|
|
6868
|
-
import { mkdir as mkdir7, readFile as
|
|
6869
|
-
import { join as
|
|
7513
|
+
import { mkdir as mkdir7, readFile as readFile7, writeFile as writeFile6, stat, unlink as unlink4 } from "node:fs/promises";
|
|
7514
|
+
import { join as join6 } from "node:path";
|
|
6870
7515
|
var ARTIFACT_DIR2 = process.env.CDP_ARTIFACT_DIR ?? "/tmp/cdp-toolkit";
|
|
6871
7516
|
var STATE_DIR2 = process.env.CDP_STATE_DIR ?? "/tmp/cdp-toolkit";
|
|
6872
7517
|
var DEFAULT_CATEGORIES = [
|
|
@@ -6884,7 +7529,7 @@ function shortId(id) {
|
|
|
6884
7529
|
return id.slice(0, 8);
|
|
6885
7530
|
}
|
|
6886
7531
|
function stateFilePath() {
|
|
6887
|
-
return
|
|
7532
|
+
return join6(STATE_DIR2, "trace-state.json");
|
|
6888
7533
|
}
|
|
6889
7534
|
function bufferTraceData(conn, sink) {
|
|
6890
7535
|
return conn.on("Tracing.dataCollected", (params) => {
|
|
@@ -6907,7 +7552,7 @@ async function endTrace(conn, sink, timeoutMs = 30000) {
|
|
|
6907
7552
|
}
|
|
6908
7553
|
async function writeTrace(events, targetId) {
|
|
6909
7554
|
await mkdir7(ARTIFACT_DIR2, { recursive: true });
|
|
6910
|
-
const path =
|
|
7555
|
+
const path = join6(ARTIFACT_DIR2, `trace-${shortId(targetId)}-${stamp2()}.json`);
|
|
6911
7556
|
const body = JSON.stringify({ traceEvents: events, metadata: { source: "cdp-toolkit" } });
|
|
6912
7557
|
await writeFile6(path, body, "utf8");
|
|
6913
7558
|
const { size } = await stat(path);
|
|
@@ -6950,7 +7595,7 @@ async function performanceStopTrace(args = {}) {
|
|
|
6950
7595
|
if (!entry) {
|
|
6951
7596
|
let hint = "";
|
|
6952
7597
|
try {
|
|
6953
|
-
const raw = await
|
|
7598
|
+
const raw = await readFile7(stateFilePath(), "utf8");
|
|
6954
7599
|
const state = JSON.parse(raw);
|
|
6955
7600
|
hint = state.pid === process.pid ? ` (state file lists target ${shortId(state.targetId)} but its live connection is gone)` : ` (trace was started by a DIFFERENT process pid=${state.pid}; live recordings cannot cross process boundaries)`;
|
|
6956
7601
|
} catch {}
|
|
@@ -7080,7 +7725,7 @@ async function performanceAnalyzeInsight(args = {}) {
|
|
|
7080
7725
|
}
|
|
7081
7726
|
let raw;
|
|
7082
7727
|
try {
|
|
7083
|
-
raw = await
|
|
7728
|
+
raw = await readFile7(args.tracePath, "utf8");
|
|
7084
7729
|
} catch (err) {
|
|
7085
7730
|
throw new CdpError(`cannot read trace file '${args.tracePath}': ${err.message}`);
|
|
7086
7731
|
}
|
|
@@ -7100,7 +7745,7 @@ async function performanceAnalyzeInsight(args = {}) {
|
|
|
7100
7745
|
init_client();
|
|
7101
7746
|
import { spawn } from "node:child_process";
|
|
7102
7747
|
import { mkdir as mkdir8, rm as rm2, stat as stat2, writeFile as writeFile7 } from "node:fs/promises";
|
|
7103
|
-
import { join as
|
|
7748
|
+
import { join as join7 } from "node:path";
|
|
7104
7749
|
var ARTIFACT_DIR3 = process.env.CDP_ARTIFACT_DIR ?? "/tmp/cdp-toolkit";
|
|
7105
7750
|
var FFMPEG_BIN = process.env.CDP_FFMPEG ?? "ffmpeg";
|
|
7106
7751
|
function frameDurationsMs(ledger, stopWallclockMs) {
|
|
@@ -7216,6 +7861,22 @@ function selectEncoder(encodersOutput) {
|
|
|
7216
7861
|
}
|
|
7217
7862
|
throw new CdpError(`ffmpeg reports none of the supported video encoders (${ENCODER_LADDER.map((c) => c.encoder).join(", ")}). ` + "Install a full ffmpeg build: 'brew install ffmpeg' (macOS) or 'apt install ffmpeg' (Debian/Ubuntu).");
|
|
7218
7863
|
}
|
|
7864
|
+
function supportsFpsMode(versionOutput) {
|
|
7865
|
+
const m = /ffmpeg version n?(\d+)\.(\d+)/.exec(versionOutput);
|
|
7866
|
+
if (!m)
|
|
7867
|
+
return /ffmpeg version N-/.test(versionOutput) ? true : undefined;
|
|
7868
|
+
const major = Number(m[1]);
|
|
7869
|
+
const minor = Number(m[2]);
|
|
7870
|
+
return major > 5 || major === 5 && minor >= 1;
|
|
7871
|
+
}
|
|
7872
|
+
function chooseFpsMode(versionOutput, helpOutput) {
|
|
7873
|
+
const byVersion = versionOutput === undefined ? undefined : supportsFpsMode(versionOutput);
|
|
7874
|
+
if (byVersion !== undefined)
|
|
7875
|
+
return byVersion;
|
|
7876
|
+
if (helpOutput !== undefined && /-vsync\b|-fps_mode\b/.test(helpOutput))
|
|
7877
|
+
return /-fps_mode\b/.test(helpOutput);
|
|
7878
|
+
return true;
|
|
7879
|
+
}
|
|
7219
7880
|
function buildFfmpegArgs(manifestPath, outPath, choice) {
|
|
7220
7881
|
return [
|
|
7221
7882
|
"-hide_banner",
|
|
@@ -7228,8 +7889,7 @@ function buildFfmpegArgs(manifestPath, outPath, choice) {
|
|
|
7228
7889
|
"0",
|
|
7229
7890
|
"-i",
|
|
7230
7891
|
manifestPath,
|
|
7231
|
-
"-vsync",
|
|
7232
|
-
"vfr",
|
|
7892
|
+
...choice.fpsMode ? ["-fps_mode", "vfr"] : ["-vsync", "vfr"],
|
|
7233
7893
|
"-vf",
|
|
7234
7894
|
"scale=trunc(iw/2)*2:trunc(ih/2)*2",
|
|
7235
7895
|
"-c:v",
|
|
@@ -7271,8 +7931,19 @@ async function probeEncoder() {
|
|
|
7271
7931
|
}
|
|
7272
7932
|
if (res.code !== 0)
|
|
7273
7933
|
throw new CdpError(`${FFMPEG_MISSING} ('-encoders' exited ${res.code}: ${res.stderr.trim().slice(0, 300)})`);
|
|
7274
|
-
|
|
7934
|
+
const choice = selectEncoder(`${res.stdout}
|
|
7275
7935
|
${res.stderr}`);
|
|
7936
|
+
const output = async (args) => {
|
|
7937
|
+
const r = await run(FFMPEG_BIN, args).catch(() => {
|
|
7938
|
+
return;
|
|
7939
|
+
});
|
|
7940
|
+
return r && r.code === 0 ? `${r.stdout}
|
|
7941
|
+
${r.stderr}` : undefined;
|
|
7942
|
+
};
|
|
7943
|
+
const version = await output(["-version"]);
|
|
7944
|
+
const byVersion = version === undefined ? undefined : supportsFpsMode(version);
|
|
7945
|
+
const help = byVersion === undefined ? await output(["-hide_banner", "-h", "full"]) : undefined;
|
|
7946
|
+
return { ...choice, fpsMode: chooseFpsMode(version, help) };
|
|
7276
7947
|
})();
|
|
7277
7948
|
encoderProbe.catch(() => {
|
|
7278
7949
|
encoderProbe = undefined;
|
|
@@ -7321,7 +7992,7 @@ async function startScreenRecording(args = {}) {
|
|
|
7321
7992
|
conn.close();
|
|
7322
7993
|
throw err;
|
|
7323
7994
|
}
|
|
7324
|
-
const spoolDir =
|
|
7995
|
+
const spoolDir = join7(ARTIFACT_DIR3, `screencast-${shortId2(target.id)}-${stamp3()}`);
|
|
7325
7996
|
try {
|
|
7326
7997
|
await mkdir8(spoolDir, { recursive: true });
|
|
7327
7998
|
} catch (err) {
|
|
@@ -7364,7 +8035,7 @@ async function startScreenRecording(args = {}) {
|
|
|
7364
8035
|
seq += 1;
|
|
7365
8036
|
const file = `frame-${String(seq).padStart(6, "0")}.${ext}`;
|
|
7366
8037
|
rec.ledger.push({ file, timestamp, width: size.width, height: size.height });
|
|
7367
|
-
rec.writes = rec.writes.then(() => writeFile7(
|
|
8038
|
+
rec.writes = rec.writes.then(() => writeFile7(join7(spoolDir, file), buf)).catch(() => {
|
|
7368
8039
|
rec.dropped += 1;
|
|
7369
8040
|
rec.unwritten.add(file);
|
|
7370
8041
|
});
|
|
@@ -7428,9 +8099,9 @@ async function stopScreenRecording(args = {}) {
|
|
|
7428
8099
|
throw new CdpError(`screen recording captured 0 frames for target ${shortId2(rec.target.id)}. Chrome emits a screencast frame only on repaint: a fully backgrounded or occluded tab may never paint. Retry with bringToFront:true, or drive the page while recording.`);
|
|
7429
8100
|
}
|
|
7430
8101
|
await mkdir8(ARTIFACT_DIR3, { recursive: true });
|
|
7431
|
-
const outPath = args.savePath ??
|
|
8102
|
+
const outPath = args.savePath ?? join7(ARTIFACT_DIR3, `screen-recording-${shortId2(rec.target.id)}-${stamp3()}.mp4`);
|
|
7432
8103
|
const frames = coalesceToConcatGrid(ledger, stoppedAt);
|
|
7433
|
-
const manifestPath =
|
|
8104
|
+
const manifestPath = join7(rec.spoolDir, "frames.ffconcat");
|
|
7434
8105
|
await writeFile7(manifestPath, buildConcatManifest(frames), "utf8");
|
|
7435
8106
|
const durationMs = frames.reduce((total, f) => total + f.durationMs, 0);
|
|
7436
8107
|
const { width, height } = evenDimensions(ledger[0].width, ledger[0].height);
|
|
@@ -7464,7 +8135,7 @@ ffmpeg said: ${res.stderr.trim().slice(0, 800)}`);
|
|
|
7464
8135
|
// src/tools/heap.ts
|
|
7465
8136
|
init_client();
|
|
7466
8137
|
import { mkdir as mkdir9, writeFile as writeFile8 } from "node:fs/promises";
|
|
7467
|
-
import { join as
|
|
8138
|
+
import { join as join8 } from "node:path";
|
|
7468
8139
|
var ARTIFACT_DIR4 = process.env.CDP_ARTIFACT_DIR ?? "/tmp/cdp-toolkit";
|
|
7469
8140
|
function stamp4(targetId) {
|
|
7470
8141
|
const short = targetId.slice(0, 8);
|
|
@@ -7490,7 +8161,7 @@ async function takeHeapsnapshot(args = {}) {
|
|
|
7490
8161
|
throw new Error("HeapProfiler.takeHeapSnapshot produced no chunks");
|
|
7491
8162
|
}
|
|
7492
8163
|
await mkdir9(ARTIFACT_DIR4, { recursive: true });
|
|
7493
|
-
const path = args.savePath ? args.savePath.startsWith("/") ? args.savePath :
|
|
8164
|
+
const path = args.savePath ? args.savePath.startsWith("/") ? args.savePath : join8(ARTIFACT_DIR4, args.savePath) : join8(ARTIFACT_DIR4, `take_heapsnapshot-${stamp4(target.id)}.heapsnapshot`);
|
|
7494
8165
|
await writeFile8(path, snapshot, "utf8");
|
|
7495
8166
|
const bytes = Buffer.byteLength(snapshot, "utf8");
|
|
7496
8167
|
return {
|
|
@@ -7504,11 +8175,12 @@ async function takeHeapsnapshot(args = {}) {
|
|
|
7504
8175
|
|
|
7505
8176
|
// src/tools/lighthouse.ts
|
|
7506
8177
|
init_client();
|
|
8178
|
+
init_endpoints();
|
|
7507
8179
|
import { spawn as spawn2 } from "node:child_process";
|
|
7508
|
-
import { mkdir as mkdir10, readFile as
|
|
8180
|
+
import { mkdir as mkdir10, readFile as readFile8, stat as stat3 } from "node:fs/promises";
|
|
7509
8181
|
var ARTIFACT_DIR5 = process.env.CDP_ARTIFACT_DIR ?? "/tmp/cdp-toolkit";
|
|
7510
8182
|
function cdpPort() {
|
|
7511
|
-
const base =
|
|
8183
|
+
const base = currentEndpoint().base;
|
|
7512
8184
|
const m = base.match(/:(\d+)/);
|
|
7513
8185
|
return m?.[1] ? Number(m[1]) : 9222;
|
|
7514
8186
|
}
|
|
@@ -7595,7 +8267,7 @@ async function lighthouseAudit(args) {
|
|
|
7595
8267
|
}
|
|
7596
8268
|
let raw;
|
|
7597
8269
|
try {
|
|
7598
|
-
raw = await
|
|
8270
|
+
raw = await readFile8(outPath, "utf8");
|
|
7599
8271
|
} catch {
|
|
7600
8272
|
raw = undefined;
|
|
7601
8273
|
}
|
|
@@ -7645,6 +8317,7 @@ init_network_mock();
|
|
|
7645
8317
|
// src/tools/dispatch-mouse.ts
|
|
7646
8318
|
init_activity();
|
|
7647
8319
|
init_client();
|
|
8320
|
+
init_leases();
|
|
7648
8321
|
var CLICK_MODIFIER_BITS2 = { Alt: 1, Control: 2, Meta: 4, Shift: 8 };
|
|
7649
8322
|
var BUTTONS = new Set(["left", "right", "middle"]);
|
|
7650
8323
|
var CDP_EVENT_TYPE = {
|
|
@@ -7681,7 +8354,7 @@ async function dispatchMouse(args) {
|
|
|
7681
8354
|
const params = { type: CDP_EVENT_TYPE[args.action], x: args.x, y: args.y, button, modifiers };
|
|
7682
8355
|
if (args.action !== "move")
|
|
7683
8356
|
params.clickCount = args.clickCount ?? 1;
|
|
7684
|
-
await sendInput(conn,
|
|
8357
|
+
await sendInput(conn, chromeLeaseBackend(), target.id, "Input.dispatchMouseEvent", params);
|
|
7685
8358
|
return { dispatched: args.action, x: args.x, y: args.y };
|
|
7686
8359
|
});
|
|
7687
8360
|
}
|
|
@@ -7689,6 +8362,7 @@ async function dispatchMouse(args) {
|
|
|
7689
8362
|
// src/tools/focus-emulation.ts
|
|
7690
8363
|
init_activity();
|
|
7691
8364
|
init_client();
|
|
8365
|
+
init_leases();
|
|
7692
8366
|
function validateFocusEmulationArgs(args) {
|
|
7693
8367
|
if (typeof args.enabled !== "boolean") {
|
|
7694
8368
|
throw new Error("focus_emulation requires { enabled } of true or false");
|
|
@@ -7755,9 +8429,10 @@ async function clickFocusGated(args) {
|
|
|
7755
8429
|
const btn = result?.value;
|
|
7756
8430
|
if (btn && !btn.disabled) {
|
|
7757
8431
|
const base = { x: btn.x, y: btn.y, button: "left", buttons: 1, clickCount: 1 };
|
|
7758
|
-
|
|
7759
|
-
await sendInput(conn,
|
|
7760
|
-
await sendInput(conn,
|
|
8432
|
+
const backend = chromeLeaseBackend();
|
|
8433
|
+
await sendInput(conn, backend, target.id, "Input.dispatchMouseEvent", { type: "mouseMoved", x: btn.x, y: btn.y });
|
|
8434
|
+
await sendInput(conn, backend, target.id, "Input.dispatchMouseEvent", { ...base, type: "mousePressed" });
|
|
8435
|
+
await sendInput(conn, backend, target.id, "Input.dispatchMouseEvent", { ...base, type: "mouseReleased" });
|
|
7761
8436
|
return {
|
|
7762
8437
|
clicked: true,
|
|
7763
8438
|
x: btn.x,
|
|
@@ -7782,16 +8457,20 @@ async function clickFocusGated(args) {
|
|
|
7782
8457
|
// src/tools/downloads.ts
|
|
7783
8458
|
init_client();
|
|
7784
8459
|
import { readdir as readdir3, rename as rename2, stat as stat4 } from "node:fs/promises";
|
|
7785
|
-
import { basename, extname, join as
|
|
8460
|
+
import { basename, extname, join as join10 } from "node:path";
|
|
7786
8461
|
|
|
7787
8462
|
// src/tools/browser-session.ts
|
|
7788
8463
|
init_client();
|
|
8464
|
+
init_endpoints();
|
|
7789
8465
|
import { mkdir as mkdir11 } from "node:fs/promises";
|
|
7790
|
-
import { join as
|
|
8466
|
+
import { join as join9 } from "node:path";
|
|
7791
8467
|
var ARTIFACT_DIR6 = process.env.CDP_ARTIFACT_DIR ?? "/tmp/cdp-toolkit";
|
|
7792
|
-
var DOWNLOADS_DIR =
|
|
8468
|
+
var DOWNLOADS_DIR = join9(ARTIFACT_DIR6, "downloads");
|
|
7793
8469
|
var DOWNLOAD_BUFFER_LIMIT = 50;
|
|
7794
|
-
var
|
|
8470
|
+
var sessions2 = new Map;
|
|
8471
|
+
function sessionKey() {
|
|
8472
|
+
return currentEndpoint().name;
|
|
8473
|
+
}
|
|
7795
8474
|
function isDisconnect(e) {
|
|
7796
8475
|
const m = e instanceof Error ? e.message : String(e);
|
|
7797
8476
|
return /connection (not open|closed)|closed by caller|websocket|ECONNREFUSED|socket/i.test(m);
|
|
@@ -7836,13 +8515,15 @@ function subscribe(s) {
|
|
|
7836
8515
|
});
|
|
7837
8516
|
}
|
|
7838
8517
|
async function getBrowserSession() {
|
|
8518
|
+
const key = sessionKey();
|
|
8519
|
+
const session = sessions2.get(key);
|
|
7839
8520
|
if (session && !session.dead)
|
|
7840
8521
|
return session;
|
|
7841
8522
|
const previousPath = session?.armedPath;
|
|
7842
8523
|
const conn = await openBrowser();
|
|
7843
8524
|
const s = { conn, downloads: [], waiters: [], dead: false };
|
|
7844
8525
|
subscribe(s);
|
|
7845
|
-
|
|
8526
|
+
sessions2.set(key, s);
|
|
7846
8527
|
if (previousPath)
|
|
7847
8528
|
await armDownloadsAt(previousPath);
|
|
7848
8529
|
return s;
|
|
@@ -7873,7 +8554,7 @@ async function armDownloads() {
|
|
|
7873
8554
|
return { downloadPath: DOWNLOADS_DIR };
|
|
7874
8555
|
}
|
|
7875
8556
|
function pendingDownloads() {
|
|
7876
|
-
return (
|
|
8557
|
+
return (sessions2.get(sessionKey())?.downloads ?? []).filter((d) => d.state === "completed" && !d.consumed);
|
|
7877
8558
|
}
|
|
7878
8559
|
async function nextCompletedDownload(timeoutMs) {
|
|
7879
8560
|
const s = await getBrowserSession();
|
|
@@ -7897,14 +8578,14 @@ async function nextCompletedDownload(timeoutMs) {
|
|
|
7897
8578
|
});
|
|
7898
8579
|
}
|
|
7899
8580
|
async function disposeBrowserSession() {
|
|
7900
|
-
const
|
|
7901
|
-
|
|
7902
|
-
|
|
7903
|
-
|
|
7904
|
-
|
|
7905
|
-
|
|
7906
|
-
|
|
7907
|
-
}
|
|
8581
|
+
const all = [...sessions2.values()];
|
|
8582
|
+
sessions2.clear();
|
|
8583
|
+
for (const s of all) {
|
|
8584
|
+
s.dead = true;
|
|
8585
|
+
try {
|
|
8586
|
+
s.conn.close();
|
|
8587
|
+
} catch {}
|
|
8588
|
+
}
|
|
7908
8589
|
}
|
|
7909
8590
|
|
|
7910
8591
|
// src/tools/downloads.ts
|
|
@@ -7951,7 +8632,7 @@ async function waitForDownload(args = {}) {
|
|
|
7951
8632
|
const taken = new Set(await readdir3(DOWNLOADS_DIR).catch(() => []));
|
|
7952
8633
|
taken.delete(basename(rec.filePath));
|
|
7953
8634
|
const finalName = nextFreeName(suggestedFilename, taken);
|
|
7954
|
-
const path =
|
|
8635
|
+
const path = join10(DOWNLOADS_DIR, finalName);
|
|
7955
8636
|
await rename2(rec.filePath, path);
|
|
7956
8637
|
const bytes = (await stat4(path)).size;
|
|
7957
8638
|
return { path, suggestedFilename, bytes, ...rec.url ? { url: rec.url } : {}, target };
|
|
@@ -8010,7 +8691,7 @@ async function grantPermissions(args = {}) {
|
|
|
8010
8691
|
// src/tools/extract.ts
|
|
8011
8692
|
init_client();
|
|
8012
8693
|
import { mkdir as mkdir12, writeFile as writeFile9 } from "node:fs/promises";
|
|
8013
|
-
import { dirname as dirname3, join as
|
|
8694
|
+
import { dirname as dirname3, join as join11 } from "node:path";
|
|
8014
8695
|
var ARTIFACT_DIR7 = process.env.CDP_ARTIFACT_DIR ?? "/tmp/cdp-toolkit";
|
|
8015
8696
|
var DEFAULT_BASE_URL = "http://127.0.0.1:8090/v1";
|
|
8016
8697
|
var DEFAULT_MODEL = "schematron";
|
|
@@ -8411,7 +9092,7 @@ async function extractPage(driver, args) {
|
|
|
8411
9092
|
};
|
|
8412
9093
|
if (args.savePath === undefined || args.savePath === "")
|
|
8413
9094
|
return result;
|
|
8414
|
-
const path = args.savePath.startsWith("/") ? args.savePath :
|
|
9095
|
+
const path = args.savePath.startsWith("/") ? args.savePath : join11(ARTIFACT_DIR7, args.savePath);
|
|
8415
9096
|
await mkdir12(dirname3(path), { recursive: true });
|
|
8416
9097
|
const json = JSON.stringify(result, null, 2);
|
|
8417
9098
|
await writeFile9(path, json, "utf8");
|
|
@@ -8489,10 +9170,10 @@ init_driver2();
|
|
|
8489
9170
|
|
|
8490
9171
|
// src/bidi/launch.ts
|
|
8491
9172
|
import { spawn as spawn4 } from "node:child_process";
|
|
8492
|
-
import { existsSync as existsSync2, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
9173
|
+
import { existsSync as existsSync2, mkdtempSync, readFileSync as readFileSync2, rmSync, writeFileSync } from "node:fs";
|
|
8493
9174
|
import { createConnection, createServer as createServer2 } from "node:net";
|
|
8494
9175
|
import { tmpdir } from "node:os";
|
|
8495
|
-
import { join as
|
|
9176
|
+
import { join as join13 } from "node:path";
|
|
8496
9177
|
function resolveBinary() {
|
|
8497
9178
|
const candidates = [
|
|
8498
9179
|
process.env.FIREFOX_BIN,
|
|
@@ -8572,8 +9253,8 @@ var LAUNCH_USER_JS_PREFS = {
|
|
|
8572
9253
|
"dom.file.createInChild": true
|
|
8573
9254
|
};
|
|
8574
9255
|
function writeLaunchUserJs(profilePath) {
|
|
8575
|
-
const target =
|
|
8576
|
-
const existing = existsSync2(target) ?
|
|
9256
|
+
const target = join13(profilePath, "user.js");
|
|
9257
|
+
const existing = existsSync2(target) ? readFileSync2(target, "utf8") : "";
|
|
8577
9258
|
const present = new Set;
|
|
8578
9259
|
for (const line of existing.split(`
|
|
8579
9260
|
`)) {
|
|
@@ -8594,7 +9275,7 @@ async function launchFirefox(opts = {}) {
|
|
|
8594
9275
|
const binary = resolveBinary();
|
|
8595
9276
|
const port = await pickPort(opts.port);
|
|
8596
9277
|
const isThrowaway = opts.profilePath === undefined;
|
|
8597
|
-
const profilePath = opts.profilePath ?? mkdtempSync(
|
|
9278
|
+
const profilePath = opts.profilePath ?? mkdtempSync(join13(tmpdir(), "cdp-toolkit-ff-profile-"));
|
|
8598
9279
|
if (isThrowaway)
|
|
8599
9280
|
writeLaunchUserJs(profilePath);
|
|
8600
9281
|
const args = ["--profile", profilePath, "--remote-debugging-port", String(port), "--no-remote"];
|
|
@@ -8954,6 +9635,7 @@ var FIREFOX_TOOLS = {
|
|
|
8954
9635
|
|
|
8955
9636
|
// src/cli.ts
|
|
8956
9637
|
init_leases();
|
|
9638
|
+
init_endpoints();
|
|
8957
9639
|
|
|
8958
9640
|
// src/manifest.ts
|
|
8959
9641
|
var MANIFEST = [
|
|
@@ -8974,6 +9656,14 @@ var MANIFEST = [
|
|
|
8974
9656
|
probe: {
|
|
8975
9657
|
type: "boolean",
|
|
8976
9658
|
description: "Probe renderer responsiveness."
|
|
9659
|
+
},
|
|
9660
|
+
endpoint: {
|
|
9661
|
+
type: "string",
|
|
9662
|
+
description: "Named CDP endpoint to list (CDP_ENDPOINTS); default endpoint when omitted."
|
|
9663
|
+
},
|
|
9664
|
+
allEndpoints: {
|
|
9665
|
+
type: "boolean",
|
|
9666
|
+
description: "List every configured endpoint; rows carry endpoint, a down one is reported, not thrown."
|
|
8977
9667
|
}
|
|
8978
9668
|
},
|
|
8979
9669
|
required: [],
|
|
@@ -8998,6 +9688,10 @@ var MANIFEST = [
|
|
|
8998
9688
|
type: "boolean",
|
|
8999
9689
|
description: "Claim the new tab."
|
|
9000
9690
|
},
|
|
9691
|
+
endpoint: {
|
|
9692
|
+
type: "string",
|
|
9693
|
+
description: "Named CDP endpoint to open the tab on (CDP_ENDPOINTS); default endpoint when omitted."
|
|
9694
|
+
},
|
|
9001
9695
|
label: {
|
|
9002
9696
|
type: "string",
|
|
9003
9697
|
description: "Agent lease label."
|
|
@@ -9023,7 +9717,7 @@ var MANIFEST = [
|
|
|
9023
9717
|
},
|
|
9024
9718
|
target: {
|
|
9025
9719
|
type: "string",
|
|
9026
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
9720
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9027
9721
|
}
|
|
9028
9722
|
},
|
|
9029
9723
|
required: [
|
|
@@ -9044,7 +9738,7 @@ var MANIFEST = [
|
|
|
9044
9738
|
},
|
|
9045
9739
|
target: {
|
|
9046
9740
|
type: "string",
|
|
9047
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
9741
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9048
9742
|
}
|
|
9049
9743
|
},
|
|
9050
9744
|
required: [
|
|
@@ -9065,7 +9759,7 @@ var MANIFEST = [
|
|
|
9065
9759
|
},
|
|
9066
9760
|
target: {
|
|
9067
9761
|
type: "string",
|
|
9068
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
9762
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9069
9763
|
},
|
|
9070
9764
|
url: {
|
|
9071
9765
|
type: "string",
|
|
@@ -9115,7 +9809,7 @@ var MANIFEST = [
|
|
|
9115
9809
|
},
|
|
9116
9810
|
target: {
|
|
9117
9811
|
type: "string",
|
|
9118
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
9812
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9119
9813
|
},
|
|
9120
9814
|
text: {
|
|
9121
9815
|
type: "string",
|
|
@@ -9148,7 +9842,7 @@ var MANIFEST = [
|
|
|
9148
9842
|
},
|
|
9149
9843
|
target: {
|
|
9150
9844
|
type: "string",
|
|
9151
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
9845
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9152
9846
|
},
|
|
9153
9847
|
wake: {
|
|
9154
9848
|
type: "boolean",
|
|
@@ -9190,7 +9884,7 @@ var MANIFEST = [
|
|
|
9190
9884
|
},
|
|
9191
9885
|
target: {
|
|
9192
9886
|
type: "string",
|
|
9193
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
9887
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9194
9888
|
},
|
|
9195
9889
|
domain: {
|
|
9196
9890
|
type: "string",
|
|
@@ -9221,7 +9915,7 @@ var MANIFEST = [
|
|
|
9221
9915
|
},
|
|
9222
9916
|
target: {
|
|
9223
9917
|
type: "string",
|
|
9224
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
9918
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9225
9919
|
},
|
|
9226
9920
|
name: {
|
|
9227
9921
|
type: "string",
|
|
@@ -9285,7 +9979,7 @@ var MANIFEST = [
|
|
|
9285
9979
|
},
|
|
9286
9980
|
target: {
|
|
9287
9981
|
type: "string",
|
|
9288
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
9982
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9289
9983
|
},
|
|
9290
9984
|
name: {
|
|
9291
9985
|
type: "string",
|
|
@@ -9322,7 +10016,7 @@ var MANIFEST = [
|
|
|
9322
10016
|
},
|
|
9323
10017
|
target: {
|
|
9324
10018
|
type: "string",
|
|
9325
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10019
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9326
10020
|
},
|
|
9327
10021
|
interactiveOnly: {
|
|
9328
10022
|
type: "boolean",
|
|
@@ -9345,7 +10039,7 @@ var MANIFEST = [
|
|
|
9345
10039
|
},
|
|
9346
10040
|
target: {
|
|
9347
10041
|
type: "string",
|
|
9348
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10042
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9349
10043
|
},
|
|
9350
10044
|
uid: {
|
|
9351
10045
|
type: "number",
|
|
@@ -9398,7 +10092,7 @@ var MANIFEST = [
|
|
|
9398
10092
|
},
|
|
9399
10093
|
target: {
|
|
9400
10094
|
type: "string",
|
|
9401
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10095
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9402
10096
|
},
|
|
9403
10097
|
uid: {
|
|
9404
10098
|
type: "number",
|
|
@@ -9425,7 +10119,7 @@ var MANIFEST = [
|
|
|
9425
10119
|
},
|
|
9426
10120
|
target: {
|
|
9427
10121
|
type: "string",
|
|
9428
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10122
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9429
10123
|
},
|
|
9430
10124
|
from: {
|
|
9431
10125
|
type: "object",
|
|
@@ -9511,7 +10205,7 @@ var MANIFEST = [
|
|
|
9511
10205
|
},
|
|
9512
10206
|
target: {
|
|
9513
10207
|
type: "string",
|
|
9514
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10208
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9515
10209
|
},
|
|
9516
10210
|
uid: {
|
|
9517
10211
|
type: "number",
|
|
@@ -9554,7 +10248,7 @@ var MANIFEST = [
|
|
|
9554
10248
|
},
|
|
9555
10249
|
target: {
|
|
9556
10250
|
type: "string",
|
|
9557
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10251
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9558
10252
|
},
|
|
9559
10253
|
action: {
|
|
9560
10254
|
type: "string",
|
|
@@ -9620,7 +10314,7 @@ var MANIFEST = [
|
|
|
9620
10314
|
},
|
|
9621
10315
|
target: {
|
|
9622
10316
|
type: "string",
|
|
9623
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10317
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9624
10318
|
},
|
|
9625
10319
|
enabled: {
|
|
9626
10320
|
type: "boolean",
|
|
@@ -9645,7 +10339,7 @@ var MANIFEST = [
|
|
|
9645
10339
|
},
|
|
9646
10340
|
target: {
|
|
9647
10341
|
type: "string",
|
|
9648
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10342
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9649
10343
|
},
|
|
9650
10344
|
selector: {
|
|
9651
10345
|
type: "string",
|
|
@@ -9684,7 +10378,7 @@ var MANIFEST = [
|
|
|
9684
10378
|
},
|
|
9685
10379
|
target: {
|
|
9686
10380
|
type: "string",
|
|
9687
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10381
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9688
10382
|
},
|
|
9689
10383
|
arm: {
|
|
9690
10384
|
type: "boolean",
|
|
@@ -9711,7 +10405,7 @@ var MANIFEST = [
|
|
|
9711
10405
|
},
|
|
9712
10406
|
target: {
|
|
9713
10407
|
type: "string",
|
|
9714
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10408
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9715
10409
|
},
|
|
9716
10410
|
permissions: {
|
|
9717
10411
|
type: "array",
|
|
@@ -9745,7 +10439,7 @@ var MANIFEST = [
|
|
|
9745
10439
|
},
|
|
9746
10440
|
target: {
|
|
9747
10441
|
type: "string",
|
|
9748
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10442
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9749
10443
|
},
|
|
9750
10444
|
uid: {
|
|
9751
10445
|
type: "number",
|
|
@@ -9778,7 +10472,7 @@ var MANIFEST = [
|
|
|
9778
10472
|
},
|
|
9779
10473
|
target: {
|
|
9780
10474
|
type: "string",
|
|
9781
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10475
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9782
10476
|
},
|
|
9783
10477
|
fields: {
|
|
9784
10478
|
type: "array",
|
|
@@ -9824,7 +10518,7 @@ var MANIFEST = [
|
|
|
9824
10518
|
},
|
|
9825
10519
|
target: {
|
|
9826
10520
|
type: "string",
|
|
9827
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10521
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9828
10522
|
},
|
|
9829
10523
|
uid: {
|
|
9830
10524
|
type: "number",
|
|
@@ -9857,7 +10551,7 @@ var MANIFEST = [
|
|
|
9857
10551
|
},
|
|
9858
10552
|
target: {
|
|
9859
10553
|
type: "string",
|
|
9860
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10554
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9861
10555
|
},
|
|
9862
10556
|
key: {
|
|
9863
10557
|
type: "string",
|
|
@@ -9889,7 +10583,7 @@ var MANIFEST = [
|
|
|
9889
10583
|
},
|
|
9890
10584
|
target: {
|
|
9891
10585
|
type: "string",
|
|
9892
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10586
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9893
10587
|
},
|
|
9894
10588
|
uid: {
|
|
9895
10589
|
type: "number",
|
|
@@ -9925,7 +10619,7 @@ var MANIFEST = [
|
|
|
9925
10619
|
},
|
|
9926
10620
|
target: {
|
|
9927
10621
|
type: "string",
|
|
9928
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10622
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9929
10623
|
},
|
|
9930
10624
|
format: {
|
|
9931
10625
|
type: "string",
|
|
@@ -9992,7 +10686,7 @@ var MANIFEST = [
|
|
|
9992
10686
|
},
|
|
9993
10687
|
target: {
|
|
9994
10688
|
type: "string",
|
|
9995
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10689
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
9996
10690
|
},
|
|
9997
10691
|
width: {
|
|
9998
10692
|
type: "number",
|
|
@@ -10092,7 +10786,7 @@ var MANIFEST = [
|
|
|
10092
10786
|
},
|
|
10093
10787
|
target: {
|
|
10094
10788
|
type: "string",
|
|
10095
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10789
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10096
10790
|
},
|
|
10097
10791
|
width: {
|
|
10098
10792
|
type: "number",
|
|
@@ -10130,7 +10824,7 @@ var MANIFEST = [
|
|
|
10130
10824
|
},
|
|
10131
10825
|
target: {
|
|
10132
10826
|
type: "string",
|
|
10133
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10827
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10134
10828
|
},
|
|
10135
10829
|
accept: {
|
|
10136
10830
|
type: "boolean",
|
|
@@ -10167,7 +10861,7 @@ var MANIFEST = [
|
|
|
10167
10861
|
},
|
|
10168
10862
|
target: {
|
|
10169
10863
|
type: "string",
|
|
10170
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10864
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10171
10865
|
},
|
|
10172
10866
|
reload: {
|
|
10173
10867
|
type: "boolean",
|
|
@@ -10198,7 +10892,7 @@ var MANIFEST = [
|
|
|
10198
10892
|
},
|
|
10199
10893
|
target: {
|
|
10200
10894
|
type: "string",
|
|
10201
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10895
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10202
10896
|
},
|
|
10203
10897
|
index: {
|
|
10204
10898
|
type: "number",
|
|
@@ -10221,7 +10915,7 @@ var MANIFEST = [
|
|
|
10221
10915
|
},
|
|
10222
10916
|
target: {
|
|
10223
10917
|
type: "string",
|
|
10224
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10918
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10225
10919
|
},
|
|
10226
10920
|
reload: {
|
|
10227
10921
|
type: "boolean",
|
|
@@ -10256,7 +10950,7 @@ var MANIFEST = [
|
|
|
10256
10950
|
},
|
|
10257
10951
|
target: {
|
|
10258
10952
|
type: "string",
|
|
10259
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10953
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10260
10954
|
},
|
|
10261
10955
|
requestId: {
|
|
10262
10956
|
type: "string",
|
|
@@ -10295,7 +10989,7 @@ var MANIFEST = [
|
|
|
10295
10989
|
},
|
|
10296
10990
|
target: {
|
|
10297
10991
|
type: "string",
|
|
10298
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
10992
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10299
10993
|
},
|
|
10300
10994
|
categories: {
|
|
10301
10995
|
type: "array",
|
|
@@ -10321,7 +11015,7 @@ var MANIFEST = [
|
|
|
10321
11015
|
},
|
|
10322
11016
|
target: {
|
|
10323
11017
|
type: "string",
|
|
10324
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
11018
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10325
11019
|
}
|
|
10326
11020
|
},
|
|
10327
11021
|
required: [],
|
|
@@ -10359,7 +11053,7 @@ var MANIFEST = [
|
|
|
10359
11053
|
},
|
|
10360
11054
|
target: {
|
|
10361
11055
|
type: "string",
|
|
10362
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
11056
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10363
11057
|
},
|
|
10364
11058
|
durationMs: {
|
|
10365
11059
|
type: "number",
|
|
@@ -10397,7 +11091,7 @@ var MANIFEST = [
|
|
|
10397
11091
|
},
|
|
10398
11092
|
target: {
|
|
10399
11093
|
type: "string",
|
|
10400
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
11094
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10401
11095
|
},
|
|
10402
11096
|
format: {
|
|
10403
11097
|
type: "string",
|
|
@@ -10444,7 +11138,7 @@ var MANIFEST = [
|
|
|
10444
11138
|
},
|
|
10445
11139
|
target: {
|
|
10446
11140
|
type: "string",
|
|
10447
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
11141
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10448
11142
|
},
|
|
10449
11143
|
savePath: {
|
|
10450
11144
|
type: "string",
|
|
@@ -10467,7 +11161,7 @@ var MANIFEST = [
|
|
|
10467
11161
|
},
|
|
10468
11162
|
target: {
|
|
10469
11163
|
type: "string",
|
|
10470
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
11164
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10471
11165
|
},
|
|
10472
11166
|
savePath: {
|
|
10473
11167
|
type: "string",
|
|
@@ -10534,7 +11228,7 @@ var MANIFEST = [
|
|
|
10534
11228
|
},
|
|
10535
11229
|
target: {
|
|
10536
11230
|
type: "string",
|
|
10537
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
11231
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10538
11232
|
},
|
|
10539
11233
|
urlPattern: {
|
|
10540
11234
|
type: "string",
|
|
@@ -10623,7 +11317,7 @@ var MANIFEST = [
|
|
|
10623
11317
|
},
|
|
10624
11318
|
target: {
|
|
10625
11319
|
type: "string",
|
|
10626
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
11320
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10627
11321
|
},
|
|
10628
11322
|
all: {
|
|
10629
11323
|
type: "boolean",
|
|
@@ -10642,7 +11336,7 @@ var MANIFEST = [
|
|
|
10642
11336
|
properties: {
|
|
10643
11337
|
target: {
|
|
10644
11338
|
type: "string",
|
|
10645
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
11339
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10646
11340
|
},
|
|
10647
11341
|
targetId: {
|
|
10648
11342
|
type: "string",
|
|
@@ -10677,7 +11371,7 @@ var MANIFEST = [
|
|
|
10677
11371
|
},
|
|
10678
11372
|
target: {
|
|
10679
11373
|
type: "string",
|
|
10680
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
11374
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10681
11375
|
},
|
|
10682
11376
|
close: {
|
|
10683
11377
|
type: "boolean",
|
|
@@ -10710,7 +11404,7 @@ var MANIFEST = [
|
|
|
10710
11404
|
},
|
|
10711
11405
|
target: {
|
|
10712
11406
|
type: "string",
|
|
10713
|
-
description: "Page selector (index:/url:/title:/label:/targetId, default active); grammar in server instructions."
|
|
11407
|
+
description: "Page selector (index:/url:/title:/label:/targetId, endpoint:<name>/ prefix, default active); grammar in server instructions."
|
|
10714
11408
|
},
|
|
10715
11409
|
schema: {
|
|
10716
11410
|
type: "object",
|
|
@@ -10774,16 +11468,112 @@ var MANIFEST = [
|
|
|
10774
11468
|
}
|
|
10775
11469
|
];
|
|
10776
11470
|
|
|
11471
|
+
// src/policy.ts
|
|
11472
|
+
var READ_TOOLS = new Set([
|
|
11473
|
+
"list_pages",
|
|
11474
|
+
"wait_for",
|
|
11475
|
+
"evaluate_script",
|
|
11476
|
+
"list_cookies",
|
|
11477
|
+
"take_snapshot",
|
|
11478
|
+
"take_screenshot",
|
|
11479
|
+
"list_console_messages",
|
|
11480
|
+
"get_console_message",
|
|
11481
|
+
"list_network_requests",
|
|
11482
|
+
"get_network_request",
|
|
11483
|
+
"performance_analyze_insight",
|
|
11484
|
+
"take_heapsnapshot",
|
|
11485
|
+
"list_mocks",
|
|
11486
|
+
"extract_page",
|
|
11487
|
+
"list_leases"
|
|
11488
|
+
]);
|
|
11489
|
+
var MUTATING_TOOLS = new Set([
|
|
11490
|
+
"new_page",
|
|
11491
|
+
"close_page",
|
|
11492
|
+
"select_page",
|
|
11493
|
+
"navigate_page",
|
|
11494
|
+
"set_cookie",
|
|
11495
|
+
"delete_cookies",
|
|
11496
|
+
"click",
|
|
11497
|
+
"hover",
|
|
11498
|
+
"drag",
|
|
11499
|
+
"scroll",
|
|
11500
|
+
"fill",
|
|
11501
|
+
"fill_form",
|
|
11502
|
+
"type_text",
|
|
11503
|
+
"press_key",
|
|
11504
|
+
"upload_file",
|
|
11505
|
+
"emulate",
|
|
11506
|
+
"resize_page",
|
|
11507
|
+
"handle_dialog",
|
|
11508
|
+
"performance_start_trace",
|
|
11509
|
+
"performance_stop_trace",
|
|
11510
|
+
"performance_trace",
|
|
11511
|
+
"start_screen_recording",
|
|
11512
|
+
"stop_screen_recording",
|
|
11513
|
+
"lighthouse_audit",
|
|
11514
|
+
"mock_request",
|
|
11515
|
+
"clear_mocks",
|
|
11516
|
+
"dispatch_mouse",
|
|
11517
|
+
"focus_emulation",
|
|
11518
|
+
"click_focus_gated",
|
|
11519
|
+
"wait_for_download",
|
|
11520
|
+
"grant_permissions",
|
|
11521
|
+
"claim_page",
|
|
11522
|
+
"release_page"
|
|
11523
|
+
]);
|
|
11524
|
+
var META_TOOLS = new Set(["search_tools", "call_tool", "describe_tool"]);
|
|
11525
|
+
|
|
11526
|
+
class PolicyError extends Error {
|
|
11527
|
+
code = "read_only_endpoint";
|
|
11528
|
+
tool;
|
|
11529
|
+
endpoint;
|
|
11530
|
+
constructor(tool, endpoint) {
|
|
11531
|
+
super(`'${tool}' is not allowed on endpoint '${endpoint}': the endpoint is read-only (tools that change state are refused before any CDP message is sent)`);
|
|
11532
|
+
this.name = "PolicyError";
|
|
11533
|
+
this.tool = tool;
|
|
11534
|
+
this.endpoint = endpoint;
|
|
11535
|
+
}
|
|
11536
|
+
}
|
|
11537
|
+
function escalatesToMutation(tool, args) {
|
|
11538
|
+
if (typeof args !== "object" || args === null)
|
|
11539
|
+
return false;
|
|
11540
|
+
const a = args;
|
|
11541
|
+
switch (tool) {
|
|
11542
|
+
case "list_console_messages":
|
|
11543
|
+
case "list_network_requests":
|
|
11544
|
+
return a.reload === true;
|
|
11545
|
+
case "get_network_request":
|
|
11546
|
+
return a.includeBody === true && typeof a.url === "string" && a.url !== "";
|
|
11547
|
+
case "take_screenshot":
|
|
11548
|
+
return a.renderWidth !== undefined || a.renderHeight !== undefined;
|
|
11549
|
+
default:
|
|
11550
|
+
return false;
|
|
11551
|
+
}
|
|
11552
|
+
}
|
|
11553
|
+
function assertAllowed(tool, endpoint, args) {
|
|
11554
|
+
if (!endpoint.readOnly)
|
|
11555
|
+
return;
|
|
11556
|
+
if (META_TOOLS.has(tool))
|
|
11557
|
+
return;
|
|
11558
|
+
if (!READ_TOOLS.has(tool))
|
|
11559
|
+
throw new PolicyError(tool, endpoint.name);
|
|
11560
|
+
if (escalatesToMutation(tool, args))
|
|
11561
|
+
throw new PolicyError(tool, endpoint.name);
|
|
11562
|
+
}
|
|
11563
|
+
function toolAnnotations(name) {
|
|
11564
|
+
return { readOnlyHint: READ_TOOLS.has(name) };
|
|
11565
|
+
}
|
|
11566
|
+
|
|
10777
11567
|
// src/install/wizard.ts
|
|
10778
11568
|
import { createInterface } from "node:readline";
|
|
10779
11569
|
import { homedir as homedir3 } from "node:os";
|
|
10780
|
-
import { join as
|
|
11570
|
+
import { join as join16 } from "node:path";
|
|
10781
11571
|
|
|
10782
11572
|
// src/install/browser.ts
|
|
10783
11573
|
import { existsSync as existsSync3 } from "node:fs";
|
|
10784
|
-
import { mkdir as mkdir14, readFile as
|
|
11574
|
+
import { mkdir as mkdir14, readFile as readFile10, writeFile as writeFile11 } from "node:fs/promises";
|
|
10785
11575
|
import { homedir } from "node:os";
|
|
10786
|
-
import { delimiter, dirname as dirname4, join as
|
|
11576
|
+
import { delimiter, dirname as dirname4, join as join14 } from "node:path";
|
|
10787
11577
|
var CANDIDATE_BUILDERS = {
|
|
10788
11578
|
chrome: () => [
|
|
10789
11579
|
process.env.CHROME_BIN,
|
|
@@ -10813,7 +11603,7 @@ function existsOnPath(cmd) {
|
|
|
10813
11603
|
for (const dir of pathEnv.split(delimiter)) {
|
|
10814
11604
|
if (!dir)
|
|
10815
11605
|
continue;
|
|
10816
|
-
if (existsSync3(
|
|
11606
|
+
if (existsSync3(join14(dir, cmd)))
|
|
10817
11607
|
return true;
|
|
10818
11608
|
}
|
|
10819
11609
|
return false;
|
|
@@ -10863,10 +11653,10 @@ function aliasLine(o) {
|
|
|
10863
11653
|
function detectShellRc() {
|
|
10864
11654
|
const shell = process.env.SHELL ?? "";
|
|
10865
11655
|
if (shell.endsWith("zsh"))
|
|
10866
|
-
return
|
|
11656
|
+
return join14(homedir(), ".zshrc");
|
|
10867
11657
|
if (shell.endsWith("bash"))
|
|
10868
|
-
return
|
|
10869
|
-
return
|
|
11658
|
+
return join14(homedir(), ".bashrc");
|
|
11659
|
+
return join14(homedir(), process.platform === "darwin" ? ".zshrc" : ".bashrc");
|
|
10870
11660
|
}
|
|
10871
11661
|
var MARKER_START = "# >>> cdp-toolkit alias >>>";
|
|
10872
11662
|
var MARKER_END = "# <<< cdp-toolkit alias <<<";
|
|
@@ -10878,7 +11668,7 @@ function ensureTrailingNewline(s) {
|
|
|
10878
11668
|
async function appendAliasToRc(rcPath, aliasBlockBody) {
|
|
10879
11669
|
let existing = "";
|
|
10880
11670
|
try {
|
|
10881
|
-
existing = await
|
|
11671
|
+
existing = await readFile10(rcPath, "utf8");
|
|
10882
11672
|
} catch (err) {
|
|
10883
11673
|
if (err.code !== "ENOENT")
|
|
10884
11674
|
throw err;
|
|
@@ -10914,9 +11704,9 @@ ${after}`);
|
|
|
10914
11704
|
|
|
10915
11705
|
// src/install/harness.ts
|
|
10916
11706
|
import { spawnSync } from "node:child_process";
|
|
10917
|
-
import { mkdir as mkdir15, readFile as
|
|
11707
|
+
import { mkdir as mkdir15, readFile as readFile11, writeFile as writeFile12 } from "node:fs/promises";
|
|
10918
11708
|
import { homedir as homedir2 } from "node:os";
|
|
10919
|
-
import { dirname as dirname5, join as
|
|
11709
|
+
import { dirname as dirname5, join as join15 } from "node:path";
|
|
10920
11710
|
async function installMcpServer(harness, name, cfg, opts = {}) {
|
|
10921
11711
|
switch (harness) {
|
|
10922
11712
|
case "claude":
|
|
@@ -10932,11 +11722,11 @@ async function installMcpServer(harness, name, cfg, opts = {}) {
|
|
|
10932
11722
|
}
|
|
10933
11723
|
}
|
|
10934
11724
|
async function installClaude(name, cfg, opts) {
|
|
10935
|
-
const path = opts.configPath ??
|
|
11725
|
+
const path = opts.configPath ?? join15(opts.homeDir ?? homedir2(), ".claude.json");
|
|
10936
11726
|
let root;
|
|
10937
11727
|
let fileExisted;
|
|
10938
11728
|
try {
|
|
10939
|
-
const text = await
|
|
11729
|
+
const text = await readFile11(path, "utf8");
|
|
10940
11730
|
const parsed = JSON.parse(text);
|
|
10941
11731
|
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
10942
11732
|
throw new Error(`${path} does not contain a JSON object at its top level`);
|
|
@@ -11200,7 +11990,7 @@ async function promptPort(def) {
|
|
|
11200
11990
|
}
|
|
11201
11991
|
function harnessTargetDesc(harness, io) {
|
|
11202
11992
|
if (harness === "claude") {
|
|
11203
|
-
return io.configPath ??
|
|
11993
|
+
return io.configPath ?? join16(io.homeDir ?? homedir3(), ".claude.json");
|
|
11204
11994
|
}
|
|
11205
11995
|
return `via \`${harness} mcp add\``;
|
|
11206
11996
|
}
|
|
@@ -11576,11 +12366,18 @@ async function main() {
|
|
|
11576
12366
|
return withLeaseScope(leaseFromArgs(parsed.args), async () => {
|
|
11577
12367
|
if (browser === "chrome") {
|
|
11578
12368
|
const fn = TOOLS[tool];
|
|
11579
|
-
const
|
|
11580
|
-
|
|
12369
|
+
const { endpoint, args: toolArgs } = stripEndpointArgs(parsed.args);
|
|
12370
|
+
assertAllowed(tool, endpoint, toolArgs);
|
|
12371
|
+
const result = await withEndpointScope(endpoint, () => fn(toolArgs));
|
|
12372
|
+
process.stdout.write(`${JSON.stringify(result ?? null, null, 2)}
|
|
11581
12373
|
`);
|
|
11582
12374
|
return 0;
|
|
11583
12375
|
}
|
|
12376
|
+
if (usesEndpointArgs(parsed.args)) {
|
|
12377
|
+
process.stderr.write(`${JSON.stringify({ error: ENDPOINTS_FIREFOX_MESSAGE })}
|
|
12378
|
+
`);
|
|
12379
|
+
return 1;
|
|
12380
|
+
}
|
|
11584
12381
|
const neutralFn = FIREFOX_TOOLS[tool];
|
|
11585
12382
|
if (!neutralFn) {
|
|
11586
12383
|
process.stderr.write(`${JSON.stringify({ error: `tool '${tool}' has no Firefox implementation wired` })}
|
|
@@ -11590,7 +12387,7 @@ async function main() {
|
|
|
11590
12387
|
const session = await startFirefoxSession({ endpoint });
|
|
11591
12388
|
try {
|
|
11592
12389
|
const result = await neutralFn(session.driver, parsed.args);
|
|
11593
|
-
process.stdout.write(`${JSON.stringify(result, null, 2)}
|
|
12390
|
+
process.stdout.write(`${JSON.stringify(result ?? null, null, 2)}
|
|
11594
12391
|
`);
|
|
11595
12392
|
return 0;
|
|
11596
12393
|
} finally {
|