scrapfly-sdk 0.10.4 → 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 (88) 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/browserconfig.ts +70 -1
  77. package/src/client.ts +34 -6
  78. package/src/crawl.ts +4 -0
  79. package/src/crawlerresult.ts +7 -0
  80. package/src/errors.ts +43 -16
  81. package/src/extractionconfig.ts +58 -16
  82. package/src/logger.ts +2 -0
  83. package/src/main.ts +27 -0
  84. package/src/monitoringconfig.ts +49 -11
  85. package/src/result.ts +2 -0
  86. package/src/scrapeconfig.ts +10 -8
  87. package/src/screenshotconfig.ts +59 -26
  88. package/src/webhookserver.ts +24 -0
@@ -1,43 +1,81 @@
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. */
1
10
  export type MonitoringDataFormat = 'structured' | 'prometheus';
2
11
 
12
+ /** Rolling time window to aggregate metrics over. */
3
13
  export type MonitoringPeriod = 'subscription' | 'last7d' | 'last24h' | 'last1h' | 'last5m';
4
14
 
15
+ /** Grouping level for aggregated metrics. */
5
16
  export type MonitoringAggregation = 'account' | 'project' | 'target';
6
17
 
18
+ /** Options common to the per-product Monitoring metrics endpoints. */
7
19
  export interface MonitoringMetricsOptions {
20
+ /** Payload format. Defaults to `structured`. */
8
21
  format?: MonitoringDataFormat;
22
+ /** Time window to aggregate over. */
9
23
  period?: MonitoringPeriod;
24
+ /** Grouping levels. */
10
25
  aggregation?: MonitoringAggregation[];
11
- /** Fold WEBHOOK origin events (callbacks executed by the webhook
12
- * worker) into this product's totals. Defaults to false to match
13
- * the dashboard's default view. */
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
+ */
14
31
  includeWebhook?: boolean;
15
32
  }
16
33
 
34
+ /** Options for the per-target ("per domain") Monitoring endpoints. */
17
35
  export interface MonitoringTargetMetricsOptions {
36
+ /** Domain to query metrics for. Required. */
18
37
  domain: string;
38
+ /** Collapse subdomains into the parent domain. */
19
39
  groupSubdomain?: boolean;
40
+ /** Time window to aggregate over. */
20
41
  period?: MonitoringPeriod;
42
+ /** Window start (takes precedence over `period` when set together). */
21
43
  start?: Date;
44
+ /** Window end. */
22
45
  end?: Date;
23
- /** Fold WEBHOOK origin events into this product's totals. */
46
+ /** Fold WEBHOOK-origin events into this product's totals. */
24
47
  includeWebhook?: boolean;
25
48
  }
26
49
 
27
- /** Options for the Cloud Browser monitoring endpoints. Cloud Browser
28
- * is session-based (one allocation = one long-lived browser, billed
29
- * by runtime + bandwidth) and exposes a distinct shape from the
30
- * request-based products. There is no `domain`/`target` and no
31
- * `includeWebhook`. */
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
+ */
32
58
  export interface CloudBrowserMonitoringOptions {
59
+ /** Time window to aggregate over. */
33
60
  period?: MonitoringPeriod;
34
- /** Optional filter to a single proxy pool
35
- * (e.g. `public_datacenter_pool`, `public_residential_pool`, `byop`). */
61
+ /**
62
+ * Optional filter to a single proxy pool
63
+ * (e.g. `public_datacenter_pool`, `public_residential_pool`, `byop`).
64
+ */
36
65
  proxyPool?: string;
66
+ /** Window start. */
37
67
  start?: Date;
68
+ /** Window end. */
38
69
  end?: Date;
39
70
  }
40
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
+ */
41
79
  export function formatMonitoringDate(d: Date): string {
42
80
  const pad = (n: number): string => n.toString().padStart(2, '0');
43
81
  const y = d.getUTCFullYear();
package/src/result.ts CHANGED
@@ -39,6 +39,7 @@ export type ConfigData = {
39
39
  auto_scroll?: boolean;
40
40
  };
41
41
 
42
+ /** Request-context metadata included on every scrape result (cost, proxy, fingerprint, ...). */
42
43
  export type ContextData = {
43
44
  asp: boolean;
44
45
  bandwidth_consumed: number;
@@ -95,6 +96,7 @@ export type ContextData = {
95
96
  webhook?: string;
96
97
  };
97
98
 
99
+ /** Payload of a successful scrape (response body, status, headers, browser trace). */
98
100
  export type ResultData = {
99
101
  browser_data: {
100
102
  javascript_evaluation_result?: string;
@@ -19,25 +19,27 @@ export enum ScreenshotFlags {
19
19
  HIGH_QUALITY = 'high_quality',
20
20
  }
21
21
 
22
+ /** Output format requested for the scraped content. */
22
23
  export enum Format {
23
- /**
24
- Format of the scraped content.
25
- Attributes:
26
- JSON: JSON format.
27
- TEXT: Text format.
28
- MARKDOWN: Markdown format.
29
- CLEAN_HTML: Clean HTML format.
30
- */
24
+ /** Structured JSON. */
31
25
  JSON = 'json',
26
+ /** Plain text with markup stripped. */
32
27
  TEXT = 'text',
28
+ /** Markdown rendering of the page. */
33
29
  MARKDOWN = 'markdown',
30
+ /** Readability-cleaned HTML. */
34
31
  CLEAN_HTML = 'clean_html',
32
+ /** Raw HTML as returned by the target. */
35
33
  RAW = 'raw',
36
34
  }
37
35
 
36
+ /** Additional filters applied to the chosen {@link Format}. */
38
37
  export enum FormatOption {
38
+ /** Strip hyperlinks from the output. */
39
39
  NO_LINKS = 'no_links',
40
+ /** Strip images from the output. */
40
41
  NO_IMAGES = 'no_images',
42
+ /** Keep only the "content" section (drop navigation, sidebars, footers). */
41
43
  ONLY_CONTENT = 'only_content',
42
44
  }
43
45
 
@@ -2,79 +2,105 @@ import { ScreenshotConfigError } from './errors.js';
2
2
  import { urlsafe_b64encode } from './utils.js';
3
3
  import { log } from './logger.js';
4
4
 
5
+ /** Toggles that customize how the screenshot is captured. */
5
6
  export enum Options {
6
- /**
7
- Options to customize the screenshot behavior
8
- Attributes:
9
- LOAD_IMAGES: Enable image rendering with the request, add extra usage for the bandwidth consumed.
10
- DARK_MODE: Enable dark mode display.
11
- BLOCK_BANNERS: Block cookies banners and overlay that cover the screen.
12
- PRINT_MEDIA_FORMAT: Render the page in the print mode.
13
- */
7
+ /** Render images before capturing. Consumes additional bandwidth. */
14
8
  LOAD_IMAGES = 'load_images',
9
+ /** Render the page in dark mode. */
15
10
  DARK_MODE = 'dark_mode',
11
+ /** Block cookie banners and overlays that would otherwise occlude the page. */
16
12
  BLOCK_BANNERS = 'block_banners',
13
+ /** Render the page using the `print` media query. */
17
14
  PRINT_MEDIA_FORMAT = 'print_media_format',
18
15
  }
19
16
 
17
+ /** Output format of the screenshot image. */
20
18
  export enum Format {
21
- /**
22
- Format of the screenshot image.
23
- Attributes:
24
- JPG: JPG format.
25
- PNG: PNG format.
26
- WEBP: WEBP format.
27
- GIF: GIF format.
28
- */
19
+ /** JPEG encoding. */
29
20
  JPG = 'jpg',
21
+ /** PNG encoding. */
30
22
  PNG = 'png',
23
+ /** WebP encoding. */
31
24
  WEBP = 'webp',
25
+ /** Animated GIF encoding. */
32
26
  GIF = 'gif',
33
27
  }
34
28
 
29
+ /**
30
+ * Visual impairments to simulate when capturing the screenshot.
31
+ * Useful for WCAG accessibility testing.
32
+ */
35
33
  export enum VisionDeficiency {
36
- /**
37
- Simulate vision deficiency for accessibility testing (WCAG compliance)
38
- Attributes:
39
- DEUTERANOPIA: Difficulty distinguishing green from red; green appears beige/gray
40
- PROTANOPIA: Reduced sensitivity to red light; red appears dark/black
41
- TRITANOPIA: Difficulty distinguishing blue from yellow and violet from red
42
- ACHROMATOPSIA: Complete inability to perceive color; sees only in grayscale
43
- REDUCED_CONTRAST: Simulates reduced contrast due to aging, low light, or other factors
44
- BLURRED_VISION: Simulates uncorrected refractive errors or age-related vision loss
45
- */
34
+ /** Difficulty distinguishing green from red; green appears beige/gray. */
46
35
  DEUTERANOPIA = 'deuteranopia',
36
+ /** Reduced sensitivity to red light; red appears dark/black. */
47
37
  PROTANOPIA = 'protanopia',
38
+ /** Difficulty distinguishing blue from yellow and violet from red. */
48
39
  TRITANOPIA = 'tritanopia',
40
+ /** Complete color blindness; the image is grayscale. */
49
41
  ACHROMATOPSIA = 'achromatopsia',
42
+ /** Reduced contrast (aging, low light). */
50
43
  REDUCED_CONTRAST = 'reducedContrast',
44
+ /** Blurred vision (uncorrected refractive errors). */
51
45
  BLURRED_VISION = 'blurredVision',
52
46
  }
53
47
 
48
+ /**
49
+ * User-facing options accepted by {@link ScreenshotConfig}. See the
50
+ * [Screenshot API reference](https://scrapfly.io/docs/screenshot-api)
51
+ * for the authoritative list of valid values.
52
+ */
54
53
  type ScreenshotConfigOptions = {
54
+ /** URL of the page to screenshot. Required. */
55
55
  url: string;
56
+ /** Output image format. Defaults to JPG. */
56
57
  format?: 'jpg' | 'png' | 'webp' | 'gif' | Format;
58
+ /** Capture area: `fullpage`, `viewport`, or a CSS selector. */
57
59
  capture?: string;
60
+ /** Viewport resolution, formatted as "widthxheight". */
58
61
  resolution?: string;
62
+ /** ISO country code for geo-targeting the rendering browser. */
59
63
  country?: string;
64
+ /** Request timeout in milliseconds. */
60
65
  timeout?: number;
66
+ /** Additional delay (ms) after load before capturing. */
61
67
  rendering_wait?: number;
68
+ /** CSS selector to wait for before capturing. */
62
69
  wait_for_selector?: string;
70
+ /** Behavior toggles. See {@link Options}. */
63
71
  options?: ('load_images' | 'dark_mode' | 'block_banners' | 'print_media_format' | Options)[];
72
+ /** Scroll to the bottom of the page before capture. */
64
73
  auto_scroll?: boolean;
74
+ /** Custom JavaScript to run on the page before capture. */
65
75
  js?: string;
76
+ /** Serve from Scrapfly's cache when possible. */
66
77
  cache?: boolean;
78
+ /** TTL in seconds for the cached entry. Ignored if cache is disabled. */
67
79
  cache_ttl?: number;
80
+ /** Invalidate the existing cache entry before capturing. */
68
81
  cache_clear?: boolean;
82
+ /** Vision deficiency to simulate. See {@link VisionDeficiency}. */
69
83
  vision_deficiency?: 'deuteranopia' | 'protanopia' | 'tritanopia' | 'achromatopsia' | 'blurredVision' | 'reducedContrast' | VisionDeficiency;
84
+ /** Webhook name to notify when the screenshot completes. */
70
85
  webhook?: string;
71
86
  };
72
87
 
88
+ /**
89
+ * Configuration for a single screenshot request. Instances are passed
90
+ * to {@link ScrapflyClient.screenshot}; this class is responsible for
91
+ * validating options and encoding them into Scrapfly's query string
92
+ * format.
93
+ */
73
94
  export class ScreenshotConfig {
95
+ /** URL of the page to screenshot. */
74
96
  url: string;
97
+ /** Output image format. */
75
98
  format?: 'jpg' | 'png' | 'webp' | 'gif' | Format;
99
+ /** Capture area (`fullpage`, `viewport`, or a CSS selector). */
76
100
  capture?: string;
101
+ /** Viewport resolution (`widthxheight`). */
77
102
  resolution?: string;
103
+ /** Geo-targeting country (ISO code). */
78
104
  country?: string = undefined;
79
105
  timeout?: number;
80
106
  rendering_wait?: number;
@@ -128,6 +154,13 @@ export class ScreenshotConfig {
128
154
  }
129
155
  }
130
156
 
157
+ /**
158
+ * Encode this config as the query-string parameters sent to the
159
+ * Screenshot API.
160
+ *
161
+ * @param options.key Scrapfly API key.
162
+ * @returns A record suitable for `URLSearchParams` construction.
163
+ */
131
164
  toApiParams(options: { key: string }): Record<string, any> {
132
165
  const params: Record<string, any> = {
133
166
  key: options.key,
@@ -7,6 +7,16 @@ export enum ResourceType {
7
7
  PING = 'ping',
8
8
  }
9
9
 
10
+ /**
11
+ * Verify that a webhook request was sent by Scrapfly by comparing the
12
+ * HMAC-SHA256 signature on the request body against the configured
13
+ * signing secret.
14
+ *
15
+ * @param body Raw request body as received (do NOT decode).
16
+ * @param signature Value of the `Scrapfly-Signature` header (hex).
17
+ * @param signingSecret Webhook signing secret configured in the dashboard (hex).
18
+ * @returns `true` if the signature matches.
19
+ */
10
20
  export function verifySignature(body: Buffer, signature: string, signingSecret: string): boolean {
11
21
  const secret = Buffer.from(signingSecret, 'hex');
12
22
  const hmac = crypto.createHmac('sha256', secret);
@@ -16,6 +26,20 @@ export function verifySignature(body: Buffer, signature: string, signingSecret:
16
26
  }
17
27
 
18
28
 
29
+ /**
30
+ * Start a tiny Express server that receives Scrapfly webhook callbacks
31
+ * and dispatches them to the provided `callback`. Intended for quick
32
+ * integration and local development; production setups should wire
33
+ * Scrapfly webhooks directly into their own HTTP stack via
34
+ * {@link verifySignature}.
35
+ *
36
+ * Requires the `express` package to be installed.
37
+ *
38
+ * @param callback Invoked with the parsed webhook body, its resource
39
+ * type (`scrape` or `ping`), and the raw Express request.
40
+ * @param app Optional pre-configured Express app to mount on.
41
+ * @returns The underlying Express app (for further configuration or `.listen()`).
42
+ */
19
43
  export async function createServer(
20
44
  callback: (data: any, resourceType: string, request: any) => void,
21
45
  app?: any,