scrapfly-sdk 0.10.2 → 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 (100) hide show
  1. package/esm/batch.d.ts.map +1 -1
  2. package/esm/batch.js +4 -0
  3. package/esm/client.d.ts.map +1 -1
  4. package/esm/client.js +17 -8
  5. package/package.json +1 -1
  6. package/script/batch.d.ts.map +1 -1
  7. package/script/batch.js +4 -0
  8. package/script/client.d.ts.map +1 -1
  9. package/script/client.js +17 -8
  10. package/src/_dnt.shims.ts +64 -0
  11. package/src/batch.ts +272 -0
  12. package/src/browserconfig.ts +118 -0
  13. package/src/client.ts +1609 -0
  14. package/src/crawl.ts +251 -0
  15. package/src/crawlerconfig.ts +363 -0
  16. package/src/crawlerresult.ts +471 -0
  17. package/src/deps/jsr.io/@std/path/1.0.1/_common/assert_path.ts +10 -0
  18. package/src/deps/jsr.io/@std/path/1.0.1/_common/basename.ts +53 -0
  19. package/src/deps/jsr.io/@std/path/1.0.1/_common/common.ts +26 -0
  20. package/src/deps/jsr.io/@std/path/1.0.1/_common/constants.ts +49 -0
  21. package/src/deps/jsr.io/@std/path/1.0.1/_common/dirname.ts +9 -0
  22. package/src/deps/jsr.io/@std/path/1.0.1/_common/format.ts +25 -0
  23. package/src/deps/jsr.io/@std/path/1.0.1/_common/from_file_url.ts +10 -0
  24. package/src/deps/jsr.io/@std/path/1.0.1/_common/glob_to_reg_exp.ts +290 -0
  25. package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize.ts +9 -0
  26. package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize_string.ts +74 -0
  27. package/src/deps/jsr.io/@std/path/1.0.1/_common/relative.ts +10 -0
  28. package/src/deps/jsr.io/@std/path/1.0.1/_common/strip_trailing_separators.ts +25 -0
  29. package/src/deps/jsr.io/@std/path/1.0.1/_common/to_file_url.ts +17 -0
  30. package/src/deps/jsr.io/@std/path/1.0.1/_os.ts +31 -0
  31. package/src/deps/jsr.io/@std/path/1.0.1/basename.ts +35 -0
  32. package/src/deps/jsr.io/@std/path/1.0.1/common.ts +35 -0
  33. package/src/deps/jsr.io/@std/path/1.0.1/constants.ts +18 -0
  34. package/src/deps/jsr.io/@std/path/1.0.1/dirname.ts +28 -0
  35. package/src/deps/jsr.io/@std/path/1.0.1/extname.ts +27 -0
  36. package/src/deps/jsr.io/@std/path/1.0.1/format.ts +30 -0
  37. package/src/deps/jsr.io/@std/path/1.0.1/from_file_url.ts +30 -0
  38. package/src/deps/jsr.io/@std/path/1.0.1/glob_to_regexp.ts +94 -0
  39. package/src/deps/jsr.io/@std/path/1.0.1/is_absolute.ts +30 -0
  40. package/src/deps/jsr.io/@std/path/1.0.1/is_glob.ts +49 -0
  41. package/src/deps/jsr.io/@std/path/1.0.1/join.ts +28 -0
  42. package/src/deps/jsr.io/@std/path/1.0.1/join_globs.ts +42 -0
  43. package/src/deps/jsr.io/@std/path/1.0.1/mod.ts +55 -0
  44. package/src/deps/jsr.io/@std/path/1.0.1/normalize.ts +31 -0
  45. package/src/deps/jsr.io/@std/path/1.0.1/normalize_glob.ts +45 -0
  46. package/src/deps/jsr.io/@std/path/1.0.1/parse.ts +44 -0
  47. package/src/deps/jsr.io/@std/path/1.0.1/posix/_util.ts +10 -0
  48. package/src/deps/jsr.io/@std/path/1.0.1/posix/basename.ts +39 -0
  49. package/src/deps/jsr.io/@std/path/1.0.1/posix/constants.ts +15 -0
  50. package/src/deps/jsr.io/@std/path/1.0.1/posix/dirname.ts +55 -0
  51. package/src/deps/jsr.io/@std/path/1.0.1/posix/extname.ts +73 -0
  52. package/src/deps/jsr.io/@std/path/1.0.1/posix/format.ts +31 -0
  53. package/src/deps/jsr.io/@std/path/1.0.1/posix/from_file_url.ts +25 -0
  54. package/src/deps/jsr.io/@std/path/1.0.1/posix/glob_to_regexp.ts +94 -0
  55. package/src/deps/jsr.io/@std/path/1.0.1/posix/is_absolute.ts +25 -0
  56. package/src/deps/jsr.io/@std/path/1.0.1/posix/join.ts +27 -0
  57. package/src/deps/jsr.io/@std/path/1.0.1/posix/join_globs.ts +44 -0
  58. package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize.ts +41 -0
  59. package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize_glob.ts +42 -0
  60. package/src/deps/jsr.io/@std/path/1.0.1/posix/parse.ts +121 -0
  61. package/src/deps/jsr.io/@std/path/1.0.1/posix/relative.ts +103 -0
  62. package/src/deps/jsr.io/@std/path/1.0.1/posix/resolve.ts +69 -0
  63. package/src/deps/jsr.io/@std/path/1.0.1/posix/to_file_url.ts +32 -0
  64. package/src/deps/jsr.io/@std/path/1.0.1/posix/to_namespaced_path.ts +21 -0
  65. package/src/deps/jsr.io/@std/path/1.0.1/relative.ts +32 -0
  66. package/src/deps/jsr.io/@std/path/1.0.1/resolve.ts +32 -0
  67. package/src/deps/jsr.io/@std/path/1.0.1/to_file_url.ts +30 -0
  68. package/src/deps/jsr.io/@std/path/1.0.1/to_namespaced_path.ts +31 -0
  69. package/src/deps/jsr.io/@std/path/1.0.1/types.ts +40 -0
  70. package/src/deps/jsr.io/@std/path/1.0.1/windows/_util.ts +28 -0
  71. package/src/deps/jsr.io/@std/path/1.0.1/windows/basename.ts +48 -0
  72. package/src/deps/jsr.io/@std/path/1.0.1/windows/constants.ts +15 -0
  73. package/src/deps/jsr.io/@std/path/1.0.1/windows/dirname.ts +114 -0
  74. package/src/deps/jsr.io/@std/path/1.0.1/windows/extname.ts +86 -0
  75. package/src/deps/jsr.io/@std/path/1.0.1/windows/format.ts +31 -0
  76. package/src/deps/jsr.io/@std/path/1.0.1/windows/from_file_url.ts +34 -0
  77. package/src/deps/jsr.io/@std/path/1.0.1/windows/glob_to_regexp.ts +92 -0
  78. package/src/deps/jsr.io/@std/path/1.0.1/windows/is_absolute.ts +40 -0
  79. package/src/deps/jsr.io/@std/path/1.0.1/windows/join.ts +72 -0
  80. package/src/deps/jsr.io/@std/path/1.0.1/windows/join_globs.ts +45 -0
  81. package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize.ts +132 -0
  82. package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize_glob.ts +42 -0
  83. package/src/deps/jsr.io/@std/path/1.0.1/windows/parse.ts +184 -0
  84. package/src/deps/jsr.io/@std/path/1.0.1/windows/relative.ts +128 -0
  85. package/src/deps/jsr.io/@std/path/1.0.1/windows/resolve.ts +174 -0
  86. package/src/deps/jsr.io/@std/path/1.0.1/windows/to_file_url.ts +38 -0
  87. package/src/deps/jsr.io/@std/path/1.0.1/windows/to_namespaced_path.ts +60 -0
  88. package/src/deps.ts +4 -0
  89. package/src/errors.ts +55 -0
  90. package/src/extractionconfig.ts +175 -0
  91. package/src/logger.ts +51 -0
  92. package/src/main.ts +61 -0
  93. package/src/monitoringconfig.ts +50 -0
  94. package/src/polyfill.ts +37 -0
  95. package/src/result.ts +338 -0
  96. package/src/scrapeconfig.ts +448 -0
  97. package/src/screenshotconfig.ts +204 -0
  98. package/src/types.ts +2 -0
  99. package/src/utils.ts +86 -0
  100. package/src/webhookserver.ts +66 -0
@@ -0,0 +1,37 @@
1
+ import * as dntShim from "./_dnt.shims.js";
2
+ export const isDeno = typeof dntShim.Deno !== 'undefined';
3
+ // @ts-ignore: Bun global
4
+ export const isBun = typeof Bun !== 'undefined';
5
+
6
+ export function readFileSync(path: string): Uint8Array {
7
+ if (isDeno) {
8
+ return dntShim.Deno.readFileSync(path);
9
+ }
10
+ // Node.js / Bun — both support require('fs')
11
+ // deno-lint-ignore no-var
12
+ // @ts-ignore: dynamic require for Node/Bun
13
+ var fs = require('fs');
14
+ return new Uint8Array(fs.readFileSync(path));
15
+ }
16
+
17
+ export async function mkdir(path: string | URL, options: dntShim.Deno.MkdirOptions): Promise<void> {
18
+ if (isDeno) {
19
+ await dntShim.Deno.mkdir(path, options);
20
+ } else {
21
+ // @ts-ignore: type for Bun
22
+ await Bun.mkdir(path.toString(), options);
23
+ }
24
+ }
25
+
26
+ export async function writeFile(
27
+ path: string | URL,
28
+ data: Uint8Array,
29
+ options: dntShim.Deno.WriteFileOptions = {},
30
+ ): Promise<void> {
31
+ if (isDeno) {
32
+ await dntShim.Deno.writeFile(path, data, options);
33
+ } else {
34
+ // @ts-ignore: type for Bun
35
+ await Bun.write(path.toString(), data, options);
36
+ }
37
+ }
package/src/result.ts ADDED
@@ -0,0 +1,338 @@
1
+ import type { HttpMethod, Rec } from './types.js';
2
+ import * as errors from './errors.js';
3
+ import { cheerio } from './deps.js';
4
+ import { normalizeHeaders } from './utils.js';
5
+
6
+ export type ConfigData = {
7
+ url: string;
8
+ retry: boolean;
9
+ method: HttpMethod;
10
+ country?: string;
11
+ render_js: boolean;
12
+ cache: boolean;
13
+ cache_clear: boolean;
14
+ ssl: boolean;
15
+ dns: boolean;
16
+ asp: boolean;
17
+ debug: boolean;
18
+ raise_on_upstream_error: boolean;
19
+ cache_ttl: number;
20
+ proxy_pool: string;
21
+ session?: string;
22
+ tags: Set<string>;
23
+ correlation_id?: string;
24
+ cookies: Record<string, string>;
25
+ body?: string;
26
+ data?: Rec<any>;
27
+ headers: Rec<string>;
28
+ js?: string;
29
+ rendering_wait?: number;
30
+ wait_for_selector?: string;
31
+ session_sticky_proxy: boolean;
32
+ screenshots?: Rec<any>;
33
+ webhook?: string;
34
+ timeout?: number; // in milliseconds
35
+ js_scenario?: Rec<any>;
36
+ extract?: Rec<any>;
37
+ lang?: string[];
38
+ os?: string;
39
+ auto_scroll?: boolean;
40
+ };
41
+
42
+ export type ContextData = {
43
+ asp: boolean;
44
+ bandwidth_consumed: number;
45
+ cache: {
46
+ state: string;
47
+ entry?: string;
48
+ };
49
+ cookies: Record<string, string>;
50
+ cost: {
51
+ details: Array<{ amount: number; code: string; description: string }>;
52
+ total: number;
53
+ };
54
+ created_at: string;
55
+ debug: {
56
+ response_url: string;
57
+ screenshot_url?: string;
58
+ };
59
+ env: string;
60
+ fingerprint: string;
61
+ headers: Record<string, string>;
62
+ is_xml_http_request: boolean;
63
+ job?: string;
64
+ lang: Array<string>;
65
+ os: {
66
+ distribution: string;
67
+ name: string;
68
+ type: string;
69
+ version: string;
70
+ };
71
+ project: string;
72
+ proxy: {
73
+ country: string;
74
+ identity: string;
75
+ network: string;
76
+ pool: string;
77
+ };
78
+ redirects: Array<string>;
79
+ retry: number;
80
+ schedule?: string;
81
+ session?: string;
82
+ spider?: any;
83
+ throttler?: any;
84
+ uri: {
85
+ base_url: string;
86
+ fragment?: string;
87
+ host: string;
88
+ params?: Rec<string>;
89
+ port: number;
90
+ query?: string;
91
+ root_domain: string;
92
+ scheme: string;
93
+ };
94
+ url: string;
95
+ webhook?: string;
96
+ };
97
+
98
+ export type ResultData = {
99
+ browser_data: {
100
+ javascript_evaluation_result?: string;
101
+ js_scenario?: {
102
+ duration: number;
103
+ executed: number;
104
+ response: any;
105
+ steps: Array<{
106
+ action: string;
107
+ config: Rec<string>;
108
+ duration: number;
109
+ executed: boolean;
110
+ result?: string;
111
+ success: boolean;
112
+ }>;
113
+ };
114
+ local_storage_data?: Rec<string>;
115
+ session_storage_data?: Rec<string>;
116
+ websockets?: Array<any>;
117
+ xhr_call?: Array<{
118
+ body?: string;
119
+ headers: Rec<string>;
120
+ method: string;
121
+ type: string;
122
+ url: string;
123
+ response: {
124
+ body: string;
125
+ duration: number;
126
+ format: string;
127
+ headers: Rec<string>;
128
+ status: number;
129
+ };
130
+ }>;
131
+ };
132
+ content: string;
133
+ content_encoding: string;
134
+ content_type: string;
135
+ cookies: Array<{
136
+ name: string;
137
+ value: string;
138
+ expires: string;
139
+ path: string;
140
+ comment: string;
141
+ domain: string;
142
+ max_age: number;
143
+ secure: boolean;
144
+ http_only: boolean;
145
+ version: string;
146
+ size: number;
147
+ }>;
148
+ data?: Rec<any>;
149
+ dns?: Rec<Array<Rec<any>>>;
150
+ duration: number;
151
+ error?: {
152
+ code: string;
153
+ http_code: number;
154
+ links: Rec<string>;
155
+ message: string;
156
+ retryable: boolean;
157
+ doc_url?: string;
158
+ };
159
+ format: string;
160
+ iframes: Array<{
161
+ url: string;
162
+ uri: {
163
+ root_domain: string;
164
+ base_url: string;
165
+ host: string;
166
+ scheme: string;
167
+ query?: string;
168
+ fragment?: string;
169
+ port: number;
170
+ params?: Rec<string>;
171
+ };
172
+ content: string;
173
+ }>;
174
+ log_url: string;
175
+ reason: string;
176
+ request_headers: Rec<string>;
177
+ response_headers: Rec<string>;
178
+ screenshots: Rec<{
179
+ css_selector?: string;
180
+ extension: string;
181
+ format: string;
182
+ size: number;
183
+ url: string;
184
+ }>;
185
+ size: number;
186
+ ssl?: {
187
+ certs: Array<Rec<any>>;
188
+ };
189
+ status: string;
190
+ status_code: number;
191
+ success: boolean;
192
+ url: string;
193
+ };
194
+
195
+ export class ScrapeResult {
196
+ config: ConfigData;
197
+ context: ContextData;
198
+ result: ResultData;
199
+ uuid: string;
200
+ private _selector: cheerio.CheerioAPI | undefined;
201
+
202
+ constructor(data: { config: ConfigData; context: ContextData; result: ResultData; uuid: string }) {
203
+ this.config = data.config;
204
+ this.context = data.context;
205
+ this.result = data.result;
206
+ this.uuid = data.uuid;
207
+ this._selector = undefined;
208
+ }
209
+
210
+ get selector(): cheerio.CheerioAPI {
211
+ if (!this._selector) {
212
+ const headers = normalizeHeaders(this.result.response_headers);
213
+ if (!headers['content-type'].includes('text/html')) {
214
+ throw new errors.ContentTypeError(
215
+ `Cannot use selector on non-html content-type, received: ${this.result.response_headers['content-type']}`,
216
+ );
217
+ }
218
+ this._selector = cheerio.load(this.result.content);
219
+ }
220
+ return this._selector;
221
+ }
222
+ }
223
+
224
+ export type ScrapeResultData = Record<string | number | symbol, never>;
225
+
226
+ export type ClassifyOptions = {
227
+ url: string;
228
+ statusCode: number;
229
+ headers?: Record<string, string>;
230
+ body?: string | null;
231
+ method?: string;
232
+ };
233
+
234
+ export type ClassifyResult = {
235
+ blocked: boolean;
236
+ antibot: string | null;
237
+ cost: number;
238
+ };
239
+
240
+ export type AccountData = {
241
+ account: {
242
+ account_id: string;
243
+ currency: string;
244
+ timezone: string;
245
+ };
246
+ project: {
247
+ allow_extra_usage: boolean;
248
+ allowed_networks: Array<string>;
249
+ budget_limit: any;
250
+ budget_spent: any;
251
+ concurrency_limit?: number;
252
+ name: string;
253
+ quota_reached: boolean;
254
+ scrape_request_count: number;
255
+ scrape_request_limit: number;
256
+ tags: Array<string>;
257
+ };
258
+ subscription: {
259
+ billing: {
260
+ current_extra_scrape_request_price: { currency: string; amount: number };
261
+ extra_scrape_request_price_per_10k: { currency: string; amount: number };
262
+ ongoing_payment: { currency: string; amount: number };
263
+ plan_price: { currency: string; amount: number };
264
+ };
265
+ extra_scrape_allowed: boolean;
266
+ max_concurrency: number;
267
+ period: {
268
+ start: string;
269
+ end: string;
270
+ };
271
+ plan_name: string;
272
+ usage: {
273
+ schedule: { current: number; limit: number };
274
+ spider: { current: number; limit: number };
275
+ scrape: {
276
+ concurrent_limit: number;
277
+ concurrent_remaining: number;
278
+ concurrent_usage: number;
279
+ current: number;
280
+ extra: number;
281
+ limit: number;
282
+ remaining: number;
283
+ };
284
+ };
285
+ };
286
+ };
287
+
288
+ export type ScreenshotMetadata = {
289
+ extension_name: string;
290
+ upstream_status_code: number;
291
+ upstream_url: string;
292
+ };
293
+
294
+ export class ScreenshotResult {
295
+ image: ArrayBuffer;
296
+ metadata: ScreenshotMetadata;
297
+ result: object | null;
298
+
299
+ constructor(response: Response, data: ArrayBuffer) {
300
+ this.image = data;
301
+ this.metadata = this.defineMetadata(response);
302
+ this.result = this.decodeResponse(response, data); // raw result
303
+ }
304
+
305
+ private defineMetadata(response: Response): ScreenshotMetadata {
306
+ const headers = normalizeHeaders(response.headers);
307
+ const contentType = headers['content-type'];
308
+ let extension_name = '';
309
+ if (contentType) {
310
+ extension_name = contentType.split('/')[1].split(';')[0];
311
+ }
312
+ return {
313
+ extension_name: extension_name,
314
+ upstream_status_code: parseInt(headers['x-scrapfly-upstream-http-code'] || '200', 10),
315
+ upstream_url: headers['x-scrapfly-upstream-url'] || '',
316
+ };
317
+ }
318
+
319
+ private decodeResponse(response: Response, data: ArrayBuffer): object | null {
320
+ const headers = normalizeHeaders(response.headers);
321
+ if (headers['content-type'] === 'json') {
322
+ return JSON.parse(new TextDecoder().decode(data));
323
+ }
324
+ return null;
325
+ }
326
+ }
327
+
328
+ export class ExtractionResult {
329
+ data: string;
330
+ content_type: string;
331
+ data_quality?: string;
332
+
333
+ constructor(response: { data: string; content_type: string; data_quality?: string }) {
334
+ this.data = response.data;
335
+ this.content_type = response.content_type;
336
+ this.data_quality = response.data_quality;
337
+ }
338
+ }