omowright 0.1.0 → 0.3.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/README.md +33 -23
- package/TOOLS.md +140 -19
- package/dist/agent-tabs.d.ts +56 -0
- package/dist/aria-hidden.d.ts +1 -0
- package/dist/aside/cli-binary.d.ts +7 -0
- package/dist/aside/cli-contract.d.ts +9 -0
- package/dist/aside/errors.d.ts +8 -0
- package/dist/aside/line-transport.d.ts +11 -0
- package/dist/aside/mcp-transport.d.ts +11 -0
- package/dist/aside/onboard.d.ts +76 -0
- package/dist/aside/session-types.d.ts +68 -0
- package/dist/aside/session.d.ts +20 -0
- package/{bin/omowright-cloak.mjs → dist/bin/omowright-cloak.js} +28 -31
- package/dist/bridge-schemas.d.ts +326 -0
- package/dist/bridge-transport.d.ts +144 -0
- package/dist/browser-context.d.ts +4024 -0
- package/dist/browser-process.d.ts +10 -0
- package/dist/bsk/browser-signals.d.ts +23 -0
- package/dist/bsk/browsers.d.ts +46 -0
- package/dist/bsk/connect.d.ts +23 -0
- package/dist/bsk/daemon-info.d.ts +16 -0
- package/dist/bsk/external-extension.d.ts +74 -0
- package/dist/bsk/identify-browser.d.ts +48 -0
- package/dist/bsk/install-cli.d.ts +37 -0
- package/dist/bsk/ipc-client.d.ts +45 -0
- package/dist/bsk/onboard.d.ts +96 -0
- package/dist/bsk/reply-fields.d.ts +3 -0
- package/dist/bsk/screenshot.d.ts +24 -0
- package/dist/bsk/session.d.ts +142 -0
- package/dist/bsk/snapshot.d.ts +16 -0
- package/dist/bsk/targets.d.ts +20 -0
- package/dist/captcha-wait.d.ts +17 -0
- package/dist/captcha.d.ts +46 -0
- package/dist/cdp-errors.d.ts +38 -0
- package/dist/cdp-methods.d.ts +10 -0
- package/dist/cdp-protocol.d.ts +459 -0
- package/dist/chrome-profile.d.ts +77 -0
- package/dist/chrome.d.ts +130 -0
- package/dist/cloak-profile.d.ts +43 -0
- package/dist/connection.d.ts +4024 -0
- package/dist/console-events.d.ts +22 -0
- package/dist/cookies.d.ts +42 -0
- package/dist/core.d.ts +10810 -0
- package/dist/cua.d.ts +56 -0
- package/dist/dialog-policy.d.ts +43 -0
- package/dist/ego/bindings.d.ts +68 -0
- package/dist/ego/cdp-client.d.ts +28 -0
- package/dist/ego/client.d.ts +8 -0
- package/dist/ego/errors.d.ts +7 -0
- package/dist/emulate.d.ts +62 -0
- package/dist/engines.d.ts +59 -0
- package/dist/events.d.ts +93 -0
- package/dist/frames-snapshot.d.ts +58 -0
- package/dist/har.d.ts +88 -0
- package/dist/human-handoff.d.ts +30 -0
- package/{src/index.js → dist/index.d.ts} +12 -53
- package/dist/index.js +39262 -0
- package/dist/injected.d.ts +1 -0
- package/dist/instance-lock.d.ts +5 -0
- package/dist/instances.d.ts +45 -0
- package/dist/internal-capability.d.ts +1 -0
- package/dist/launch-endpoint.d.ts +34 -0
- package/dist/layers.d.ts +31 -0
- package/dist/llm.d.ts +1349 -0
- package/dist/lock-backend-win32-xbcm60vz.js +57 -0
- package/dist/lock-backend-win32.d.ts +3 -0
- package/dist/lock-backend.d.ts +6 -0
- package/dist/native-host-win32.d.ts +38 -0
- package/dist/network-events.d.ts +66 -0
- package/dist/network-snoop.d.ts +56 -0
- package/dist/nul-frames.d.ts +4 -0
- package/dist/open-shadow-query.d.ts +1 -0
- package/dist/owner-only.d.ts +15 -0
- package/dist/page-bundle.js +1587 -0
- package/dist/page-events.d.ts +25 -0
- package/dist/page-routes.d.ts +30 -0
- package/dist/page-setup.d.ts +55 -0
- package/dist/pipe.d.ts +71 -0
- package/dist/process-start-time.d.ts +2 -0
- package/dist/read-property.d.ts +2 -0
- package/dist/routes.d.ts +60 -0
- package/dist/running-browser.d.ts +39 -0
- package/dist/scroll-collect.d.ts +36 -0
- package/dist/serial-emitter.d.ts +5 -0
- package/dist/shared-connections.d.ts +11 -0
- package/dist/snapshot-diff.d.ts +57 -0
- package/dist/text-matching.d.ts +21 -0
- package/dist/trace.d.ts +59 -0
- package/dist/url-match.d.ts +7 -0
- package/dist/viewport.d.ts +26 -0
- package/dist/ws.d.ts +62 -0
- package/package.json +47 -26
- package/presets/agent-events/SKILL.md +1 -1
- package/presets/captcha/SKILL.md +1 -3
- package/presets/chrome/SKILL.md +11 -7
- package/presets/cloak-bridge/SKILL.md +11 -11
- package/presets/network/SKILL.md +15 -13
- package/presets/visual-browse/SKILL.md +14 -10
- package/skills/omowright/SKILL.md +77 -47
- package/skills/omowright/references/aside.md +50 -0
- package/skills/omowright/references/browserskill.md +39 -33
- package/skills/omowright/references/ego.md +24 -0
- package/skills/omowright/references/eval-kernel.md +44 -23
- package/skills/omowright/references/frames-layers-human.md +16 -7
- package/skills/omowright/references/quickstart.md +17 -4
- package/skills/omowright/references/stealth.md +7 -7
- package/src/agent-tabs.js +0 -160
- package/src/bridge-schemas.js +0 -44
- package/src/bridge-transport.js +0 -57
- package/src/browser-process.js +0 -122
- package/src/bsk/browser-signals.js +0 -90
- package/src/bsk/browsers.js +0 -114
- package/src/bsk/connect.js +0 -45
- package/src/bsk/daemon-info.js +0 -100
- package/src/bsk/external-extension.js +0 -110
- package/src/bsk/identify-browser.js +0 -88
- package/src/bsk/install-cli.js +0 -81
- package/src/bsk/ipc-client.js +0 -236
- package/src/bsk/onboard.js +0 -133
- package/src/bsk/screenshot.js +0 -49
- package/src/bsk/session.js +0 -131
- package/src/bsk/snapshot.js +0 -56
- package/src/bsk/targets.js +0 -25
- package/src/captcha-wait.js +0 -74
- package/src/captcha.js +0 -142
- package/src/cdp-errors.js +0 -23
- package/src/cdp-methods.js +0 -61
- package/src/chrome-profile.js +0 -176
- package/src/chrome.js +0 -192
- package/src/cloak-profile.js +0 -266
- package/src/connection.js +0 -135
- package/src/cookies.js +0 -29
- package/src/core.js +0 -1710
- package/src/cua.js +0 -74
- package/src/dialog-policy.js +0 -80
- package/src/emulate.js +0 -127
- package/src/events.js +0 -111
- package/src/frames-snapshot.js +0 -138
- package/src/har.js +0 -103
- package/src/human-handoff.js +0 -122
- package/src/injected.js +0 -3
- package/src/instance-lock.js +0 -230
- package/src/instances.js +0 -493
- package/src/internal-capability.js +0 -1
- package/src/launch-endpoint.js +0 -185
- package/src/layers.js +0 -138
- package/src/llm.js +0 -201
- package/src/lock-file.js +0 -130
- package/src/native-host-win32.js +0 -201
- package/src/network-events.js +0 -201
- package/src/network-snoop.js +0 -229
- package/src/open-shadow-query.js +0 -22
- package/src/owner-only.js +0 -109
- package/src/page-bundle.js +0 -1397
- package/src/page-routes.js +0 -94
- package/src/pipe.js +0 -391
- package/src/routes.js +0 -256
- package/src/running-browser.js +0 -85
- package/src/scroll-collect.js +0 -73
- package/src/serial-emitter.js +0 -17
- package/src/shared-connections.js +0 -278
- package/src/snapshot-diff.js +0 -75
- package/src/trace.js +0 -183
- package/src/url-match.js +0 -148
- package/src/viewport.js +0 -12
package/src/instances.js
DELETED
|
@@ -1,493 +0,0 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
-
import {
|
|
4
|
-
mkdir,
|
|
5
|
-
open,
|
|
6
|
-
readFile,
|
|
7
|
-
readdir,
|
|
8
|
-
rename,
|
|
9
|
-
unlink,
|
|
10
|
-
} from "node:fs/promises";
|
|
11
|
-
import os from "node:os";
|
|
12
|
-
import path from "node:path";
|
|
13
|
-
import { profileDirFromArgs, terminateChild } from "./browser-process.js";
|
|
14
|
-
import { openConnection } from "./connection.js";
|
|
15
|
-
import { isPidAlive, unlinkIfPresent, withLock } from "./instance-lock.js";
|
|
16
|
-
import {
|
|
17
|
-
assertLaunchedBrowser,
|
|
18
|
-
discoverLaunchedEndpoint,
|
|
19
|
-
snapshotActivePort,
|
|
20
|
-
} from "./launch-endpoint.js";
|
|
21
|
-
import { restrictToOwner } from "./owner-only.js";
|
|
22
|
-
import { acquireSharedConnection } from "./shared-connections.js";
|
|
23
|
-
|
|
24
|
-
const RECORD_VERSION = 2;
|
|
25
|
-
const DEFAULT_REGISTRY_ROOT = path.join(
|
|
26
|
-
os.homedir(),
|
|
27
|
-
".local",
|
|
28
|
-
"share",
|
|
29
|
-
"omowright",
|
|
30
|
-
"instances",
|
|
31
|
-
);
|
|
32
|
-
const DEFAULT_PROBE_TIMEOUT_MS = 1_000;
|
|
33
|
-
const DEFAULT_LAUNCH_TIMEOUT_MS = 10_000;
|
|
34
|
-
const DEFAULT_CONNECT_TIMEOUT_MS = 10_000;
|
|
35
|
-
const BROWSER_CLOSE_TIMEOUT_MS = 2_000;
|
|
36
|
-
// Waiting for the lock has to outlast the slowest honest critical section
|
|
37
|
-
// (probe + launch + CDP attach); a shorter budget makes a second caller give up
|
|
38
|
-
// at the exact moment the first one is about to publish its record.
|
|
39
|
-
const LOCK_TIMEOUT_MARGIN_MS = 5_000;
|
|
40
|
-
// Same sum the per-call resolver below computes, for callers with no overrides.
|
|
41
|
-
const DEFAULT_LOCK_TIMEOUT_MS = resolveLockTimeoutMs({});
|
|
42
|
-
// A lock whose content never became readable counts as held until it is older
|
|
43
|
-
// than any legitimate hold: an unreadable lock is far more likely to be a
|
|
44
|
-
// contender caught mid-acquire than an abandoned file.
|
|
45
|
-
const STALE_LOCK_GRACE_MS = DEFAULT_LOCK_TIMEOUT_MS;
|
|
46
|
-
|
|
47
|
-
function normalizeName(name) {
|
|
48
|
-
if (
|
|
49
|
-
typeof name !== "string" ||
|
|
50
|
-
!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(name)
|
|
51
|
-
) {
|
|
52
|
-
throw new TypeError(
|
|
53
|
-
"instance name must start with a letter or number and contain only letters, numbers, dots, dashes, or underscores",
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
return name;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function registryPaths(registryRoot, name) {
|
|
60
|
-
const root = path.resolve(registryRoot ?? DEFAULT_REGISTRY_ROOT);
|
|
61
|
-
return {
|
|
62
|
-
root,
|
|
63
|
-
recordPath: path.join(root, `${name}.json`),
|
|
64
|
-
lockPath: path.join(root, `${name}.lock`),
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async function ensureRegistryRoot(root) {
|
|
69
|
-
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
70
|
-
await restrictToOwner(root, "directory");
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function isRecord(value, expectedName) {
|
|
74
|
-
return (
|
|
75
|
-
value !== null &&
|
|
76
|
-
typeof value === "object" &&
|
|
77
|
-
value.version === RECORD_VERSION &&
|
|
78
|
-
value.name === expectedName &&
|
|
79
|
-
Number.isInteger(value.pid) &&
|
|
80
|
-
value.pid > 0 &&
|
|
81
|
-
typeof value.endpoint === "string" &&
|
|
82
|
-
value.endpoint.length > 0 &&
|
|
83
|
-
typeof value.webSocketDebuggerUrl === "string" &&
|
|
84
|
-
value.webSocketDebuggerUrl.length > 0 &&
|
|
85
|
-
(typeof value.profileDir === "string" || value.profileDir === null) &&
|
|
86
|
-
typeof value.browserPath === "string" &&
|
|
87
|
-
value.browserPath.length > 0 &&
|
|
88
|
-
typeof value.startedAt === "string" &&
|
|
89
|
-
!Number.isNaN(Date.parse(value.startedAt))
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async function readRecord(recordPath, expectedName) {
|
|
94
|
-
let text;
|
|
95
|
-
try {
|
|
96
|
-
text = await readFile(recordPath, "utf8");
|
|
97
|
-
} catch (error) {
|
|
98
|
-
if (error?.code === "ENOENT") return { exists: false, record: null };
|
|
99
|
-
throw error;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
try {
|
|
103
|
-
const record = JSON.parse(text);
|
|
104
|
-
return {
|
|
105
|
-
exists: true,
|
|
106
|
-
record: isRecord(record, expectedName) ? record : null,
|
|
107
|
-
};
|
|
108
|
-
} catch {
|
|
109
|
-
return { exists: true, record: null };
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async function writeRecordAtomic(root, recordPath, record) {
|
|
114
|
-
const tempPath = path.join(
|
|
115
|
-
root,
|
|
116
|
-
`.${record.name}.${process.pid}.${randomUUID()}.tmp`,
|
|
117
|
-
);
|
|
118
|
-
let handle;
|
|
119
|
-
try {
|
|
120
|
-
handle = await open(tempPath, "wx", 0o600);
|
|
121
|
-
await handle.writeFile(`${JSON.stringify(record, null, 2)}\n`, "utf8");
|
|
122
|
-
await handle.close();
|
|
123
|
-
handle = null;
|
|
124
|
-
await restrictToOwner(tempPath, "file");
|
|
125
|
-
await rename(tempPath, recordPath);
|
|
126
|
-
await restrictToOwner(recordPath, "file");
|
|
127
|
-
} catch (error) {
|
|
128
|
-
await handle?.close().catch(() => {});
|
|
129
|
-
await unlink(tempPath).catch(() => {});
|
|
130
|
-
throw error;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function isConnectionRefused(error) {
|
|
135
|
-
for (let cause = error; cause; cause = cause?.cause) {
|
|
136
|
-
if (
|
|
137
|
-
cause.code === "ECONNREFUSED" ||
|
|
138
|
-
cause.code === "ENOTFOUND" ||
|
|
139
|
-
// Bun's fetch reports refused connections with this code.
|
|
140
|
-
cause.code === "ConnectionRefused"
|
|
141
|
-
) {
|
|
142
|
-
return true;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
async function probeEndpoint(endpoint, timeoutMs) {
|
|
149
|
-
const versionUrl = new URL("/json/version", endpoint);
|
|
150
|
-
const response = await fetch(versionUrl, {
|
|
151
|
-
signal: AbortSignal.timeout(timeoutMs),
|
|
152
|
-
});
|
|
153
|
-
if (!response.ok) {
|
|
154
|
-
throw new Error(
|
|
155
|
-
`CDP discovery request failed (${response.status} ${response.statusText})`,
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
const version = await response.json();
|
|
159
|
-
if (
|
|
160
|
-
typeof version?.webSocketDebuggerUrl !== "string" ||
|
|
161
|
-
version.webSocketDebuggerUrl.length === 0
|
|
162
|
-
) {
|
|
163
|
-
throw new Error("CDP discovery response lacks webSocketDebuggerUrl");
|
|
164
|
-
}
|
|
165
|
-
return version;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function resolveConnectTimeoutMs(options) {
|
|
169
|
-
return options.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function resolveLockTimeoutMs(options) {
|
|
173
|
-
if (options.lockTimeoutMs !== undefined) return options.lockTimeoutMs;
|
|
174
|
-
return (
|
|
175
|
-
(options.launchTimeoutMs ?? DEFAULT_LAUNCH_TIMEOUT_MS) +
|
|
176
|
-
(options.probeTimeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS) +
|
|
177
|
-
resolveConnectTimeoutMs(options) +
|
|
178
|
-
LOCK_TIMEOUT_MARGIN_MS
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function nameLockOptions(options) {
|
|
183
|
-
return {
|
|
184
|
-
timeoutMs: resolveLockTimeoutMs(options),
|
|
185
|
-
staleGraceMs: STALE_LOCK_GRACE_MS,
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// `/json/version` answering proves nothing about the WebSocket upgrade behind
|
|
190
|
-
// it, so the session handshake carries its own bound.
|
|
191
|
-
async function connectWithin(endpoint, options, timeoutMs) {
|
|
192
|
-
const pending = openConnection(endpoint, options);
|
|
193
|
-
let timer;
|
|
194
|
-
try {
|
|
195
|
-
return await Promise.race([
|
|
196
|
-
pending,
|
|
197
|
-
new Promise((_, reject) => {
|
|
198
|
-
timer = setTimeout(
|
|
199
|
-
() =>
|
|
200
|
-
reject(
|
|
201
|
-
new Error(
|
|
202
|
-
`timed out after ${timeoutMs}ms opening a CDP session on ${endpoint}`,
|
|
203
|
-
),
|
|
204
|
-
),
|
|
205
|
-
timeoutMs,
|
|
206
|
-
);
|
|
207
|
-
}),
|
|
208
|
-
]);
|
|
209
|
-
} catch (error) {
|
|
210
|
-
// A session that lands after the bound would otherwise hold a live socket.
|
|
211
|
-
pending.then(
|
|
212
|
-
(connection) => connection.close().catch(() => {}),
|
|
213
|
-
() => {},
|
|
214
|
-
);
|
|
215
|
-
throw error;
|
|
216
|
-
} finally {
|
|
217
|
-
clearTimeout(timer);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
async function removeOwnedRecord(recordPath, name, pid) {
|
|
222
|
-
const loaded = await readRecord(recordPath, name);
|
|
223
|
-
if (loaded.record?.pid !== pid) return;
|
|
224
|
-
await unlinkIfPresent(recordPath);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function connectionOptions(options, profileDir) {
|
|
228
|
-
const result = { ...(options.connectionOptions ?? {}) };
|
|
229
|
-
if (options.storageRoot !== undefined) result.storageRoot = options.storageRoot;
|
|
230
|
-
else if (result.storageRoot === undefined && profileDir) {
|
|
231
|
-
result.storageRoot = profileDir;
|
|
232
|
-
}
|
|
233
|
-
if (options.client !== undefined) {
|
|
234
|
-
result.client = {
|
|
235
|
-
...(result.client ?? {}),
|
|
236
|
-
...options.client,
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
// Part of the sharing key too: callers asking for different dialog policies
|
|
240
|
-
// never share one connection.
|
|
241
|
-
if (options.dialogPolicy !== undefined) result.dialogPolicy = options.dialogPolicy;
|
|
242
|
-
return result;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Asked over CDP first, as the pipe transport does: on Windows no signal can
|
|
246
|
-
// ask a browser to quit, and Chrome cleans up only on a clean quit. Only a live
|
|
247
|
-
// browser on a live socket is asked: CdpClient.send would otherwise reconnect
|
|
248
|
-
// through the recorded port, which is not an identity (another process may
|
|
249
|
-
// hold it by now). The connection is closed as soon as the request is answered,
|
|
250
|
-
// before the quitting browser drops its socket: a drop on an open client starts
|
|
251
|
-
// its auto-reconnect loop, which would keep the caller's process alive. Then,
|
|
252
|
-
// like the pipe transport, the browser gets a bounded wait to exit before
|
|
253
|
-
// terminateChild steps in.
|
|
254
|
-
async function askBrowserToQuit(connection, child, disconnect) {
|
|
255
|
-
if (child.exitCode !== null || child.signalCode !== null || !connection.cdp.isConnected) {
|
|
256
|
-
await disconnect();
|
|
257
|
-
return false;
|
|
258
|
-
}
|
|
259
|
-
const exited = new Promise((resolve) => child.once("exit", resolve));
|
|
260
|
-
const quitRequested = await Promise.race([
|
|
261
|
-
connection.cdp
|
|
262
|
-
.send("Browser.close", undefined, undefined, { timeoutMs: BROWSER_CLOSE_TIMEOUT_MS })
|
|
263
|
-
.then(() => true, () => false),
|
|
264
|
-
exited.then(() => true),
|
|
265
|
-
]);
|
|
266
|
-
await disconnect();
|
|
267
|
-
if (child.exitCode === null && child.signalCode === null) {
|
|
268
|
-
let timer;
|
|
269
|
-
await Promise.race([
|
|
270
|
-
exited,
|
|
271
|
-
new Promise((resolve) => { timer = setTimeout(resolve, BROWSER_CLOSE_TIMEOUT_MS); }),
|
|
272
|
-
]);
|
|
273
|
-
clearTimeout(timer);
|
|
274
|
-
}
|
|
275
|
-
return quitRequested;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
async function launchNamed(
|
|
279
|
-
name,
|
|
280
|
-
{ root, recordPath, lockPath },
|
|
281
|
-
options,
|
|
282
|
-
) {
|
|
283
|
-
if (typeof options.browserPath !== "string" || options.browserPath.length === 0) {
|
|
284
|
-
throw new TypeError(
|
|
285
|
-
`connectNamed("${name}") requires browserPath when no healthy instance is registered`,
|
|
286
|
-
);
|
|
287
|
-
}
|
|
288
|
-
const browserArgs = [...(options.browserArgs ?? [])];
|
|
289
|
-
const profileDir = options.profileDir
|
|
290
|
-
? path.resolve(options.profileDir)
|
|
291
|
-
: profileDirFromArgs(browserArgs);
|
|
292
|
-
const previousActivePort = await snapshotActivePort(profileDir);
|
|
293
|
-
const child = spawn(
|
|
294
|
-
options.browserPath,
|
|
295
|
-
[
|
|
296
|
-
...browserArgs,
|
|
297
|
-
"--remote-debugging-address=127.0.0.1",
|
|
298
|
-
"--remote-debugging-port=0",
|
|
299
|
-
],
|
|
300
|
-
{
|
|
301
|
-
// A named browser is shared through the registry, so it has to outlive
|
|
302
|
-
// the process that launched it. On Windows libuv puts every non-detached
|
|
303
|
-
// child in a kill-on-close job object, which kills the browser the
|
|
304
|
-
// moment its launcher exits (#90); detached keeps it out of that job
|
|
305
|
-
// (DETACHED_PROCESS also gives a console-subsystem browser no console).
|
|
306
|
-
// No windowsHide: it would start a headful browser's window hidden.
|
|
307
|
-
...(process.platform === "win32" ? { detached: true } : {}),
|
|
308
|
-
...(options.spawnOptions ?? {}),
|
|
309
|
-
stdio: ["ignore", "ignore", "pipe"],
|
|
310
|
-
},
|
|
311
|
-
);
|
|
312
|
-
|
|
313
|
-
let connection;
|
|
314
|
-
try {
|
|
315
|
-
const launchTimeoutMs = options.launchTimeoutMs ?? DEFAULT_LAUNCH_TIMEOUT_MS;
|
|
316
|
-
const report = await discoverLaunchedEndpoint(child, {
|
|
317
|
-
profileDir,
|
|
318
|
-
timeoutMs: launchTimeoutMs,
|
|
319
|
-
previousActivePort,
|
|
320
|
-
});
|
|
321
|
-
const { endpoint } = report;
|
|
322
|
-
// The browser's own WebSocket URL carries its GUID: recording it makes a
|
|
323
|
-
// later attach prove it reached this browser and not whatever else ends up
|
|
324
|
-
// on the port once this pid is gone.
|
|
325
|
-
const { webSocketDebuggerUrl } = await probeEndpoint(
|
|
326
|
-
endpoint,
|
|
327
|
-
launchTimeoutMs,
|
|
328
|
-
);
|
|
329
|
-
assertLaunchedBrowser(child, report, webSocketDebuggerUrl);
|
|
330
|
-
connection = await connectWithin(
|
|
331
|
-
endpoint,
|
|
332
|
-
connectionOptions(options, profileDir),
|
|
333
|
-
resolveConnectTimeoutMs(options),
|
|
334
|
-
);
|
|
335
|
-
const record = {
|
|
336
|
-
version: RECORD_VERSION,
|
|
337
|
-
name,
|
|
338
|
-
pid: child.pid,
|
|
339
|
-
endpoint,
|
|
340
|
-
webSocketDebuggerUrl,
|
|
341
|
-
profileDir,
|
|
342
|
-
browserPath: path.resolve(options.browserPath),
|
|
343
|
-
startedAt: new Date().toISOString(),
|
|
344
|
-
};
|
|
345
|
-
await writeRecordAtomic(root, recordPath, record);
|
|
346
|
-
connection.browserProcess = child;
|
|
347
|
-
const disconnect = connection.close.bind(connection);
|
|
348
|
-
connection.close = async () => {
|
|
349
|
-
let failure;
|
|
350
|
-
const quitRequested = await askBrowserToQuit(connection, child, async () => {
|
|
351
|
-
try {
|
|
352
|
-
await disconnect();
|
|
353
|
-
} catch (error) {
|
|
354
|
-
failure = error;
|
|
355
|
-
}
|
|
356
|
-
});
|
|
357
|
-
await terminateChild(child, { quitRequested });
|
|
358
|
-
// The browser this record points at is gone, so the record goes with it
|
|
359
|
-
// instead of waiting for the next caller to clean it up.
|
|
360
|
-
try {
|
|
361
|
-
await withLock(lockPath, nameLockOptions(options), () =>
|
|
362
|
-
removeOwnedRecord(recordPath, name, child.pid),
|
|
363
|
-
);
|
|
364
|
-
} catch (error) {
|
|
365
|
-
failure ??= error;
|
|
366
|
-
}
|
|
367
|
-
if (failure) throw failure;
|
|
368
|
-
};
|
|
369
|
-
return connection;
|
|
370
|
-
} catch (error) {
|
|
371
|
-
await connection?.close().catch(() => {});
|
|
372
|
-
await terminateChild(child);
|
|
373
|
-
throw error;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
export async function withEngineLock(registryRoot, engineKey, run) {
|
|
378
|
-
const root = path.resolve(registryRoot ?? DEFAULT_REGISTRY_ROOT);
|
|
379
|
-
await ensureRegistryRoot(root);
|
|
380
|
-
const digest = createHash("sha256")
|
|
381
|
-
.update(path.resolve(engineKey))
|
|
382
|
-
.digest("hex")
|
|
383
|
-
.slice(0, 16);
|
|
384
|
-
const lockPath = path.join(root, `_engine-${digest}.lock`);
|
|
385
|
-
return withLock(
|
|
386
|
-
lockPath,
|
|
387
|
-
{ timeoutMs: DEFAULT_LOCK_TIMEOUT_MS, staleGraceMs: STALE_LOCK_GRACE_MS },
|
|
388
|
-
run,
|
|
389
|
-
);
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
export async function connectNamed(name, options = {}) {
|
|
393
|
-
const normalizedName = normalizeName(name);
|
|
394
|
-
const paths = registryPaths(options.registryRoot, normalizedName);
|
|
395
|
-
await ensureRegistryRoot(paths.root);
|
|
396
|
-
return withLock(paths.lockPath, nameLockOptions(options), () =>
|
|
397
|
-
attachOrLaunch(normalizedName, paths, options),
|
|
398
|
-
);
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
async function attachOrLaunch(normalizedName, paths, options) {
|
|
402
|
-
const loaded = await readRecord(paths.recordPath, normalizedName);
|
|
403
|
-
if (loaded.record) {
|
|
404
|
-
await restrictToOwner(paths.recordPath, "file");
|
|
405
|
-
if (isPidAlive(loaded.record.pid)) {
|
|
406
|
-
let version = null;
|
|
407
|
-
try {
|
|
408
|
-
version = await probeEndpoint(
|
|
409
|
-
loaded.record.endpoint,
|
|
410
|
-
options.probeTimeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS,
|
|
411
|
-
);
|
|
412
|
-
} catch (error) {
|
|
413
|
-
if (!isConnectionRefused(error)) {
|
|
414
|
-
throw new Error(
|
|
415
|
-
`unresponsive holder "${normalizedName}" pid ${loaded.record.pid} endpoint ${loaded.record.endpoint}: ${error.message}`,
|
|
416
|
-
{ cause: error },
|
|
417
|
-
);
|
|
418
|
-
}
|
|
419
|
-
// A live pid whose recorded debug port refuses connections is a
|
|
420
|
-
// reused pid, not a holder: the record is stale and the launch
|
|
421
|
-
// below reclaims it.
|
|
422
|
-
}
|
|
423
|
-
// A pid that came back and a port any local process can rebind are
|
|
424
|
-
// not an identity. Only the recorded WebSocket URL (it carries the
|
|
425
|
-
// browser GUID) says this is our browser; anything else answering
|
|
426
|
-
// there is a stranger, so the record is stale, not a holder.
|
|
427
|
-
if (
|
|
428
|
-
version?.webSocketDebuggerUrl === loaded.record.webSocketDebuggerUrl
|
|
429
|
-
) {
|
|
430
|
-
// Connecting to the recorded GUID URL, not the port, keeps the shared
|
|
431
|
-
// connection's auto-reconnect from landing on a different browser.
|
|
432
|
-
const { webSocketDebuggerUrl } = loaded.record;
|
|
433
|
-
const timeoutMs = resolveConnectTimeoutMs(options);
|
|
434
|
-
try {
|
|
435
|
-
return await acquireSharedConnection(
|
|
436
|
-
webSocketDebuggerUrl,
|
|
437
|
-
connectionOptions(options, loaded.record.profileDir),
|
|
438
|
-
{
|
|
439
|
-
open: (shared) =>
|
|
440
|
-
connectWithin(webSocketDebuggerUrl, shared, timeoutMs),
|
|
441
|
-
timeoutMs,
|
|
442
|
-
},
|
|
443
|
-
);
|
|
444
|
-
} catch (error) {
|
|
445
|
-
throw new Error(
|
|
446
|
-
`unresponsive holder "${normalizedName}" pid ${loaded.record.pid} endpoint ${loaded.record.endpoint}: ${error.message}`,
|
|
447
|
-
{ cause: error },
|
|
448
|
-
);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
if (loaded.exists) {
|
|
455
|
-
await unlink(paths.recordPath).catch((error) => {
|
|
456
|
-
if (error?.code !== "ENOENT") throw error;
|
|
457
|
-
});
|
|
458
|
-
}
|
|
459
|
-
return await launchNamed(normalizedName, paths, options);
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
export async function listInstances(options = {}) {
|
|
463
|
-
const root = path.resolve(options.registryRoot ?? DEFAULT_REGISTRY_ROOT);
|
|
464
|
-
await ensureRegistryRoot(root);
|
|
465
|
-
const entries = await readdir(root, { withFileTypes: true });
|
|
466
|
-
const records = await Promise.all(
|
|
467
|
-
entries
|
|
468
|
-
.filter((entry) => entry.isFile() && entry.name.endsWith(".json"))
|
|
469
|
-
.map(async (entry) => {
|
|
470
|
-
const expectedName = entry.name.slice(0, -".json".length);
|
|
471
|
-
const recordPath = path.join(root, entry.name);
|
|
472
|
-
const loaded = await readRecord(recordPath, expectedName);
|
|
473
|
-
if (!loaded.record) return null;
|
|
474
|
-
await restrictToOwner(recordPath, "file");
|
|
475
|
-
const alive = isPidAlive(loaded.record.pid);
|
|
476
|
-
let responsive = false;
|
|
477
|
-
if (alive) {
|
|
478
|
-
responsive = await probeEndpoint(
|
|
479
|
-
loaded.record.endpoint,
|
|
480
|
-
options.probeTimeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS,
|
|
481
|
-
).then(
|
|
482
|
-
(version) =>
|
|
483
|
-
version.webSocketDebuggerUrl === loaded.record.webSocketDebuggerUrl,
|
|
484
|
-
() => false,
|
|
485
|
-
);
|
|
486
|
-
}
|
|
487
|
-
return { ...loaded.record, alive, responsive };
|
|
488
|
-
}),
|
|
489
|
-
);
|
|
490
|
-
return records
|
|
491
|
-
.filter((record) => record !== null)
|
|
492
|
-
.sort((left, right) => left.name.localeCompare(right.name));
|
|
493
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const targetCreationCapability = Symbol("omowright.targetCreationCapability");
|
package/src/launch-endpoint.js
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import { watch } from "node:fs";
|
|
2
|
-
import { readFile, stat } from "node:fs/promises";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
|
|
5
|
-
// fs.watch can drop or delay directory events on a loaded host (seconds, or
|
|
6
|
-
// never), so the file is also re-read on this interval until the launch settles.
|
|
7
|
-
const ACTIVE_PORT_POLL_MS = 100;
|
|
8
|
-
|
|
9
|
-
function httpEndpointFromWebSocket(webSocketUrl) {
|
|
10
|
-
const endpoint = new URL(webSocketUrl);
|
|
11
|
-
endpoint.protocol = endpoint.protocol === "wss:" ? "https:" : "http:";
|
|
12
|
-
endpoint.pathname = "/";
|
|
13
|
-
endpoint.search = "";
|
|
14
|
-
endpoint.hash = "";
|
|
15
|
-
return endpoint.toString().replace(/\/$/, "");
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// Chromium writes `<port>\n/devtools/browser/<guid>`; the second line names the browser.
|
|
19
|
-
function parseActivePort(text) {
|
|
20
|
-
const [portLine, browserPath] = text.trim().split(/\r?\n/);
|
|
21
|
-
const port = Number(portLine);
|
|
22
|
-
if (!Number.isInteger(port) || port < 1 || port > 65_535) return null;
|
|
23
|
-
return {
|
|
24
|
-
endpoint: `http://127.0.0.1:${port}`,
|
|
25
|
-
browserPath: browserPath?.trim() || null,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function activePortIdentity(info) {
|
|
30
|
-
return { ino: info.ino, mtimeMs: info.mtimeMs, size: info.size };
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// A DevToolsActivePort that is already there before the spawn was written by
|
|
34
|
-
// an earlier run or by another browser on the same profile (#74). Taken before
|
|
35
|
-
// the spawn, so discovery can tell it from the file the new browser writes.
|
|
36
|
-
// The bytes are kept as well as the identity: an identity change does not prove
|
|
37
|
-
// the bytes changed (Linux updates mtime before it copies a write's data, and a
|
|
38
|
-
// touch or an in-place rewrite moves it too), and stale bytes name another
|
|
39
|
-
// browser whose id the launch check would then compare against itself (#107).
|
|
40
|
-
export async function snapshotActivePort(profileDir) {
|
|
41
|
-
if (!profileDir) return null;
|
|
42
|
-
const activePortPath = path.join(profileDir, "DevToolsActivePort");
|
|
43
|
-
try {
|
|
44
|
-
const identity = activePortIdentity(await stat(activePortPath));
|
|
45
|
-
return { ...identity, text: await readFile(activePortPath, "utf8") };
|
|
46
|
-
} catch (error) {
|
|
47
|
-
if (error?.code === "ENOENT") return null;
|
|
48
|
-
throw error;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function isSameFile(info, previous) {
|
|
53
|
-
return (
|
|
54
|
-
previous !== null &&
|
|
55
|
-
info.ino === previous.ino &&
|
|
56
|
-
info.mtimeMs === previous.mtimeMs &&
|
|
57
|
-
info.size === previous.size
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// The endpoint is the launched browser's only if it answers with the browser id
|
|
62
|
-
// that browser reported, and the browser is still the one we spawned.
|
|
63
|
-
export function assertLaunchedBrowser(child, report, webSocketDebuggerUrl) {
|
|
64
|
-
if (child.exitCode !== null || child.signalCode !== null) {
|
|
65
|
-
throw new Error(
|
|
66
|
-
`browser exited before its DevTools endpoint ${report.endpoint} could be verified`,
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
const answered = new URL(webSocketDebuggerUrl).pathname;
|
|
70
|
-
if (report.browserPath === null || answered !== report.browserPath) {
|
|
71
|
-
throw new Error(
|
|
72
|
-
`DevTools endpoint ${report.endpoint} answers as ${answered}, not as the launched browser (${report.browserPath ?? "no browser id reported"})`,
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export async function discoverLaunchedEndpoint(
|
|
78
|
-
child,
|
|
79
|
-
{ profileDir, timeoutMs, previousActivePort = null } = {},
|
|
80
|
-
) {
|
|
81
|
-
const activePortPath = profileDir
|
|
82
|
-
? path.join(profileDir, "DevToolsActivePort")
|
|
83
|
-
: null;
|
|
84
|
-
|
|
85
|
-
return new Promise((resolve, reject) => {
|
|
86
|
-
let settled = false;
|
|
87
|
-
let stderr = "";
|
|
88
|
-
let activePortWatcher;
|
|
89
|
-
let activePortReading = false;
|
|
90
|
-
let activePortChangedWhileReading = false;
|
|
91
|
-
const cleanups = [];
|
|
92
|
-
|
|
93
|
-
const finish = (error, endpoint) => {
|
|
94
|
-
if (settled) return;
|
|
95
|
-
settled = true;
|
|
96
|
-
for (const cleanup of cleanups.splice(0)) cleanup();
|
|
97
|
-
activePortWatcher?.close();
|
|
98
|
-
if (error) reject(error);
|
|
99
|
-
else resolve(endpoint);
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const readActivePort = async () => {
|
|
103
|
-
const info = await stat(activePortPath);
|
|
104
|
-
if (isSameFile(activePortIdentity(info), previousActivePort)) return;
|
|
105
|
-
const text = await readFile(activePortPath, "utf8");
|
|
106
|
-
if (previousActivePort !== null && text === previousActivePort.text) return;
|
|
107
|
-
const report = parseActivePort(text);
|
|
108
|
-
if (report) finish(null, report);
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
const tryActivePort = async () => {
|
|
112
|
-
if (!activePortPath || settled) return;
|
|
113
|
-
if (activePortReading) {
|
|
114
|
-
activePortChangedWhileReading = true;
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
activePortReading = true;
|
|
118
|
-
try {
|
|
119
|
-
do {
|
|
120
|
-
activePortChangedWhileReading = false;
|
|
121
|
-
await readActivePort();
|
|
122
|
-
} while (activePortChangedWhileReading && !settled);
|
|
123
|
-
} catch (error) {
|
|
124
|
-
if (error?.code !== "ENOENT") finish(error);
|
|
125
|
-
} finally {
|
|
126
|
-
activePortReading = false;
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
const onStderr = (chunk) => {
|
|
131
|
-
stderr = `${stderr}${chunk}`.slice(-16_384);
|
|
132
|
-
const match = /DevTools listening on (ws:\/\/\S+)/.exec(stderr);
|
|
133
|
-
if (match) {
|
|
134
|
-
finish(null, {
|
|
135
|
-
endpoint: httpEndpointFromWebSocket(match[1]),
|
|
136
|
-
browserPath: new URL(match[1]).pathname,
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
else void tryActivePort();
|
|
140
|
-
};
|
|
141
|
-
const onError = (error) => finish(error);
|
|
142
|
-
const onExit = (code, signal) => {
|
|
143
|
-
finish(
|
|
144
|
-
new Error(
|
|
145
|
-
`browser exited before exposing DevTools (code ${code}, signal ${signal ?? "none"})${stderr ? `\n${stderr}` : ""}`,
|
|
146
|
-
),
|
|
147
|
-
);
|
|
148
|
-
};
|
|
149
|
-
child.stderr.setEncoding("utf8");
|
|
150
|
-
child.stderr.on("data", onStderr);
|
|
151
|
-
child.once("error", onError);
|
|
152
|
-
child.once("exit", onExit);
|
|
153
|
-
cleanups.push(() => child.stderr.off("data", onStderr));
|
|
154
|
-
cleanups.push(() => child.off("error", onError));
|
|
155
|
-
cleanups.push(() => child.off("exit", onExit));
|
|
156
|
-
|
|
157
|
-
if (activePortPath) {
|
|
158
|
-
try {
|
|
159
|
-
activePortWatcher = watch(profileDir, (event, filename) => {
|
|
160
|
-
if (
|
|
161
|
-
(event === "rename" || event === "change") &&
|
|
162
|
-
(!filename ||
|
|
163
|
-
filename.toString() === path.basename(activePortPath))
|
|
164
|
-
) {
|
|
165
|
-
void tryActivePort();
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
} catch (error) {
|
|
169
|
-
if (error?.code !== "ENOENT") finish(error);
|
|
170
|
-
}
|
|
171
|
-
void tryActivePort();
|
|
172
|
-
const poll = setInterval(() => void tryActivePort(), ACTIVE_PORT_POLL_MS);
|
|
173
|
-
cleanups.push(() => clearInterval(poll));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const timeout = setTimeout(() => {
|
|
177
|
-
finish(
|
|
178
|
-
new Error(
|
|
179
|
-
`browser did not expose a DevTools endpoint within ${timeoutMs}ms${stderr ? `\n${stderr}` : ""}`,
|
|
180
|
-
),
|
|
181
|
-
);
|
|
182
|
-
}, timeoutMs);
|
|
183
|
-
cleanups.push(() => clearTimeout(timeout));
|
|
184
|
-
});
|
|
185
|
-
}
|