scrapfly-sdk 0.10.4 → 0.10.6
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/batch.d.ts.map +1 -1
- package/esm/batch.js +6 -0
- package/esm/browserconfig.d.ts +80 -1
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +54 -1
- package/esm/client.d.ts +65 -0
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +157 -5
- package/esm/crawl.d.ts +4 -0
- package/esm/crawl.d.ts.map +1 -1
- package/esm/crawl.js +4 -0
- package/esm/crawlerresult.d.ts +7 -0
- package/esm/crawlerresult.d.ts.map +1 -1
- package/esm/errors.d.ts +43 -0
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js +43 -16
- package/esm/extractionconfig.d.ts +54 -9
- package/esm/extractionconfig.d.ts.map +1 -1
- package/esm/extractionconfig.js +37 -13
- package/esm/logger.d.ts +1 -0
- package/esm/logger.d.ts.map +1 -1
- package/esm/logger.js +1 -0
- package/esm/main.d.ts +29 -0
- package/esm/main.d.ts.map +1 -1
- package/esm/main.js +28 -0
- package/esm/monitoringconfig.d.ts +48 -11
- package/esm/monitoringconfig.d.ts.map +1 -1
- package/esm/monitoringconfig.js +14 -0
- package/esm/result.d.ts +2 -0
- package/esm/result.d.ts.map +1 -1
- package/esm/schedule.d.ts +93 -0
- package/esm/schedule.d.ts.map +1 -0
- package/esm/schedule.js +40 -0
- package/esm/scrapeconfig.d.ts +10 -8
- package/esm/scrapeconfig.d.ts.map +1 -1
- package/esm/scrapeconfig.js +10 -8
- package/esm/screenshotconfig.d.ts +59 -26
- package/esm/screenshotconfig.d.ts.map +1 -1
- package/esm/screenshotconfig.js +38 -26
- package/esm/webhookserver.d.ts +28 -0
- package/esm/webhookserver.d.ts.map +1 -1
- package/esm/webhookserver.js +34 -3
- package/package.json +1 -1
- package/script/batch.d.ts.map +1 -1
- package/script/batch.js +6 -0
- package/script/browserconfig.d.ts +80 -1
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +54 -1
- package/script/client.d.ts +65 -0
- package/script/client.d.ts.map +1 -1
- package/script/client.js +157 -5
- package/script/crawl.d.ts +4 -0
- package/script/crawl.d.ts.map +1 -1
- package/script/crawl.js +4 -0
- package/script/crawlerresult.d.ts +7 -0
- package/script/crawlerresult.d.ts.map +1 -1
- package/script/errors.d.ts +43 -0
- package/script/errors.d.ts.map +1 -1
- package/script/errors.js +43 -16
- package/script/extractionconfig.d.ts +54 -9
- package/script/extractionconfig.d.ts.map +1 -1
- package/script/extractionconfig.js +37 -13
- package/script/logger.d.ts +1 -0
- package/script/logger.d.ts.map +1 -1
- package/script/logger.js +1 -0
- package/script/main.d.ts +29 -0
- package/script/main.d.ts.map +1 -1
- package/script/main.js +30 -1
- package/script/monitoringconfig.d.ts +48 -11
- package/script/monitoringconfig.d.ts.map +1 -1
- package/script/monitoringconfig.js +14 -0
- package/script/result.d.ts +2 -0
- package/script/result.d.ts.map +1 -1
- package/script/schedule.d.ts +93 -0
- package/script/schedule.d.ts.map +1 -0
- package/script/schedule.js +44 -0
- package/script/scrapeconfig.d.ts +10 -8
- package/script/scrapeconfig.d.ts.map +1 -1
- package/script/scrapeconfig.js +10 -8
- package/script/screenshotconfig.d.ts +59 -26
- package/script/screenshotconfig.d.ts.map +1 -1
- package/script/screenshotconfig.js +38 -26
- package/script/webhookserver.d.ts +28 -0
- package/script/webhookserver.d.ts.map +1 -1
- package/script/webhookserver.js +34 -3
- package/src/batch.ts +7 -1
- package/src/browserconfig.ts +82 -1
- package/src/client.ts +227 -7
- package/src/crawl.ts +4 -0
- package/src/crawlerresult.ts +7 -0
- package/src/errors.ts +43 -16
- package/src/extractionconfig.ts +58 -16
- package/src/logger.ts +2 -0
- package/src/main.ts +37 -0
- package/src/monitoringconfig.ts +49 -11
- package/src/result.ts +2 -0
- package/src/schedule.ts +101 -0
- package/src/scrapeconfig.ts +10 -8
- package/src/screenshotconfig.ts +59 -26
- package/src/webhookserver.ts +34 -3
package/src/extractionconfig.ts
CHANGED
|
@@ -2,58 +2,93 @@ import * as errors from './errors.js';
|
|
|
2
2
|
import { urlsafe_b64encode } from './utils.js';
|
|
3
3
|
import { ExtractionConfigError } from './errors.js';
|
|
4
4
|
|
|
5
|
+
/** Compression format declared for documents submitted to the Extraction API. */
|
|
5
6
|
export enum CompressionFormat {
|
|
6
|
-
/**
|
|
7
|
-
Document compression format.
|
|
8
|
-
|
|
9
|
-
Attributes:
|
|
10
|
-
GZIP: gzip format.
|
|
11
|
-
ZSTD: zstd format.
|
|
12
|
-
DEFLATE: deflate.
|
|
13
|
-
"""
|
|
14
|
-
*/
|
|
7
|
+
/** gzip compression. */
|
|
15
8
|
GZIP = 'gzip',
|
|
9
|
+
/** zstd compression. */
|
|
16
10
|
ZSTD = 'zstd',
|
|
11
|
+
/** DEFLATE compression. */
|
|
17
12
|
DEFLATE = 'deflate',
|
|
18
13
|
}
|
|
19
14
|
|
|
15
|
+
/**
|
|
16
|
+
* User-facing options accepted by {@link ExtractionConfig}. See the
|
|
17
|
+
* [Extraction API reference](https://scrapfly.io/docs/extraction-api)
|
|
18
|
+
* for the authoritative list of valid values.
|
|
19
|
+
*/
|
|
20
20
|
type ExtractionConfigOptions = {
|
|
21
|
+
/** Document body to extract from. */
|
|
21
22
|
body: string;
|
|
23
|
+
/** Content-Type of the body (e.g. "text/html"). */
|
|
22
24
|
content_type: string;
|
|
25
|
+
/** URL associated with the document (used for link resolution). */
|
|
23
26
|
url?: string;
|
|
27
|
+
/** Character set of the document. */
|
|
24
28
|
charset?: string;
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
/** Name of a saved extraction template. */
|
|
30
|
+
extraction_template?: string;
|
|
31
|
+
/** Inline extraction template, used instead of a saved one. */
|
|
32
|
+
extraction_ephemeral_template?: object;
|
|
33
|
+
/** Natural-language prompt for LLM-based extraction. */
|
|
27
34
|
extraction_prompt?: string;
|
|
35
|
+
/** Override the LLM model used for prompt-based extraction. */
|
|
28
36
|
extraction_model?: string;
|
|
37
|
+
/** Whether the provided body is already compressed. */
|
|
29
38
|
is_document_compressed?: boolean;
|
|
39
|
+
/** Compression format of the body. See {@link CompressionFormat}. */
|
|
30
40
|
document_compression_format?: 'gzip' | 'zstd' | 'deflate' | CompressionFormat;
|
|
41
|
+
/** Webhook name to notify when extraction completes. */
|
|
31
42
|
webhook?: string;
|
|
43
|
+
/** Request timeout in milliseconds. */
|
|
32
44
|
timeout?: number;
|
|
33
45
|
|
|
34
|
-
|
|
46
|
+
/** @deprecated Use {@link extraction_template} instead. */
|
|
35
47
|
template?: string;
|
|
48
|
+
/** @deprecated Use {@link extraction_ephemeral_template} instead. */
|
|
36
49
|
ephemeral_template?: object;
|
|
37
50
|
};
|
|
38
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Configuration for an Extraction API call. Holds the document to
|
|
54
|
+
* extract from and the template/prompt/model that should drive the
|
|
55
|
+
* extraction.
|
|
56
|
+
*/
|
|
39
57
|
export class ExtractionConfig {
|
|
58
|
+
/** Document body. */
|
|
40
59
|
body: string | Uint8Array;
|
|
60
|
+
/** Content-Type of the body. */
|
|
41
61
|
content_type: string;
|
|
62
|
+
/** URL associated with the document. */
|
|
42
63
|
url?: string;
|
|
64
|
+
/** Character set of the document. */
|
|
43
65
|
charset?: string;
|
|
44
|
-
|
|
45
|
-
|
|
66
|
+
/** Saved extraction template name. */
|
|
67
|
+
extraction_template?: string;
|
|
68
|
+
/** Inline extraction template. */
|
|
69
|
+
extraction_ephemeral_template?: object;
|
|
70
|
+
/** Natural-language extraction prompt. */
|
|
46
71
|
extraction_prompt?: string;
|
|
72
|
+
/** LLM model override for prompt-based extraction. */
|
|
47
73
|
extraction_model?: string;
|
|
74
|
+
/** Whether the body is pre-compressed. */
|
|
48
75
|
is_document_compressed?: boolean;
|
|
76
|
+
/** Compression format of the body. */
|
|
49
77
|
document_compression_format?: 'gzip' | 'zstd' | 'deflate' | CompressionFormat;
|
|
78
|
+
/** Webhook name to notify on completion. */
|
|
50
79
|
webhook?: string;
|
|
80
|
+
/** Request timeout in milliseconds. */
|
|
51
81
|
timeout?: number;
|
|
52
82
|
|
|
53
|
-
|
|
83
|
+
/** @deprecated Use {@link extraction_template} instead. */
|
|
54
84
|
template?: string;
|
|
85
|
+
/** @deprecated Use {@link extraction_ephemeral_template} instead. */
|
|
55
86
|
ephemeral_template?: object;
|
|
56
87
|
|
|
88
|
+
/**
|
|
89
|
+
* @param options See {@link ExtractionConfigOptions}.
|
|
90
|
+
* @throws {@link errors.ExtractionConfigError} if an unknown option is supplied.
|
|
91
|
+
*/
|
|
57
92
|
constructor(options: ExtractionConfigOptions) {
|
|
58
93
|
this.validateOptions(options);
|
|
59
94
|
if (options.template) {
|
|
@@ -104,11 +139,18 @@ export class ExtractionConfig {
|
|
|
104
139
|
}
|
|
105
140
|
}
|
|
106
141
|
|
|
142
|
+
/**
|
|
143
|
+
* Encode this config as the query-string parameters sent to the
|
|
144
|
+
* Extraction API.
|
|
145
|
+
*
|
|
146
|
+
* @param options.key Scrapfly API key.
|
|
147
|
+
* @returns A record suitable for `URLSearchParams` construction.
|
|
148
|
+
* @throws {@link ExtractionConfigError} on conflicting or incompatible options.
|
|
149
|
+
*/
|
|
107
150
|
toApiParams(options: { key: string }): Record<string, any> {
|
|
108
151
|
const params: Record<string, any> = {
|
|
109
152
|
key: options.key,
|
|
110
153
|
};
|
|
111
|
-
// params.body = this.body;
|
|
112
154
|
params.content_type = this.content_type;
|
|
113
155
|
|
|
114
156
|
if (this.url) {
|
package/src/logger.ts
CHANGED
package/src/main.ts
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
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
|
+
*/
|
|
1
27
|
export { ScrapflyClient } from './client.js';
|
|
2
28
|
export {
|
|
3
29
|
Format as ScrapeFormat,
|
|
@@ -14,6 +40,7 @@ export {
|
|
|
14
40
|
export { ExtractionConfig } from './extractionconfig.js';
|
|
15
41
|
export { BrowserConfig, ProxyPool, OperatingSystem } from './browserconfig.js';
|
|
16
42
|
export type { BrowserConfigOptions } from './browserconfig.js';
|
|
43
|
+
/** Namespace of every error class the SDK can throw. See {@link ScrapflyError}. */
|
|
17
44
|
export * as errors from './errors.js';
|
|
18
45
|
|
|
19
46
|
export { ExtractionResult, ScrapeResult, ScreenshotResult } from './result.js';
|
|
@@ -56,6 +83,16 @@ export type {
|
|
|
56
83
|
MonitoringTargetMetricsOptions,
|
|
57
84
|
} from './monitoringconfig.js';
|
|
58
85
|
|
|
86
|
+
export { ScheduleAPIError } from './schedule.js';
|
|
87
|
+
export type {
|
|
88
|
+
CreateScheduleRequest,
|
|
89
|
+
ListSchedulesOptions,
|
|
90
|
+
Schedule,
|
|
91
|
+
ScheduleEnd,
|
|
92
|
+
ScheduleRecurrence,
|
|
93
|
+
UpdateScheduleRequest,
|
|
94
|
+
} from './schedule.js';
|
|
95
|
+
|
|
59
96
|
export { log } from './logger.js';
|
|
60
97
|
|
|
61
98
|
export { createServer, verifySignature } from './webhookserver.js';
|
package/src/monitoringconfig.ts
CHANGED
|
@@ -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
|
-
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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
|
|
46
|
+
/** Fold WEBHOOK-origin events into this product's totals. */
|
|
24
47
|
includeWebhook?: boolean;
|
|
25
48
|
}
|
|
26
49
|
|
|
27
|
-
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
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
|
-
/**
|
|
35
|
-
*
|
|
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;
|
package/src/schedule.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public schedule client for the Scrapfly API.
|
|
3
|
+
*
|
|
4
|
+
* Wraps `/scrape/schedules`, `/screenshot/schedules`, `/crawl/schedules`
|
|
5
|
+
* and the cross-kind `/schedules` endpoints. The server returns fully-formed
|
|
6
|
+
* Schedule objects; we surface them as-is so callers always see the live
|
|
7
|
+
* server shape.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Bounds a recurring schedule by either a date or a fire count. */
|
|
11
|
+
export interface ScheduleEnd {
|
|
12
|
+
type: 'date' | 'count';
|
|
13
|
+
date?: string;
|
|
14
|
+
count?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* When a schedule fires next. Cron mode wins when `cron` is set; otherwise
|
|
19
|
+
* `interval` + `unit` drive the cadence.
|
|
20
|
+
*/
|
|
21
|
+
export interface ScheduleRecurrence {
|
|
22
|
+
cron?: string;
|
|
23
|
+
interval?: number;
|
|
24
|
+
unit?: 'minute' | 'hour' | 'day' | 'week' | 'month';
|
|
25
|
+
days?: string[];
|
|
26
|
+
ends?: ScheduleEnd;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Public-facing request envelope for creating a schedule. The kind-specific
|
|
31
|
+
* config (`scrape_config` / `screenshot_config` / `crawler_config`) is
|
|
32
|
+
* supplied as a separate argument by the matching `create*Schedule` method.
|
|
33
|
+
*/
|
|
34
|
+
export interface CreateScheduleRequest {
|
|
35
|
+
webhook_name: string;
|
|
36
|
+
recurrence?: ScheduleRecurrence;
|
|
37
|
+
scheduled_date?: string;
|
|
38
|
+
allow_concurrency?: boolean;
|
|
39
|
+
retry_on_failure?: boolean;
|
|
40
|
+
max_retries?: number;
|
|
41
|
+
notes?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** PATCH payload. Only fields explicitly set are forwarded. */
|
|
45
|
+
export interface UpdateScheduleRequest {
|
|
46
|
+
recurrence?: ScheduleRecurrence;
|
|
47
|
+
scheduled_date?: string;
|
|
48
|
+
allow_concurrency?: boolean;
|
|
49
|
+
retry_on_failure?: boolean;
|
|
50
|
+
max_retries?: number;
|
|
51
|
+
notes?: string;
|
|
52
|
+
scrape_config?: Record<string, unknown>;
|
|
53
|
+
screenshot_config?: Record<string, unknown>;
|
|
54
|
+
crawler_config?: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Server-side schedule record. Returned by every read or mutation endpoint. */
|
|
58
|
+
export interface Schedule {
|
|
59
|
+
id: string;
|
|
60
|
+
kind: string;
|
|
61
|
+
status: string;
|
|
62
|
+
next_scheduled_date?: string;
|
|
63
|
+
scheduled_date?: string;
|
|
64
|
+
recurrence?: ScheduleRecurrence;
|
|
65
|
+
metadata?: Record<string, unknown>;
|
|
66
|
+
notes?: string | null;
|
|
67
|
+
created_by?: string | null;
|
|
68
|
+
created_at: string;
|
|
69
|
+
updated_at: string;
|
|
70
|
+
cancelled_at?: string | null;
|
|
71
|
+
allow_concurrency: boolean;
|
|
72
|
+
retry_on_failure: boolean;
|
|
73
|
+
max_retries: number;
|
|
74
|
+
webhook_uuid?: string | null;
|
|
75
|
+
user_uuid?: string | null;
|
|
76
|
+
consecutive_failures?: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface ListSchedulesOptions {
|
|
80
|
+
status?: string;
|
|
81
|
+
kind?: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Raised on any non-2xx response from a `/schedules/*` endpoint. The
|
|
86
|
+
* `code` property carries the public `ERR::SCHEDULER::*` identifier so
|
|
87
|
+
* callers can branch on it without parsing the message string.
|
|
88
|
+
*/
|
|
89
|
+
export class ScheduleAPIError extends Error {
|
|
90
|
+
code: string;
|
|
91
|
+
httpStatusCode: number;
|
|
92
|
+
details: unknown;
|
|
93
|
+
|
|
94
|
+
constructor(args: { code: string; message: string; httpStatusCode: number; details?: unknown }) {
|
|
95
|
+
super(args.message);
|
|
96
|
+
this.name = 'ScheduleAPIError';
|
|
97
|
+
this.code = args.code;
|
|
98
|
+
this.httpStatusCode = args.httpStatusCode;
|
|
99
|
+
this.details = args.details;
|
|
100
|
+
}
|
|
101
|
+
}
|
package/src/scrapeconfig.ts
CHANGED
|
@@ -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
|
|
package/src/screenshotconfig.ts
CHANGED
|
@@ -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,
|
package/src/webhookserver.ts
CHANGED
|
@@ -7,15 +7,46 @@ 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 (verify BEFORE any decompression
|
|
16
|
+
* or JSON parsing — the signature is computed on the bytes
|
|
17
|
+
* Scrapfly sent on the wire).
|
|
18
|
+
* @param signature Value of the `X-Scrapfly-Webhook-Signature` header
|
|
19
|
+
* (uppercase hex digest).
|
|
20
|
+
* @param signingSecret Webhook signing secret from the dashboard, passed as-is
|
|
21
|
+
* (UTF-8 string; not hex-encoded).
|
|
22
|
+
* @returns `true` if the signature matches.
|
|
23
|
+
*/
|
|
10
24
|
export function verifySignature(body: Buffer, signature: string, signingSecret: string): boolean {
|
|
11
|
-
const
|
|
12
|
-
const hmac = crypto.createHmac('sha256', secret);
|
|
25
|
+
const hmac = crypto.createHmac('sha256', Buffer.from(signingSecret, 'utf8'));
|
|
13
26
|
hmac.update(body);
|
|
14
27
|
const computed = hmac.digest('hex').toUpperCase();
|
|
15
|
-
|
|
28
|
+
const received = signature.toUpperCase();
|
|
29
|
+
if (computed.length !== received.length) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
return crypto.timingSafeEqual(Buffer.from(computed), Buffer.from(received));
|
|
16
33
|
}
|
|
17
34
|
|
|
18
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Start a tiny Express server that receives Scrapfly webhook callbacks
|
|
38
|
+
* and dispatches them to the provided `callback`. Intended for quick
|
|
39
|
+
* integration and local development; production setups should wire
|
|
40
|
+
* Scrapfly webhooks directly into their own HTTP stack via
|
|
41
|
+
* {@link verifySignature}.
|
|
42
|
+
*
|
|
43
|
+
* Requires the `express` package to be installed.
|
|
44
|
+
*
|
|
45
|
+
* @param callback Invoked with the parsed webhook body, its resource
|
|
46
|
+
* type (`scrape` or `ping`), and the raw Express request.
|
|
47
|
+
* @param app Optional pre-configured Express app to mount on.
|
|
48
|
+
* @returns The underlying Express app (for further configuration or `.listen()`).
|
|
49
|
+
*/
|
|
19
50
|
export async function createServer(
|
|
20
51
|
callback: (data: any, resourceType: string, request: any) => void,
|
|
21
52
|
app?: any,
|