scrapfly-sdk 0.10.3 → 0.10.5
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/browserconfig.d.ts +70 -1
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +44 -1
- package/esm/client.d.ts +33 -0
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +33 -5
- package/esm/crawl.d.ts +4 -0
- package/esm/crawl.d.ts.map +1 -1
- package/esm/crawl.js +4 -0
- package/esm/crawlerresult.d.ts +7 -0
- package/esm/crawlerresult.d.ts.map +1 -1
- package/esm/errors.d.ts +43 -0
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js +43 -16
- package/esm/extractionconfig.d.ts +54 -9
- package/esm/extractionconfig.d.ts.map +1 -1
- package/esm/extractionconfig.js +37 -13
- package/esm/logger.d.ts +1 -0
- package/esm/logger.d.ts.map +1 -1
- package/esm/logger.js +1 -0
- package/esm/main.d.ts +27 -0
- package/esm/main.d.ts.map +1 -1
- package/esm/main.js +27 -0
- package/esm/monitoringconfig.d.ts +48 -11
- package/esm/monitoringconfig.d.ts.map +1 -1
- package/esm/monitoringconfig.js +14 -0
- package/esm/result.d.ts +2 -0
- package/esm/result.d.ts.map +1 -1
- package/esm/scrapeconfig.d.ts +10 -8
- package/esm/scrapeconfig.d.ts.map +1 -1
- package/esm/scrapeconfig.js +10 -8
- package/esm/screenshotconfig.d.ts +59 -26
- package/esm/screenshotconfig.d.ts.map +1 -1
- package/esm/screenshotconfig.js +38 -26
- package/esm/webhookserver.d.ts +24 -0
- package/esm/webhookserver.d.ts.map +1 -1
- package/esm/webhookserver.js +24 -0
- package/package.json +1 -1
- package/script/browserconfig.d.ts +70 -1
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +44 -1
- package/script/client.d.ts +33 -0
- package/script/client.d.ts.map +1 -1
- package/script/client.js +33 -5
- package/script/crawl.d.ts +4 -0
- package/script/crawl.d.ts.map +1 -1
- package/script/crawl.js +4 -0
- package/script/crawlerresult.d.ts +7 -0
- package/script/crawlerresult.d.ts.map +1 -1
- package/script/errors.d.ts +43 -0
- package/script/errors.d.ts.map +1 -1
- package/script/errors.js +43 -16
- package/script/extractionconfig.d.ts +54 -9
- package/script/extractionconfig.d.ts.map +1 -1
- package/script/extractionconfig.js +37 -13
- package/script/logger.d.ts +1 -0
- package/script/logger.d.ts.map +1 -1
- package/script/logger.js +1 -0
- package/script/main.d.ts +27 -0
- package/script/main.d.ts.map +1 -1
- package/script/main.js +27 -0
- package/script/monitoringconfig.d.ts +48 -11
- package/script/monitoringconfig.d.ts.map +1 -1
- package/script/monitoringconfig.js +14 -0
- package/script/result.d.ts +2 -0
- package/script/result.d.ts.map +1 -1
- package/script/scrapeconfig.d.ts +10 -8
- package/script/scrapeconfig.d.ts.map +1 -1
- package/script/scrapeconfig.js +10 -8
- package/script/screenshotconfig.d.ts +59 -26
- package/script/screenshotconfig.d.ts.map +1 -1
- package/script/screenshotconfig.js +38 -26
- package/script/webhookserver.d.ts +24 -0
- package/script/webhookserver.d.ts.map +1 -1
- package/script/webhookserver.js +24 -0
- package/src/_dnt.shims.ts +64 -0
- package/src/batch.ts +272 -0
- package/src/browserconfig.ts +187 -0
- package/src/client.ts +1637 -0
- package/src/crawl.ts +255 -0
- package/src/crawlerconfig.ts +363 -0
- package/src/crawlerresult.ts +478 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/assert_path.ts +10 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/basename.ts +53 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/common.ts +26 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/constants.ts +49 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/dirname.ts +9 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/format.ts +25 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/from_file_url.ts +10 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/glob_to_reg_exp.ts +290 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize.ts +9 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize_string.ts +74 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/relative.ts +10 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/strip_trailing_separators.ts +25 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/to_file_url.ts +17 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_os.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/basename.ts +35 -0
- package/src/deps/jsr.io/@std/path/1.0.1/common.ts +35 -0
- package/src/deps/jsr.io/@std/path/1.0.1/constants.ts +18 -0
- package/src/deps/jsr.io/@std/path/1.0.1/dirname.ts +28 -0
- package/src/deps/jsr.io/@std/path/1.0.1/extname.ts +27 -0
- package/src/deps/jsr.io/@std/path/1.0.1/format.ts +30 -0
- package/src/deps/jsr.io/@std/path/1.0.1/from_file_url.ts +30 -0
- package/src/deps/jsr.io/@std/path/1.0.1/glob_to_regexp.ts +94 -0
- package/src/deps/jsr.io/@std/path/1.0.1/is_absolute.ts +30 -0
- package/src/deps/jsr.io/@std/path/1.0.1/is_glob.ts +49 -0
- package/src/deps/jsr.io/@std/path/1.0.1/join.ts +28 -0
- package/src/deps/jsr.io/@std/path/1.0.1/join_globs.ts +42 -0
- package/src/deps/jsr.io/@std/path/1.0.1/mod.ts +55 -0
- package/src/deps/jsr.io/@std/path/1.0.1/normalize.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/normalize_glob.ts +45 -0
- package/src/deps/jsr.io/@std/path/1.0.1/parse.ts +44 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/_util.ts +10 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/basename.ts +39 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/constants.ts +15 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/dirname.ts +55 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/extname.ts +73 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/format.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/from_file_url.ts +25 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/glob_to_regexp.ts +94 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/is_absolute.ts +25 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/join.ts +27 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/join_globs.ts +44 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize.ts +41 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize_glob.ts +42 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/parse.ts +121 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/relative.ts +103 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/resolve.ts +69 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/to_file_url.ts +32 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/to_namespaced_path.ts +21 -0
- package/src/deps/jsr.io/@std/path/1.0.1/relative.ts +32 -0
- package/src/deps/jsr.io/@std/path/1.0.1/resolve.ts +32 -0
- package/src/deps/jsr.io/@std/path/1.0.1/to_file_url.ts +30 -0
- package/src/deps/jsr.io/@std/path/1.0.1/to_namespaced_path.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/types.ts +40 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/_util.ts +28 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/basename.ts +48 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/constants.ts +15 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/dirname.ts +114 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/extname.ts +86 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/format.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/from_file_url.ts +34 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/glob_to_regexp.ts +92 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/is_absolute.ts +40 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/join.ts +72 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/join_globs.ts +45 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize.ts +132 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize_glob.ts +42 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/parse.ts +184 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/relative.ts +128 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/resolve.ts +174 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/to_file_url.ts +38 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/to_namespaced_path.ts +60 -0
- package/src/deps.ts +4 -0
- package/src/errors.ts +82 -0
- package/src/extractionconfig.ts +217 -0
- package/src/logger.ts +53 -0
- package/src/main.ts +88 -0
- package/src/monitoringconfig.ts +88 -0
- package/src/polyfill.ts +37 -0
- package/src/result.ts +340 -0
- package/src/scrapeconfig.ts +450 -0
- package/src/screenshotconfig.ts +237 -0
- package/src/types.ts +2 -0
- package/src/utils.ts +86 -0
- package/src/webhookserver.ts +90 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Deno } from "@deno/shim-deno";
|
|
2
|
+
export { Deno } from "@deno/shim-deno";
|
|
3
|
+
|
|
4
|
+
const dntGlobals = {
|
|
5
|
+
Deno,
|
|
6
|
+
};
|
|
7
|
+
export const dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
|
|
8
|
+
|
|
9
|
+
function createMergeProxy<T extends object, U extends object>(
|
|
10
|
+
baseObj: T,
|
|
11
|
+
extObj: U,
|
|
12
|
+
): Omit<T, keyof U> & U {
|
|
13
|
+
return new Proxy(baseObj, {
|
|
14
|
+
get(_target, prop, _receiver) {
|
|
15
|
+
if (prop in extObj) {
|
|
16
|
+
return (extObj as any)[prop];
|
|
17
|
+
} else {
|
|
18
|
+
return (baseObj as any)[prop];
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
set(_target, prop, value) {
|
|
22
|
+
if (prop in extObj) {
|
|
23
|
+
delete (extObj as any)[prop];
|
|
24
|
+
}
|
|
25
|
+
(baseObj as any)[prop] = value;
|
|
26
|
+
return true;
|
|
27
|
+
},
|
|
28
|
+
deleteProperty(_target, prop) {
|
|
29
|
+
let success = false;
|
|
30
|
+
if (prop in extObj) {
|
|
31
|
+
delete (extObj as any)[prop];
|
|
32
|
+
success = true;
|
|
33
|
+
}
|
|
34
|
+
if (prop in baseObj) {
|
|
35
|
+
delete (baseObj as any)[prop];
|
|
36
|
+
success = true;
|
|
37
|
+
}
|
|
38
|
+
return success;
|
|
39
|
+
},
|
|
40
|
+
ownKeys(_target) {
|
|
41
|
+
const baseKeys = Reflect.ownKeys(baseObj);
|
|
42
|
+
const extKeys = Reflect.ownKeys(extObj);
|
|
43
|
+
const extKeysSet = new Set(extKeys);
|
|
44
|
+
return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
|
|
45
|
+
},
|
|
46
|
+
defineProperty(_target, prop, desc) {
|
|
47
|
+
if (prop in extObj) {
|
|
48
|
+
delete (extObj as any)[prop];
|
|
49
|
+
}
|
|
50
|
+
Reflect.defineProperty(baseObj, prop, desc);
|
|
51
|
+
return true;
|
|
52
|
+
},
|
|
53
|
+
getOwnPropertyDescriptor(_target, prop) {
|
|
54
|
+
if (prop in extObj) {
|
|
55
|
+
return Reflect.getOwnPropertyDescriptor(extObj, prop);
|
|
56
|
+
} else {
|
|
57
|
+
return Reflect.getOwnPropertyDescriptor(baseObj, prop);
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
has(_target, prop) {
|
|
61
|
+
return prop in extObj || prop in baseObj;
|
|
62
|
+
},
|
|
63
|
+
}) as any;
|
|
64
|
+
}
|
package/src/batch.ts
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming multipart/mixed parser for POST /scrape/batch.
|
|
3
|
+
*
|
|
4
|
+
* The API emits one part per scrape as each completes. This module
|
|
5
|
+
* reads the response body as a ReadableStream and yields
|
|
6
|
+
* `(headers, body)` pairs as each part lands.
|
|
7
|
+
*
|
|
8
|
+
* No external dependencies — uses Web Streams (native in Node 18+
|
|
9
|
+
* and all modern browsers).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const CR = 0x0d;
|
|
13
|
+
const LF = 0x0a;
|
|
14
|
+
const CRLF = new Uint8Array([CR, LF]);
|
|
15
|
+
const DOUBLE_CRLF = new Uint8Array([CR, LF, CR, LF]);
|
|
16
|
+
|
|
17
|
+
function indexOf(buf: Uint8Array, needle: Uint8Array, start = 0): number {
|
|
18
|
+
if (needle.length === 0) return start;
|
|
19
|
+
const last = buf.length - needle.length;
|
|
20
|
+
outer: for (let i = start; i <= last; i++) {
|
|
21
|
+
for (let j = 0; j < needle.length; j++) {
|
|
22
|
+
if (buf[i + j] !== needle[j]) continue outer;
|
|
23
|
+
}
|
|
24
|
+
return i;
|
|
25
|
+
}
|
|
26
|
+
return -1;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function concat(a: Uint8Array, b: Uint8Array): Uint8Array {
|
|
30
|
+
const out = new Uint8Array(a.length + b.length);
|
|
31
|
+
out.set(a, 0);
|
|
32
|
+
out.set(b, a.length);
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parseContentType(value: string): { mime: string; params: Record<string, string> } {
|
|
37
|
+
const semi = value.indexOf(';');
|
|
38
|
+
if (semi < 0) return { mime: value.trim().toLowerCase(), params: {} };
|
|
39
|
+
const mime = value.slice(0, semi).trim().toLowerCase();
|
|
40
|
+
const params: Record<string, string> = {};
|
|
41
|
+
for (const piece of value.slice(semi + 1).split(';')) {
|
|
42
|
+
const eq = piece.indexOf('=');
|
|
43
|
+
if (eq < 0) continue;
|
|
44
|
+
const k = piece.slice(0, eq).trim().toLowerCase();
|
|
45
|
+
let v = piece.slice(eq + 1).trim();
|
|
46
|
+
if (v.startsWith('"') && v.endsWith('"')) v = v.slice(1, -1);
|
|
47
|
+
params[k] = v;
|
|
48
|
+
}
|
|
49
|
+
return { mime, params };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface BatchPart {
|
|
53
|
+
headers: Record<string, string>;
|
|
54
|
+
body: Uint8Array;
|
|
55
|
+
}
|
|
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
|
+
export async function* iterBatchParts(response: Response): AsyncGenerator<BatchPart> {
|
|
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
|
+
|
|
78
|
+
const boundary = new TextEncoder().encode('--' + boundaryStr);
|
|
79
|
+
const nextBoundarySep = new TextEncoder().encode('\r\n--' + boundaryStr);
|
|
80
|
+
const reader = response.body.getReader();
|
|
81
|
+
|
|
82
|
+
let buf = new Uint8Array(0);
|
|
83
|
+
let eof = false;
|
|
84
|
+
let pastPrefix = false;
|
|
85
|
+
|
|
86
|
+
const pump = async (): Promise<boolean> => {
|
|
87
|
+
if (eof) return false;
|
|
88
|
+
const { done, value } = await reader.read();
|
|
89
|
+
if (done) {
|
|
90
|
+
eof = true;
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
if (value && value.length > 0) {
|
|
94
|
+
// reader.read() returns Uint8Array<ArrayBufferLike> on newer Deno/TS
|
|
95
|
+
// lib types while dnt's Node target still sees Uint8Array as
|
|
96
|
+
// non-generic. Cast to the non-generic form so the same source
|
|
97
|
+
// type-checks on both targets.
|
|
98
|
+
buf = concat(buf, value as unknown as Uint8Array);
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
return false;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const readUntil = async (needle: Uint8Array): Promise<Uint8Array> => {
|
|
105
|
+
while (true) {
|
|
106
|
+
const idx = indexOf(buf, needle);
|
|
107
|
+
if (idx !== -1) {
|
|
108
|
+
const out = buf.subarray(0, idx);
|
|
109
|
+
buf = buf.subarray(idx + needle.length);
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
if (eof) {
|
|
113
|
+
const out = buf;
|
|
114
|
+
buf = new Uint8Array(0);
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
if (!(await pump())) {
|
|
118
|
+
const out = buf;
|
|
119
|
+
buf = new Uint8Array(0);
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const readExact = async (n: number): Promise<Uint8Array> => {
|
|
126
|
+
while (buf.length < n && !eof) {
|
|
127
|
+
await pump();
|
|
128
|
+
}
|
|
129
|
+
const out = buf.subarray(0, Math.min(n, buf.length));
|
|
130
|
+
buf = buf.subarray(out.length);
|
|
131
|
+
return out;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
while (true) {
|
|
135
|
+
if (!pastPrefix) {
|
|
136
|
+
await readUntil(boundary);
|
|
137
|
+
pastPrefix = true;
|
|
138
|
+
}
|
|
139
|
+
// After each --boundary, expect CRLF (more parts) or `--` (terminator).
|
|
140
|
+
// RFC 2046 mandates CRLF; any server deviating from that is broken —
|
|
141
|
+
// return cleanly rather than try to guess.
|
|
142
|
+
const suffix = await readExact(2);
|
|
143
|
+
if (suffix.length < 2) return;
|
|
144
|
+
if (suffix[0] === 0x2d && suffix[1] === 0x2d) {
|
|
145
|
+
// Final boundary marker "--"
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (!(suffix[0] === CR && suffix[1] === LF)) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Read headers up to blank line (CRLF CRLF).
|
|
153
|
+
const headerBlock = await readUntil(DOUBLE_CRLF);
|
|
154
|
+
const headers: Record<string, string> = {};
|
|
155
|
+
for (const lineBytes of splitByCRLF(headerBlock)) {
|
|
156
|
+
const line = new TextDecoder('utf-8').decode(lineBytes);
|
|
157
|
+
const colon = line.indexOf(':');
|
|
158
|
+
if (colon <= 0) continue;
|
|
159
|
+
headers[line.slice(0, colon).trim().toLowerCase()] = line.slice(colon + 1).trim();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Body framing: prefer Content-Length (server always emits it).
|
|
163
|
+
// When Content-Length is known, yield the part IMMEDIATELY after
|
|
164
|
+
// reading the body bytes — don't wait for the next boundary
|
|
165
|
+
// separator to arrive. Otherwise the caller is blocked until
|
|
166
|
+
// part N+1 starts streaming, which defeats the whole point of
|
|
167
|
+
// per-part streaming (especially when scrape N+1 is a slow one).
|
|
168
|
+
let body: Uint8Array;
|
|
169
|
+
const cl = headers['content-length'];
|
|
170
|
+
let readSeparatorAfterYield = false;
|
|
171
|
+
if (cl && /^\d+$/.test(cl)) {
|
|
172
|
+
body = await readExact(parseInt(cl, 10));
|
|
173
|
+
readSeparatorAfterYield = true;
|
|
174
|
+
} else {
|
|
175
|
+
body = await readUntil(nextBoundarySep);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
yield { headers, body };
|
|
179
|
+
|
|
180
|
+
// With Content-Length framing, the "\r\n--<boundary>" separator
|
|
181
|
+
// follows the body but is not part of it. Consume it now — any
|
|
182
|
+
// blocking that happens here blocks the NEXT iteration, not this
|
|
183
|
+
// part's delivery.
|
|
184
|
+
if (readSeparatorAfterYield) {
|
|
185
|
+
await readUntil(nextBoundarySep);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function splitByCRLF(buf: Uint8Array): Uint8Array[] {
|
|
191
|
+
const out: Uint8Array[] = [];
|
|
192
|
+
let start = 0;
|
|
193
|
+
for (let i = 0; i < buf.length - 1; i++) {
|
|
194
|
+
if (buf[i] === CR && buf[i + 1] === LF) {
|
|
195
|
+
out.push(buf.subarray(start, i));
|
|
196
|
+
start = i + 2;
|
|
197
|
+
i++;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (start < buf.length) out.push(buf.subarray(start));
|
|
201
|
+
return out;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// msgpack decoder. The module is npm:@msgpack/msgpack — dnt rewrites
|
|
205
|
+
// this to a regular npm import in the published package.
|
|
206
|
+
import { decode as msgpackDecode } from '@msgpack/msgpack';
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Decode a part body according to its content-type header. Supports
|
|
210
|
+
* both application/json (default) and application/msgpack.
|
|
211
|
+
*/
|
|
212
|
+
export function decodePartBody<T = unknown>(part: BatchPart): T {
|
|
213
|
+
const ct = part.headers['content-type'] ?? 'application/json';
|
|
214
|
+
if (ct.startsWith('application/json')) {
|
|
215
|
+
return JSON.parse(new TextDecoder('utf-8').decode(part.body)) as T;
|
|
216
|
+
}
|
|
217
|
+
if (ct.startsWith('application/msgpack') || ct.startsWith('application/x-msgpack')) {
|
|
218
|
+
return msgpackDecode(part.body) as T;
|
|
219
|
+
}
|
|
220
|
+
throw new Error(`scrapeBatch: unsupported part Content-Type: ${ct}`);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Header prefix used by the server to forward upstream response
|
|
224
|
+
// headers on proxified batch parts (avoids collision with the
|
|
225
|
+
// multipart envelope's own headers).
|
|
226
|
+
const UPSTREAM_PREFIX = 'x-scrapfly-upstream-';
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Synthesize a native fetch `Response` from a proxified batch part.
|
|
230
|
+
* The part body is the raw upstream bytes and the part carries:
|
|
231
|
+
* * `Content-Type` — the upstream's content-type
|
|
232
|
+
* * `X-Scrapfly-Scrape-Status` — the upstream's HTTP status
|
|
233
|
+
* * `X-Scrapfly-Upstream-<Name>` — upstream response headers
|
|
234
|
+
* * `X-Scrapfly-Log-Uuid`, `X-Scrapfly-Content-Format` — scrapfly metadata
|
|
235
|
+
*
|
|
236
|
+
* We return a Response with those values restored so the caller
|
|
237
|
+
* gets the same shape as a single proxified scrape.
|
|
238
|
+
*/
|
|
239
|
+
export function buildProxifiedResponseFromPart(part: BatchPart): Response {
|
|
240
|
+
const statusHeader = part.headers['x-scrapfly-scrape-status'];
|
|
241
|
+
const status = statusHeader ? parseInt(statusHeader, 10) || 200 : 200;
|
|
242
|
+
|
|
243
|
+
const headers = new Headers();
|
|
244
|
+
for (const [key, value] of Object.entries(part.headers)) {
|
|
245
|
+
const lower = key.toLowerCase();
|
|
246
|
+
if (lower === 'content-type') {
|
|
247
|
+
headers.set('Content-Type', value);
|
|
248
|
+
} else if (lower.startsWith(UPSTREAM_PREFIX)) {
|
|
249
|
+
headers.set(key.slice(UPSTREAM_PREFIX.length), value);
|
|
250
|
+
} else if (lower.startsWith('x-scrapfly-')) {
|
|
251
|
+
headers.set(key, value);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Normalize X-Scrapfly-Log-Uuid → X-Scrapfly-Log for parity with
|
|
256
|
+
// the single-scrape proxified response headers.
|
|
257
|
+
if (!headers.has('X-Scrapfly-Log') && headers.has('X-Scrapfly-Log-Uuid')) {
|
|
258
|
+
headers.set('X-Scrapfly-Log', headers.get('X-Scrapfly-Log-Uuid') ?? '');
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Wrap the Uint8Array in an ArrayBuffer so fetch Response accepts
|
|
262
|
+
// it (Node's fetch is picky about BodyInit).
|
|
263
|
+
// Copy into a fresh ArrayBuffer to avoid SharedArrayBuffer edge
|
|
264
|
+
// cases when the underlying bytes came from a larger buffer.
|
|
265
|
+
const bodyBuffer = new Uint8Array(part.body.length);
|
|
266
|
+
bodyBuffer.set(part.body);
|
|
267
|
+
|
|
268
|
+
return new Response(bodyBuffer, {
|
|
269
|
+
status,
|
|
270
|
+
headers,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/** Proxy pool to use for the Cloud Browser session. */
|
|
2
|
+
export enum ProxyPool {
|
|
3
|
+
/** Datacenter proxies — fast and cheap; best for non-protected targets. */
|
|
4
|
+
DATACENTER = 'datacenter',
|
|
5
|
+
/** Residential proxies — real ISP IPs; required by most anti-bot protections. */
|
|
6
|
+
RESIDENTIAL = 'residential',
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Operating system to emulate for the Cloud Browser session fingerprint. */
|
|
10
|
+
export enum OperatingSystem {
|
|
11
|
+
/** Emulate a Linux browser fingerprint. */
|
|
12
|
+
LINUX = 'linux',
|
|
13
|
+
/** Emulate a Windows browser fingerprint. */
|
|
14
|
+
WINDOWS = 'windows',
|
|
15
|
+
/** Emulate a macOS browser fingerprint. */
|
|
16
|
+
MACOS = 'macos',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* User-facing options accepted by {@link BrowserConfig}. All fields are
|
|
21
|
+
* optional; any field left undefined falls back to the Scrapfly Cloud
|
|
22
|
+
* Browser defaults.
|
|
23
|
+
*/
|
|
24
|
+
export type BrowserConfigOptions = {
|
|
25
|
+
/** Proxy pool to route the session through. */
|
|
26
|
+
proxy_pool?: ProxyPool | string;
|
|
27
|
+
/** Operating system fingerprint to emulate. */
|
|
28
|
+
os?: OperatingSystem | string;
|
|
29
|
+
/** Session ID for sticky sessions (IP + cookies reused across scrapes). */
|
|
30
|
+
session?: string;
|
|
31
|
+
/** ISO country code for geo-targeting. */
|
|
32
|
+
country?: string;
|
|
33
|
+
/** Close the browser automatically when the connection drops. */
|
|
34
|
+
auto_close?: boolean;
|
|
35
|
+
/** Session timeout in seconds (max 1800 = 30 minutes). */
|
|
36
|
+
timeout?: number;
|
|
37
|
+
/** Enable debug trace for the session. */
|
|
38
|
+
debug?: boolean;
|
|
39
|
+
/** Browser extensions to load (by ID). */
|
|
40
|
+
extensions?: string[];
|
|
41
|
+
/** Block image requests to speed up navigation. */
|
|
42
|
+
block_images?: boolean;
|
|
43
|
+
/** Block stylesheet requests. */
|
|
44
|
+
block_styles?: boolean;
|
|
45
|
+
/** Block font requests. */
|
|
46
|
+
block_fonts?: boolean;
|
|
47
|
+
/** Block media (video/audio) requests. */
|
|
48
|
+
block_media?: boolean;
|
|
49
|
+
/** Capture a screenshot at session end. */
|
|
50
|
+
screenshot?: boolean;
|
|
51
|
+
/** Viewport resolution, formatted as "widthxheight" (e.g. "1920x1080"). */
|
|
52
|
+
resolution?: string;
|
|
53
|
+
/** URL to navigate to after session start. */
|
|
54
|
+
target_url?: string;
|
|
55
|
+
/** Reuse cached responses where possible. */
|
|
56
|
+
cache?: boolean;
|
|
57
|
+
/** Apply Scrapfly's abuse blacklist. */
|
|
58
|
+
blacklist?: boolean;
|
|
59
|
+
/** Enable anti-scraping-protection bypass. */
|
|
60
|
+
unblock?: boolean;
|
|
61
|
+
/** Timeout in seconds for the unblock step. */
|
|
62
|
+
unblock_timeout?: number;
|
|
63
|
+
/** Browser brand to emulate (e.g. "chrome", "firefox"). */
|
|
64
|
+
browser_brand?: string;
|
|
65
|
+
/** Enable the MCP (Model Context Protocol) bridge. */
|
|
66
|
+
enable_mcp?: boolean;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Configuration for a Scrapfly Cloud Browser session. Builds the
|
|
71
|
+
* WebSocket URL that clients such as Puppeteer or Playwright can
|
|
72
|
+
* connect to via CDP.
|
|
73
|
+
*/
|
|
74
|
+
export class BrowserConfig {
|
|
75
|
+
/** See {@link BrowserConfigOptions.proxy_pool}. */
|
|
76
|
+
proxy_pool?: string;
|
|
77
|
+
/** See {@link BrowserConfigOptions.os}. */
|
|
78
|
+
os?: string;
|
|
79
|
+
/** See {@link BrowserConfigOptions.session}. */
|
|
80
|
+
session?: string;
|
|
81
|
+
/** See {@link BrowserConfigOptions.country}. */
|
|
82
|
+
country?: string;
|
|
83
|
+
/** See {@link BrowserConfigOptions.auto_close}. */
|
|
84
|
+
auto_close?: boolean;
|
|
85
|
+
/** See {@link BrowserConfigOptions.timeout}. */
|
|
86
|
+
timeout?: number;
|
|
87
|
+
/** See {@link BrowserConfigOptions.debug}. */
|
|
88
|
+
debug?: boolean;
|
|
89
|
+
/** See {@link BrowserConfigOptions.extensions}. */
|
|
90
|
+
extensions?: string[];
|
|
91
|
+
/** See {@link BrowserConfigOptions.block_images}. */
|
|
92
|
+
block_images?: boolean;
|
|
93
|
+
/** See {@link BrowserConfigOptions.block_styles}. */
|
|
94
|
+
block_styles?: boolean;
|
|
95
|
+
/** See {@link BrowserConfigOptions.block_fonts}. */
|
|
96
|
+
block_fonts?: boolean;
|
|
97
|
+
/** See {@link BrowserConfigOptions.block_media}. */
|
|
98
|
+
block_media?: boolean;
|
|
99
|
+
/** See {@link BrowserConfigOptions.screenshot}. */
|
|
100
|
+
screenshot?: boolean;
|
|
101
|
+
/** See {@link BrowserConfigOptions.resolution}. */
|
|
102
|
+
resolution?: string;
|
|
103
|
+
/** See {@link BrowserConfigOptions.target_url}. */
|
|
104
|
+
target_url?: string;
|
|
105
|
+
/** See {@link BrowserConfigOptions.cache}. */
|
|
106
|
+
cache?: boolean;
|
|
107
|
+
/** See {@link BrowserConfigOptions.blacklist}. */
|
|
108
|
+
blacklist?: boolean;
|
|
109
|
+
/** See {@link BrowserConfigOptions.unblock}. */
|
|
110
|
+
unblock?: boolean;
|
|
111
|
+
/** See {@link BrowserConfigOptions.unblock_timeout}. */
|
|
112
|
+
unblock_timeout?: number;
|
|
113
|
+
/** See {@link BrowserConfigOptions.browser_brand}. */
|
|
114
|
+
browser_brand?: string;
|
|
115
|
+
/** See {@link BrowserConfigOptions.enable_mcp}. */
|
|
116
|
+
enable_mcp?: boolean;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @param options Session options. See {@link BrowserConfigOptions}.
|
|
120
|
+
* @throws `Error` if `timeout` exceeds the upper bound of 1800 seconds.
|
|
121
|
+
*/
|
|
122
|
+
constructor(options: BrowserConfigOptions = {}) {
|
|
123
|
+
if (options.timeout !== undefined && options.timeout > 1800) {
|
|
124
|
+
throw new Error('timeout cannot exceed 1800 seconds (30 minutes)');
|
|
125
|
+
}
|
|
126
|
+
this.proxy_pool = options.proxy_pool;
|
|
127
|
+
this.os = options.os;
|
|
128
|
+
this.session = options.session;
|
|
129
|
+
this.country = options.country;
|
|
130
|
+
this.auto_close = options.auto_close;
|
|
131
|
+
this.timeout = options.timeout;
|
|
132
|
+
this.debug = options.debug;
|
|
133
|
+
this.extensions = options.extensions;
|
|
134
|
+
this.block_images = options.block_images;
|
|
135
|
+
this.block_styles = options.block_styles;
|
|
136
|
+
this.block_fonts = options.block_fonts;
|
|
137
|
+
this.block_media = options.block_media;
|
|
138
|
+
this.screenshot = options.screenshot;
|
|
139
|
+
this.resolution = options.resolution;
|
|
140
|
+
this.target_url = options.target_url;
|
|
141
|
+
this.cache = options.cache;
|
|
142
|
+
this.blacklist = options.blacklist;
|
|
143
|
+
this.unblock = options.unblock;
|
|
144
|
+
this.unblock_timeout = options.unblock_timeout;
|
|
145
|
+
this.browser_brand = options.browser_brand;
|
|
146
|
+
this.enable_mcp = options.enable_mcp;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Build the WebSocket URL clients use to connect to the Cloud Browser
|
|
151
|
+
* session (compatible with Puppeteer `browserWSEndpoint` and the
|
|
152
|
+
* Playwright `connectOverCDP` API).
|
|
153
|
+
*
|
|
154
|
+
* @param apiKey Scrapfly API key.
|
|
155
|
+
* @param host Optional override for the Cloud Browser host (defaults to `wss://browser.scrapfly.io`).
|
|
156
|
+
* @returns A fully-qualified `wss://…` URL with all options encoded as query parameters.
|
|
157
|
+
*/
|
|
158
|
+
websocketUrl(apiKey: string, host?: string): string {
|
|
159
|
+
const params = new URLSearchParams();
|
|
160
|
+
params.set('api_key', apiKey);
|
|
161
|
+
|
|
162
|
+
if (this.proxy_pool !== undefined) params.set('proxy_pool', this.proxy_pool);
|
|
163
|
+
if (this.os !== undefined) params.set('os', this.os);
|
|
164
|
+
if (this.session !== undefined) params.set('session', this.session);
|
|
165
|
+
if (this.country !== undefined) params.set('country', this.country);
|
|
166
|
+
if (this.auto_close !== undefined) params.set('auto_close', String(this.auto_close));
|
|
167
|
+
if (this.timeout !== undefined) params.set('timeout', String(this.timeout));
|
|
168
|
+
if (this.debug !== undefined) params.set('debug', String(this.debug));
|
|
169
|
+
if (this.extensions && this.extensions.length > 0) params.set('extensions', this.extensions.join(','));
|
|
170
|
+
if (this.block_images !== undefined) params.set('block_images', String(this.block_images));
|
|
171
|
+
if (this.block_styles !== undefined) params.set('block_styles', String(this.block_styles));
|
|
172
|
+
if (this.block_fonts !== undefined) params.set('block_fonts', String(this.block_fonts));
|
|
173
|
+
if (this.block_media !== undefined) params.set('block_media', String(this.block_media));
|
|
174
|
+
if (this.screenshot !== undefined) params.set('screenshot', String(this.screenshot));
|
|
175
|
+
if (this.resolution !== undefined) params.set('resolution', this.resolution);
|
|
176
|
+
if (this.target_url !== undefined) params.set('target_url', this.target_url);
|
|
177
|
+
if (this.cache !== undefined) params.set('cache', String(this.cache));
|
|
178
|
+
if (this.blacklist !== undefined) params.set('blacklist', String(this.blacklist));
|
|
179
|
+
if (this.unblock !== undefined) params.set('unblock', String(this.unblock));
|
|
180
|
+
if (this.unblock_timeout !== undefined) params.set('unblock_timeout', String(this.unblock_timeout));
|
|
181
|
+
if (this.browser_brand !== undefined) params.set('browser_brand', this.browser_brand);
|
|
182
|
+
if (this.enable_mcp !== undefined) params.set('enable_mcp', String(this.enable_mcp));
|
|
183
|
+
|
|
184
|
+
const baseHost = host || 'wss://browser.scrapfly.io';
|
|
185
|
+
return `${baseHost}?${params.toString()}`;
|
|
186
|
+
}
|
|
187
|
+
}
|