bidi-browser-mcp 0.1.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/LICENSE +21 -0
- package/README.md +343 -0
- package/dist/bin.d.ts +6 -0
- package/dist/bin.js +37 -0
- package/dist/config.d.ts +6 -0
- package/dist/config.js +122 -0
- package/dist/connection/bidi-client.d.ts +29 -0
- package/dist/connection/bidi-client.js +298 -0
- package/dist/connection/browser-discovery.d.ts +30 -0
- package/dist/connection/browser-discovery.js +393 -0
- package/dist/connection/chromium-mapper.d.ts +55 -0
- package/dist/connection/chromium-mapper.js +293 -0
- package/dist/connection/event-pipeline.d.ts +37 -0
- package/dist/connection/event-pipeline.js +167 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +66 -0
- package/dist/profile/profile-store.d.ts +88 -0
- package/dist/profile/profile-store.js +350 -0
- package/dist/session/context-manager.d.ts +17 -0
- package/dist/session/context-manager.js +179 -0
- package/dist/session/session-manager.d.ts +31 -0
- package/dist/session/session-manager.js +366 -0
- package/dist/snapshot/cache.d.ts +15 -0
- package/dist/snapshot/cache.js +37 -0
- package/dist/snapshot/dom-snapshot.d.ts +5 -0
- package/dist/snapshot/dom-snapshot.js +270 -0
- package/dist/snapshot/element-resolver.d.ts +13 -0
- package/dist/snapshot/element-resolver.js +25 -0
- package/dist/snapshot/token-optimizer.d.ts +16 -0
- package/dist/snapshot/token-optimizer.js +103 -0
- package/dist/tools/base-tool.d.ts +34 -0
- package/dist/tools/base-tool.js +55 -0
- package/dist/tools/browser.d.ts +1 -0
- package/dist/tools/browser.js +75 -0
- package/dist/tools/cleanup.d.ts +1 -0
- package/dist/tools/cleanup.js +64 -0
- package/dist/tools/console.d.ts +1 -0
- package/dist/tools/console.js +87 -0
- package/dist/tools/context.d.ts +1 -0
- package/dist/tools/context.js +156 -0
- package/dist/tools/observe.d.ts +1 -0
- package/dist/tools/observe.js +92 -0
- package/dist/tools/page.d.ts +1 -0
- package/dist/tools/page.js +139 -0
- package/dist/tools/ping.d.ts +1 -0
- package/dist/tools/ping.js +71 -0
- package/dist/tools/registry.d.ts +13 -0
- package/dist/tools/registry.js +47 -0
- package/dist/tools/spaces.d.ts +1 -0
- package/dist/tools/spaces.js +98 -0
- package/dist/tools/tabs.d.ts +1 -0
- package/dist/tools/tabs.js +155 -0
- package/dist/types.d.ts +277 -0
- package/dist/types.js +5 -0
- package/dist/utils/assert.d.ts +12 -0
- package/dist/utils/assert.js +74 -0
- package/dist/utils/errors.d.ts +24 -0
- package/dist/utils/errors.js +60 -0
- package/dist/utils/result.d.ts +14 -0
- package/dist/utils/result.js +37 -0
- package/package.json +86 -0
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { BiDiClient } from '../connection/bidi-client.js';
|
|
2
|
+
import { openChromiumMapperSocket } from '../connection/chromium-mapper.js';
|
|
3
|
+
import { discoverBrowsers, endpointAllowed, } from '../connection/browser-discovery.js';
|
|
4
|
+
import { EventPipeline } from '../connection/event-pipeline.js';
|
|
5
|
+
import { ContextManager } from './context-manager.js';
|
|
6
|
+
import { SnapshotCache } from '../snapshot/cache.js';
|
|
7
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
import { ActionableError, formatError } from '../utils/errors.js';
|
|
10
|
+
/** Disk record so a later process can end a session this one left behind. */
|
|
11
|
+
const LAST_SESSION_FILE = 'last-bidi-session.json';
|
|
12
|
+
/**
|
|
13
|
+
* The mapper reports CDP-shaped capabilities: `browserName` is a product
|
|
14
|
+
* string ("Chrome/153.0.8010.53"), `browserVersion` is the CDP revision, and
|
|
15
|
+
* unused fields are empty strings. Normalize to the BiDi shape tools expect.
|
|
16
|
+
*/
|
|
17
|
+
export function normalizeChromiumCapabilities(capabilities) {
|
|
18
|
+
const normalized = { ...capabilities };
|
|
19
|
+
const product = typeof normalized.browserName === 'string' ? normalized.browserName : '';
|
|
20
|
+
const [name, version] = product.split('/');
|
|
21
|
+
if (name !== undefined && name !== '') {
|
|
22
|
+
normalized.browserName = name.toLowerCase();
|
|
23
|
+
}
|
|
24
|
+
if (version !== undefined && version !== '') {
|
|
25
|
+
normalized.browserVersion = version;
|
|
26
|
+
}
|
|
27
|
+
else if (typeof normalized.browserVersion === 'string' &&
|
|
28
|
+
normalized.browserVersion.startsWith('@')) {
|
|
29
|
+
// A bare CDP revision is not a browser version.
|
|
30
|
+
delete normalized.browserVersion;
|
|
31
|
+
}
|
|
32
|
+
for (const key of ['platformName', 'webSocketUrl']) {
|
|
33
|
+
if (normalized[key] === '') {
|
|
34
|
+
delete normalized[key];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return normalized;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Owns the single BiDi session for this MCP server process.
|
|
41
|
+
* It never relaunches, mutates, or closes the user's browser.
|
|
42
|
+
*/
|
|
43
|
+
export class SessionManager {
|
|
44
|
+
#config;
|
|
45
|
+
#serverVersion;
|
|
46
|
+
#client;
|
|
47
|
+
#sessionId;
|
|
48
|
+
#capabilities;
|
|
49
|
+
#profile;
|
|
50
|
+
#connecting;
|
|
51
|
+
#runtime;
|
|
52
|
+
/** Live-session services (events, contexts); undefined when disconnected. */
|
|
53
|
+
get runtime() {
|
|
54
|
+
return this.#runtime;
|
|
55
|
+
}
|
|
56
|
+
constructor(options) {
|
|
57
|
+
this.#config = options.config;
|
|
58
|
+
this.#serverVersion = options.serverVersion;
|
|
59
|
+
}
|
|
60
|
+
async connect() {
|
|
61
|
+
const existing = this.#status();
|
|
62
|
+
if (existing.connected) {
|
|
63
|
+
return existing;
|
|
64
|
+
}
|
|
65
|
+
if (this.#connecting !== undefined) {
|
|
66
|
+
return this.#connecting;
|
|
67
|
+
}
|
|
68
|
+
this.#connecting = this.#connectOnce();
|
|
69
|
+
try {
|
|
70
|
+
return await this.#connecting;
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
this.#connecting = undefined;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async disconnect() {
|
|
77
|
+
// A concurrent client may still be connecting; wait for it so the session
|
|
78
|
+
// is guaranteed to be torn down rather than silently left open.
|
|
79
|
+
if (this.#connecting !== undefined) {
|
|
80
|
+
try {
|
|
81
|
+
await this.#connecting;
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// Connect failed; nothing to tear down.
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const client = this.#client;
|
|
88
|
+
try {
|
|
89
|
+
if (client !== undefined && this.#sessionId !== undefined) {
|
|
90
|
+
// Best effort: the socket may already be dead. Recovery must not
|
|
91
|
+
// depend on session.end succeeding.
|
|
92
|
+
try {
|
|
93
|
+
await client.request('session.end', {});
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
// Session already gone — proceed with teardown below.
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
this.#sessionId = undefined;
|
|
102
|
+
this.#capabilities = undefined;
|
|
103
|
+
this.#profile = undefined;
|
|
104
|
+
this.#clearLastSession();
|
|
105
|
+
this.#runtime?.events.detach();
|
|
106
|
+
this.#runtime = undefined;
|
|
107
|
+
if (client !== undefined) {
|
|
108
|
+
await client.close();
|
|
109
|
+
}
|
|
110
|
+
this.#client = undefined;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async status() {
|
|
114
|
+
return this.#status();
|
|
115
|
+
}
|
|
116
|
+
/** Send a BiDi command on the live session. */
|
|
117
|
+
async request(method, params = {}) {
|
|
118
|
+
const client = this.#client;
|
|
119
|
+
if (client === undefined || this.#sessionId === undefined) {
|
|
120
|
+
throw new ActionableError('CONNECTION_FAILED', 'No live browser session.', {
|
|
121
|
+
fix: 'Call browser.connect first.',
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return client.request(method, params);
|
|
125
|
+
}
|
|
126
|
+
async profiles() {
|
|
127
|
+
const candidates = await discoverBrowsers(this.#config);
|
|
128
|
+
const connectedEndpoint = this.#client?.url;
|
|
129
|
+
return candidates.map((candidate) => ({
|
|
130
|
+
...candidate,
|
|
131
|
+
...(connectedEndpoint === candidate.endpoint ? { live: true } : {}),
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
async version() {
|
|
135
|
+
const status = this.#status();
|
|
136
|
+
const browserVersion = status.browser?.browserVersion;
|
|
137
|
+
return {
|
|
138
|
+
serverVersion: this.#serverVersion,
|
|
139
|
+
...(browserVersion === undefined ? {} : { browserVersion }),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
async #connectOnce() {
|
|
143
|
+
const candidates = await discoverBrowsers(this.#config);
|
|
144
|
+
if (candidates.length === 0) {
|
|
145
|
+
throw new ActionableError('DISCOVERY_FAILED', 'No reachable browser endpoint was found.', {
|
|
146
|
+
fix: 'Start Firefox or Zen with --remote-debugging-port, start a Chromium browser' +
|
|
147
|
+
' with --remote-debugging-port, or set BIDI_WS_URL (BIDI_CHROMIUM_DATA_DIR' +
|
|
148
|
+
' for portable Chromium installs).',
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
let lastError;
|
|
152
|
+
// SESSION_BUSY means a previous process left a session behind. If we know
|
|
153
|
+
// its id we can reattach via its dedicated path and end it, then retry.
|
|
154
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
155
|
+
if (attempt === 1) {
|
|
156
|
+
const recovered = await this.#attemptRecovery();
|
|
157
|
+
if (!recovered) {
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const result = await this.#tryCandidates(candidates);
|
|
162
|
+
if (result.ok) {
|
|
163
|
+
return result.status;
|
|
164
|
+
}
|
|
165
|
+
lastError = result.error;
|
|
166
|
+
if (!(result.busy && attempt === 0)) {
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
throw new ActionableError('DISCOVERY_FAILED', 'No reachable browser endpoint accepted a session.', {
|
|
171
|
+
fix: 'Start Firefox or Zen with --remote-debugging-port, start a Chromium browser' +
|
|
172
|
+
' with --remote-debugging-port, or set BIDI_WS_URL.' +
|
|
173
|
+
' If a previous client died holding a Firefox session, Firefox frees it only on browser restart.' +
|
|
174
|
+
' For Chromium, approve the debugging permission dialog in the browser.',
|
|
175
|
+
...(lastError === undefined
|
|
176
|
+
? {}
|
|
177
|
+
: { details: { lastError: formatError(lastError) } }),
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
async #tryCandidates(candidates) {
|
|
181
|
+
let lastError;
|
|
182
|
+
let sawBusy = false;
|
|
183
|
+
for (const candidate of candidates) {
|
|
184
|
+
const client = new BiDiClient({
|
|
185
|
+
url: candidate.endpoint,
|
|
186
|
+
timeoutMs: this.#config.timeoutMs,
|
|
187
|
+
// Chromium serves CDP only: host the mapper in-process so the rest of
|
|
188
|
+
// the stack still speaks BiDi.
|
|
189
|
+
...(candidate.browserFamily === 'chromium'
|
|
190
|
+
? {
|
|
191
|
+
openSocket: () => openChromiumMapperSocket({
|
|
192
|
+
cdpUrl: candidate.endpoint,
|
|
193
|
+
timeoutMs: this.#config.timeoutMs,
|
|
194
|
+
}),
|
|
195
|
+
}
|
|
196
|
+
: {}),
|
|
197
|
+
});
|
|
198
|
+
try {
|
|
199
|
+
await client.connect();
|
|
200
|
+
// Confirm the endpoint before taking a session.
|
|
201
|
+
await client.request('session.status', {});
|
|
202
|
+
// Firefox/Zen assert that `capabilities` is present.
|
|
203
|
+
const result = await client.request('session.new', { capabilities: {} });
|
|
204
|
+
const sessionId = typeof result.sessionId === 'string' ? result.sessionId : undefined;
|
|
205
|
+
const rawCapabilities = (result.capabilities ?? {});
|
|
206
|
+
const capabilities = candidate.browserFamily === 'chromium'
|
|
207
|
+
? normalizeChromiumCapabilities(rawCapabilities)
|
|
208
|
+
: rawCapabilities;
|
|
209
|
+
const pipeline = new EventPipeline();
|
|
210
|
+
const contexts = new ContextManager(client);
|
|
211
|
+
const snapshots = new SnapshotCache();
|
|
212
|
+
pipeline.registerSink((event) => {
|
|
213
|
+
contexts.handleEvent(event);
|
|
214
|
+
snapshots.handleEvent(event);
|
|
215
|
+
});
|
|
216
|
+
try {
|
|
217
|
+
await pipeline.attach(client);
|
|
218
|
+
await contexts.sync();
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
// The session exists but the session services failed: end it so it
|
|
222
|
+
// does not leak in the browser (a dead WebSocket cannot end it later).
|
|
223
|
+
await client.request('session.end', {}).catch(() => { });
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
// Commit the adopted session only when everything is in place.
|
|
227
|
+
this.#client = client;
|
|
228
|
+
this.#sessionId = sessionId;
|
|
229
|
+
this.#capabilities = capabilities;
|
|
230
|
+
this.#profile = candidate.profilePath;
|
|
231
|
+
this.#runtime = { events: pipeline, contexts, snapshots };
|
|
232
|
+
if (sessionId !== undefined && candidate.browserFamily !== 'chromium') {
|
|
233
|
+
// The mapper reports a synthetic session id and holds no
|
|
234
|
+
// browser-side session, so there is nothing to recover.
|
|
235
|
+
this.#persistLastSession(sessionId, candidate.endpoint);
|
|
236
|
+
}
|
|
237
|
+
// A dropped socket must not leave a ghost session behind: clear the
|
|
238
|
+
// adopted state so status() reports honestly and the next connect()
|
|
239
|
+
// can recover via the persisted session id.
|
|
240
|
+
client.onClose(() => {
|
|
241
|
+
if (this.#client === client) {
|
|
242
|
+
this.#client = undefined;
|
|
243
|
+
this.#sessionId = undefined;
|
|
244
|
+
this.#capabilities = undefined;
|
|
245
|
+
this.#profile = undefined;
|
|
246
|
+
this.#runtime?.events.detach();
|
|
247
|
+
this.#runtime = undefined;
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
return { ok: true, status: this.#status() };
|
|
251
|
+
}
|
|
252
|
+
catch (error) {
|
|
253
|
+
// Firefox allows one BiDi session per process - never leave a socket
|
|
254
|
+
// behind for a candidate we are not keeping, and roll back any
|
|
255
|
+
// partially adopted state so status() cannot report a ghost session.
|
|
256
|
+
if (this.#client === client) {
|
|
257
|
+
this.#client = undefined;
|
|
258
|
+
this.#sessionId = undefined;
|
|
259
|
+
this.#capabilities = undefined;
|
|
260
|
+
this.#profile = undefined;
|
|
261
|
+
this.#runtime = undefined;
|
|
262
|
+
}
|
|
263
|
+
await client.close().catch(() => { });
|
|
264
|
+
if (error instanceof ActionableError && error.code === 'SESSION_BUSY') {
|
|
265
|
+
// The busy error is the meaningful one; a later candidate's
|
|
266
|
+
// handshake failure must not mask it.
|
|
267
|
+
lastError = error;
|
|
268
|
+
sawBusy = true;
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
lastError = error;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return { ok: false, busy: sawBusy, error: lastError };
|
|
276
|
+
}
|
|
277
|
+
/** Remember the live session so a later process can end it if we die. */
|
|
278
|
+
#persistLastSession(sessionId, endpoint) {
|
|
279
|
+
try {
|
|
280
|
+
const file = join(this.#config.tempDir, LAST_SESSION_FILE);
|
|
281
|
+
mkdirSync(this.#config.tempDir, { recursive: true });
|
|
282
|
+
const record = { sessionId, endpoint };
|
|
283
|
+
writeFileSync(file, JSON.stringify(record), 'utf8');
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
// Persistence is best-effort; recovery simply stays unavailable.
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
#clearLastSession() {
|
|
290
|
+
try {
|
|
291
|
+
rmSync(join(this.#config.tempDir, LAST_SESSION_FILE), { force: true });
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
// Best-effort.
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
#readLastSession() {
|
|
298
|
+
try {
|
|
299
|
+
const file = join(this.#config.tempDir, LAST_SESSION_FILE);
|
|
300
|
+
if (!existsSync(file)) {
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8'));
|
|
304
|
+
if (typeof parsed.sessionId !== 'string' || typeof parsed.endpoint !== 'string') {
|
|
305
|
+
return undefined;
|
|
306
|
+
}
|
|
307
|
+
return { sessionId: parsed.sessionId, endpoint: parsed.endpoint };
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* End a session left behind by a dead process: reattach through its
|
|
315
|
+
* dedicated WebSocket path (`/session/<id>`) and send `session.end`.
|
|
316
|
+
* Firefox registers that path only for sessions that expose `webSocketUrl`;
|
|
317
|
+
* when it does not exist the orphan can only be freed by a browser restart.
|
|
318
|
+
*/
|
|
319
|
+
async #attemptRecovery() {
|
|
320
|
+
const persisted = this.#readLastSession();
|
|
321
|
+
if (persisted === undefined) {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
let origin;
|
|
325
|
+
try {
|
|
326
|
+
origin = new URL(persisted.endpoint);
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
const recoveryUrl = `${origin.protocol}//${origin.host}/session/${persisted.sessionId}`;
|
|
332
|
+
// The persisted file is user-writable data: apply the same loopback /
|
|
333
|
+
// BIDI_ALLOW_REMOTE gate discovery uses before dialing it.
|
|
334
|
+
if (!endpointAllowed(this.#config, recoveryUrl)) {
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
const client = new BiDiClient({
|
|
338
|
+
url: recoveryUrl,
|
|
339
|
+
timeoutMs: Math.min(this.#config.timeoutMs, 5000),
|
|
340
|
+
});
|
|
341
|
+
try {
|
|
342
|
+
await client.connect();
|
|
343
|
+
await client.request('session.end', {});
|
|
344
|
+
this.#clearLastSession();
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
catch {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
finally {
|
|
351
|
+
await client.close().catch(() => { });
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
#status() {
|
|
355
|
+
if (this.#client === undefined || this.#sessionId === undefined) {
|
|
356
|
+
return { connected: false };
|
|
357
|
+
}
|
|
358
|
+
return {
|
|
359
|
+
connected: true,
|
|
360
|
+
sessionId: this.#sessionId,
|
|
361
|
+
browser: this.#capabilities,
|
|
362
|
+
...(this.#profile === undefined ? {} : { profile: this.#profile }),
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
//# sourceMappingURL=session-manager.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { BiDiEvent, PageSnapshot, SnapshotCacheLike } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Per-context snapshot cache. Entries live until the page navigates
|
|
4
|
+
* (domContentLoaded), the context closes, or the session ends — the browser
|
|
5
|
+
* tree and page events are the source of truth, so there is no TTL.
|
|
6
|
+
*/
|
|
7
|
+
export declare class SnapshotCache implements SnapshotCacheLike {
|
|
8
|
+
#private;
|
|
9
|
+
get(context: string): PageSnapshot | undefined;
|
|
10
|
+
set(snapshot: PageSnapshot): void;
|
|
11
|
+
invalidate(context: string): void;
|
|
12
|
+
handleEvent(event: BiDiEvent): void;
|
|
13
|
+
clear(): void;
|
|
14
|
+
get size(): number;
|
|
15
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-context snapshot cache. Entries live until the page navigates
|
|
3
|
+
* (domContentLoaded), the context closes, or the session ends — the browser
|
|
4
|
+
* tree and page events are the source of truth, so there is no TTL.
|
|
5
|
+
*/
|
|
6
|
+
export class SnapshotCache {
|
|
7
|
+
#entries = new Map();
|
|
8
|
+
get(context) {
|
|
9
|
+
return this.#entries.get(context);
|
|
10
|
+
}
|
|
11
|
+
set(snapshot) {
|
|
12
|
+
this.#entries.set(snapshot.context, snapshot);
|
|
13
|
+
}
|
|
14
|
+
invalidate(context) {
|
|
15
|
+
this.#entries.delete(context);
|
|
16
|
+
}
|
|
17
|
+
handleEvent(event) {
|
|
18
|
+
const context = event.params.context;
|
|
19
|
+
if (typeof context !== 'string') {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (event.method === 'browsingContext.domContentLoaded' ||
|
|
23
|
+
// A SPA navigation changes the URL without a domContentLoaded event, so
|
|
24
|
+
// the cached snapshot would describe a page that no longer exists.
|
|
25
|
+
event.method === 'browsingContext.urlChanged' ||
|
|
26
|
+
event.method === 'browsingContext.contextDestroyed') {
|
|
27
|
+
this.#entries.delete(context);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
clear() {
|
|
31
|
+
this.#entries.clear();
|
|
32
|
+
}
|
|
33
|
+
get size() {
|
|
34
|
+
return this.#entries.size;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=cache.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { PageSnapshot } from '../types.js';
|
|
2
|
+
/** Assign uids and validate the raw traversal output. Pure. */
|
|
3
|
+
export declare function parseSnapshotResult(context: string, raw: unknown): Omit<PageSnapshot, 'builtAt'>;
|
|
4
|
+
/** Build a fresh snapshot for one context via script.evaluate. */
|
|
5
|
+
export declare function buildSnapshot(request: (method: string, params?: Record<string, unknown>) => Promise<Record<string, unknown>>, context: string): Promise<Omit<PageSnapshot, 'builtAt'>>;
|