scrapfly-sdk 0.7.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/browserconfig.d.ts +59 -0
- package/esm/browserconfig.d.ts.map +1 -0
- package/esm/browserconfig.js +207 -0
- package/esm/client.d.ts +193 -1
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +786 -5
- package/esm/crawl.d.ts +118 -0
- package/esm/crawl.d.ts.map +1 -0
- package/esm/crawl.js +230 -0
- package/esm/crawlerconfig.d.ts +119 -0
- package/esm/crawlerconfig.d.ts.map +1 -0
- package/esm/crawlerconfig.js +398 -0
- package/esm/crawlerresult.d.ts +253 -0
- package/esm/crawlerresult.d.ts.map +1 -0
- package/esm/crawlerresult.js +327 -0
- package/esm/errors.d.ts +4 -0
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js +6 -0
- package/esm/extractionconfig.d.ts +2 -0
- package/esm/extractionconfig.d.ts.map +1 -1
- package/esm/extractionconfig.js +10 -0
- package/esm/main.d.ts +8 -1
- package/esm/main.d.ts.map +1 -1
- package/esm/main.js +6 -1
- package/esm/polyfill.d.ts +2 -0
- package/esm/polyfill.d.ts.map +1 -1
- package/esm/polyfill.js +12 -0
- package/esm/scrapeconfig.d.ts +27 -0
- package/esm/scrapeconfig.d.ts.map +1 -1
- package/esm/scrapeconfig.js +44 -1
- package/esm/screenshotconfig.d.ts +2 -2
- package/esm/screenshotconfig.d.ts.map +1 -1
- package/esm/utils.d.ts.map +1 -1
- package/esm/utils.js +16 -2
- package/esm/webhookserver.d.ts +1 -0
- package/esm/webhookserver.d.ts.map +1 -1
- package/esm/webhookserver.js +1 -0
- package/package.json +1 -1
- package/script/browserconfig.d.ts +59 -0
- package/script/browserconfig.d.ts.map +1 -0
- package/script/browserconfig.js +211 -0
- package/script/client.d.ts +193 -1
- package/script/client.d.ts.map +1 -1
- package/script/client.js +785 -4
- package/script/crawl.d.ts +118 -0
- package/script/crawl.d.ts.map +1 -0
- package/script/crawl.js +257 -0
- package/script/crawlerconfig.d.ts +119 -0
- package/script/crawlerconfig.d.ts.map +1 -0
- package/script/crawlerconfig.js +402 -0
- package/script/crawlerresult.d.ts +253 -0
- package/script/crawlerresult.d.ts.map +1 -0
- package/script/crawlerresult.js +335 -0
- package/script/errors.d.ts +4 -0
- package/script/errors.d.ts.map +1 -1
- package/script/errors.js +9 -1
- package/script/extractionconfig.d.ts +2 -0
- package/script/extractionconfig.d.ts.map +1 -1
- package/script/extractionconfig.js +10 -0
- package/script/main.d.ts +8 -1
- package/script/main.d.ts.map +1 -1
- package/script/main.js +17 -1
- package/script/polyfill.d.ts +2 -0
- package/script/polyfill.d.ts.map +1 -1
- package/script/polyfill.js +14 -1
- package/script/scrapeconfig.d.ts +27 -0
- package/script/scrapeconfig.d.ts.map +1 -1
- package/script/scrapeconfig.js +44 -1
- package/script/screenshotconfig.d.ts +2 -2
- package/script/screenshotconfig.d.ts.map +1 -1
- package/script/utils.d.ts.map +1 -1
- package/script/utils.js +16 -2
- package/script/webhookserver.d.ts +1 -0
- package/script/webhookserver.d.ts.map +1 -1
- package/script/webhookserver.js +2 -1
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
import { CrawlerConfigError } from './errors.js';
|
|
2
|
+
const VALID_CONTENT_FORMATS = new Set([
|
|
3
|
+
'html',
|
|
4
|
+
'clean_html',
|
|
5
|
+
'markdown',
|
|
6
|
+
'text',
|
|
7
|
+
'json',
|
|
8
|
+
'extracted_data',
|
|
9
|
+
'page_metadata',
|
|
10
|
+
]);
|
|
11
|
+
const VALID_WEBHOOK_EVENTS = new Set([
|
|
12
|
+
'crawler_started',
|
|
13
|
+
'crawler_url_visited',
|
|
14
|
+
'crawler_url_skipped',
|
|
15
|
+
'crawler_url_discovered',
|
|
16
|
+
'crawler_url_failed',
|
|
17
|
+
'crawler_stopped',
|
|
18
|
+
'crawler_cancelled',
|
|
19
|
+
'crawler_finished',
|
|
20
|
+
]);
|
|
21
|
+
/**
|
|
22
|
+
* Configuration for a Scrapfly Crawler API job.
|
|
23
|
+
*
|
|
24
|
+
* Mirrors the documented `POST /crawl` request body field-for-field. Fields left
|
|
25
|
+
* unset are dropped from the serialized output so the server applies its own defaults.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* const config = new CrawlerConfig({
|
|
30
|
+
* url: 'https://web-scraping.dev/products',
|
|
31
|
+
* page_limit: 10,
|
|
32
|
+
* content_formats: ['markdown'],
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export class CrawlerConfig {
|
|
37
|
+
constructor(options) {
|
|
38
|
+
Object.defineProperty(this, "url", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: void 0
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(this, "page_limit", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: void 0
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(this, "max_depth", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: void 0
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(this, "max_duration", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true,
|
|
59
|
+
writable: true,
|
|
60
|
+
value: void 0
|
|
61
|
+
});
|
|
62
|
+
Object.defineProperty(this, "max_api_credit", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: void 0
|
|
67
|
+
});
|
|
68
|
+
Object.defineProperty(this, "exclude_paths", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: void 0
|
|
73
|
+
});
|
|
74
|
+
Object.defineProperty(this, "include_only_paths", {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
configurable: true,
|
|
77
|
+
writable: true,
|
|
78
|
+
value: void 0
|
|
79
|
+
});
|
|
80
|
+
Object.defineProperty(this, "ignore_base_path_restriction", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true,
|
|
84
|
+
value: void 0
|
|
85
|
+
});
|
|
86
|
+
Object.defineProperty(this, "follow_external_links", {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
configurable: true,
|
|
89
|
+
writable: true,
|
|
90
|
+
value: void 0
|
|
91
|
+
});
|
|
92
|
+
Object.defineProperty(this, "allowed_external_domains", {
|
|
93
|
+
enumerable: true,
|
|
94
|
+
configurable: true,
|
|
95
|
+
writable: true,
|
|
96
|
+
value: void 0
|
|
97
|
+
});
|
|
98
|
+
Object.defineProperty(this, "follow_internal_subdomains", {
|
|
99
|
+
enumerable: true,
|
|
100
|
+
configurable: true,
|
|
101
|
+
writable: true,
|
|
102
|
+
value: void 0
|
|
103
|
+
});
|
|
104
|
+
Object.defineProperty(this, "allowed_internal_subdomains", {
|
|
105
|
+
enumerable: true,
|
|
106
|
+
configurable: true,
|
|
107
|
+
writable: true,
|
|
108
|
+
value: void 0
|
|
109
|
+
});
|
|
110
|
+
Object.defineProperty(this, "headers", {
|
|
111
|
+
enumerable: true,
|
|
112
|
+
configurable: true,
|
|
113
|
+
writable: true,
|
|
114
|
+
value: void 0
|
|
115
|
+
});
|
|
116
|
+
Object.defineProperty(this, "delay", {
|
|
117
|
+
enumerable: true,
|
|
118
|
+
configurable: true,
|
|
119
|
+
writable: true,
|
|
120
|
+
value: void 0
|
|
121
|
+
});
|
|
122
|
+
Object.defineProperty(this, "user_agent", {
|
|
123
|
+
enumerable: true,
|
|
124
|
+
configurable: true,
|
|
125
|
+
writable: true,
|
|
126
|
+
value: void 0
|
|
127
|
+
});
|
|
128
|
+
Object.defineProperty(this, "max_concurrency", {
|
|
129
|
+
enumerable: true,
|
|
130
|
+
configurable: true,
|
|
131
|
+
writable: true,
|
|
132
|
+
value: void 0
|
|
133
|
+
});
|
|
134
|
+
Object.defineProperty(this, "rendering_delay", {
|
|
135
|
+
enumerable: true,
|
|
136
|
+
configurable: true,
|
|
137
|
+
writable: true,
|
|
138
|
+
value: void 0
|
|
139
|
+
});
|
|
140
|
+
Object.defineProperty(this, "use_sitemaps", {
|
|
141
|
+
enumerable: true,
|
|
142
|
+
configurable: true,
|
|
143
|
+
writable: true,
|
|
144
|
+
value: void 0
|
|
145
|
+
});
|
|
146
|
+
Object.defineProperty(this, "respect_robots_txt", {
|
|
147
|
+
enumerable: true,
|
|
148
|
+
configurable: true,
|
|
149
|
+
writable: true,
|
|
150
|
+
value: void 0
|
|
151
|
+
});
|
|
152
|
+
Object.defineProperty(this, "ignore_no_follow", {
|
|
153
|
+
enumerable: true,
|
|
154
|
+
configurable: true,
|
|
155
|
+
writable: true,
|
|
156
|
+
value: void 0
|
|
157
|
+
});
|
|
158
|
+
Object.defineProperty(this, "cache", {
|
|
159
|
+
enumerable: true,
|
|
160
|
+
configurable: true,
|
|
161
|
+
writable: true,
|
|
162
|
+
value: void 0
|
|
163
|
+
});
|
|
164
|
+
Object.defineProperty(this, "cache_ttl", {
|
|
165
|
+
enumerable: true,
|
|
166
|
+
configurable: true,
|
|
167
|
+
writable: true,
|
|
168
|
+
value: void 0
|
|
169
|
+
});
|
|
170
|
+
Object.defineProperty(this, "cache_clear", {
|
|
171
|
+
enumerable: true,
|
|
172
|
+
configurable: true,
|
|
173
|
+
writable: true,
|
|
174
|
+
value: void 0
|
|
175
|
+
});
|
|
176
|
+
Object.defineProperty(this, "content_formats", {
|
|
177
|
+
enumerable: true,
|
|
178
|
+
configurable: true,
|
|
179
|
+
writable: true,
|
|
180
|
+
value: void 0
|
|
181
|
+
});
|
|
182
|
+
Object.defineProperty(this, "extraction_rules", {
|
|
183
|
+
enumerable: true,
|
|
184
|
+
configurable: true,
|
|
185
|
+
writable: true,
|
|
186
|
+
value: void 0
|
|
187
|
+
});
|
|
188
|
+
Object.defineProperty(this, "asp", {
|
|
189
|
+
enumerable: true,
|
|
190
|
+
configurable: true,
|
|
191
|
+
writable: true,
|
|
192
|
+
value: void 0
|
|
193
|
+
});
|
|
194
|
+
Object.defineProperty(this, "proxy_pool", {
|
|
195
|
+
enumerable: true,
|
|
196
|
+
configurable: true,
|
|
197
|
+
writable: true,
|
|
198
|
+
value: void 0
|
|
199
|
+
});
|
|
200
|
+
Object.defineProperty(this, "country", {
|
|
201
|
+
enumerable: true,
|
|
202
|
+
configurable: true,
|
|
203
|
+
writable: true,
|
|
204
|
+
value: void 0
|
|
205
|
+
});
|
|
206
|
+
Object.defineProperty(this, "webhook_name", {
|
|
207
|
+
enumerable: true,
|
|
208
|
+
configurable: true,
|
|
209
|
+
writable: true,
|
|
210
|
+
value: void 0
|
|
211
|
+
});
|
|
212
|
+
Object.defineProperty(this, "webhook_events", {
|
|
213
|
+
enumerable: true,
|
|
214
|
+
configurable: true,
|
|
215
|
+
writable: true,
|
|
216
|
+
value: void 0
|
|
217
|
+
});
|
|
218
|
+
this.validateOptions(options);
|
|
219
|
+
if (typeof options.url !== 'string' || options.url.trim() === '') {
|
|
220
|
+
throw new CrawlerConfigError('url is required and must be a non-empty string');
|
|
221
|
+
}
|
|
222
|
+
this.url = options.url;
|
|
223
|
+
// Mutually exclusive path filters
|
|
224
|
+
if (options.exclude_paths && options.include_only_paths) {
|
|
225
|
+
throw new CrawlerConfigError('exclude_paths and include_only_paths are mutually exclusive');
|
|
226
|
+
}
|
|
227
|
+
// Bounds checks (mirroring the public docs)
|
|
228
|
+
if (options.page_limit !== undefined && options.page_limit < 0) {
|
|
229
|
+
throw new CrawlerConfigError('page_limit must be >= 0 (0 = unlimited)');
|
|
230
|
+
}
|
|
231
|
+
if (options.max_depth !== undefined && options.max_depth < 0) {
|
|
232
|
+
throw new CrawlerConfigError('max_depth must be >= 0');
|
|
233
|
+
}
|
|
234
|
+
if (options.rendering_delay !== undefined && (options.rendering_delay < 0 || options.rendering_delay > 25000)) {
|
|
235
|
+
throw new CrawlerConfigError('rendering_delay must be between 0 and 25000 (ms)');
|
|
236
|
+
}
|
|
237
|
+
if (options.delay !== undefined && (options.delay < 0 || options.delay > 15000)) {
|
|
238
|
+
throw new CrawlerConfigError('delay must be between 0 and 15000 (ms)');
|
|
239
|
+
}
|
|
240
|
+
if (options.cache_ttl !== undefined && (options.cache_ttl < 0 || options.cache_ttl > 604800)) {
|
|
241
|
+
throw new CrawlerConfigError('cache_ttl must be between 0 and 604800 (seconds, max 7 days)');
|
|
242
|
+
}
|
|
243
|
+
if (options.max_duration !== undefined && (options.max_duration < 15 || options.max_duration > 10800)) {
|
|
244
|
+
throw new CrawlerConfigError('max_duration must be between 15 and 10800 (seconds)');
|
|
245
|
+
}
|
|
246
|
+
if (options.max_api_credit !== undefined && options.max_api_credit < 0) {
|
|
247
|
+
throw new CrawlerConfigError('max_api_credit must be >= 0 (0 = no limit)');
|
|
248
|
+
}
|
|
249
|
+
if (options.exclude_paths && options.exclude_paths.length > 100) {
|
|
250
|
+
throw new CrawlerConfigError('exclude_paths is limited to 100 entries');
|
|
251
|
+
}
|
|
252
|
+
if (options.include_only_paths && options.include_only_paths.length > 100) {
|
|
253
|
+
throw new CrawlerConfigError('include_only_paths is limited to 100 entries');
|
|
254
|
+
}
|
|
255
|
+
if (options.allowed_external_domains && options.allowed_external_domains.length > 250) {
|
|
256
|
+
throw new CrawlerConfigError('allowed_external_domains is limited to 250 entries');
|
|
257
|
+
}
|
|
258
|
+
if (options.allowed_internal_subdomains && options.allowed_internal_subdomains.length > 250) {
|
|
259
|
+
throw new CrawlerConfigError('allowed_internal_subdomains is limited to 250 entries');
|
|
260
|
+
}
|
|
261
|
+
// Validate content_formats values
|
|
262
|
+
if (options.content_formats) {
|
|
263
|
+
for (const fmt of options.content_formats) {
|
|
264
|
+
if (!VALID_CONTENT_FORMATS.has(fmt)) {
|
|
265
|
+
throw new CrawlerConfigError(`Invalid content_formats value: ${fmt}`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// Validate webhook_events values
|
|
270
|
+
if (options.webhook_events) {
|
|
271
|
+
for (const ev of options.webhook_events) {
|
|
272
|
+
if (!VALID_WEBHOOK_EVENTS.has(ev)) {
|
|
273
|
+
throw new CrawlerConfigError(`Invalid webhook_events value: ${ev}`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
// Assign all the optional fields by reference
|
|
278
|
+
this.page_limit = options.page_limit;
|
|
279
|
+
this.max_depth = options.max_depth;
|
|
280
|
+
this.max_duration = options.max_duration;
|
|
281
|
+
this.max_api_credit = options.max_api_credit;
|
|
282
|
+
this.exclude_paths = options.exclude_paths;
|
|
283
|
+
this.include_only_paths = options.include_only_paths;
|
|
284
|
+
this.ignore_base_path_restriction = options.ignore_base_path_restriction;
|
|
285
|
+
this.follow_external_links = options.follow_external_links;
|
|
286
|
+
this.allowed_external_domains = options.allowed_external_domains;
|
|
287
|
+
this.follow_internal_subdomains = options.follow_internal_subdomains;
|
|
288
|
+
this.allowed_internal_subdomains = options.allowed_internal_subdomains;
|
|
289
|
+
this.headers = options.headers;
|
|
290
|
+
this.delay = options.delay;
|
|
291
|
+
this.user_agent = options.user_agent;
|
|
292
|
+
this.max_concurrency = options.max_concurrency;
|
|
293
|
+
this.rendering_delay = options.rendering_delay;
|
|
294
|
+
this.use_sitemaps = options.use_sitemaps;
|
|
295
|
+
this.respect_robots_txt = options.respect_robots_txt;
|
|
296
|
+
this.ignore_no_follow = options.ignore_no_follow;
|
|
297
|
+
this.cache = options.cache;
|
|
298
|
+
this.cache_ttl = options.cache_ttl;
|
|
299
|
+
this.cache_clear = options.cache_clear;
|
|
300
|
+
this.content_formats = options.content_formats;
|
|
301
|
+
this.extraction_rules = options.extraction_rules;
|
|
302
|
+
this.asp = options.asp;
|
|
303
|
+
this.proxy_pool = options.proxy_pool;
|
|
304
|
+
this.country = options.country;
|
|
305
|
+
this.webhook_name = options.webhook_name;
|
|
306
|
+
this.webhook_events = options.webhook_events;
|
|
307
|
+
}
|
|
308
|
+
validateOptions(options) {
|
|
309
|
+
const validKeys = new Set([
|
|
310
|
+
'url',
|
|
311
|
+
'page_limit',
|
|
312
|
+
'max_depth',
|
|
313
|
+
'max_duration',
|
|
314
|
+
'max_api_credit',
|
|
315
|
+
'exclude_paths',
|
|
316
|
+
'include_only_paths',
|
|
317
|
+
'ignore_base_path_restriction',
|
|
318
|
+
'follow_external_links',
|
|
319
|
+
'allowed_external_domains',
|
|
320
|
+
'follow_internal_subdomains',
|
|
321
|
+
'allowed_internal_subdomains',
|
|
322
|
+
'headers',
|
|
323
|
+
'delay',
|
|
324
|
+
'user_agent',
|
|
325
|
+
'max_concurrency',
|
|
326
|
+
'rendering_delay',
|
|
327
|
+
'use_sitemaps',
|
|
328
|
+
'respect_robots_txt',
|
|
329
|
+
'ignore_no_follow',
|
|
330
|
+
'cache',
|
|
331
|
+
'cache_ttl',
|
|
332
|
+
'cache_clear',
|
|
333
|
+
'content_formats',
|
|
334
|
+
'extraction_rules',
|
|
335
|
+
'asp',
|
|
336
|
+
'proxy_pool',
|
|
337
|
+
'country',
|
|
338
|
+
'webhook_name',
|
|
339
|
+
'webhook_events',
|
|
340
|
+
]);
|
|
341
|
+
for (const key in options) {
|
|
342
|
+
if (!validKeys.has(key)) {
|
|
343
|
+
throw new CrawlerConfigError(`Invalid option provided: ${key}`);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Serialize to a flat object suitable for `JSON.stringify()` as a `POST /crawl` body.
|
|
349
|
+
*
|
|
350
|
+
* Unlike `ScrapeConfig.toApiParams()`, the crawler API takes its config as a JSON
|
|
351
|
+
* **body**, not URL query parameters. The API key is therefore NOT included here —
|
|
352
|
+
* it's added to the URL by the client.
|
|
353
|
+
*
|
|
354
|
+
* Fields left undefined are dropped so the server applies its own documented
|
|
355
|
+
* defaults (e.g. `respect_robots_txt=true`).
|
|
356
|
+
*/
|
|
357
|
+
toApiParams() {
|
|
358
|
+
const params = { url: this.url };
|
|
359
|
+
const fields = [
|
|
360
|
+
'page_limit',
|
|
361
|
+
'max_depth',
|
|
362
|
+
'max_duration',
|
|
363
|
+
'max_api_credit',
|
|
364
|
+
'exclude_paths',
|
|
365
|
+
'include_only_paths',
|
|
366
|
+
'ignore_base_path_restriction',
|
|
367
|
+
'follow_external_links',
|
|
368
|
+
'allowed_external_domains',
|
|
369
|
+
'follow_internal_subdomains',
|
|
370
|
+
'allowed_internal_subdomains',
|
|
371
|
+
'headers',
|
|
372
|
+
'delay',
|
|
373
|
+
'user_agent',
|
|
374
|
+
'max_concurrency',
|
|
375
|
+
'rendering_delay',
|
|
376
|
+
'use_sitemaps',
|
|
377
|
+
'respect_robots_txt',
|
|
378
|
+
'ignore_no_follow',
|
|
379
|
+
'cache',
|
|
380
|
+
'cache_ttl',
|
|
381
|
+
'cache_clear',
|
|
382
|
+
'content_formats',
|
|
383
|
+
'extraction_rules',
|
|
384
|
+
'asp',
|
|
385
|
+
'proxy_pool',
|
|
386
|
+
'country',
|
|
387
|
+
'webhook_name',
|
|
388
|
+
'webhook_events',
|
|
389
|
+
];
|
|
390
|
+
for (const key of fields) {
|
|
391
|
+
const v = this[key];
|
|
392
|
+
if (v !== undefined) {
|
|
393
|
+
params[key] = v;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return params;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Crawler job status values returned by the API.
|
|
3
|
+
*
|
|
4
|
+
* Note: there is **no** `COMPLETED` or `FAILED` value. A finished crawl always
|
|
5
|
+
* has `status === 'DONE'`; success vs. failure is signaled by `is_success`.
|
|
6
|
+
* `CANCELLED` is a separate terminal state set when the user cancels the job.
|
|
7
|
+
*/
|
|
8
|
+
export type CrawlerStatusValue = 'PENDING' | 'RUNNING' | 'DONE' | 'CANCELLED';
|
|
9
|
+
/**
|
|
10
|
+
* Reasons documented for why a crawler stopped.
|
|
11
|
+
*/
|
|
12
|
+
export type CrawlerStopReason = 'no_more_urls' | 'page_limit' | 'max_duration' | 'max_api_credit' | 'seed_url_failed' | 'user_cancelled' | 'crawler_error' | 'no_api_credit_left' | 'storage_error';
|
|
13
|
+
/**
|
|
14
|
+
* Per-job state metrics returned in the status response and webhook payloads.
|
|
15
|
+
*
|
|
16
|
+
* Note on nullable fields: while the crawler is in `PENDING` (before any
|
|
17
|
+
* worker has picked up the job), `start_time`, `stop_time`, and `stop_reason`
|
|
18
|
+
* are all `null`. They become populated once the crawl starts progressing.
|
|
19
|
+
* `duration` is always a number (0 when nothing has happened yet).
|
|
20
|
+
*/
|
|
21
|
+
export interface CrawlerState {
|
|
22
|
+
urls_visited: number;
|
|
23
|
+
urls_extracted: number;
|
|
24
|
+
urls_failed: number;
|
|
25
|
+
urls_skipped: number;
|
|
26
|
+
urls_to_crawl: number;
|
|
27
|
+
api_credit_used: number;
|
|
28
|
+
duration: number;
|
|
29
|
+
/** `null` until the crawler stops. */
|
|
30
|
+
stop_reason: CrawlerStopReason | null;
|
|
31
|
+
/** `null` until the crawler actually starts processing (still PENDING). */
|
|
32
|
+
start_time: number | null;
|
|
33
|
+
/** `null` until the crawler stops. */
|
|
34
|
+
stop_time: number | null;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Wraps the JSON response of `GET /crawl/{uuid}/status`.
|
|
38
|
+
*
|
|
39
|
+
* Strict parsing: every field documented in the public API contract is
|
|
40
|
+
* required. A missing or wrong-typed field throws a clear error at parse
|
|
41
|
+
* time so API contract drift surfaces loud and early instead of silently
|
|
42
|
+
* producing zero-valued metrics.
|
|
43
|
+
*/
|
|
44
|
+
export declare class CrawlerStatus {
|
|
45
|
+
crawler_uuid: string;
|
|
46
|
+
status: CrawlerStatusValue;
|
|
47
|
+
is_finished: boolean;
|
|
48
|
+
/** `null` while the crawler is still running; bool once terminal. */
|
|
49
|
+
is_success: boolean | null;
|
|
50
|
+
state: CrawlerState;
|
|
51
|
+
constructor(data: Record<string, any>);
|
|
52
|
+
/** True while the crawler is still working (PENDING or RUNNING). */
|
|
53
|
+
get isRunning(): boolean;
|
|
54
|
+
/** True if the crawler reached a terminal state successfully. */
|
|
55
|
+
get isComplete(): boolean;
|
|
56
|
+
/** True if the crawler reached a terminal state but failed. */
|
|
57
|
+
get isFailed(): boolean;
|
|
58
|
+
/** True if the crawler was cancelled by the user. */
|
|
59
|
+
get isCancelled(): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Rough progress estimate based on visited vs. extracted URLs (0–100).
|
|
62
|
+
* Returns 0 when nothing has been extracted yet.
|
|
63
|
+
*/
|
|
64
|
+
get progressPct(): number;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Single URL entry from `GET /crawl/{uuid}/urls`.
|
|
68
|
+
*
|
|
69
|
+
* The endpoint streams one record per line as `text/plain`. For `visited`
|
|
70
|
+
* URLs each line is just the URL; for `failed` or `skipped` URLs the line is
|
|
71
|
+
* `url,reason` (reason is the first token after the first comma — URLs never
|
|
72
|
+
* contain a comma once percent-encoded, so this is unambiguous).
|
|
73
|
+
*
|
|
74
|
+
* Streaming text is used because the endpoint is expected to scale to
|
|
75
|
+
* millions of URLs per job; JSON is not a suitable wire format at that
|
|
76
|
+
* volume.
|
|
77
|
+
*/
|
|
78
|
+
export interface CrawlerUrlEntry {
|
|
79
|
+
url: string;
|
|
80
|
+
/** Only populated when the caller passed an explicit `status` filter. */
|
|
81
|
+
status?: 'visited' | 'pending' | 'failed' | 'skipped';
|
|
82
|
+
/** Only present for `failed` or `skipped` URLs. */
|
|
83
|
+
reason?: string;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Wraps the streaming text response of `GET /crawl/{uuid}/urls`.
|
|
87
|
+
*
|
|
88
|
+
* The server returns one record per line with `Content-Type: text/plain`;
|
|
89
|
+
* this class parses that stream into a materialised list for caller
|
|
90
|
+
* convenience. For very large jobs, prefer {@link ScrapflyClient.crawlUrlsStream}
|
|
91
|
+
* (if provided) or call the endpoint via `client.fetch` directly and iterate
|
|
92
|
+
* the body as a stream.
|
|
93
|
+
*
|
|
94
|
+
* Pagination metadata fields (`page`, `per_page`) are echoes of the caller's
|
|
95
|
+
* request parameters — the wire protocol carries no global `total`, only the
|
|
96
|
+
* records in the current page. Use `urls.length` for the page size and
|
|
97
|
+
* request further pages by incrementing `page` until an empty response.
|
|
98
|
+
*/
|
|
99
|
+
export declare class CrawlerUrls {
|
|
100
|
+
urls: CrawlerUrlEntry[];
|
|
101
|
+
page: number;
|
|
102
|
+
per_page: number;
|
|
103
|
+
constructor(urls: CrawlerUrlEntry[], page: number, perPage: number);
|
|
104
|
+
/**
|
|
105
|
+
* Parse the raw text body returned by `GET /crawl/{uuid}/urls`.
|
|
106
|
+
*
|
|
107
|
+
* - Empty lines are ignored (trailing newlines, blank records).
|
|
108
|
+
* - `visited` status → one URL per line, record has `{url, status: 'visited'}`.
|
|
109
|
+
* - `failed`/`skipped` status → `url,reason`, record includes `reason`.
|
|
110
|
+
* - When the caller didn't pass a `status` filter, the server defaults to
|
|
111
|
+
* `visited`; we pass that as the `statusHint` so every parsed record gets
|
|
112
|
+
* the right status tag.
|
|
113
|
+
*
|
|
114
|
+
* @param body Raw text body of the response.
|
|
115
|
+
* @param statusHint The status filter passed on the request (for tagging).
|
|
116
|
+
* @param page Caller-provided page (echoed on the response object).
|
|
117
|
+
* @param perPage Caller-provided per_page (echoed on the response object).
|
|
118
|
+
*/
|
|
119
|
+
static fromText(body: string, statusHint: 'visited' | 'pending' | 'failed' | 'skipped', page: number, perPage: number): CrawlerUrls;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Wraps the JSON-mode response of `GET /crawl/{uuid}/contents` (i.e. `plain=false`).
|
|
123
|
+
*
|
|
124
|
+
* The `contents` field maps each URL to a map of format → content. When
|
|
125
|
+
* `plain=true` is used instead, {@link ScrapflyClient.crawlContents} returns
|
|
126
|
+
* a raw `string` directly rather than this class.
|
|
127
|
+
*
|
|
128
|
+
* Strict parsing: `contents` and `links` are both required per the documented
|
|
129
|
+
* contract. A response missing either field indicates an API contract change
|
|
130
|
+
* and throws at parse time rather than producing an empty envelope that looks
|
|
131
|
+
* valid.
|
|
132
|
+
*/
|
|
133
|
+
export declare class CrawlerContents {
|
|
134
|
+
contents: Record<string, Record<string, string>>;
|
|
135
|
+
links: {
|
|
136
|
+
crawled_urls?: string;
|
|
137
|
+
next?: string | null;
|
|
138
|
+
prev?: string | null;
|
|
139
|
+
};
|
|
140
|
+
constructor(data: Record<string, any>);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Artifact type accepted by `GET /crawl/{uuid}/artifact?type=...`.
|
|
144
|
+
*/
|
|
145
|
+
export type CrawlerArtifactType = 'warc' | 'har';
|
|
146
|
+
/**
|
|
147
|
+
* Holds the raw bytes of a downloaded WARC or HAR artifact.
|
|
148
|
+
*
|
|
149
|
+
* **Note on parsing:** the SDK does NOT bundle WARC or HAR parsers. Use a dedicated
|
|
150
|
+
* library (e.g. `warcio` on npm for WARC) if you need to walk the records. The
|
|
151
|
+
* `save()` method is provided for the common case of writing the artifact to disk.
|
|
152
|
+
*/
|
|
153
|
+
export declare class CrawlerArtifact {
|
|
154
|
+
type: CrawlerArtifactType;
|
|
155
|
+
data: Uint8Array;
|
|
156
|
+
constructor(type: CrawlerArtifactType, data: Uint8Array);
|
|
157
|
+
/** Write the artifact to a file on disk. */
|
|
158
|
+
save(filepath: string): Promise<void>;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Fields common to every crawler webhook payload.
|
|
162
|
+
*/
|
|
163
|
+
export interface CrawlerWebhookCommon {
|
|
164
|
+
crawler_uuid: string;
|
|
165
|
+
project: string;
|
|
166
|
+
env: string;
|
|
167
|
+
action: string;
|
|
168
|
+
state: CrawlerState;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Webhook for the four "lifecycle" events that share an identical payload shape:
|
|
172
|
+
* `crawler_started`, `crawler_stopped`, `crawler_cancelled`, `crawler_finished`.
|
|
173
|
+
*
|
|
174
|
+
* Verified against `apps/scrapfly/web-app/src/Template/Docs/crawler-api/webhooks_example/`.
|
|
175
|
+
*/
|
|
176
|
+
export interface CrawlerLifecycleWebhook {
|
|
177
|
+
event: 'crawler_started' | 'crawler_stopped' | 'crawler_cancelled' | 'crawler_finished';
|
|
178
|
+
payload: CrawlerWebhookCommon & {
|
|
179
|
+
seed_url: string;
|
|
180
|
+
links: {
|
|
181
|
+
status: string;
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
/** Payload for `crawler_url_visited` events. */
|
|
186
|
+
export interface CrawlerUrlVisitedWebhook {
|
|
187
|
+
event: 'crawler_url_visited';
|
|
188
|
+
payload: CrawlerWebhookCommon & {
|
|
189
|
+
url: string;
|
|
190
|
+
scrape: {
|
|
191
|
+
status_code: number;
|
|
192
|
+
country?: string;
|
|
193
|
+
log_uuid?: string;
|
|
194
|
+
log_url?: string;
|
|
195
|
+
content: Record<string, string>;
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/** Payload for `crawler_url_skipped` events. */
|
|
200
|
+
export interface CrawlerUrlSkippedWebhook {
|
|
201
|
+
event: 'crawler_url_skipped';
|
|
202
|
+
payload: CrawlerWebhookCommon & {
|
|
203
|
+
/** Map of skipped URL → reason (e.g. `"page_limit"`). */
|
|
204
|
+
urls: Record<string, string>;
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
/** Payload for `crawler_url_discovered` events. */
|
|
208
|
+
export interface CrawlerUrlDiscoveredWebhook {
|
|
209
|
+
event: 'crawler_url_discovered';
|
|
210
|
+
payload: CrawlerWebhookCommon & {
|
|
211
|
+
origin: string;
|
|
212
|
+
discovered_urls: string[];
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
/** Payload for `crawler_url_failed` events. */
|
|
216
|
+
export interface CrawlerUrlFailedWebhook {
|
|
217
|
+
event: 'crawler_url_failed';
|
|
218
|
+
payload: CrawlerWebhookCommon & {
|
|
219
|
+
url: string;
|
|
220
|
+
error: string;
|
|
221
|
+
scrape_config: Record<string, any>;
|
|
222
|
+
links: {
|
|
223
|
+
log: string | null;
|
|
224
|
+
};
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Discriminated union of every crawler webhook payload type.
|
|
229
|
+
*
|
|
230
|
+
* Use {@link parseCrawlerWebhook} to safely turn an `unknown` body into one of these.
|
|
231
|
+
*/
|
|
232
|
+
export type CrawlerWebhookPayload = CrawlerLifecycleWebhook | CrawlerUrlVisitedWebhook | CrawlerUrlSkippedWebhook | CrawlerUrlDiscoveredWebhook | CrawlerUrlFailedWebhook;
|
|
233
|
+
/**
|
|
234
|
+
* Parse a raw webhook body (JSON-decoded object) into a typed crawler webhook payload.
|
|
235
|
+
*
|
|
236
|
+
* Throws `Error` if the body is not an object, has no `event` field, or carries an
|
|
237
|
+
* unknown event name. Use this in your webhook handler after `JSON.parse()`.
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* import { parseCrawlerWebhook } from '@scrapfly/scrapfly-sdk';
|
|
242
|
+
*
|
|
243
|
+
* app.post('/webhook', async (req, res) => {
|
|
244
|
+
* const body = await req.json();
|
|
245
|
+
* const webhook = parseCrawlerWebhook(body);
|
|
246
|
+
* if (webhook.event === 'crawler_finished') {
|
|
247
|
+
* console.log('done', webhook.payload.state.urls_visited);
|
|
248
|
+
* }
|
|
249
|
+
* });
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
export declare function parseCrawlerWebhook(body: unknown): CrawlerWebhookPayload;
|
|
253
|
+
//# sourceMappingURL=crawlerresult.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crawlerresult.d.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