scrapfly-sdk 1.0.0 → 2.1.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.
Files changed (44) hide show
  1. package/esm/browserconfig.d.ts +14 -2
  2. package/esm/browserconfig.d.ts.map +1 -1
  3. package/esm/browserconfig.js +24 -0
  4. package/esm/client.d.ts +1 -0
  5. package/esm/client.d.ts.map +1 -1
  6. package/esm/client.js +22 -9
  7. package/esm/crawlerconfig.d.ts +2 -1
  8. package/esm/crawlerconfig.d.ts.map +1 -1
  9. package/esm/crawlerconfig.js +1 -0
  10. package/esm/crawlerresult.d.ts +16 -3
  11. package/esm/crawlerresult.d.ts.map +1 -1
  12. package/esm/main.d.ts +2 -2
  13. package/esm/main.d.ts.map +1 -1
  14. package/esm/scrapeconfig.d.ts +4 -2
  15. package/esm/scrapeconfig.d.ts.map +1 -1
  16. package/esm/scrapeconfig.js +2 -0
  17. package/esm/utils.d.ts.map +1 -1
  18. package/esm/utils.js +7 -1
  19. package/package.json +1 -1
  20. package/script/browserconfig.d.ts +14 -2
  21. package/script/browserconfig.d.ts.map +1 -1
  22. package/script/browserconfig.js +24 -0
  23. package/script/client.d.ts +1 -0
  24. package/script/client.d.ts.map +1 -1
  25. package/script/client.js +22 -8
  26. package/script/crawlerconfig.d.ts +2 -1
  27. package/script/crawlerconfig.d.ts.map +1 -1
  28. package/script/crawlerconfig.js +1 -0
  29. package/script/crawlerresult.d.ts +16 -3
  30. package/script/crawlerresult.d.ts.map +1 -1
  31. package/script/main.d.ts +2 -2
  32. package/script/main.d.ts.map +1 -1
  33. package/script/scrapeconfig.d.ts +4 -2
  34. package/script/scrapeconfig.d.ts.map +1 -1
  35. package/script/scrapeconfig.js +2 -0
  36. package/script/utils.d.ts.map +1 -1
  37. package/script/utils.js +7 -1
  38. package/src/browserconfig.ts +25 -2
  39. package/src/client.ts +21 -9
  40. package/src/crawlerconfig.ts +3 -0
  41. package/src/crawlerresult.ts +27 -5
  42. package/src/main.ts +2 -1
  43. package/src/scrapeconfig.ts +4 -2
  44. package/src/utils.ts +10 -1
@@ -7,6 +7,7 @@ import type { Rec } from './types.js';
7
7
  * - `html` — original HTML
8
8
  * - `clean_html` — sanitized/cleaned HTML
9
9
  * - `markdown` — LLM-ready markdown
10
+ * - `axtree` — compact accessibility tree outline
10
11
  * - `text` — plain-text extraction
11
12
  * - `json` — parse as JSON (for API responses)
12
13
  * - `extracted_data` — structured data from extraction rules
@@ -16,6 +17,7 @@ export type CrawlerContentFormat =
16
17
  | 'html'
17
18
  | 'clean_html'
18
19
  | 'markdown'
20
+ | 'axtree'
19
21
  | 'text'
20
22
  | 'json'
21
23
  | 'extracted_data'
@@ -25,6 +27,7 @@ const VALID_CONTENT_FORMATS: ReadonlySet<string> = new Set([
25
27
  'html',
26
28
  'clean_html',
27
29
  'markdown',
30
+ 'axtree',
28
31
  'text',
29
32
  'json',
30
33
  'extracted_data',
@@ -62,18 +62,40 @@ function optionalField<T>(
62
62
  export type CrawlerStatusValue = 'PENDING' | 'RUNNING' | 'DONE' | 'CANCELLED';
63
63
 
64
64
  /**
65
- * Reasons documented for why a crawler stopped.
65
+ * Why a crawler stopped, as `state.stop_reason` reports it.
66
+ *
67
+ * `backlog_empty` and `retry_backlog_only` are a normal completion: every URL found was crawled, or only URLs waiting
68
+ * for a retry were left. A stop on a limit (`page_limit`, `max_duration_reached`, `max_api_credit`) or on
69
+ * `crawler_error` after at least one visited page keeps its pages and ends with `is_success: true`: a stop on
70
+ * `max_api_credit` is a success.
66
71
  */
67
72
  export type CrawlerStopReason =
73
+ | 'backlog_empty'
74
+ | 'retry_backlog_only'
68
75
  | 'no_more_urls'
69
76
  | 'page_limit'
70
- | 'max_duration'
77
+ | 'max_duration_reached'
71
78
  | 'max_api_credit'
72
79
  | 'seed_url_failed'
73
80
  | 'user_cancelled'
74
81
  | 'crawler_error'
75
- | 'no_api_credit_left'
76
- | 'storage_error';
82
+ | 'target_not_allowed'
83
+ | 'webhook_not_found'
84
+ | 'storage_error'
85
+ | 'remote_list_fetch_failed'
86
+ | 'remote_list_too_large'
87
+ | 'worker_interrupted'
88
+ | 'internal_error'
89
+ | LegacyCrawlerStopReason;
90
+
91
+ /**
92
+ * Values the API no longer sets, kept so code that names them still compiles: a stop on `max_duration` is
93
+ * `max_duration_reached`, `no_api_credit_left` is never returned, and `worker_interupted` is the misspelled value
94
+ * that crawls stored before its correction still report.
95
+ *
96
+ * @deprecated Compare with `worker_interrupted` and `max_duration_reached`; the API never returns the others.
97
+ */
98
+ export type LegacyCrawlerStopReason = 'max_duration' | 'no_api_credit_left' | 'worker_interupted';
77
99
 
78
100
  /**
79
101
  * Per-job state metrics returned in the status response and webhook payloads.
@@ -98,7 +120,7 @@ export interface CrawlerState {
98
120
  api_credit_used: number;
99
121
  /** Elapsed time in seconds since the crawl started (0 while PENDING). */
100
122
  duration: number;
101
- /** `null` until the crawler stops. */
123
+ /** Why the crawler stopped, `null` until it does. */
102
124
  stop_reason: CrawlerStopReason | null;
103
125
  /** `null` until the crawler actually starts processing (still PENDING). */
104
126
  start_time: number | null;
package/src/main.ts CHANGED
@@ -39,7 +39,7 @@ export {
39
39
  } from './screenshotconfig.js';
40
40
  export { ExtractionConfig } from './extractionconfig.js';
41
41
  export { BrowserConfig, ProxyPool, OperatingSystem } from './browserconfig.js';
42
- export type { BrowserConfigOptions } from './browserconfig.js';
42
+ export type { BrowserConfigOptions, NetworkPolicy } from './browserconfig.js';
43
43
  export { BrowserDownloadError, CloudBrowserDownloads, parseDownloads } from './browserdownloads.js';
44
44
  export type { BrowserDownload, CdpSend, CdpSession, CloudBrowserDownloadsOptions, WaitForOptions } from './browserdownloads.js';
45
45
  // Enum, hence a value export: callers name VaultLinkedService.ONE_PASSWORD.
@@ -115,6 +115,7 @@ export type {
115
115
  CrawlerState,
116
116
  CrawlerStatusValue,
117
117
  CrawlerStopReason,
118
+ LegacyCrawlerStopReason,
118
119
  CrawlerUpdatedWebhook,
119
120
  CrawlerUrlDiscoveredWebhook,
120
121
  CrawlerUrlEntry,
@@ -28,6 +28,8 @@ export enum Format {
28
28
  TEXT = 'text',
29
29
  /** Markdown rendering of the page. */
30
30
  MARKDOWN = 'markdown',
31
+ /** Compact accessibility tree outline of the page. */
32
+ AXTREE = 'axtree',
31
33
  /** Readability-cleaned HTML. */
32
34
  CLEAN_HTML = 'clean_html',
33
35
  /** Raw HTML as returned by the target. */
@@ -78,7 +80,7 @@ export type ScrapeConfigOptions = {
78
80
  proxy_pool?: string;
79
81
  session?: string;
80
82
  tags?: string[];
81
- format?: 'json' | 'text' | 'markdown' | 'clean_html' | 'raw' | Format;
83
+ format?: 'json' | 'text' | 'markdown' | 'axtree' | 'clean_html' | 'raw' | Format;
82
84
  format_options?: ('no_links' | 'no_images' | 'only_content' | FormatOption)[];
83
85
  extraction_template?: string; // saved template name
84
86
  extraction_ephemeral_template?: object; // ephemeraly declared json template
@@ -206,7 +208,7 @@ export class ScrapeConfig<O extends ScrapeConfigOptions = ScrapeConfigOptions> {
206
208
  proxy_pool?: string;
207
209
  session?: string;
208
210
  tags: Set<string> = new Set<string>();
209
- format?: 'json' | 'text' | 'markdown' | 'clean_html' | 'raw' | Format;
211
+ format?: 'json' | 'text' | 'markdown' | 'axtree' | 'clean_html' | 'raw' | Format;
210
212
  format_options?: ('no_links' | 'no_images' | 'only_content' | FormatOption)[];
211
213
  extraction_template?: string; // saved template name
212
214
  extraction_ephemeral_template?: object; // ephemeraly declared json template
package/src/utils.ts CHANGED
@@ -1,10 +1,19 @@
1
1
  import { log } from './logger.js';
2
2
  import type { Rec } from './types.js';
3
3
 
4
+ const BASE64_CHUNK_BYTES = 0x8000;
5
+
4
6
  export function urlsafe_b64encode(data: string): string {
5
7
  const encoder = new TextEncoder();
6
8
  const encoded = encoder.encode(data);
7
- const base64 = btoa(String.fromCharCode(...encoded))
9
+ let binary = '';
10
+
11
+ // Encoded in chunks of BASE64_CHUNK_BYTES bytes.
12
+ for (let offset = 0; offset < encoded.length; offset += BASE64_CHUNK_BYTES) {
13
+ binary += String.fromCharCode(...encoded.subarray(offset, offset + BASE64_CHUNK_BYTES));
14
+ }
15
+
16
+ const base64 = btoa(binary)
8
17
  .replace(/\+/g, '-')
9
18
  .replace(/\//g, '_')
10
19
  .replace(/=+$/, '');