scrapfly-sdk 0.10.3 → 0.10.4

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.
Files changed (92) hide show
  1. package/package.json +1 -1
  2. package/src/_dnt.shims.ts +64 -0
  3. package/src/batch.ts +272 -0
  4. package/src/browserconfig.ts +118 -0
  5. package/src/client.ts +1609 -0
  6. package/src/crawl.ts +251 -0
  7. package/src/crawlerconfig.ts +363 -0
  8. package/src/crawlerresult.ts +471 -0
  9. package/src/deps/jsr.io/@std/path/1.0.1/_common/assert_path.ts +10 -0
  10. package/src/deps/jsr.io/@std/path/1.0.1/_common/basename.ts +53 -0
  11. package/src/deps/jsr.io/@std/path/1.0.1/_common/common.ts +26 -0
  12. package/src/deps/jsr.io/@std/path/1.0.1/_common/constants.ts +49 -0
  13. package/src/deps/jsr.io/@std/path/1.0.1/_common/dirname.ts +9 -0
  14. package/src/deps/jsr.io/@std/path/1.0.1/_common/format.ts +25 -0
  15. package/src/deps/jsr.io/@std/path/1.0.1/_common/from_file_url.ts +10 -0
  16. package/src/deps/jsr.io/@std/path/1.0.1/_common/glob_to_reg_exp.ts +290 -0
  17. package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize.ts +9 -0
  18. package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize_string.ts +74 -0
  19. package/src/deps/jsr.io/@std/path/1.0.1/_common/relative.ts +10 -0
  20. package/src/deps/jsr.io/@std/path/1.0.1/_common/strip_trailing_separators.ts +25 -0
  21. package/src/deps/jsr.io/@std/path/1.0.1/_common/to_file_url.ts +17 -0
  22. package/src/deps/jsr.io/@std/path/1.0.1/_os.ts +31 -0
  23. package/src/deps/jsr.io/@std/path/1.0.1/basename.ts +35 -0
  24. package/src/deps/jsr.io/@std/path/1.0.1/common.ts +35 -0
  25. package/src/deps/jsr.io/@std/path/1.0.1/constants.ts +18 -0
  26. package/src/deps/jsr.io/@std/path/1.0.1/dirname.ts +28 -0
  27. package/src/deps/jsr.io/@std/path/1.0.1/extname.ts +27 -0
  28. package/src/deps/jsr.io/@std/path/1.0.1/format.ts +30 -0
  29. package/src/deps/jsr.io/@std/path/1.0.1/from_file_url.ts +30 -0
  30. package/src/deps/jsr.io/@std/path/1.0.1/glob_to_regexp.ts +94 -0
  31. package/src/deps/jsr.io/@std/path/1.0.1/is_absolute.ts +30 -0
  32. package/src/deps/jsr.io/@std/path/1.0.1/is_glob.ts +49 -0
  33. package/src/deps/jsr.io/@std/path/1.0.1/join.ts +28 -0
  34. package/src/deps/jsr.io/@std/path/1.0.1/join_globs.ts +42 -0
  35. package/src/deps/jsr.io/@std/path/1.0.1/mod.ts +55 -0
  36. package/src/deps/jsr.io/@std/path/1.0.1/normalize.ts +31 -0
  37. package/src/deps/jsr.io/@std/path/1.0.1/normalize_glob.ts +45 -0
  38. package/src/deps/jsr.io/@std/path/1.0.1/parse.ts +44 -0
  39. package/src/deps/jsr.io/@std/path/1.0.1/posix/_util.ts +10 -0
  40. package/src/deps/jsr.io/@std/path/1.0.1/posix/basename.ts +39 -0
  41. package/src/deps/jsr.io/@std/path/1.0.1/posix/constants.ts +15 -0
  42. package/src/deps/jsr.io/@std/path/1.0.1/posix/dirname.ts +55 -0
  43. package/src/deps/jsr.io/@std/path/1.0.1/posix/extname.ts +73 -0
  44. package/src/deps/jsr.io/@std/path/1.0.1/posix/format.ts +31 -0
  45. package/src/deps/jsr.io/@std/path/1.0.1/posix/from_file_url.ts +25 -0
  46. package/src/deps/jsr.io/@std/path/1.0.1/posix/glob_to_regexp.ts +94 -0
  47. package/src/deps/jsr.io/@std/path/1.0.1/posix/is_absolute.ts +25 -0
  48. package/src/deps/jsr.io/@std/path/1.0.1/posix/join.ts +27 -0
  49. package/src/deps/jsr.io/@std/path/1.0.1/posix/join_globs.ts +44 -0
  50. package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize.ts +41 -0
  51. package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize_glob.ts +42 -0
  52. package/src/deps/jsr.io/@std/path/1.0.1/posix/parse.ts +121 -0
  53. package/src/deps/jsr.io/@std/path/1.0.1/posix/relative.ts +103 -0
  54. package/src/deps/jsr.io/@std/path/1.0.1/posix/resolve.ts +69 -0
  55. package/src/deps/jsr.io/@std/path/1.0.1/posix/to_file_url.ts +32 -0
  56. package/src/deps/jsr.io/@std/path/1.0.1/posix/to_namespaced_path.ts +21 -0
  57. package/src/deps/jsr.io/@std/path/1.0.1/relative.ts +32 -0
  58. package/src/deps/jsr.io/@std/path/1.0.1/resolve.ts +32 -0
  59. package/src/deps/jsr.io/@std/path/1.0.1/to_file_url.ts +30 -0
  60. package/src/deps/jsr.io/@std/path/1.0.1/to_namespaced_path.ts +31 -0
  61. package/src/deps/jsr.io/@std/path/1.0.1/types.ts +40 -0
  62. package/src/deps/jsr.io/@std/path/1.0.1/windows/_util.ts +28 -0
  63. package/src/deps/jsr.io/@std/path/1.0.1/windows/basename.ts +48 -0
  64. package/src/deps/jsr.io/@std/path/1.0.1/windows/constants.ts +15 -0
  65. package/src/deps/jsr.io/@std/path/1.0.1/windows/dirname.ts +114 -0
  66. package/src/deps/jsr.io/@std/path/1.0.1/windows/extname.ts +86 -0
  67. package/src/deps/jsr.io/@std/path/1.0.1/windows/format.ts +31 -0
  68. package/src/deps/jsr.io/@std/path/1.0.1/windows/from_file_url.ts +34 -0
  69. package/src/deps/jsr.io/@std/path/1.0.1/windows/glob_to_regexp.ts +92 -0
  70. package/src/deps/jsr.io/@std/path/1.0.1/windows/is_absolute.ts +40 -0
  71. package/src/deps/jsr.io/@std/path/1.0.1/windows/join.ts +72 -0
  72. package/src/deps/jsr.io/@std/path/1.0.1/windows/join_globs.ts +45 -0
  73. package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize.ts +132 -0
  74. package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize_glob.ts +42 -0
  75. package/src/deps/jsr.io/@std/path/1.0.1/windows/parse.ts +184 -0
  76. package/src/deps/jsr.io/@std/path/1.0.1/windows/relative.ts +128 -0
  77. package/src/deps/jsr.io/@std/path/1.0.1/windows/resolve.ts +174 -0
  78. package/src/deps/jsr.io/@std/path/1.0.1/windows/to_file_url.ts +38 -0
  79. package/src/deps/jsr.io/@std/path/1.0.1/windows/to_namespaced_path.ts +60 -0
  80. package/src/deps.ts +4 -0
  81. package/src/errors.ts +55 -0
  82. package/src/extractionconfig.ts +175 -0
  83. package/src/logger.ts +51 -0
  84. package/src/main.ts +61 -0
  85. package/src/monitoringconfig.ts +50 -0
  86. package/src/polyfill.ts +37 -0
  87. package/src/result.ts +338 -0
  88. package/src/scrapeconfig.ts +448 -0
  89. package/src/screenshotconfig.ts +204 -0
  90. package/src/types.ts +2 -0
  91. package/src/utils.ts +86 -0
  92. package/src/webhookserver.ts +66 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scrapfly-sdk",
3
- "version": "0.10.3",
3
+ "version": "0.10.4",
4
4
  "description": "SDK for Scrapfly.io API for web scraping, screenshotting and data extraction",
5
5
  "keywords": [
6
6
  "web scraping",
@@ -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,118 @@
1
+ export enum ProxyPool {
2
+ DATACENTER = 'datacenter',
3
+ RESIDENTIAL = 'residential',
4
+ }
5
+
6
+ export enum OperatingSystem {
7
+ LINUX = 'linux',
8
+ WINDOWS = 'windows',
9
+ MACOS = 'macos',
10
+ }
11
+
12
+ export type BrowserConfigOptions = {
13
+ proxy_pool?: ProxyPool | string;
14
+ os?: OperatingSystem | string;
15
+ session?: string;
16
+ country?: string;
17
+ auto_close?: boolean;
18
+ timeout?: number;
19
+ debug?: boolean;
20
+ extensions?: string[];
21
+ block_images?: boolean;
22
+ block_styles?: boolean;
23
+ block_fonts?: boolean;
24
+ block_media?: boolean;
25
+ screenshot?: boolean;
26
+ resolution?: string;
27
+ target_url?: string;
28
+ cache?: boolean;
29
+ blacklist?: boolean;
30
+ unblock?: boolean;
31
+ unblock_timeout?: number;
32
+ browser_brand?: string;
33
+ enable_mcp?: boolean;
34
+ };
35
+
36
+ export class BrowserConfig {
37
+ proxy_pool?: string;
38
+ os?: string;
39
+ session?: string;
40
+ country?: string;
41
+ auto_close?: boolean;
42
+ timeout?: number;
43
+ debug?: boolean;
44
+ extensions?: string[];
45
+ block_images?: boolean;
46
+ block_styles?: boolean;
47
+ block_fonts?: boolean;
48
+ block_media?: boolean;
49
+ screenshot?: boolean;
50
+ resolution?: string;
51
+ target_url?: string;
52
+ cache?: boolean;
53
+ blacklist?: boolean;
54
+ unblock?: boolean;
55
+ unblock_timeout?: number;
56
+ browser_brand?: string;
57
+ enable_mcp?: boolean;
58
+
59
+ constructor(options: BrowserConfigOptions = {}) {
60
+ if (options.timeout !== undefined && options.timeout > 1800) {
61
+ throw new Error('timeout cannot exceed 1800 seconds (30 minutes)');
62
+ }
63
+ this.proxy_pool = options.proxy_pool;
64
+ this.os = options.os;
65
+ this.session = options.session;
66
+ this.country = options.country;
67
+ this.auto_close = options.auto_close;
68
+ this.timeout = options.timeout;
69
+ this.debug = options.debug;
70
+ this.extensions = options.extensions;
71
+ this.block_images = options.block_images;
72
+ this.block_styles = options.block_styles;
73
+ this.block_fonts = options.block_fonts;
74
+ this.block_media = options.block_media;
75
+ this.screenshot = options.screenshot;
76
+ this.resolution = options.resolution;
77
+ this.target_url = options.target_url;
78
+ this.cache = options.cache;
79
+ this.blacklist = options.blacklist;
80
+ this.unblock = options.unblock;
81
+ this.unblock_timeout = options.unblock_timeout;
82
+ this.browser_brand = options.browser_brand;
83
+ this.enable_mcp = options.enable_mcp;
84
+ }
85
+
86
+ /**
87
+ * Generate the WebSocket URL for a Cloud Browser session.
88
+ */
89
+ websocketUrl(apiKey: string, host?: string): string {
90
+ const params = new URLSearchParams();
91
+ params.set('api_key', apiKey);
92
+
93
+ if (this.proxy_pool !== undefined) params.set('proxy_pool', this.proxy_pool);
94
+ if (this.os !== undefined) params.set('os', this.os);
95
+ if (this.session !== undefined) params.set('session', this.session);
96
+ if (this.country !== undefined) params.set('country', this.country);
97
+ if (this.auto_close !== undefined) params.set('auto_close', String(this.auto_close));
98
+ if (this.timeout !== undefined) params.set('timeout', String(this.timeout));
99
+ if (this.debug !== undefined) params.set('debug', String(this.debug));
100
+ if (this.extensions && this.extensions.length > 0) params.set('extensions', this.extensions.join(','));
101
+ if (this.block_images !== undefined) params.set('block_images', String(this.block_images));
102
+ if (this.block_styles !== undefined) params.set('block_styles', String(this.block_styles));
103
+ if (this.block_fonts !== undefined) params.set('block_fonts', String(this.block_fonts));
104
+ if (this.block_media !== undefined) params.set('block_media', String(this.block_media));
105
+ if (this.screenshot !== undefined) params.set('screenshot', String(this.screenshot));
106
+ if (this.resolution !== undefined) params.set('resolution', this.resolution);
107
+ if (this.target_url !== undefined) params.set('target_url', this.target_url);
108
+ if (this.cache !== undefined) params.set('cache', String(this.cache));
109
+ if (this.blacklist !== undefined) params.set('blacklist', String(this.blacklist));
110
+ if (this.unblock !== undefined) params.set('unblock', String(this.unblock));
111
+ if (this.unblock_timeout !== undefined) params.set('unblock_timeout', String(this.unblock_timeout));
112
+ if (this.browser_brand !== undefined) params.set('browser_brand', this.browser_brand);
113
+ if (this.enable_mcp !== undefined) params.set('enable_mcp', String(this.enable_mcp));
114
+
115
+ const baseHost = host || 'wss://browser.scrapfly.io';
116
+ return `${baseHost}?${params.toString()}`;
117
+ }
118
+ }