cdp-mcp-server 5.0.3 → 5.1.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 +14 -0
- package/dist/config.js +2 -0
- package/dist/config.js.map +1 -1
- package/dist/connection/capabilities.d.ts +58 -0
- package/dist/connection/capabilities.js +126 -0
- package/dist/connection/capabilities.js.map +1 -0
- package/dist/connection/index.d.ts +1 -0
- package/dist/connection/index.js +1 -0
- package/dist/connection/index.js.map +1 -1
- package/dist/connection/tab-session-service.d.ts +12 -2
- package/dist/connection/tab-session-service.js +48 -14
- package/dist/connection/tab-session-service.js.map +1 -1
- package/dist/index.js +44 -4
- package/dist/index.js.map +1 -1
- package/dist/snapshot/accessibility.d.ts +2 -0
- package/dist/snapshot/accessibility.js +1 -1
- package/dist/snapshot/accessibility.js.map +1 -1
- package/dist/tools/device-presets.d.ts +24 -0
- package/dist/tools/device-presets.js +62 -0
- package/dist/tools/device-presets.js.map +1 -0
- package/dist/tools/emulate.js +79 -0
- package/dist/tools/emulate.js.map +1 -1
- package/dist/tools/interact.js +191 -31
- package/dist/tools/interact.js.map +1 -1
- package/dist/tools/observe.js +201 -27
- package/dist/tools/observe.js.map +1 -1
- package/dist/tools/page.d.ts +52 -1
- package/dist/tools/page.js +242 -25
- package/dist/tools/page.js.map +1 -1
- package/dist/tools/storage.d.ts +1 -1
- package/dist/tools/storage.js +115 -3
- package/dist/tools/storage.js.map +1 -1
- package/dist/tools/tabs.js +4 -2
- package/dist/tools/tabs.js.map +1 -1
- package/dist/types.d.ts +14 -0
- package/dist/utils/actionability.d.ts +62 -0
- package/dist/utils/actionability.js +190 -0
- package/dist/utils/actionability.js.map +1 -0
- package/dist/utils/wait.d.ts +0 -25
- package/dist/utils/wait.js +0 -76
- package/dist/utils/wait.js.map +1 -1
- package/package.json +1 -1
- package/dist/utils/retry.d.ts +0 -40
- package/dist/utils/retry.js +0 -75
- package/dist/utils/retry.js.map +0 -1
package/dist/utils/wait.d.ts
CHANGED
|
@@ -19,31 +19,6 @@ export declare function sleep(ms: number): Promise<void>;
|
|
|
19
19
|
* - `"commit"` — returns immediately (navigation response received)
|
|
20
20
|
*/
|
|
21
21
|
export declare function waitForReadyState(cdpSend: CdpSend, sessionId: string, waitUntil: WaitUntil, timeout: number): Promise<void>;
|
|
22
|
-
export interface CompletionResult {
|
|
23
|
-
/** The value returned by the wrapped action. */
|
|
24
|
-
result: unknown;
|
|
25
|
-
/** Human-readable summary of observed network events. */
|
|
26
|
-
networkEvents: string[];
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Wrap an action and wait for any triggered navigation / network requests
|
|
30
|
-
* to settle. Mirrors Playwright's auto-waiting after click, fill, etc.
|
|
31
|
-
*
|
|
32
|
-
* The caller must supply callbacks for network-monitoring state because
|
|
33
|
-
* the monitor is owned by the server, not by this utility.
|
|
34
|
-
*/
|
|
35
|
-
export declare function waitForCompletion(cdpSend: CdpSend, sessionId: string, actionFn: () => Promise<unknown>, opts?: {
|
|
36
|
-
timeout?: number;
|
|
37
|
-
/** Enable network domain (idempotent). */
|
|
38
|
-
enableNetworkMonitoring?: () => Promise<void>;
|
|
39
|
-
/** Return a Map of requestId → request metadata. */
|
|
40
|
-
getNetworkRequests?: () => Map<string, {
|
|
41
|
-
url: string;
|
|
42
|
-
method: string;
|
|
43
|
-
type?: string;
|
|
44
|
-
status?: number;
|
|
45
|
-
}>;
|
|
46
|
-
}): Promise<CompletionResult>;
|
|
47
22
|
/**
|
|
48
23
|
* Poll until `text` appears in the page body, or timeout expires.
|
|
49
24
|
* Returns `true` if found, `false` on timeout.
|
package/dist/utils/wait.js
CHANGED
|
@@ -60,82 +60,6 @@ export async function waitForReadyState(cdpSend, sessionId, waitUntil, timeout)
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
/**
|
|
64
|
-
* Wrap an action and wait for any triggered navigation / network requests
|
|
65
|
-
* to settle. Mirrors Playwright's auto-waiting after click, fill, etc.
|
|
66
|
-
*
|
|
67
|
-
* The caller must supply callbacks for network-monitoring state because
|
|
68
|
-
* the monitor is owned by the server, not by this utility.
|
|
69
|
-
*/
|
|
70
|
-
export async function waitForCompletion(cdpSend, sessionId, actionFn, opts = {}) {
|
|
71
|
-
const timeout = opts.timeout ?? 5000;
|
|
72
|
-
const completedEvents = [];
|
|
73
|
-
// Optionally enable network monitoring
|
|
74
|
-
if (opts.enableNetworkMonitoring) {
|
|
75
|
-
await opts.enableNetworkMonitoring();
|
|
76
|
-
}
|
|
77
|
-
// Snapshot current request IDs before the action
|
|
78
|
-
const reqMap = opts.getNetworkRequests?.() ?? new Map();
|
|
79
|
-
const prevRequestIds = new Set(reqMap.keys());
|
|
80
|
-
// Execute the action
|
|
81
|
-
const result = await actionFn();
|
|
82
|
-
// Brief pause to let triggered requests appear
|
|
83
|
-
await sleep(150);
|
|
84
|
-
// Detect new requests
|
|
85
|
-
const currentReqMap = opts.getNetworkRequests?.() ?? new Map();
|
|
86
|
-
const pendingRequests = new Map();
|
|
87
|
-
let navigationDetected = false;
|
|
88
|
-
for (const [reqId, req] of currentReqMap) {
|
|
89
|
-
if (!prevRequestIds.has(reqId) && !req.status) {
|
|
90
|
-
pendingRequests.set(reqId, req);
|
|
91
|
-
if ((req.type ?? '').toLowerCase() === 'document') {
|
|
92
|
-
navigationDetected = true;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
// Wait for page load on navigation
|
|
97
|
-
if (navigationDetected) {
|
|
98
|
-
const navTimeout = Math.min(timeout * 2, 10000);
|
|
99
|
-
const start = Date.now();
|
|
100
|
-
while (Date.now() - start < navTimeout) {
|
|
101
|
-
try {
|
|
102
|
-
const r = await cdpSend('Runtime.evaluate', { expression: 'document.readyState', returnByValue: true }, sessionId, 3000);
|
|
103
|
-
if (r.result.value === 'complete')
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
catch {
|
|
107
|
-
/* page transitioning */
|
|
108
|
-
}
|
|
109
|
-
await sleep(300);
|
|
110
|
-
}
|
|
111
|
-
completedEvents.push('[NAV] Page navigation detected and loaded');
|
|
112
|
-
}
|
|
113
|
-
// Wait for pending XHR/fetch to complete
|
|
114
|
-
if (pendingRequests.size > 0 && opts.getNetworkRequests) {
|
|
115
|
-
const start = Date.now();
|
|
116
|
-
while (Date.now() - start < timeout && pendingRequests.size > 0) {
|
|
117
|
-
const updatedMap = opts.getNetworkRequests();
|
|
118
|
-
for (const [reqId] of pendingRequests) {
|
|
119
|
-
const updated = updatedMap.get(reqId);
|
|
120
|
-
if (updated?.status) {
|
|
121
|
-
completedEvents.push(`[${(updated.type ?? 'XHR').toUpperCase()}] ${updated.method} ${updated.url.substring(0, 80)} → ${updated.status}`);
|
|
122
|
-
pendingRequests.delete(reqId);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (pendingRequests.size > 0)
|
|
126
|
-
await sleep(100);
|
|
127
|
-
}
|
|
128
|
-
// Report timed-out requests
|
|
129
|
-
for (const [, req] of pendingRequests) {
|
|
130
|
-
completedEvents.push(`[PENDING] ${req.method} ${req.url.substring(0, 80)} (still loading)`);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
// Extra settling if any network activity occurred
|
|
134
|
-
if (completedEvents.length > 0) {
|
|
135
|
-
await sleep(200);
|
|
136
|
-
}
|
|
137
|
-
return { result, networkEvents: completedEvents };
|
|
138
|
-
}
|
|
139
63
|
// ─── waitForText ────────────────────────────────────────────────────
|
|
140
64
|
/**
|
|
141
65
|
* Poll until `text` appears in the page body, or timeout expires.
|
package/dist/utils/wait.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wait.js","sourceRoot":"","sources":["../../src/utils/wait.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAgBH,uEAAuE;AAEvE,2BAA2B;AAC3B,MAAM,UAAU,KAAK,CAAC,EAAU;IAC9B,OAAO,IAAI,OAAO,CAAC,CAAC,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K,CAAC;AACf,CAAC"}
|
package/package.json
CHANGED
package/dist/utils/retry.d.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Retry utilities for transient CDP failures and stale element refs.
|
|
3
|
-
*
|
|
4
|
-
* Provides generic retries with configurable back-off, plus a
|
|
5
|
-
* specialised wrapper for element-resolution actions that need
|
|
6
|
-
* Playwright-style auto-waiting.
|
|
7
|
-
*/
|
|
8
|
-
export interface RetryOptions {
|
|
9
|
-
/** Maximum number of attempts (default: 3). */
|
|
10
|
-
maxAttempts?: number;
|
|
11
|
-
/** Initial delay between attempts in ms (default: 500). */
|
|
12
|
-
delayMs?: number;
|
|
13
|
-
/** Multiplier applied to the delay after each failure (default: 2). */
|
|
14
|
-
backoffMultiplier?: number;
|
|
15
|
-
/** Predicate: return `true` to retry, `false` to rethrow immediately. */
|
|
16
|
-
retryOn?: (error: Error) => boolean;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Retry `fn` up to `maxAttempts` times with exponential back-off.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* const result = await withRetry(() => fetchSomething(), {
|
|
23
|
-
* maxAttempts: 4,
|
|
24
|
-
* delayMs: 300,
|
|
25
|
-
* retryOn: (e) => e.message.includes('ECONNRESET'),
|
|
26
|
-
* });
|
|
27
|
-
*/
|
|
28
|
-
export declare function withRetry<T>(fn: () => Promise<T>, options?: RetryOptions): Promise<T>;
|
|
29
|
-
/**
|
|
30
|
-
* Retry wrapper tuned for element resolution and actionability checks.
|
|
31
|
-
*
|
|
32
|
-
* Uses the same logic as the server.js `withRetry` — poll with a fixed
|
|
33
|
-
* interval until timeout, fast-failing on errors that can never recover
|
|
34
|
-
* (stale uid refs, missing params).
|
|
35
|
-
*
|
|
36
|
-
* @param fn - Async function to retry (resolve element → act on it)
|
|
37
|
-
* @param timeout - Overall timeout in ms (default: 5 000)
|
|
38
|
-
* @param interval - Polling interval in ms (default: 200)
|
|
39
|
-
*/
|
|
40
|
-
export declare function retryElementAction<T>(fn: () => Promise<T>, timeout?: number, interval?: number): Promise<T>;
|
package/dist/utils/retry.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Retry utilities for transient CDP failures and stale element refs.
|
|
3
|
-
*
|
|
4
|
-
* Provides generic retries with configurable back-off, plus a
|
|
5
|
-
* specialised wrapper for element-resolution actions that need
|
|
6
|
-
* Playwright-style auto-waiting.
|
|
7
|
-
*/
|
|
8
|
-
import { sleep } from './wait.js';
|
|
9
|
-
// ─── withRetry ──────────────────────────────────────────────────────
|
|
10
|
-
/**
|
|
11
|
-
* Retry `fn` up to `maxAttempts` times with exponential back-off.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* const result = await withRetry(() => fetchSomething(), {
|
|
15
|
-
* maxAttempts: 4,
|
|
16
|
-
* delayMs: 300,
|
|
17
|
-
* retryOn: (e) => e.message.includes('ECONNRESET'),
|
|
18
|
-
* });
|
|
19
|
-
*/
|
|
20
|
-
export async function withRetry(fn, options = {}) {
|
|
21
|
-
const { maxAttempts = 3, delayMs = 500, backoffMultiplier = 2, retryOn, } = options;
|
|
22
|
-
let lastError;
|
|
23
|
-
let currentDelay = delayMs;
|
|
24
|
-
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
25
|
-
try {
|
|
26
|
-
return await fn();
|
|
27
|
-
}
|
|
28
|
-
catch (err) {
|
|
29
|
-
lastError = err instanceof Error ? err : new Error(String(err));
|
|
30
|
-
// If a predicate was supplied and it says "don't retry", rethrow now
|
|
31
|
-
if (retryOn && !retryOn(lastError))
|
|
32
|
-
throw lastError;
|
|
33
|
-
// Last attempt — don't wait, just throw
|
|
34
|
-
if (attempt === maxAttempts)
|
|
35
|
-
break;
|
|
36
|
-
await sleep(currentDelay);
|
|
37
|
-
currentDelay = Math.round(currentDelay * backoffMultiplier);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
throw lastError;
|
|
41
|
-
}
|
|
42
|
-
// ─── retryElementAction ─────────────────────────────────────────────
|
|
43
|
-
/**
|
|
44
|
-
* Retry wrapper tuned for element resolution and actionability checks.
|
|
45
|
-
*
|
|
46
|
-
* Uses the same logic as the server.js `withRetry` — poll with a fixed
|
|
47
|
-
* interval until timeout, fast-failing on errors that can never recover
|
|
48
|
-
* (stale uid refs, missing params).
|
|
49
|
-
*
|
|
50
|
-
* @param fn - Async function to retry (resolve element → act on it)
|
|
51
|
-
* @param timeout - Overall timeout in ms (default: 5 000)
|
|
52
|
-
* @param interval - Polling interval in ms (default: 200)
|
|
53
|
-
*/
|
|
54
|
-
export async function retryElementAction(fn, timeout = 5000, interval = 200) {
|
|
55
|
-
const deadline = Date.now() + timeout;
|
|
56
|
-
let lastError;
|
|
57
|
-
while (true) {
|
|
58
|
-
try {
|
|
59
|
-
return await fn();
|
|
60
|
-
}
|
|
61
|
-
catch (e) {
|
|
62
|
-
lastError = e instanceof Error ? e : new Error(String(e));
|
|
63
|
-
const msg = lastError.message;
|
|
64
|
-
// Fast-fail on errors that won't recover with retries
|
|
65
|
-
if (msg.includes('ref=') && msg.includes('not found'))
|
|
66
|
-
throw lastError;
|
|
67
|
-
if (msg.includes('Provide either'))
|
|
68
|
-
throw lastError;
|
|
69
|
-
if (Date.now() + interval > deadline)
|
|
70
|
-
throw lastError;
|
|
71
|
-
await sleep(interval);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
//# sourceMappingURL=retry.js.map
|
package/dist/utils/retry.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"retry.js","sourceRoot":"","sources":["../../src/utils/retry.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAelC,uEAAuE;AAEvE;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,EAAoB,EACpB,UAAwB,EAAE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