scrapfly-sdk 0.9.0 → 0.10.2
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/esm/batch.d.ts +40 -0
- package/esm/batch.d.ts.map +1 -0
- package/esm/batch.js +251 -0
- package/esm/browserconfig.d.ts +2 -0
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +9 -0
- package/esm/client.d.ts +47 -1
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +300 -0
- package/esm/main.d.ts +2 -1
- package/esm/main.d.ts.map +1 -1
- package/esm/monitoringconfig.d.ts +36 -0
- package/esm/monitoringconfig.d.ts.map +1 -0
- package/esm/monitoringconfig.js +10 -0
- package/esm/result.d.ts +12 -0
- package/esm/result.d.ts.map +1 -1
- package/package.json +2 -1
- package/script/batch.d.ts +40 -0
- package/script/batch.d.ts.map +1 -0
- package/script/batch.js +256 -0
- package/script/browserconfig.d.ts +2 -0
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +9 -0
- package/script/client.d.ts +47 -1
- package/script/client.d.ts.map +1 -1
- package/script/client.js +300 -0
- package/script/main.d.ts +2 -1
- package/script/main.d.ts.map +1 -1
- package/script/monitoringconfig.d.ts +36 -0
- package/script/monitoringconfig.d.ts.map +1 -0
- package/script/monitoringconfig.js +13 -0
- package/script/result.d.ts +12 -0
- package/script/result.d.ts.map +1 -1
package/script/batch.js
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Streaming multipart/mixed parser for POST /scrape/batch.
|
|
4
|
+
*
|
|
5
|
+
* The API emits one part per scrape as each completes. This module
|
|
6
|
+
* reads the response body as a ReadableStream and yields
|
|
7
|
+
* `(headers, body)` pairs as each part lands.
|
|
8
|
+
*
|
|
9
|
+
* No external dependencies — uses Web Streams (native in Node 18+
|
|
10
|
+
* and all modern browsers).
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.iterBatchParts = iterBatchParts;
|
|
14
|
+
exports.decodePartBody = decodePartBody;
|
|
15
|
+
exports.buildProxifiedResponseFromPart = buildProxifiedResponseFromPart;
|
|
16
|
+
const CR = 0x0d;
|
|
17
|
+
const LF = 0x0a;
|
|
18
|
+
const CRLF = new Uint8Array([CR, LF]);
|
|
19
|
+
const DOUBLE_CRLF = new Uint8Array([CR, LF, CR, LF]);
|
|
20
|
+
function indexOf(buf, needle, start = 0) {
|
|
21
|
+
if (needle.length === 0)
|
|
22
|
+
return start;
|
|
23
|
+
const last = buf.length - needle.length;
|
|
24
|
+
outer: for (let i = start; i <= last; i++) {
|
|
25
|
+
for (let j = 0; j < needle.length; j++) {
|
|
26
|
+
if (buf[i + j] !== needle[j])
|
|
27
|
+
continue outer;
|
|
28
|
+
}
|
|
29
|
+
return i;
|
|
30
|
+
}
|
|
31
|
+
return -1;
|
|
32
|
+
}
|
|
33
|
+
function concat(a, b) {
|
|
34
|
+
const out = new Uint8Array(a.length + b.length);
|
|
35
|
+
out.set(a, 0);
|
|
36
|
+
out.set(b, a.length);
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
function parseContentType(value) {
|
|
40
|
+
const semi = value.indexOf(';');
|
|
41
|
+
if (semi < 0)
|
|
42
|
+
return { mime: value.trim().toLowerCase(), params: {} };
|
|
43
|
+
const mime = value.slice(0, semi).trim().toLowerCase();
|
|
44
|
+
const params = {};
|
|
45
|
+
for (const piece of value.slice(semi + 1).split(';')) {
|
|
46
|
+
const eq = piece.indexOf('=');
|
|
47
|
+
if (eq < 0)
|
|
48
|
+
continue;
|
|
49
|
+
const k = piece.slice(0, eq).trim().toLowerCase();
|
|
50
|
+
let v = piece.slice(eq + 1).trim();
|
|
51
|
+
if (v.startsWith('"') && v.endsWith('"'))
|
|
52
|
+
v = v.slice(1, -1);
|
|
53
|
+
params[k] = v;
|
|
54
|
+
}
|
|
55
|
+
return { mime, params };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Async iterator over multipart/mixed parts from a fetch Response.
|
|
59
|
+
*
|
|
60
|
+
* Yields one `BatchPart` per multipart section as it arrives on the
|
|
61
|
+
* wire. Throws if the response Content-Type is not multipart/mixed
|
|
62
|
+
* or the boundary is missing.
|
|
63
|
+
*/
|
|
64
|
+
async function* iterBatchParts(response) {
|
|
65
|
+
const ct = response.headers.get('content-type') ?? '';
|
|
66
|
+
const { mime, params } = parseContentType(ct);
|
|
67
|
+
if (mime !== 'multipart/mixed') {
|
|
68
|
+
throw new Error(`scrapeBatch: expected Content-Type multipart/mixed, got ${JSON.stringify(ct)}`);
|
|
69
|
+
}
|
|
70
|
+
const boundaryStr = params['boundary'];
|
|
71
|
+
if (!boundaryStr) {
|
|
72
|
+
throw new Error(`scrapeBatch: Content-Type multipart/mixed is missing boundary parameter: ${JSON.stringify(ct)}`);
|
|
73
|
+
}
|
|
74
|
+
if (!response.body) {
|
|
75
|
+
throw new Error('scrapeBatch: response has no body stream');
|
|
76
|
+
}
|
|
77
|
+
const boundary = new TextEncoder().encode('--' + boundaryStr);
|
|
78
|
+
const nextBoundarySep = new TextEncoder().encode('\r\n--' + boundaryStr);
|
|
79
|
+
const reader = response.body.getReader();
|
|
80
|
+
let buf = new Uint8Array(0);
|
|
81
|
+
let eof = false;
|
|
82
|
+
let pastPrefix = false;
|
|
83
|
+
const pump = async () => {
|
|
84
|
+
if (eof)
|
|
85
|
+
return false;
|
|
86
|
+
const { done, value } = await reader.read();
|
|
87
|
+
if (done) {
|
|
88
|
+
eof = true;
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
if (value && value.length > 0) {
|
|
92
|
+
buf = concat(buf, value);
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
};
|
|
97
|
+
const readUntil = async (needle) => {
|
|
98
|
+
while (true) {
|
|
99
|
+
const idx = indexOf(buf, needle);
|
|
100
|
+
if (idx !== -1) {
|
|
101
|
+
const out = buf.subarray(0, idx);
|
|
102
|
+
buf = buf.subarray(idx + needle.length);
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
if (eof) {
|
|
106
|
+
const out = buf;
|
|
107
|
+
buf = new Uint8Array(0);
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
if (!(await pump())) {
|
|
111
|
+
const out = buf;
|
|
112
|
+
buf = new Uint8Array(0);
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const readExact = async (n) => {
|
|
118
|
+
while (buf.length < n && !eof) {
|
|
119
|
+
await pump();
|
|
120
|
+
}
|
|
121
|
+
const out = buf.subarray(0, Math.min(n, buf.length));
|
|
122
|
+
buf = buf.subarray(out.length);
|
|
123
|
+
return out;
|
|
124
|
+
};
|
|
125
|
+
while (true) {
|
|
126
|
+
if (!pastPrefix) {
|
|
127
|
+
await readUntil(boundary);
|
|
128
|
+
pastPrefix = true;
|
|
129
|
+
}
|
|
130
|
+
// After each --boundary, expect CRLF (more parts) or `--` (terminator).
|
|
131
|
+
// RFC 2046 mandates CRLF; any server deviating from that is broken —
|
|
132
|
+
// return cleanly rather than try to guess.
|
|
133
|
+
const suffix = await readExact(2);
|
|
134
|
+
if (suffix.length < 2)
|
|
135
|
+
return;
|
|
136
|
+
if (suffix[0] === 0x2d && suffix[1] === 0x2d) {
|
|
137
|
+
// Final boundary marker "--"
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (!(suffix[0] === CR && suffix[1] === LF)) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
// Read headers up to blank line (CRLF CRLF).
|
|
144
|
+
const headerBlock = await readUntil(DOUBLE_CRLF);
|
|
145
|
+
const headers = {};
|
|
146
|
+
for (const lineBytes of splitByCRLF(headerBlock)) {
|
|
147
|
+
const line = new TextDecoder('utf-8').decode(lineBytes);
|
|
148
|
+
const colon = line.indexOf(':');
|
|
149
|
+
if (colon <= 0)
|
|
150
|
+
continue;
|
|
151
|
+
headers[line.slice(0, colon).trim().toLowerCase()] = line.slice(colon + 1).trim();
|
|
152
|
+
}
|
|
153
|
+
// Body framing: prefer Content-Length (server always emits it).
|
|
154
|
+
// When Content-Length is known, yield the part IMMEDIATELY after
|
|
155
|
+
// reading the body bytes — don't wait for the next boundary
|
|
156
|
+
// separator to arrive. Otherwise the caller is blocked until
|
|
157
|
+
// part N+1 starts streaming, which defeats the whole point of
|
|
158
|
+
// per-part streaming (especially when scrape N+1 is a slow one).
|
|
159
|
+
let body;
|
|
160
|
+
const cl = headers['content-length'];
|
|
161
|
+
let readSeparatorAfterYield = false;
|
|
162
|
+
if (cl && /^\d+$/.test(cl)) {
|
|
163
|
+
body = await readExact(parseInt(cl, 10));
|
|
164
|
+
readSeparatorAfterYield = true;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
body = await readUntil(nextBoundarySep);
|
|
168
|
+
}
|
|
169
|
+
yield { headers, body };
|
|
170
|
+
// With Content-Length framing, the "\r\n--<boundary>" separator
|
|
171
|
+
// follows the body but is not part of it. Consume it now — any
|
|
172
|
+
// blocking that happens here blocks the NEXT iteration, not this
|
|
173
|
+
// part's delivery.
|
|
174
|
+
if (readSeparatorAfterYield) {
|
|
175
|
+
await readUntil(nextBoundarySep);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function splitByCRLF(buf) {
|
|
180
|
+
const out = [];
|
|
181
|
+
let start = 0;
|
|
182
|
+
for (let i = 0; i < buf.length - 1; i++) {
|
|
183
|
+
if (buf[i] === CR && buf[i + 1] === LF) {
|
|
184
|
+
out.push(buf.subarray(start, i));
|
|
185
|
+
start = i + 2;
|
|
186
|
+
i++;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (start < buf.length)
|
|
190
|
+
out.push(buf.subarray(start));
|
|
191
|
+
return out;
|
|
192
|
+
}
|
|
193
|
+
// msgpack decoder. The module is npm:@msgpack/msgpack — dnt rewrites
|
|
194
|
+
// this to a regular npm import in the published package.
|
|
195
|
+
const msgpack_1 = require("@msgpack/msgpack");
|
|
196
|
+
/**
|
|
197
|
+
* Decode a part body according to its content-type header. Supports
|
|
198
|
+
* both application/json (default) and application/msgpack.
|
|
199
|
+
*/
|
|
200
|
+
function decodePartBody(part) {
|
|
201
|
+
const ct = part.headers['content-type'] ?? 'application/json';
|
|
202
|
+
if (ct.startsWith('application/json')) {
|
|
203
|
+
return JSON.parse(new TextDecoder('utf-8').decode(part.body));
|
|
204
|
+
}
|
|
205
|
+
if (ct.startsWith('application/msgpack') || ct.startsWith('application/x-msgpack')) {
|
|
206
|
+
return (0, msgpack_1.decode)(part.body);
|
|
207
|
+
}
|
|
208
|
+
throw new Error(`scrapeBatch: unsupported part Content-Type: ${ct}`);
|
|
209
|
+
}
|
|
210
|
+
// Header prefix used by the server to forward upstream response
|
|
211
|
+
// headers on proxified batch parts (avoids collision with the
|
|
212
|
+
// multipart envelope's own headers).
|
|
213
|
+
const UPSTREAM_PREFIX = 'x-scrapfly-upstream-';
|
|
214
|
+
/**
|
|
215
|
+
* Synthesize a native fetch `Response` from a proxified batch part.
|
|
216
|
+
* The part body is the raw upstream bytes and the part carries:
|
|
217
|
+
* * `Content-Type` — the upstream's content-type
|
|
218
|
+
* * `X-Scrapfly-Scrape-Status` — the upstream's HTTP status
|
|
219
|
+
* * `X-Scrapfly-Upstream-<Name>` — upstream response headers
|
|
220
|
+
* * `X-Scrapfly-Log-Uuid`, `X-Scrapfly-Content-Format` — scrapfly metadata
|
|
221
|
+
*
|
|
222
|
+
* We return a Response with those values restored so the caller
|
|
223
|
+
* gets the same shape as a single proxified scrape.
|
|
224
|
+
*/
|
|
225
|
+
function buildProxifiedResponseFromPart(part) {
|
|
226
|
+
const statusHeader = part.headers['x-scrapfly-scrape-status'];
|
|
227
|
+
const status = statusHeader ? parseInt(statusHeader, 10) || 200 : 200;
|
|
228
|
+
const headers = new Headers();
|
|
229
|
+
for (const [key, value] of Object.entries(part.headers)) {
|
|
230
|
+
const lower = key.toLowerCase();
|
|
231
|
+
if (lower === 'content-type') {
|
|
232
|
+
headers.set('Content-Type', value);
|
|
233
|
+
}
|
|
234
|
+
else if (lower.startsWith(UPSTREAM_PREFIX)) {
|
|
235
|
+
headers.set(key.slice(UPSTREAM_PREFIX.length), value);
|
|
236
|
+
}
|
|
237
|
+
else if (lower.startsWith('x-scrapfly-')) {
|
|
238
|
+
headers.set(key, value);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// Normalize X-Scrapfly-Log-Uuid → X-Scrapfly-Log for parity with
|
|
242
|
+
// the single-scrape proxified response headers.
|
|
243
|
+
if (!headers.has('X-Scrapfly-Log') && headers.has('X-Scrapfly-Log-Uuid')) {
|
|
244
|
+
headers.set('X-Scrapfly-Log', headers.get('X-Scrapfly-Log-Uuid') ?? '');
|
|
245
|
+
}
|
|
246
|
+
// Wrap the Uint8Array in an ArrayBuffer so fetch Response accepts
|
|
247
|
+
// it (Node's fetch is picky about BodyInit).
|
|
248
|
+
// Copy into a fresh ArrayBuffer to avoid SharedArrayBuffer edge
|
|
249
|
+
// cases when the underlying bytes came from a larger buffer.
|
|
250
|
+
const bodyBuffer = new Uint8Array(part.body.length);
|
|
251
|
+
bodyBuffer.set(part.body);
|
|
252
|
+
return new Response(bodyBuffer, {
|
|
253
|
+
status,
|
|
254
|
+
headers,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
@@ -28,6 +28,7 @@ export type BrowserConfigOptions = {
|
|
|
28
28
|
unblock?: boolean;
|
|
29
29
|
unblock_timeout?: number;
|
|
30
30
|
browser_brand?: string;
|
|
31
|
+
enable_mcp?: boolean;
|
|
31
32
|
};
|
|
32
33
|
export declare class BrowserConfig {
|
|
33
34
|
proxy_pool?: string;
|
|
@@ -50,6 +51,7 @@ export declare class BrowserConfig {
|
|
|
50
51
|
unblock?: boolean;
|
|
51
52
|
unblock_timeout?: number;
|
|
52
53
|
browser_brand?: string;
|
|
54
|
+
enable_mcp?: boolean;
|
|
53
55
|
constructor(options?: BrowserConfigOptions);
|
|
54
56
|
/**
|
|
55
57
|
* Generate the WebSocket URL for a Cloud Browser session.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browserconfig.d.ts","sourceRoot":"","sources":["../src/browserconfig.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS;IACnB,UAAU,eAAe;IACzB,WAAW,gBAAgB;CAC5B;AAED,oBAAY,eAAe;IACzB,KAAK,UAAU;IACf,OAAO,YAAY;IACnB,KAAK,UAAU;CAChB;AAED,MAAM,MAAM,oBAAoB,GAAG;IACjC,UAAU,CAAC,EAAE,SAAS,GAAG,MAAM,CAAC;IAChC,EAAE,CAAC,EAAE,eAAe,GAAG,MAAM,CAAC;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,aAAa,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"browserconfig.d.ts","sourceRoot":"","sources":["../src/browserconfig.ts"],"names":[],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AC,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,CAAC,EAAE,OAAO,CAAC;gBAET,OAAO,GAAE,oBAAyB;IA2B9C;;OAEG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM;CA6BpD"}
|
package/script/browserconfig.js
CHANGED
|
@@ -134,6 +134,12 @@ class BrowserConfig {
|
|
|
134
134
|
writable: true,
|
|
135
135
|
value: void 0
|
|
136
136
|
});
|
|
137
|
+
Object.defineProperty(this, "enable_mcp", {
|
|
138
|
+
enumerable: true,
|
|
139
|
+
configurable: true,
|
|
140
|
+
writable: true,
|
|
141
|
+
value: void 0
|
|
142
|
+
});
|
|
137
143
|
if (options.timeout !== undefined && options.timeout > 1800) {
|
|
138
144
|
throw new Error('timeout cannot exceed 1800 seconds (30 minutes)');
|
|
139
145
|
}
|
|
@@ -157,6 +163,7 @@ class BrowserConfig {
|
|
|
157
163
|
this.unblock = options.unblock;
|
|
158
164
|
this.unblock_timeout = options.unblock_timeout;
|
|
159
165
|
this.browser_brand = options.browser_brand;
|
|
166
|
+
this.enable_mcp = options.enable_mcp;
|
|
160
167
|
}
|
|
161
168
|
/**
|
|
162
169
|
* Generate the WebSocket URL for a Cloud Browser session.
|
|
@@ -204,6 +211,8 @@ class BrowserConfig {
|
|
|
204
211
|
params.set('unblock_timeout', String(this.unblock_timeout));
|
|
205
212
|
if (this.browser_brand !== undefined)
|
|
206
213
|
params.set('browser_brand', this.browser_brand);
|
|
214
|
+
if (this.enable_mcp !== undefined)
|
|
215
|
+
params.set('enable_mcp', String(this.enable_mcp));
|
|
207
216
|
const baseHost = host || 'wss://browser.scrapfly.io';
|
|
208
217
|
return `${baseHost}?${params.toString()}`;
|
|
209
218
|
}
|
package/script/client.d.ts
CHANGED
|
@@ -4,9 +4,11 @@ import type { ScrapeConfig } from './scrapeconfig.js';
|
|
|
4
4
|
import type { ScreenshotConfig } from './screenshotconfig.js';
|
|
5
5
|
import type { ExtractionConfig } from './extractionconfig.js';
|
|
6
6
|
import { BrowserConfig } from './browserconfig.js';
|
|
7
|
-
import { type AccountData, ExtractionResult, ScrapeResult, ScreenshotResult } from './result.js';
|
|
7
|
+
import { type AccountData, type ClassifyOptions, type ClassifyResult, ExtractionResult, ScrapeResult, ScreenshotResult } from './result.js';
|
|
8
|
+
import type { Rec } from './types.js';
|
|
8
9
|
import type { CrawlerConfig, CrawlerContentFormat } from './crawlerconfig.js';
|
|
9
10
|
import { CrawlerArtifact, type CrawlerArtifactType, CrawlerContents, CrawlerStatus, CrawlerUrls } from './crawlerresult.js';
|
|
11
|
+
import { type CloudBrowserMonitoringOptions, type MonitoringMetricsOptions, type MonitoringTargetMetricsOptions } from './monitoringconfig.js';
|
|
10
12
|
export declare class ScrapflyClient {
|
|
11
13
|
HOST: string;
|
|
12
14
|
CLOUD_BROWSER_HOST: string;
|
|
@@ -37,6 +39,29 @@ export declare class ScrapflyClient {
|
|
|
37
39
|
* Retrieve Scrapfly account details
|
|
38
40
|
*/
|
|
39
41
|
account(): Promise<AccountData>;
|
|
42
|
+
/**
|
|
43
|
+
* Classify an already-fetched HTTP response for anti-bot blocking.
|
|
44
|
+
*
|
|
45
|
+
* Runs the same anti-bot detection pipeline used by live Scrapfly
|
|
46
|
+
* scrapes against a response you already have (from your own proxy,
|
|
47
|
+
* cache, etc). 1 API credit per call. See
|
|
48
|
+
* https://scrapfly.io/docs/scrape-api/classify for the full contract.
|
|
49
|
+
*/
|
|
50
|
+
classify(options: ClassifyOptions): Promise<ClassifyResult>;
|
|
51
|
+
private monitoringRequest;
|
|
52
|
+
private buildMetricsParams;
|
|
53
|
+
private buildTargetParams;
|
|
54
|
+
getMonitoringMetrics(options?: MonitoringMetricsOptions): Promise<Rec<any>>;
|
|
55
|
+
getMonitoringTargetMetrics(options: MonitoringTargetMetricsOptions): Promise<Rec<any>>;
|
|
56
|
+
getScreenshotMonitoringMetrics(options?: MonitoringMetricsOptions): Promise<Rec<any>>;
|
|
57
|
+
getScreenshotMonitoringTargetMetrics(options: MonitoringTargetMetricsOptions): Promise<Rec<any>>;
|
|
58
|
+
getExtractionMonitoringMetrics(options?: MonitoringMetricsOptions): Promise<Rec<any>>;
|
|
59
|
+
getExtractionMonitoringTargetMetrics(options: MonitoringTargetMetricsOptions): Promise<Rec<any>>;
|
|
60
|
+
getCrawlerMonitoringMetrics(options?: MonitoringMetricsOptions): Promise<Rec<any>>;
|
|
61
|
+
getCrawlerMonitoringTargetMetrics(options: MonitoringTargetMetricsOptions): Promise<Rec<any>>;
|
|
62
|
+
getBrowserMonitoringMetrics(options?: CloudBrowserMonitoringOptions): Promise<Rec<any>>;
|
|
63
|
+
getBrowserMonitoringTimeseries(options?: CloudBrowserMonitoringOptions): Promise<Rec<any>>;
|
|
64
|
+
private buildBrowserParams;
|
|
40
65
|
/**
|
|
41
66
|
* Issue a single scrape command from a given scrape configuration
|
|
42
67
|
*/
|
|
@@ -58,6 +83,25 @@ export declare class ScrapflyClient {
|
|
|
58
83
|
@param concurrencyLimit: if not set it will be taken from your account info
|
|
59
84
|
*/
|
|
60
85
|
concurrentScrape(configs: ScrapeConfig[], concurrencyLimit?: number): AsyncGenerator<ScrapeResult | Error | undefined, void, undefined>;
|
|
86
|
+
/**
|
|
87
|
+
* Issue a single batch request against POST /scrape/batch and
|
|
88
|
+
* stream results back as each scrape completes.
|
|
89
|
+
*
|
|
90
|
+
* Yields `[correlation_id, resultOrError]` tuples. Results arrive
|
|
91
|
+
* OUT OF ORDER — whichever scrape finishes first is yielded first.
|
|
92
|
+
* Every ScrapeConfig MUST carry a unique `correlation_id`; missing
|
|
93
|
+
* or duplicate values are caught client-side before the batch is
|
|
94
|
+
* sent (so callers fail fast on misconfiguration).
|
|
95
|
+
*
|
|
96
|
+
* Usage:
|
|
97
|
+
* for await (const [id, result] of client.scrapeBatch(configs)) {
|
|
98
|
+
* if (result instanceof errors.ScrapflyError) { ... }
|
|
99
|
+
* else { ... }
|
|
100
|
+
* }
|
|
101
|
+
*/
|
|
102
|
+
scrapeBatch(configs: ScrapeConfig[], options?: {
|
|
103
|
+
format?: 'json' | 'msgpack';
|
|
104
|
+
}): AsyncGenerator<[string, ScrapeResult | Response | errors.ScrapflyError], void, undefined>;
|
|
61
105
|
/**
|
|
62
106
|
* Save screenshot response to a file
|
|
63
107
|
*/
|
|
@@ -196,10 +240,12 @@ export declare class ScrapflyClient {
|
|
|
196
240
|
headers?: Record<string, string>;
|
|
197
241
|
body?: string;
|
|
198
242
|
method?: string;
|
|
243
|
+
enable_mcp?: boolean;
|
|
199
244
|
}): Promise<{
|
|
200
245
|
ws_url: string;
|
|
201
246
|
session_id: string;
|
|
202
247
|
run_id: string;
|
|
248
|
+
mcp_endpoint?: string;
|
|
203
249
|
}>;
|
|
204
250
|
/**
|
|
205
251
|
* Stop a Cloud Browser session.
|
package/script/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,EAAE,YAAY,EAAE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