mcp-browser-dev-tools 0.0.1-beta.7 → 0.0.1-beta.9
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/package.json +1 -1
- package/src/cli.mjs +96 -2
- package/src/firefox-bidi-client.mjs +55 -82
package/package.json
CHANGED
package/src/cli.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { spawn } from "node:child_process";
|
|
3
|
-
import { mkdir } from "node:fs/promises";
|
|
3
|
+
import { mkdir, readFile } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
4
5
|
import process from "node:process";
|
|
5
6
|
import { pathToFileURL } from "node:url";
|
|
6
7
|
|
|
@@ -94,6 +95,8 @@ function sleep(ms) {
|
|
|
94
95
|
});
|
|
95
96
|
}
|
|
96
97
|
|
|
98
|
+
const FIREFOX_BIDI_SERVER_FILENAME = "WebDriverBiDiServer.json";
|
|
99
|
+
|
|
97
100
|
function isHttpLikeUrl(value) {
|
|
98
101
|
try {
|
|
99
102
|
const url = new URL(value);
|
|
@@ -103,6 +106,88 @@ function isHttpLikeUrl(value) {
|
|
|
103
106
|
}
|
|
104
107
|
}
|
|
105
108
|
|
|
109
|
+
function normalizeFirefoxDoctorHost(value) {
|
|
110
|
+
const host = typeof value === "string" ? value.trim() : "";
|
|
111
|
+
if (!host || host === "0.0.0.0" || host === "::" || host === "[::]") {
|
|
112
|
+
return "127.0.0.1";
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return host;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function normalizeFirefoxDoctorPath(value) {
|
|
119
|
+
if (typeof value !== "string") {
|
|
120
|
+
return "/";
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const trimmed = value.trim();
|
|
124
|
+
if (!trimmed) {
|
|
125
|
+
return "/";
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function parseFirefoxBidiServerInfo(contents) {
|
|
132
|
+
const parsed = JSON.parse(contents);
|
|
133
|
+
if (!parsed || typeof parsed !== "object") {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const directUrl =
|
|
138
|
+
parsed.webSocketUrl ??
|
|
139
|
+
parsed.websocketUrl ??
|
|
140
|
+
parsed.wsUrl ??
|
|
141
|
+
parsed.ws_url ??
|
|
142
|
+
null;
|
|
143
|
+
if (typeof directUrl === "string" && directUrl.trim()) {
|
|
144
|
+
return directUrl.trim().replace(/\/+$/, "");
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const port =
|
|
148
|
+
parsed.ws_port ?? parsed.port ?? parsed.remoteDebuggingPort ?? null;
|
|
149
|
+
if (!Number.isInteger(port) && !/^\d+$/.test(String(port ?? "").trim())) {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const host = normalizeFirefoxDoctorHost(
|
|
154
|
+
parsed.ws_host ?? parsed.host ?? parsed.hostname ?? "127.0.0.1",
|
|
155
|
+
);
|
|
156
|
+
const protocol =
|
|
157
|
+
parsed.protocol === "https" || parsed.protocol === "wss" ? "wss:" : "ws:";
|
|
158
|
+
const endpoint = new URL(`${protocol}//${host}`);
|
|
159
|
+
endpoint.port = String(port);
|
|
160
|
+
endpoint.pathname = normalizeFirefoxDoctorPath(
|
|
161
|
+
parsed.path ?? parsed.ws_path ?? "/",
|
|
162
|
+
);
|
|
163
|
+
return endpoint.toString().replace(/\/+$/, "");
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export async function resolveFirefoxDoctorEndpoint({
|
|
167
|
+
userDataDir,
|
|
168
|
+
fallbackPort,
|
|
169
|
+
}) {
|
|
170
|
+
if (!userDataDir) {
|
|
171
|
+
return `ws://127.0.0.1:${fallbackPort}`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
try {
|
|
175
|
+
const serverInfo = await readFile(
|
|
176
|
+
path.join(userDataDir, FIREFOX_BIDI_SERVER_FILENAME),
|
|
177
|
+
"utf8",
|
|
178
|
+
);
|
|
179
|
+
return (
|
|
180
|
+
parseFirefoxBidiServerInfo(serverInfo) ?? `ws://127.0.0.1:${fallbackPort}`
|
|
181
|
+
);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
if (error?.code === "ENOENT") {
|
|
184
|
+
return `ws://127.0.0.1:${fallbackPort}`;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return `ws://127.0.0.1:${fallbackPort}`;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
106
191
|
async function runServe() {
|
|
107
192
|
const app = createBrowserDevToolsApp();
|
|
108
193
|
app.installSignalHandlers();
|
|
@@ -200,7 +285,10 @@ async function runOpen(positional, options) {
|
|
|
200
285
|
};
|
|
201
286
|
|
|
202
287
|
if (family === "firefox") {
|
|
203
|
-
doctorEnv.FIREFOX_BIDI_WS_URL =
|
|
288
|
+
doctorEnv.FIREFOX_BIDI_WS_URL = await resolveFirefoxDoctorEndpoint({
|
|
289
|
+
userDataDir,
|
|
290
|
+
fallbackPort: resolvedPort,
|
|
291
|
+
});
|
|
204
292
|
} else {
|
|
205
293
|
const address = requestedAddress || "127.0.0.1";
|
|
206
294
|
doctorEnv.CDP_BASE_URL = `http://${address}:${resolvedPort}`;
|
|
@@ -216,6 +304,12 @@ async function runOpen(positional, options) {
|
|
|
216
304
|
Date.now() - startedAt < timeoutMs
|
|
217
305
|
) {
|
|
218
306
|
await sleep(250);
|
|
307
|
+
if (family === "firefox") {
|
|
308
|
+
doctorEnv.FIREFOX_BIDI_WS_URL = await resolveFirefoxDoctorEndpoint({
|
|
309
|
+
userDataDir,
|
|
310
|
+
fallbackPort: resolvedPort,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
219
313
|
report = await collectDoctorReport({
|
|
220
314
|
env: doctorEnv,
|
|
221
315
|
url: isHttpLikeUrl(url) ? url : null,
|
|
@@ -239,10 +239,6 @@ function normalizeRequestedDepth(depth) {
|
|
|
239
239
|
return Number.isInteger(depth) && depth > 0 ? depth : 2;
|
|
240
240
|
}
|
|
241
241
|
|
|
242
|
-
function isWebSocketProtocol(protocol) {
|
|
243
|
-
return protocol === "ws:" || protocol === "wss:";
|
|
244
|
-
}
|
|
245
|
-
|
|
246
242
|
function isHttpProtocol(protocol) {
|
|
247
243
|
return protocol === "http:" || protocol === "https:";
|
|
248
244
|
}
|
|
@@ -251,12 +247,12 @@ function isRootPathname(pathname) {
|
|
|
251
247
|
return pathname === "" || pathname === "/";
|
|
252
248
|
}
|
|
253
249
|
|
|
254
|
-
function
|
|
250
|
+
function toWebSocketEndpointUrl(endpointUrl) {
|
|
255
251
|
const url = new URL(endpointUrl);
|
|
256
|
-
if (url.protocol === "
|
|
257
|
-
url.protocol = "
|
|
258
|
-
} else if (url.protocol === "
|
|
259
|
-
url.protocol = "
|
|
252
|
+
if (url.protocol === "http:") {
|
|
253
|
+
url.protocol = "ws:";
|
|
254
|
+
} else if (url.protocol === "https:") {
|
|
255
|
+
url.protocol = "wss:";
|
|
260
256
|
}
|
|
261
257
|
|
|
262
258
|
return url;
|
|
@@ -341,7 +337,7 @@ export class FirefoxBidiSessionManager {
|
|
|
341
337
|
this.sessions = new Map();
|
|
342
338
|
this.websocketFactory =
|
|
343
339
|
options.websocketFactory ?? ((url) => new WebSocket(url));
|
|
344
|
-
this.
|
|
340
|
+
this.connectionTimeoutMs = options.connectionTimeoutMs ?? 2_000;
|
|
345
341
|
this.pending = new Map();
|
|
346
342
|
this.nextMessageId = 1;
|
|
347
343
|
this.websocket = null;
|
|
@@ -349,7 +345,6 @@ export class FirefoxBidiSessionManager {
|
|
|
349
345
|
this.capabilities = null;
|
|
350
346
|
this.connectPromise = null;
|
|
351
347
|
this.resolvedWebSocketUrl = null;
|
|
352
|
-
this.deleteSessionUrl = null;
|
|
353
348
|
}
|
|
354
349
|
|
|
355
350
|
async ensureConnected() {
|
|
@@ -375,19 +370,39 @@ export class FirefoxBidiSessionManager {
|
|
|
375
370
|
async openConnection() {
|
|
376
371
|
const endpoint = await this.resolveConnectionEndpoint();
|
|
377
372
|
this.resolvedWebSocketUrl = endpoint.webSocketUrl;
|
|
378
|
-
this.deleteSessionUrl = endpoint.deleteSessionUrl ?? null;
|
|
379
373
|
this.websocket = this.websocketFactory(endpoint.webSocketUrl);
|
|
380
374
|
|
|
381
375
|
await new Promise((resolve, reject) => {
|
|
382
376
|
let settled = false;
|
|
377
|
+
const timer = setTimeout(() => {
|
|
378
|
+
if (settled) {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
settled = true;
|
|
383
|
+
this.websocket?.close?.();
|
|
384
|
+
reject(
|
|
385
|
+
new Error(
|
|
386
|
+
`Timed out connecting to Firefox BiDi at ${endpoint.webSocketUrl}`,
|
|
387
|
+
),
|
|
388
|
+
);
|
|
389
|
+
}, this.connectionTimeoutMs);
|
|
390
|
+
timer.unref?.();
|
|
391
|
+
|
|
392
|
+
const settle = (callback) => {
|
|
393
|
+
if (settled) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
settled = true;
|
|
398
|
+
clearTimeout(timer);
|
|
399
|
+
callback();
|
|
400
|
+
};
|
|
383
401
|
|
|
384
402
|
this.websocket.addEventListener(
|
|
385
403
|
"open",
|
|
386
404
|
() => {
|
|
387
|
-
|
|
388
|
-
settled = true;
|
|
389
|
-
resolve();
|
|
390
|
-
}
|
|
405
|
+
settle(resolve);
|
|
391
406
|
},
|
|
392
407
|
{ once: true },
|
|
393
408
|
);
|
|
@@ -395,12 +410,11 @@ export class FirefoxBidiSessionManager {
|
|
|
395
410
|
this.websocket.addEventListener(
|
|
396
411
|
"error",
|
|
397
412
|
() => {
|
|
398
|
-
|
|
399
|
-
settled = true;
|
|
413
|
+
settle(() => {
|
|
400
414
|
reject(
|
|
401
415
|
new Error(`Failed to connect to ${this.config.firefoxBidiWsUrl}`),
|
|
402
416
|
);
|
|
403
|
-
}
|
|
417
|
+
});
|
|
404
418
|
},
|
|
405
419
|
{ once: true },
|
|
406
420
|
);
|
|
@@ -415,12 +429,11 @@ export class FirefoxBidiSessionManager {
|
|
|
415
429
|
this.browserSessionId = null;
|
|
416
430
|
this.capabilities = null;
|
|
417
431
|
this.sessions.clear();
|
|
418
|
-
|
|
419
|
-
settled = true;
|
|
432
|
+
settle(() => {
|
|
420
433
|
reject(
|
|
421
434
|
new Error("Firefox BiDi connection closed before it connected"),
|
|
422
435
|
);
|
|
423
|
-
}
|
|
436
|
+
});
|
|
424
437
|
});
|
|
425
438
|
});
|
|
426
439
|
|
|
@@ -442,63 +455,32 @@ export class FirefoxBidiSessionManager {
|
|
|
442
455
|
|
|
443
456
|
async resolveConnectionEndpoint() {
|
|
444
457
|
const configuredUrl = new URL(this.config.firefoxBidiWsUrl);
|
|
445
|
-
if (
|
|
446
|
-
isHttpProtocol(configuredUrl.protocol) ||
|
|
447
|
-
(isWebSocketProtocol(configuredUrl.protocol) &&
|
|
448
|
-
isRootPathname(configuredUrl.pathname))
|
|
449
|
-
) {
|
|
450
|
-
return this.createWebDriverSession(configuredUrl);
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
return {
|
|
454
|
-
webSocketUrl: configuredUrl.toString(),
|
|
455
|
-
requiresSessionNew: true,
|
|
456
|
-
sessionId: null,
|
|
457
|
-
capabilities: null,
|
|
458
|
-
deleteSessionUrl: null,
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
async createWebDriverSession(endpointUrl) {
|
|
463
|
-
const sessionUrl = joinUrlPath(toHttpEndpointUrl(endpointUrl), "/session");
|
|
464
|
-
const response = await this.fetchImpl(sessionUrl, {
|
|
465
|
-
method: "POST",
|
|
466
|
-
headers: {
|
|
467
|
-
"content-type": "application/json",
|
|
468
|
-
},
|
|
469
|
-
body: JSON.stringify({
|
|
470
|
-
capabilities: {
|
|
471
|
-
alwaysMatch: {
|
|
472
|
-
webSocketUrl: true,
|
|
473
|
-
},
|
|
474
|
-
},
|
|
475
|
-
}),
|
|
476
|
-
});
|
|
477
458
|
|
|
478
|
-
if (
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
459
|
+
if (isHttpProtocol(configuredUrl.protocol)) {
|
|
460
|
+
return {
|
|
461
|
+
webSocketUrl: joinUrlPath(
|
|
462
|
+
toWebSocketEndpointUrl(configuredUrl),
|
|
463
|
+
"/session",
|
|
464
|
+
).toString(),
|
|
465
|
+
requiresSessionNew: true,
|
|
466
|
+
sessionId: null,
|
|
467
|
+
capabilities: null,
|
|
468
|
+
};
|
|
482
469
|
}
|
|
483
470
|
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
const sessionId = value?.sessionId ?? null;
|
|
487
|
-
const capabilities = value?.capabilities ?? {};
|
|
488
|
-
const webSocketUrl = capabilities?.webSocketUrl ?? null;
|
|
489
|
-
|
|
490
|
-
if (!sessionId || !webSocketUrl) {
|
|
491
|
-
throw new Error(
|
|
492
|
-
`Firefox WebDriver session at ${sessionUrl} did not return a webSocketUrl`,
|
|
493
|
-
);
|
|
471
|
+
if (isRootPathname(configuredUrl.pathname)) {
|
|
472
|
+
configuredUrl.pathname = "/session";
|
|
494
473
|
}
|
|
495
474
|
|
|
496
475
|
return {
|
|
497
|
-
webSocketUrl,
|
|
498
|
-
requiresSessionNew:
|
|
499
|
-
sessionId
|
|
500
|
-
|
|
501
|
-
|
|
476
|
+
webSocketUrl: configuredUrl.toString(),
|
|
477
|
+
requiresSessionNew: configuredUrl.pathname === "/session",
|
|
478
|
+
sessionId:
|
|
479
|
+
configuredUrl.pathname === "/session"
|
|
480
|
+
? null
|
|
481
|
+
: configuredUrl.pathname.split("/").at(-1) ||
|
|
482
|
+
configuredUrl.toString(),
|
|
483
|
+
capabilities: null,
|
|
502
484
|
};
|
|
503
485
|
}
|
|
504
486
|
|
|
@@ -528,14 +510,6 @@ export class FirefoxBidiSessionManager {
|
|
|
528
510
|
return;
|
|
529
511
|
}
|
|
530
512
|
|
|
531
|
-
if (this.deleteSessionUrl) {
|
|
532
|
-
await this.fetchImpl(this.deleteSessionUrl, {
|
|
533
|
-
method: "DELETE",
|
|
534
|
-
signal: AbortSignal.timeout(2_000),
|
|
535
|
-
}).catch(() => {});
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
|
|
539
513
|
if (this.websocket?.readyState === WebSocket.OPEN) {
|
|
540
514
|
await this.send("session.end", {}).catch(() => {});
|
|
541
515
|
}
|
|
@@ -1076,6 +1050,5 @@ export class FirefoxBidiSessionManager {
|
|
|
1076
1050
|
this.browserSessionId = null;
|
|
1077
1051
|
this.capabilities = null;
|
|
1078
1052
|
this.resolvedWebSocketUrl = null;
|
|
1079
|
-
this.deleteSessionUrl = null;
|
|
1080
1053
|
}
|
|
1081
1054
|
}
|