scrapfly-sdk 0.11.1 → 0.12.1

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 (101) hide show
  1. package/README.md +72 -5
  2. package/esm/browserconfig.d.ts +57 -9
  3. package/esm/browserconfig.d.ts.map +1 -1
  4. package/esm/browserconfig.js +63 -5
  5. package/esm/browserdownloads.d.ts +70 -0
  6. package/esm/browserdownloads.d.ts.map +1 -0
  7. package/esm/browserdownloads.js +215 -0
  8. package/esm/client.d.ts +224 -8
  9. package/esm/client.d.ts.map +1 -1
  10. package/esm/client.js +646 -62
  11. package/esm/crawl.d.ts +83 -1
  12. package/esm/crawl.d.ts.map +1 -1
  13. package/esm/crawl.js +73 -0
  14. package/esm/crawlerconfig.d.ts +54 -3
  15. package/esm/crawlerconfig.d.ts.map +1 -1
  16. package/esm/crawlerconfig.js +96 -2
  17. package/esm/crawlerresult.d.ts +374 -2
  18. package/esm/crawlerresult.d.ts.map +1 -1
  19. package/esm/crawlerresult.js +367 -1
  20. package/esm/errors.d.ts +12 -0
  21. package/esm/errors.d.ts.map +1 -1
  22. package/esm/errors.js +11 -0
  23. package/esm/main.d.ts +12 -7
  24. package/esm/main.d.ts.map +1 -1
  25. package/esm/main.js +10 -5
  26. package/esm/monitoringconfig.d.ts +2 -2
  27. package/esm/result.d.ts +11 -1
  28. package/esm/result.d.ts.map +1 -1
  29. package/esm/result.js +14 -5
  30. package/esm/scrapeconfig.d.ts +65 -1
  31. package/esm/scrapeconfig.d.ts.map +1 -1
  32. package/esm/scrapeconfig.js +88 -4
  33. package/esm/screenshotconfig.d.ts +9 -1
  34. package/esm/screenshotconfig.d.ts.map +1 -1
  35. package/esm/screenshotconfig.js +12 -1
  36. package/esm/types.d.ts +128 -0
  37. package/esm/types.d.ts.map +1 -1
  38. package/esm/types.js +22 -1
  39. package/esm/utils.d.ts.map +1 -1
  40. package/esm/utils.js +16 -2
  41. package/esm/webhookserver.d.ts +33 -15
  42. package/esm/webhookserver.d.ts.map +1 -1
  43. package/esm/webhookserver.js +197 -38
  44. package/package.json +1 -1
  45. package/script/browserconfig.d.ts +57 -9
  46. package/script/browserconfig.d.ts.map +1 -1
  47. package/script/browserconfig.js +63 -5
  48. package/script/browserdownloads.d.ts +70 -0
  49. package/script/browserdownloads.d.ts.map +1 -0
  50. package/script/browserdownloads.js +221 -0
  51. package/script/client.d.ts +224 -8
  52. package/script/client.d.ts.map +1 -1
  53. package/script/client.js +645 -61
  54. package/script/crawl.d.ts +83 -1
  55. package/script/crawl.d.ts.map +1 -1
  56. package/script/crawl.js +73 -0
  57. package/script/crawlerconfig.d.ts +54 -3
  58. package/script/crawlerconfig.d.ts.map +1 -1
  59. package/script/crawlerconfig.js +97 -3
  60. package/script/crawlerresult.d.ts +374 -2
  61. package/script/crawlerresult.d.ts.map +1 -1
  62. package/script/crawlerresult.js +374 -2
  63. package/script/errors.d.ts +12 -0
  64. package/script/errors.d.ts.map +1 -1
  65. package/script/errors.js +12 -1
  66. package/script/main.d.ts +12 -7
  67. package/script/main.d.ts.map +1 -1
  68. package/script/main.js +20 -4
  69. package/script/monitoringconfig.d.ts +2 -2
  70. package/script/result.d.ts +11 -1
  71. package/script/result.d.ts.map +1 -1
  72. package/script/result.js +14 -5
  73. package/script/scrapeconfig.d.ts +65 -1
  74. package/script/scrapeconfig.d.ts.map +1 -1
  75. package/script/scrapeconfig.js +88 -4
  76. package/script/screenshotconfig.d.ts +9 -1
  77. package/script/screenshotconfig.d.ts.map +1 -1
  78. package/script/screenshotconfig.js +12 -1
  79. package/script/types.d.ts +128 -0
  80. package/script/types.d.ts.map +1 -1
  81. package/script/types.js +23 -0
  82. package/script/utils.d.ts.map +1 -1
  83. package/script/utils.js +16 -2
  84. package/script/webhookserver.d.ts +33 -15
  85. package/script/webhookserver.d.ts.map +1 -1
  86. package/script/webhookserver.js +197 -38
  87. package/src/browserconfig.ts +74 -8
  88. package/src/browserdownloads.ts +266 -0
  89. package/src/client.ts +796 -65
  90. package/src/crawl.ts +106 -0
  91. package/src/crawlerconfig.ts +128 -5
  92. package/src/crawlerresult.ts +607 -3
  93. package/src/errors.ts +12 -0
  94. package/src/main.ts +55 -5
  95. package/src/monitoringconfig.ts +2 -2
  96. package/src/result.ts +17 -5
  97. package/src/scrapeconfig.ts +92 -3
  98. package/src/screenshotconfig.ts +14 -1
  99. package/src/types.ts +159 -1
  100. package/src/utils.ts +16 -2
  101. package/src/webhookserver.ts +235 -39
package/src/crawl.ts CHANGED
@@ -6,6 +6,13 @@ import type {
6
6
  CrawlerArtifact,
7
7
  CrawlerArtifactType,
8
8
  CrawlerContents,
9
+ CrawlerExtractResponse,
10
+ CrawlerPromptAnswer,
11
+ CrawlerPromptEvent,
12
+ CrawlerRefreshEntry,
13
+ CrawlerRefreshState,
14
+ CrawlerSearchMode,
15
+ CrawlerSearchResponse,
9
16
  CrawlerStatus,
10
17
  CrawlerUrls,
11
18
  } from './crawlerresult.js';
@@ -194,6 +201,105 @@ export class Crawl {
194
201
  return await this.client.crawlUrls(uuid, opts);
195
202
  }
196
203
 
204
+ /**
205
+ * Search this crawl's index.
206
+ *
207
+ * Sugar over `client.crawlSearch([uuid], ...)`. The cross-crawl call is the
208
+ * real endpoint; both share its implementation so single and multi-crawl
209
+ * search cannot drift.
210
+ *
211
+ * Requires the crawl to have been started with `search: true` and its index
212
+ * to have reached `READY`/`PARTIAL`; poll `CrawlerStatus.search` or
213
+ * subscribe to the `crawler_search_ready` webhook. An index that is not
214
+ * ready yet is reported in the response's `skipped` list, not as an error.
215
+ */
216
+ async search(
217
+ query: string,
218
+ opts?: { limit?: number; mode?: CrawlerSearchMode; filters?: Record<string, any>; cursor?: string },
219
+ ): Promise<CrawlerSearchResponse> {
220
+ const uuid = this.requireUuid();
221
+ return await this.client.crawlSearch([uuid], query, opts);
222
+ }
223
+
224
+ /**
225
+ * Ask a question answered from this crawl's content.
226
+ *
227
+ * Sugar over `client.crawlPrompt([uuid], ...)`; yields the same frames.
228
+ */
229
+ prompt(
230
+ prompt: string,
231
+ opts?: {
232
+ search?: { limit?: number; mode?: CrawlerSearchMode; filters?: Record<string, any> };
233
+ model?: string;
234
+ },
235
+ ): AsyncGenerator<CrawlerPromptEvent, void, undefined> {
236
+ const uuid = this.requireUuid();
237
+ return this.client.crawlPrompt([uuid], prompt, opts);
238
+ }
239
+
240
+ /**
241
+ * Ask a question answered from this crawl's content, the whole answer at once.
242
+ *
243
+ * Sugar over `client.crawlPromptAnswer([uuid], ...)`.
244
+ */
245
+ async promptAnswer(
246
+ prompt: string,
247
+ opts?: {
248
+ search?: { limit?: number; mode?: CrawlerSearchMode; filters?: Record<string, any> };
249
+ model?: string;
250
+ },
251
+ ): Promise<CrawlerPromptAnswer> {
252
+ const uuid = this.requireUuid();
253
+ return this.client.crawlPromptAnswer([uuid], prompt, opts);
254
+ }
255
+
256
+ /**
257
+ * Extract a structured object from this crawl's content, shaped by `schema`.
258
+ *
259
+ * Sugar over `client.crawlExtract([uuid], ...)`.
260
+ */
261
+ async extract<T = any>(
262
+ prompt: string,
263
+ schema: Record<string, any>,
264
+ opts?: { search?: { limit?: number; mode?: CrawlerSearchMode; filters?: Record<string, any> } },
265
+ ): Promise<CrawlerExtractResponse<T>> {
266
+ const uuid = this.requireUuid();
267
+ return this.client.crawlExtract<T>([uuid], prompt, schema, opts);
268
+ }
269
+
270
+ /**
271
+ * Re-scrape this crawl's URLs in place, right now.
272
+ *
273
+ * Sugar over `client.crawlRefreshNow(uuid)`. The crawl keeps its uuid, its
274
+ * artifacts and its search index; only pages whose content changed are
275
+ * re-indexed and pages that disappeared are dropped.
276
+ */
277
+ async refreshNow(): Promise<CrawlerRefreshState> {
278
+ const uuid = this.requireUuid();
279
+ return await this.client.crawlRefreshNow(uuid);
280
+ }
281
+
282
+ /**
283
+ * Change this crawl's refresh schedule.
284
+ *
285
+ * Sugar over `client.crawlRefreshSettings(uuid, ...)`; only what is passed
286
+ * is changed.
287
+ */
288
+ async refreshSettings(settings: { enabled?: boolean; interval_seconds?: number }): Promise<CrawlerRefreshState> {
289
+ const uuid = this.requireUuid();
290
+ return await this.client.crawlRefreshSettings(uuid, settings);
291
+ }
292
+
293
+ /**
294
+ * Read this crawl's refresh timeline, newest last.
295
+ *
296
+ * Sugar over `client.crawlRefreshHistory(uuid, ...)`.
297
+ */
298
+ async refreshHistory(opts?: { limit?: number }): Promise<CrawlerRefreshEntry[]> {
299
+ const uuid = this.requireUuid();
300
+ return await this.client.crawlRefreshHistory(uuid, opts);
301
+ }
302
+
197
303
  /**
198
304
  * Read raw content for a single crawled URL using `plain=true` mode.
199
305
  *
@@ -34,8 +34,8 @@ const VALID_CONTENT_FORMATS: ReadonlySet<string> = new Set([
34
34
  /**
35
35
  * Webhook event names emitted by the crawler.
36
36
  *
37
- * Source of truth: `apps/scrapfly/scrape-engine/scrape_engine/scrape_engine/crawler/webhook_manager.py`
38
- * lines 11-20. Verified to match the public docs and the JSON example payloads.
37
+ * Event names match the wire format documented in the Crawler API webhook reference.
38
+ * Verified against the public docs and the JSON example payloads.
39
39
  */
40
40
  export type CrawlerWebhookEventName =
41
41
  | 'crawler_started'
@@ -45,7 +45,16 @@ export type CrawlerWebhookEventName =
45
45
  | 'crawler_url_failed'
46
46
  | 'crawler_stopped'
47
47
  | 'crawler_cancelled'
48
- | 'crawler_finished';
48
+ | 'crawler_finished'
49
+ | 'crawler_search_ready'
50
+ | 'crawler_search_failed'
51
+ | 'crawler_updated';
52
+
53
+ /**
54
+ * Auto-refresh interval bounds, in seconds (1 hour to 90 days).
55
+ */
56
+ export const REFRESH_MIN_INTERVAL = 3600;
57
+ export const REFRESH_MAX_INTERVAL = 90 * 24 * 3600;
49
58
 
50
59
  const VALID_WEBHOOK_EVENTS: ReadonlySet<string> = new Set([
51
60
  'crawler_started',
@@ -56,6 +65,9 @@ const VALID_WEBHOOK_EVENTS: ReadonlySet<string> = new Set([
56
65
  'crawler_stopped',
57
66
  'crawler_cancelled',
58
67
  'crawler_finished',
68
+ 'crawler_search_ready',
69
+ 'crawler_search_failed',
70
+ 'crawler_updated',
59
71
  ]);
60
72
 
61
73
  /**
@@ -110,8 +122,38 @@ export type CrawlerConfigOptions = {
110
122
  content_formats?: CrawlerContentFormat[];
111
123
  extraction_rules?: Rec<any>;
112
124
 
125
+ // Search index built while the crawl runs. Query it with
126
+ // ScrapflyClient.crawlSearch / crawlPrompt once the index is READY.
127
+ search?: boolean;
128
+
129
+ // Auto-refresh: re-scrape this crawl's own URLs in place, on a period. Same
130
+ // crawler_uuid, same artifacts, only changed pages re-indexed.
131
+ refresh?: boolean;
132
+ refresh_interval?: number;
133
+
113
134
  // Web scraping features
135
+ /**
136
+ * The Unblocker (anti-bot bypass), the current name for what used to be
137
+ * called `asp`. Sent in the `POST /crawl` body as `asp`; only the SDK-facing
138
+ * name changed.
139
+ *
140
+ * @deprecated Use `proxy_pool` (`'unblocker_ultra'` for the Unblocker). The
141
+ * migration guide covers requests written before the proxy pools:
142
+ * https://scrapfly.io/new-pricing#legacy-spellings
143
+ */
144
+ unblocker?: boolean;
145
+ /**
146
+ * @deprecated Alias of {@link CrawlerConfigOptions.unblocker}: use
147
+ * `proxy_pool` (`'unblocker_ultra'` for the Unblocker). When both are
148
+ * supplied `asp` wins, so existing code cannot have its meaning changed by a
149
+ * later `unblocker`.
150
+ */
114
151
  asp?: boolean;
152
+ /**
153
+ * Proxy pool for every crawled page: `basic`, `unblocker`, `unblocker_ultra`
154
+ * or `auto` (see ScrapeConfig.BASIC), each of which sets the Unblocker
155
+ * itself.
156
+ */
115
157
  proxy_pool?: string;
116
158
  country?: string;
117
159
 
@@ -171,6 +213,17 @@ export class CrawlerConfig {
171
213
  content_formats?: CrawlerContentFormat[];
172
214
  extraction_rules?: Rec<any>;
173
215
 
216
+ search?: boolean;
217
+
218
+ refresh?: boolean;
219
+ refresh_interval?: number;
220
+
221
+ /**
222
+ * Storage for the anti-bot bypass toggle, under its deprecated name.
223
+ * `unblocker` is an accessor over this same field — see below.
224
+ *
225
+ * @deprecated Use `proxy_pool` (`'unblocker_ultra'` for the Unblocker).
226
+ */
174
227
  asp?: boolean;
175
228
  proxy_pool?: string;
176
229
  country?: string;
@@ -234,6 +287,20 @@ export class CrawlerConfig {
234
287
  if (options.allowed_internal_subdomains && options.allowed_internal_subdomains.length > 250) {
235
288
  throw new CrawlerConfigError('allowed_internal_subdomains is limited to 250 entries');
236
289
  }
290
+ // The floor decides the cost: a crawl refreshing every minute re-scrapes
291
+ // the whole site 1,440 times a day.
292
+ if (
293
+ options.refresh_interval !== undefined &&
294
+ (options.refresh_interval < REFRESH_MIN_INTERVAL || options.refresh_interval > REFRESH_MAX_INTERVAL)
295
+ ) {
296
+ throw new CrawlerConfigError(
297
+ `refresh_interval must be between ${REFRESH_MIN_INTERVAL} and ${REFRESH_MAX_INTERVAL} (seconds, 1 hour to 90 days)`,
298
+ );
299
+ }
300
+ // A period with the feature off would silently never run.
301
+ if (options.refresh_interval !== undefined && !options.refresh) {
302
+ throw new CrawlerConfigError('refresh_interval requires refresh: true');
303
+ }
237
304
 
238
305
  // Validate content_formats values
239
306
  if (options.content_formats) {
@@ -278,13 +345,40 @@ export class CrawlerConfig {
278
345
  this.cache_clear = options.cache_clear;
279
346
  this.content_formats = options.content_formats;
280
347
  this.extraction_rules = options.extraction_rules;
281
- this.asp = options.asp;
348
+ this.search = options.search;
349
+ this.refresh = options.refresh;
350
+ this.refresh_interval = options.refresh_interval;
351
+ // An explicitly supplied `asp` always wins; the deprecated alias is only
352
+ // superseded by `unblocker` when it was not supplied at all. `??` and not
353
+ // `||`, so an explicit `false` on either name still reaches the server.
354
+ // Both unset stays `undefined`, which toApiParams drops so the server
355
+ // applies its own default.
356
+ this.asp = options.asp ?? options.unblocker;
282
357
  this.proxy_pool = options.proxy_pool;
283
358
  this.country = options.country;
284
359
  this.webhook_name = options.webhook_name;
285
360
  this.webhook_events = options.webhook_events;
286
361
  }
287
362
 
363
+ /**
364
+ * The Unblocker toggle, the current name for what used to be `asp`.
365
+ * Deprecated: use `proxy_pool` (`'unblocker_ultra'` for the Unblocker). The
366
+ * migration guide covers requests written before the proxy pools:
367
+ * https://scrapfly.io/new-pricing#legacy-spellings
368
+ *
369
+ * An accessor over the single `asp` field rather than a second stored
370
+ * property: one storage slot means the two names can never disagree, and
371
+ * `toApiParams` keeps serializing the one field under the wire key `asp`.
372
+ * `undefined` propagates in both directions, so "unset" stays unset.
373
+ */
374
+ get unblocker(): boolean | undefined {
375
+ return this.asp;
376
+ }
377
+
378
+ set unblocker(value: boolean | undefined) {
379
+ this.asp = value;
380
+ }
381
+
288
382
  private validateOptions(options: Partial<CrawlerConfigOptions>) {
289
383
  const validKeys = new Set<string>([
290
384
  'url',
@@ -314,6 +408,10 @@ export class CrawlerConfig {
314
408
  'cache_clear',
315
409
  'content_formats',
316
410
  'extraction_rules',
411
+ 'search',
412
+ 'refresh',
413
+ 'refresh_interval',
414
+ 'unblocker',
317
415
  'asp',
318
416
  'proxy_pool',
319
417
  'country',
@@ -367,7 +465,9 @@ export class CrawlerConfig {
367
465
  'cache_clear',
368
466
  'content_formats',
369
467
  'extraction_rules',
370
- 'asp',
468
+ 'search',
469
+ 'refresh',
470
+ 'refresh_interval',
371
471
  'proxy_pool',
372
472
  'country',
373
473
  'webhook_name',
@@ -379,6 +479,29 @@ export class CrawlerConfig {
379
479
  params[key] = v;
380
480
  }
381
481
  }
482
+
483
+ // The anti-bot toggle is serialized apart from the loop above because it is
484
+ // the one field where `false` and "not supplied" must collapse.
485
+ //
486
+ // The key is `asp` and never `unblocker`: the wire key is unchanged for
487
+ // both names. A published package version is immutable and upgraded per
488
+ // installation, so emitting `unblocker` against an API deployment that has
489
+ // not learned that name yet would silently drop a paid feature — the crawl
490
+ // runs, is billed, and comes back blocked. Renaming the emitted key is a
491
+ // separate, later release.
492
+ //
493
+ // Only `true` is emitted. CROSS-SDK SHAPE: the Python, Go and Rust SDKs all
494
+ // omit the key when the feature is off (Python's `asp` is a plain `bool`,
495
+ // Go's is a `bool`, Rust's carries `skip_serializing_if = "is_false"`), so
496
+ // carrying an explicit `asp: false` here would make the same customer
497
+ // intent produce two different POST /crawl bodies depending on which SDK
498
+ // they picked. The API cannot tell the two apart either: it resolves a
499
+ // present-false and an absent pair to the same `false`, so an explicit
500
+ // `asp: false` on the wire buys nothing. One shape, all four SDKs.
501
+ if (this.asp === true) {
502
+ params.asp = true;
503
+ }
504
+
382
505
  return params;
383
506
  }
384
507
  }