scrapfly-sdk 0.10.3 → 0.10.5

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 (166) hide show
  1. package/esm/browserconfig.d.ts +70 -1
  2. package/esm/browserconfig.d.ts.map +1 -1
  3. package/esm/browserconfig.js +44 -1
  4. package/esm/client.d.ts +33 -0
  5. package/esm/client.d.ts.map +1 -1
  6. package/esm/client.js +33 -5
  7. package/esm/crawl.d.ts +4 -0
  8. package/esm/crawl.d.ts.map +1 -1
  9. package/esm/crawl.js +4 -0
  10. package/esm/crawlerresult.d.ts +7 -0
  11. package/esm/crawlerresult.d.ts.map +1 -1
  12. package/esm/errors.d.ts +43 -0
  13. package/esm/errors.d.ts.map +1 -1
  14. package/esm/errors.js +43 -16
  15. package/esm/extractionconfig.d.ts +54 -9
  16. package/esm/extractionconfig.d.ts.map +1 -1
  17. package/esm/extractionconfig.js +37 -13
  18. package/esm/logger.d.ts +1 -0
  19. package/esm/logger.d.ts.map +1 -1
  20. package/esm/logger.js +1 -0
  21. package/esm/main.d.ts +27 -0
  22. package/esm/main.d.ts.map +1 -1
  23. package/esm/main.js +27 -0
  24. package/esm/monitoringconfig.d.ts +48 -11
  25. package/esm/monitoringconfig.d.ts.map +1 -1
  26. package/esm/monitoringconfig.js +14 -0
  27. package/esm/result.d.ts +2 -0
  28. package/esm/result.d.ts.map +1 -1
  29. package/esm/scrapeconfig.d.ts +10 -8
  30. package/esm/scrapeconfig.d.ts.map +1 -1
  31. package/esm/scrapeconfig.js +10 -8
  32. package/esm/screenshotconfig.d.ts +59 -26
  33. package/esm/screenshotconfig.d.ts.map +1 -1
  34. package/esm/screenshotconfig.js +38 -26
  35. package/esm/webhookserver.d.ts +24 -0
  36. package/esm/webhookserver.d.ts.map +1 -1
  37. package/esm/webhookserver.js +24 -0
  38. package/package.json +1 -1
  39. package/script/browserconfig.d.ts +70 -1
  40. package/script/browserconfig.d.ts.map +1 -1
  41. package/script/browserconfig.js +44 -1
  42. package/script/client.d.ts +33 -0
  43. package/script/client.d.ts.map +1 -1
  44. package/script/client.js +33 -5
  45. package/script/crawl.d.ts +4 -0
  46. package/script/crawl.d.ts.map +1 -1
  47. package/script/crawl.js +4 -0
  48. package/script/crawlerresult.d.ts +7 -0
  49. package/script/crawlerresult.d.ts.map +1 -1
  50. package/script/errors.d.ts +43 -0
  51. package/script/errors.d.ts.map +1 -1
  52. package/script/errors.js +43 -16
  53. package/script/extractionconfig.d.ts +54 -9
  54. package/script/extractionconfig.d.ts.map +1 -1
  55. package/script/extractionconfig.js +37 -13
  56. package/script/logger.d.ts +1 -0
  57. package/script/logger.d.ts.map +1 -1
  58. package/script/logger.js +1 -0
  59. package/script/main.d.ts +27 -0
  60. package/script/main.d.ts.map +1 -1
  61. package/script/main.js +27 -0
  62. package/script/monitoringconfig.d.ts +48 -11
  63. package/script/monitoringconfig.d.ts.map +1 -1
  64. package/script/monitoringconfig.js +14 -0
  65. package/script/result.d.ts +2 -0
  66. package/script/result.d.ts.map +1 -1
  67. package/script/scrapeconfig.d.ts +10 -8
  68. package/script/scrapeconfig.d.ts.map +1 -1
  69. package/script/scrapeconfig.js +10 -8
  70. package/script/screenshotconfig.d.ts +59 -26
  71. package/script/screenshotconfig.d.ts.map +1 -1
  72. package/script/screenshotconfig.js +38 -26
  73. package/script/webhookserver.d.ts +24 -0
  74. package/script/webhookserver.d.ts.map +1 -1
  75. package/script/webhookserver.js +24 -0
  76. package/src/_dnt.shims.ts +64 -0
  77. package/src/batch.ts +272 -0
  78. package/src/browserconfig.ts +187 -0
  79. package/src/client.ts +1637 -0
  80. package/src/crawl.ts +255 -0
  81. package/src/crawlerconfig.ts +363 -0
  82. package/src/crawlerresult.ts +478 -0
  83. package/src/deps/jsr.io/@std/path/1.0.1/_common/assert_path.ts +10 -0
  84. package/src/deps/jsr.io/@std/path/1.0.1/_common/basename.ts +53 -0
  85. package/src/deps/jsr.io/@std/path/1.0.1/_common/common.ts +26 -0
  86. package/src/deps/jsr.io/@std/path/1.0.1/_common/constants.ts +49 -0
  87. package/src/deps/jsr.io/@std/path/1.0.1/_common/dirname.ts +9 -0
  88. package/src/deps/jsr.io/@std/path/1.0.1/_common/format.ts +25 -0
  89. package/src/deps/jsr.io/@std/path/1.0.1/_common/from_file_url.ts +10 -0
  90. package/src/deps/jsr.io/@std/path/1.0.1/_common/glob_to_reg_exp.ts +290 -0
  91. package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize.ts +9 -0
  92. package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize_string.ts +74 -0
  93. package/src/deps/jsr.io/@std/path/1.0.1/_common/relative.ts +10 -0
  94. package/src/deps/jsr.io/@std/path/1.0.1/_common/strip_trailing_separators.ts +25 -0
  95. package/src/deps/jsr.io/@std/path/1.0.1/_common/to_file_url.ts +17 -0
  96. package/src/deps/jsr.io/@std/path/1.0.1/_os.ts +31 -0
  97. package/src/deps/jsr.io/@std/path/1.0.1/basename.ts +35 -0
  98. package/src/deps/jsr.io/@std/path/1.0.1/common.ts +35 -0
  99. package/src/deps/jsr.io/@std/path/1.0.1/constants.ts +18 -0
  100. package/src/deps/jsr.io/@std/path/1.0.1/dirname.ts +28 -0
  101. package/src/deps/jsr.io/@std/path/1.0.1/extname.ts +27 -0
  102. package/src/deps/jsr.io/@std/path/1.0.1/format.ts +30 -0
  103. package/src/deps/jsr.io/@std/path/1.0.1/from_file_url.ts +30 -0
  104. package/src/deps/jsr.io/@std/path/1.0.1/glob_to_regexp.ts +94 -0
  105. package/src/deps/jsr.io/@std/path/1.0.1/is_absolute.ts +30 -0
  106. package/src/deps/jsr.io/@std/path/1.0.1/is_glob.ts +49 -0
  107. package/src/deps/jsr.io/@std/path/1.0.1/join.ts +28 -0
  108. package/src/deps/jsr.io/@std/path/1.0.1/join_globs.ts +42 -0
  109. package/src/deps/jsr.io/@std/path/1.0.1/mod.ts +55 -0
  110. package/src/deps/jsr.io/@std/path/1.0.1/normalize.ts +31 -0
  111. package/src/deps/jsr.io/@std/path/1.0.1/normalize_glob.ts +45 -0
  112. package/src/deps/jsr.io/@std/path/1.0.1/parse.ts +44 -0
  113. package/src/deps/jsr.io/@std/path/1.0.1/posix/_util.ts +10 -0
  114. package/src/deps/jsr.io/@std/path/1.0.1/posix/basename.ts +39 -0
  115. package/src/deps/jsr.io/@std/path/1.0.1/posix/constants.ts +15 -0
  116. package/src/deps/jsr.io/@std/path/1.0.1/posix/dirname.ts +55 -0
  117. package/src/deps/jsr.io/@std/path/1.0.1/posix/extname.ts +73 -0
  118. package/src/deps/jsr.io/@std/path/1.0.1/posix/format.ts +31 -0
  119. package/src/deps/jsr.io/@std/path/1.0.1/posix/from_file_url.ts +25 -0
  120. package/src/deps/jsr.io/@std/path/1.0.1/posix/glob_to_regexp.ts +94 -0
  121. package/src/deps/jsr.io/@std/path/1.0.1/posix/is_absolute.ts +25 -0
  122. package/src/deps/jsr.io/@std/path/1.0.1/posix/join.ts +27 -0
  123. package/src/deps/jsr.io/@std/path/1.0.1/posix/join_globs.ts +44 -0
  124. package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize.ts +41 -0
  125. package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize_glob.ts +42 -0
  126. package/src/deps/jsr.io/@std/path/1.0.1/posix/parse.ts +121 -0
  127. package/src/deps/jsr.io/@std/path/1.0.1/posix/relative.ts +103 -0
  128. package/src/deps/jsr.io/@std/path/1.0.1/posix/resolve.ts +69 -0
  129. package/src/deps/jsr.io/@std/path/1.0.1/posix/to_file_url.ts +32 -0
  130. package/src/deps/jsr.io/@std/path/1.0.1/posix/to_namespaced_path.ts +21 -0
  131. package/src/deps/jsr.io/@std/path/1.0.1/relative.ts +32 -0
  132. package/src/deps/jsr.io/@std/path/1.0.1/resolve.ts +32 -0
  133. package/src/deps/jsr.io/@std/path/1.0.1/to_file_url.ts +30 -0
  134. package/src/deps/jsr.io/@std/path/1.0.1/to_namespaced_path.ts +31 -0
  135. package/src/deps/jsr.io/@std/path/1.0.1/types.ts +40 -0
  136. package/src/deps/jsr.io/@std/path/1.0.1/windows/_util.ts +28 -0
  137. package/src/deps/jsr.io/@std/path/1.0.1/windows/basename.ts +48 -0
  138. package/src/deps/jsr.io/@std/path/1.0.1/windows/constants.ts +15 -0
  139. package/src/deps/jsr.io/@std/path/1.0.1/windows/dirname.ts +114 -0
  140. package/src/deps/jsr.io/@std/path/1.0.1/windows/extname.ts +86 -0
  141. package/src/deps/jsr.io/@std/path/1.0.1/windows/format.ts +31 -0
  142. package/src/deps/jsr.io/@std/path/1.0.1/windows/from_file_url.ts +34 -0
  143. package/src/deps/jsr.io/@std/path/1.0.1/windows/glob_to_regexp.ts +92 -0
  144. package/src/deps/jsr.io/@std/path/1.0.1/windows/is_absolute.ts +40 -0
  145. package/src/deps/jsr.io/@std/path/1.0.1/windows/join.ts +72 -0
  146. package/src/deps/jsr.io/@std/path/1.0.1/windows/join_globs.ts +45 -0
  147. package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize.ts +132 -0
  148. package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize_glob.ts +42 -0
  149. package/src/deps/jsr.io/@std/path/1.0.1/windows/parse.ts +184 -0
  150. package/src/deps/jsr.io/@std/path/1.0.1/windows/relative.ts +128 -0
  151. package/src/deps/jsr.io/@std/path/1.0.1/windows/resolve.ts +174 -0
  152. package/src/deps/jsr.io/@std/path/1.0.1/windows/to_file_url.ts +38 -0
  153. package/src/deps/jsr.io/@std/path/1.0.1/windows/to_namespaced_path.ts +60 -0
  154. package/src/deps.ts +4 -0
  155. package/src/errors.ts +82 -0
  156. package/src/extractionconfig.ts +217 -0
  157. package/src/logger.ts +53 -0
  158. package/src/main.ts +88 -0
  159. package/src/monitoringconfig.ts +88 -0
  160. package/src/polyfill.ts +37 -0
  161. package/src/result.ts +340 -0
  162. package/src/scrapeconfig.ts +450 -0
  163. package/src/screenshotconfig.ts +237 -0
  164. package/src/types.ts +2 -0
  165. package/src/utils.ts +86 -0
  166. package/src/webhookserver.ts +90 -0
package/src/main.ts ADDED
@@ -0,0 +1,88 @@
1
+ /**
2
+ * # Scrapfly SDK for TypeScript / JavaScript
3
+ *
4
+ * Official client library for the [Scrapfly](https://scrapfly.io) web
5
+ * scraping API. Provides typed wrappers around the Scrape, Screenshot,
6
+ * Extraction, Crawler, Monitoring, and Cloud Browser APIs.
7
+ *
8
+ * ## Quick start
9
+ *
10
+ * ```ts
11
+ * import { ScrapflyClient, ScrapeConfig } from '@scrapfly/scrapfly-sdk';
12
+ *
13
+ * const client = new ScrapflyClient({ key: 'your-api-key' });
14
+ * const result = await client.scrape(new ScrapeConfig({
15
+ * url: 'https://web-scraping.dev/products',
16
+ * render_js: true,
17
+ * asp: true,
18
+ * }));
19
+ * console.log(result.content);
20
+ * ```
21
+ *
22
+ * See the [Scrapfly docs](https://scrapfly.io/docs) for the full API
23
+ * reference.
24
+ *
25
+ * @module
26
+ */
27
+ export { ScrapflyClient } from './client.js';
28
+ export {
29
+ Format as ScrapeFormat,
30
+ FormatOption as ScrapeFormatOptions,
31
+ ScrapeConfig,
32
+ ScreenshotFlags,
33
+ } from './scrapeconfig.js';
34
+ export {
35
+ Format as ScreenshotFormat,
36
+ Options as ScreenshotOptions,
37
+ ScreenshotConfig,
38
+ VisionDeficiency,
39
+ } from './screenshotconfig.js';
40
+ export { ExtractionConfig } from './extractionconfig.js';
41
+ export { BrowserConfig, ProxyPool, OperatingSystem } from './browserconfig.js';
42
+ export type { BrowserConfigOptions } from './browserconfig.js';
43
+ /** Namespace of every error class the SDK can throw. See {@link ScrapflyError}. */
44
+ export * as errors from './errors.js';
45
+
46
+ export { ExtractionResult, ScrapeResult, ScreenshotResult } from './result.js';
47
+
48
+ export type { AccountData, ClassifyOptions, ClassifyResult, ConfigData, ContextData, ResultData, ScreenshotMetadata } from './result.js';
49
+
50
+ // Crawler API
51
+ export { CrawlerConfig } from './crawlerconfig.js';
52
+ export type { CrawlerConfigOptions, CrawlerContentFormat, CrawlerWebhookEventName } from './crawlerconfig.js';
53
+ export { Crawl } from './crawl.js';
54
+ export {
55
+ CrawlerArtifact,
56
+ CrawlerContents,
57
+ CrawlerStatus,
58
+ CrawlerUrls,
59
+ parseCrawlerWebhook,
60
+ } from './crawlerresult.js';
61
+ export type {
62
+ CrawlerArtifactType,
63
+ CrawlerLifecycleWebhook,
64
+ CrawlerState,
65
+ CrawlerStatusValue,
66
+ CrawlerStopReason,
67
+ CrawlerUrlDiscoveredWebhook,
68
+ CrawlerUrlEntry,
69
+ CrawlerUrlFailedWebhook,
70
+ CrawlerUrlSkippedWebhook,
71
+ CrawlerUrlVisitedWebhook,
72
+ CrawlerWebhookCommon,
73
+ CrawlerWebhookPayload,
74
+ } from './crawlerresult.js';
75
+
76
+ // Monitoring API
77
+ export type {
78
+ CloudBrowserMonitoringOptions,
79
+ MonitoringAggregation,
80
+ MonitoringDataFormat,
81
+ MonitoringMetricsOptions,
82
+ MonitoringPeriod,
83
+ MonitoringTargetMetricsOptions,
84
+ } from './monitoringconfig.js';
85
+
86
+ export { log } from './logger.js';
87
+
88
+ export { createServer, verifySignature } from './webhookserver.js';
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Option types for the Scrapfly Monitoring API endpoints.
3
+ *
4
+ * The Monitoring API exposes dashboard metrics for each product (scrape,
5
+ * screenshot, extraction, crawler, cloud browser). The types here mirror
6
+ * the query parameters accepted by those endpoints.
7
+ */
8
+
9
+ /** Output format of the monitoring payload. */
10
+ export type MonitoringDataFormat = 'structured' | 'prometheus';
11
+
12
+ /** Rolling time window to aggregate metrics over. */
13
+ export type MonitoringPeriod = 'subscription' | 'last7d' | 'last24h' | 'last1h' | 'last5m';
14
+
15
+ /** Grouping level for aggregated metrics. */
16
+ export type MonitoringAggregation = 'account' | 'project' | 'target';
17
+
18
+ /** Options common to the per-product Monitoring metrics endpoints. */
19
+ export interface MonitoringMetricsOptions {
20
+ /** Payload format. Defaults to `structured`. */
21
+ format?: MonitoringDataFormat;
22
+ /** Time window to aggregate over. */
23
+ period?: MonitoringPeriod;
24
+ /** Grouping levels. */
25
+ aggregation?: MonitoringAggregation[];
26
+ /**
27
+ * Fold WEBHOOK-origin events (callbacks executed by the webhook
28
+ * worker) into this product's totals. Defaults to false to match the
29
+ * dashboard's default view.
30
+ */
31
+ includeWebhook?: boolean;
32
+ }
33
+
34
+ /** Options for the per-target ("per domain") Monitoring endpoints. */
35
+ export interface MonitoringTargetMetricsOptions {
36
+ /** Domain to query metrics for. Required. */
37
+ domain: string;
38
+ /** Collapse subdomains into the parent domain. */
39
+ groupSubdomain?: boolean;
40
+ /** Time window to aggregate over. */
41
+ period?: MonitoringPeriod;
42
+ /** Window start (takes precedence over `period` when set together). */
43
+ start?: Date;
44
+ /** Window end. */
45
+ end?: Date;
46
+ /** Fold WEBHOOK-origin events into this product's totals. */
47
+ includeWebhook?: boolean;
48
+ }
49
+
50
+ /**
51
+ * Options for the Cloud Browser monitoring endpoints.
52
+ *
53
+ * Cloud Browser is session-based (one allocation = one long-lived
54
+ * browser, billed by runtime + bandwidth) and exposes a distinct shape
55
+ * from the request-based products. There is no `domain`/`target` and
56
+ * no `includeWebhook`.
57
+ */
58
+ export interface CloudBrowserMonitoringOptions {
59
+ /** Time window to aggregate over. */
60
+ period?: MonitoringPeriod;
61
+ /**
62
+ * Optional filter to a single proxy pool
63
+ * (e.g. `public_datacenter_pool`, `public_residential_pool`, `byop`).
64
+ */
65
+ proxyPool?: string;
66
+ /** Window start. */
67
+ start?: Date;
68
+ /** Window end. */
69
+ end?: Date;
70
+ }
71
+
72
+ /**
73
+ * Format a Date as a UTC string accepted by the Monitoring API date
74
+ * filters (`YYYY-MM-DD HH:MM:SS`).
75
+ *
76
+ * @param d Date to format.
77
+ * @returns UTC timestamp string.
78
+ */
79
+ export function formatMonitoringDate(d: Date): string {
80
+ const pad = (n: number): string => n.toString().padStart(2, '0');
81
+ const y = d.getUTCFullYear();
82
+ const mo = pad(d.getUTCMonth() + 1);
83
+ const da = pad(d.getUTCDate());
84
+ const h = pad(d.getUTCHours());
85
+ const mi = pad(d.getUTCMinutes());
86
+ const s = pad(d.getUTCSeconds());
87
+ return `${y}-${mo}-${da} ${h}:${mi}:${s}`;
88
+ }
@@ -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,340 @@
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
+ /** Request-context metadata included on every scrape result (cost, proxy, fingerprint, ...). */
43
+ export type ContextData = {
44
+ asp: boolean;
45
+ bandwidth_consumed: number;
46
+ cache: {
47
+ state: string;
48
+ entry?: string;
49
+ };
50
+ cookies: Record<string, string>;
51
+ cost: {
52
+ details: Array<{ amount: number; code: string; description: string }>;
53
+ total: number;
54
+ };
55
+ created_at: string;
56
+ debug: {
57
+ response_url: string;
58
+ screenshot_url?: string;
59
+ };
60
+ env: string;
61
+ fingerprint: string;
62
+ headers: Record<string, string>;
63
+ is_xml_http_request: boolean;
64
+ job?: string;
65
+ lang: Array<string>;
66
+ os: {
67
+ distribution: string;
68
+ name: string;
69
+ type: string;
70
+ version: string;
71
+ };
72
+ project: string;
73
+ proxy: {
74
+ country: string;
75
+ identity: string;
76
+ network: string;
77
+ pool: string;
78
+ };
79
+ redirects: Array<string>;
80
+ retry: number;
81
+ schedule?: string;
82
+ session?: string;
83
+ spider?: any;
84
+ throttler?: any;
85
+ uri: {
86
+ base_url: string;
87
+ fragment?: string;
88
+ host: string;
89
+ params?: Rec<string>;
90
+ port: number;
91
+ query?: string;
92
+ root_domain: string;
93
+ scheme: string;
94
+ };
95
+ url: string;
96
+ webhook?: string;
97
+ };
98
+
99
+ /** Payload of a successful scrape (response body, status, headers, browser trace). */
100
+ export type ResultData = {
101
+ browser_data: {
102
+ javascript_evaluation_result?: string;
103
+ js_scenario?: {
104
+ duration: number;
105
+ executed: number;
106
+ response: any;
107
+ steps: Array<{
108
+ action: string;
109
+ config: Rec<string>;
110
+ duration: number;
111
+ executed: boolean;
112
+ result?: string;
113
+ success: boolean;
114
+ }>;
115
+ };
116
+ local_storage_data?: Rec<string>;
117
+ session_storage_data?: Rec<string>;
118
+ websockets?: Array<any>;
119
+ xhr_call?: Array<{
120
+ body?: string;
121
+ headers: Rec<string>;
122
+ method: string;
123
+ type: string;
124
+ url: string;
125
+ response: {
126
+ body: string;
127
+ duration: number;
128
+ format: string;
129
+ headers: Rec<string>;
130
+ status: number;
131
+ };
132
+ }>;
133
+ };
134
+ content: string;
135
+ content_encoding: string;
136
+ content_type: string;
137
+ cookies: Array<{
138
+ name: string;
139
+ value: string;
140
+ expires: string;
141
+ path: string;
142
+ comment: string;
143
+ domain: string;
144
+ max_age: number;
145
+ secure: boolean;
146
+ http_only: boolean;
147
+ version: string;
148
+ size: number;
149
+ }>;
150
+ data?: Rec<any>;
151
+ dns?: Rec<Array<Rec<any>>>;
152
+ duration: number;
153
+ error?: {
154
+ code: string;
155
+ http_code: number;
156
+ links: Rec<string>;
157
+ message: string;
158
+ retryable: boolean;
159
+ doc_url?: string;
160
+ };
161
+ format: string;
162
+ iframes: Array<{
163
+ url: string;
164
+ uri: {
165
+ root_domain: string;
166
+ base_url: string;
167
+ host: string;
168
+ scheme: string;
169
+ query?: string;
170
+ fragment?: string;
171
+ port: number;
172
+ params?: Rec<string>;
173
+ };
174
+ content: string;
175
+ }>;
176
+ log_url: string;
177
+ reason: string;
178
+ request_headers: Rec<string>;
179
+ response_headers: Rec<string>;
180
+ screenshots: Rec<{
181
+ css_selector?: string;
182
+ extension: string;
183
+ format: string;
184
+ size: number;
185
+ url: string;
186
+ }>;
187
+ size: number;
188
+ ssl?: {
189
+ certs: Array<Rec<any>>;
190
+ };
191
+ status: string;
192
+ status_code: number;
193
+ success: boolean;
194
+ url: string;
195
+ };
196
+
197
+ export class ScrapeResult {
198
+ config: ConfigData;
199
+ context: ContextData;
200
+ result: ResultData;
201
+ uuid: string;
202
+ private _selector: cheerio.CheerioAPI | undefined;
203
+
204
+ constructor(data: { config: ConfigData; context: ContextData; result: ResultData; uuid: string }) {
205
+ this.config = data.config;
206
+ this.context = data.context;
207
+ this.result = data.result;
208
+ this.uuid = data.uuid;
209
+ this._selector = undefined;
210
+ }
211
+
212
+ get selector(): cheerio.CheerioAPI {
213
+ if (!this._selector) {
214
+ const headers = normalizeHeaders(this.result.response_headers);
215
+ if (!headers['content-type'].includes('text/html')) {
216
+ throw new errors.ContentTypeError(
217
+ `Cannot use selector on non-html content-type, received: ${this.result.response_headers['content-type']}`,
218
+ );
219
+ }
220
+ this._selector = cheerio.load(this.result.content);
221
+ }
222
+ return this._selector;
223
+ }
224
+ }
225
+
226
+ export type ScrapeResultData = Record<string | number | symbol, never>;
227
+
228
+ export type ClassifyOptions = {
229
+ url: string;
230
+ statusCode: number;
231
+ headers?: Record<string, string>;
232
+ body?: string | null;
233
+ method?: string;
234
+ };
235
+
236
+ export type ClassifyResult = {
237
+ blocked: boolean;
238
+ antibot: string | null;
239
+ cost: number;
240
+ };
241
+
242
+ export type AccountData = {
243
+ account: {
244
+ account_id: string;
245
+ currency: string;
246
+ timezone: string;
247
+ };
248
+ project: {
249
+ allow_extra_usage: boolean;
250
+ allowed_networks: Array<string>;
251
+ budget_limit: any;
252
+ budget_spent: any;
253
+ concurrency_limit?: number;
254
+ name: string;
255
+ quota_reached: boolean;
256
+ scrape_request_count: number;
257
+ scrape_request_limit: number;
258
+ tags: Array<string>;
259
+ };
260
+ subscription: {
261
+ billing: {
262
+ current_extra_scrape_request_price: { currency: string; amount: number };
263
+ extra_scrape_request_price_per_10k: { currency: string; amount: number };
264
+ ongoing_payment: { currency: string; amount: number };
265
+ plan_price: { currency: string; amount: number };
266
+ };
267
+ extra_scrape_allowed: boolean;
268
+ max_concurrency: number;
269
+ period: {
270
+ start: string;
271
+ end: string;
272
+ };
273
+ plan_name: string;
274
+ usage: {
275
+ schedule: { current: number; limit: number };
276
+ spider: { current: number; limit: number };
277
+ scrape: {
278
+ concurrent_limit: number;
279
+ concurrent_remaining: number;
280
+ concurrent_usage: number;
281
+ current: number;
282
+ extra: number;
283
+ limit: number;
284
+ remaining: number;
285
+ };
286
+ };
287
+ };
288
+ };
289
+
290
+ export type ScreenshotMetadata = {
291
+ extension_name: string;
292
+ upstream_status_code: number;
293
+ upstream_url: string;
294
+ };
295
+
296
+ export class ScreenshotResult {
297
+ image: ArrayBuffer;
298
+ metadata: ScreenshotMetadata;
299
+ result: object | null;
300
+
301
+ constructor(response: Response, data: ArrayBuffer) {
302
+ this.image = data;
303
+ this.metadata = this.defineMetadata(response);
304
+ this.result = this.decodeResponse(response, data); // raw result
305
+ }
306
+
307
+ private defineMetadata(response: Response): ScreenshotMetadata {
308
+ const headers = normalizeHeaders(response.headers);
309
+ const contentType = headers['content-type'];
310
+ let extension_name = '';
311
+ if (contentType) {
312
+ extension_name = contentType.split('/')[1].split(';')[0];
313
+ }
314
+ return {
315
+ extension_name: extension_name,
316
+ upstream_status_code: parseInt(headers['x-scrapfly-upstream-http-code'] || '200', 10),
317
+ upstream_url: headers['x-scrapfly-upstream-url'] || '',
318
+ };
319
+ }
320
+
321
+ private decodeResponse(response: Response, data: ArrayBuffer): object | null {
322
+ const headers = normalizeHeaders(response.headers);
323
+ if (headers['content-type'] === 'json') {
324
+ return JSON.parse(new TextDecoder().decode(data));
325
+ }
326
+ return null;
327
+ }
328
+ }
329
+
330
+ export class ExtractionResult {
331
+ data: string;
332
+ content_type: string;
333
+ data_quality?: string;
334
+
335
+ constructor(response: { data: string; content_type: string; data_quality?: string }) {
336
+ this.data = response.data;
337
+ this.content_type = response.content_type;
338
+ this.data_quality = response.data_quality;
339
+ }
340
+ }