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.
- package/README.md +72 -5
- package/esm/browserconfig.d.ts +57 -9
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +63 -5
- package/esm/browserdownloads.d.ts +70 -0
- package/esm/browserdownloads.d.ts.map +1 -0
- package/esm/browserdownloads.js +215 -0
- package/esm/client.d.ts +224 -8
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +646 -62
- package/esm/crawl.d.ts +83 -1
- package/esm/crawl.d.ts.map +1 -1
- package/esm/crawl.js +73 -0
- package/esm/crawlerconfig.d.ts +54 -3
- package/esm/crawlerconfig.d.ts.map +1 -1
- package/esm/crawlerconfig.js +96 -2
- package/esm/crawlerresult.d.ts +374 -2
- package/esm/crawlerresult.d.ts.map +1 -1
- package/esm/crawlerresult.js +367 -1
- package/esm/errors.d.ts +12 -0
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js +11 -0
- package/esm/main.d.ts +12 -7
- package/esm/main.d.ts.map +1 -1
- package/esm/main.js +10 -5
- package/esm/monitoringconfig.d.ts +2 -2
- package/esm/result.d.ts +11 -1
- package/esm/result.d.ts.map +1 -1
- package/esm/result.js +14 -5
- package/esm/scrapeconfig.d.ts +65 -1
- package/esm/scrapeconfig.d.ts.map +1 -1
- package/esm/scrapeconfig.js +88 -4
- package/esm/screenshotconfig.d.ts +9 -1
- package/esm/screenshotconfig.d.ts.map +1 -1
- package/esm/screenshotconfig.js +12 -1
- package/esm/types.d.ts +128 -0
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js +22 -1
- package/esm/utils.d.ts.map +1 -1
- package/esm/utils.js +16 -2
- package/esm/webhookserver.d.ts +33 -15
- package/esm/webhookserver.d.ts.map +1 -1
- package/esm/webhookserver.js +197 -38
- package/package.json +1 -1
- package/script/browserconfig.d.ts +57 -9
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +63 -5
- package/script/browserdownloads.d.ts +70 -0
- package/script/browserdownloads.d.ts.map +1 -0
- package/script/browserdownloads.js +221 -0
- package/script/client.d.ts +224 -8
- package/script/client.d.ts.map +1 -1
- package/script/client.js +645 -61
- package/script/crawl.d.ts +83 -1
- package/script/crawl.d.ts.map +1 -1
- package/script/crawl.js +73 -0
- package/script/crawlerconfig.d.ts +54 -3
- package/script/crawlerconfig.d.ts.map +1 -1
- package/script/crawlerconfig.js +97 -3
- package/script/crawlerresult.d.ts +374 -2
- package/script/crawlerresult.d.ts.map +1 -1
- package/script/crawlerresult.js +374 -2
- package/script/errors.d.ts +12 -0
- package/script/errors.d.ts.map +1 -1
- package/script/errors.js +12 -1
- package/script/main.d.ts +12 -7
- package/script/main.d.ts.map +1 -1
- package/script/main.js +20 -4
- package/script/monitoringconfig.d.ts +2 -2
- package/script/result.d.ts +11 -1
- package/script/result.d.ts.map +1 -1
- package/script/result.js +14 -5
- package/script/scrapeconfig.d.ts +65 -1
- package/script/scrapeconfig.d.ts.map +1 -1
- package/script/scrapeconfig.js +88 -4
- package/script/screenshotconfig.d.ts +9 -1
- package/script/screenshotconfig.d.ts.map +1 -1
- package/script/screenshotconfig.js +12 -1
- package/script/types.d.ts +128 -0
- package/script/types.d.ts.map +1 -1
- package/script/types.js +23 -0
- package/script/utils.d.ts.map +1 -1
- package/script/utils.js +16 -2
- package/script/webhookserver.d.ts +33 -15
- package/script/webhookserver.d.ts.map +1 -1
- package/script/webhookserver.js +197 -38
- package/src/browserconfig.ts +74 -8
- package/src/browserdownloads.ts +266 -0
- package/src/client.ts +796 -65
- package/src/crawl.ts +106 -0
- package/src/crawlerconfig.ts +128 -5
- package/src/crawlerresult.ts +607 -3
- package/src/errors.ts +12 -0
- package/src/main.ts +55 -5
- package/src/monitoringconfig.ts +2 -2
- package/src/result.ts +17 -5
- package/src/scrapeconfig.ts +92 -3
- package/src/screenshotconfig.ts +14 -1
- package/src/types.ts +159 -1
- package/src/utils.ts +16 -2
- package/src/webhookserver.ts +235 -39
package/esm/webhookserver.d.ts
CHANGED
|
@@ -1,36 +1,54 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer';
|
|
2
|
+
/**
|
|
3
|
+
* Values Scrapfly sends in the `X-Scrapfly-Webhook-Resource-Type` header.
|
|
4
|
+
*
|
|
5
|
+
* ALERT is signed over `<timestamp>.<body>` rather than over the body, and so
|
|
6
|
+
* is verified through a separate path.
|
|
7
|
+
*/
|
|
2
8
|
export declare enum ResourceType {
|
|
3
9
|
SCRAPE = "scrape",
|
|
4
|
-
PING = "ping"
|
|
10
|
+
PING = "ping",
|
|
11
|
+
CRAWL = "crawl",
|
|
12
|
+
EXTRACTION = "extraction",
|
|
13
|
+
SCREENSHOT = "screenshot",
|
|
14
|
+
ALERT = "alert"
|
|
5
15
|
}
|
|
6
16
|
/**
|
|
7
17
|
* Verify that a webhook request was sent by Scrapfly by comparing the
|
|
8
18
|
* HMAC-SHA256 signature on the request body against the configured
|
|
9
19
|
* signing secret.
|
|
10
20
|
*
|
|
11
|
-
* @param body
|
|
12
|
-
*
|
|
13
|
-
*
|
|
21
|
+
* @param body Body as Scrapfly signed it: decompressed, but not JSON-parsed.
|
|
22
|
+
* Signing happens before `Content-Encoding` is applied, so a
|
|
23
|
+
* compressed request must be inflated first.
|
|
14
24
|
* @param signature Value of the `X-Scrapfly-Webhook-Signature` header
|
|
15
|
-
* (uppercase hex digest).
|
|
16
|
-
*
|
|
17
|
-
*
|
|
25
|
+
* (uppercase hex digest). The `-Lowercase` header carries
|
|
26
|
+
* the same digest and is accepted too.
|
|
27
|
+
* @param signingSecret Webhook signing secret, passed as-is (UTF-8 string; not
|
|
28
|
+
* hex-encoded). Pass an array to accept any of several
|
|
29
|
+
* secrets, for key rotation.
|
|
18
30
|
* @returns `true` if the signature matches.
|
|
19
31
|
*/
|
|
20
|
-
export declare function verifySignature(body: Buffer, signature: string, signingSecret: string): boolean;
|
|
32
|
+
export declare function verifySignature(body: Buffer, signature: string | undefined | null, signingSecret: string | string[]): boolean;
|
|
21
33
|
/**
|
|
22
|
-
* Start a tiny Express server that receives Scrapfly webhook callbacks
|
|
23
|
-
* and dispatches them to the provided `callback`.
|
|
24
|
-
* integration and local development; production setups
|
|
25
|
-
* Scrapfly webhooks directly into their own HTTP stack via
|
|
34
|
+
* Start a tiny Express server that receives Scrapfly webhook callbacks,
|
|
35
|
+
* verifies their signature, and dispatches them to the provided `callback`.
|
|
36
|
+
* Intended for quick integration and local development; production setups
|
|
37
|
+
* should wire Scrapfly webhooks directly into their own HTTP stack via
|
|
26
38
|
* {@link verifySignature}.
|
|
27
39
|
*
|
|
40
|
+
* Every request must be signed. A body with no `X-Scrapfly-Webhook-Signature`
|
|
41
|
+
* is rejected with 401 rather than delivered.
|
|
42
|
+
*
|
|
28
43
|
* Requires the `express` package to be installed.
|
|
29
44
|
*
|
|
30
|
-
* @param
|
|
31
|
-
*
|
|
45
|
+
* @param signingSecrets Webhook signing secret(s) from your webhook settings.
|
|
46
|
+
* @param callback Invoked with the verified body, its resource type, and the
|
|
47
|
+
* raw Express request. Awaited, so an async callback's failure yields 500. Bodies are JSON-parsed when the content type says so;
|
|
48
|
+
* binary payloads (screenshot images) arrive as a Buffer.
|
|
32
49
|
* @param app Optional pre-configured Express app to mount on.
|
|
50
|
+
* @param maxBodyBytes Reject bodies larger than this, before any decompression.
|
|
33
51
|
* @returns The underlying Express app (for further configuration or `.listen()`).
|
|
34
52
|
*/
|
|
35
|
-
export declare function createServer(callback: (data: any, resourceType: string, request: any) => void
|
|
53
|
+
export declare function createServer(signingSecrets: string | string[], callback: (data: any, resourceType: string, request: any) => void | Promise<void>, app?: any, maxBodyBytes?: number): Promise<any>;
|
|
36
54
|
//# sourceMappingURL=webhookserver.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webhookserver.d.ts","sourceRoot":"","sources":["../src/webhookserver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"webhookserver.d.ts","sourceRoot":"","sources":["../src/webhookserver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAKrC;;;;;GAKG;AACH,oBAAY,YAAY;IACtB,MAAM,WAAW;IACjB,IAAI,SAAS;IACb,KAAK,UAAU;IACf,UAAU,eAAe;IACzB,UAAU,eAAe;IACzB,KAAK,UAAU;CAChB;AAkCD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,EACpC,aAAa,EAAE,MAAM,GAAG,MAAM,EAAE,GAC/B,OAAO,CA2BT;AAgFD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,YAAY,CAChC,cAAc,EAAE,MAAM,GAAG,MAAM,EAAE,EACjC,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EACjF,GAAG,CAAC,EAAE,GAAG,EACT,YAAY,GAAE,MAAuB,GACpC,OAAO,CAAC,GAAG,CAAC,CAwFd"}
|
package/esm/webhookserver.js
CHANGED
|
@@ -1,50 +1,172 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer';
|
|
2
2
|
import crypto from 'node:crypto';
|
|
3
|
+
import zlib from 'node:zlib';
|
|
3
4
|
import { log } from './logger.js';
|
|
5
|
+
/**
|
|
6
|
+
* Values Scrapfly sends in the `X-Scrapfly-Webhook-Resource-Type` header.
|
|
7
|
+
*
|
|
8
|
+
* ALERT is signed over `<timestamp>.<body>` rather than over the body, and so
|
|
9
|
+
* is verified through a separate path.
|
|
10
|
+
*/
|
|
4
11
|
export var ResourceType;
|
|
5
12
|
(function (ResourceType) {
|
|
6
13
|
ResourceType["SCRAPE"] = "scrape";
|
|
7
14
|
ResourceType["PING"] = "ping";
|
|
15
|
+
ResourceType["CRAWL"] = "crawl";
|
|
16
|
+
ResourceType["EXTRACTION"] = "extraction";
|
|
17
|
+
ResourceType["SCREENSHOT"] = "screenshot";
|
|
18
|
+
ResourceType["ALERT"] = "alert";
|
|
8
19
|
})(ResourceType || (ResourceType = {}));
|
|
20
|
+
/** Replay window for alert webhooks, applied either side of now to tolerate clock skew. */
|
|
21
|
+
const ALERT_TIMESTAMP_TOLERANCE_SECONDS = 300;
|
|
22
|
+
/**
|
|
23
|
+
* Rebuild the `<timestamp>.<body>` message an alert webhook is signed over, or
|
|
24
|
+
* return null when the timestamp is absent, unparseable, or outside the replay
|
|
25
|
+
* window.
|
|
26
|
+
*/
|
|
27
|
+
function alertSignedMessage(timestamp, body, tolerance = ALERT_TIMESTAMP_TOLERANCE_SECONDS) {
|
|
28
|
+
if (!timestamp || !/^\d+$/.test(timestamp)) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
if (Math.abs(Date.now() / 1000 - Number(timestamp)) > tolerance) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
return Buffer.concat([Buffer.from(`${timestamp}.`, 'utf8'), body]);
|
|
35
|
+
}
|
|
36
|
+
const RESOURCE_TYPES = Object.values(ResourceType);
|
|
37
|
+
/**
|
|
38
|
+
* The signature covers the decompressed bytes, so decompression runs before the
|
|
39
|
+
* request is authenticated and needs its own ceiling.
|
|
40
|
+
*/
|
|
41
|
+
const MAX_BODY_BYTES = 256 * 1024 * 1024;
|
|
9
42
|
/**
|
|
10
43
|
* Verify that a webhook request was sent by Scrapfly by comparing the
|
|
11
44
|
* HMAC-SHA256 signature on the request body against the configured
|
|
12
45
|
* signing secret.
|
|
13
46
|
*
|
|
14
|
-
* @param body
|
|
15
|
-
*
|
|
16
|
-
*
|
|
47
|
+
* @param body Body as Scrapfly signed it: decompressed, but not JSON-parsed.
|
|
48
|
+
* Signing happens before `Content-Encoding` is applied, so a
|
|
49
|
+
* compressed request must be inflated first.
|
|
17
50
|
* @param signature Value of the `X-Scrapfly-Webhook-Signature` header
|
|
18
|
-
* (uppercase hex digest).
|
|
19
|
-
*
|
|
20
|
-
*
|
|
51
|
+
* (uppercase hex digest). The `-Lowercase` header carries
|
|
52
|
+
* the same digest and is accepted too.
|
|
53
|
+
* @param signingSecret Webhook signing secret, passed as-is (UTF-8 string; not
|
|
54
|
+
* hex-encoded). Pass an array to accept any of several
|
|
55
|
+
* secrets, for key rotation.
|
|
21
56
|
* @returns `true` if the signature matches.
|
|
22
57
|
*/
|
|
23
58
|
export function verifySignature(body, signature, signingSecret) {
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
59
|
+
const secrets = (Array.isArray(signingSecret) ? signingSecret : [signingSecret]).filter(Boolean);
|
|
60
|
+
// An empty key is a usable HMAC key: verifying against one accepts a digest
|
|
61
|
+
// anybody can compute.
|
|
62
|
+
if (secrets.length === 0) {
|
|
63
|
+
throw new Error('verifySignature requires a non-empty signing secret');
|
|
64
|
+
}
|
|
65
|
+
if (!signature) {
|
|
29
66
|
return false;
|
|
30
67
|
}
|
|
31
|
-
|
|
68
|
+
const received = Buffer.from(signature.trim().toUpperCase(), 'utf8');
|
|
69
|
+
return secrets.some((secret) => {
|
|
70
|
+
const hmac = crypto.createHmac('sha256', Buffer.from(secret, 'utf8'));
|
|
71
|
+
hmac.update(body);
|
|
72
|
+
const computed = Buffer.from(hmac.digest('hex').toUpperCase(), 'utf8');
|
|
73
|
+
// timingSafeEqual throws on a length mismatch; hex digests are fixed-width.
|
|
74
|
+
if (computed.length !== received.length) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
return crypto.timingSafeEqual(computed, received);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Reverse the `Content-Encoding` applied to a webhook body.
|
|
82
|
+
*/
|
|
83
|
+
function decompress(body, contentEncoding, limit) {
|
|
84
|
+
// maxOutputLength aborts mid-inflate; without it the ceiling is buffer.kMaxLength.
|
|
85
|
+
const options = { maxOutputLength: limit };
|
|
86
|
+
switch ((contentEncoding || '').toLowerCase()) {
|
|
87
|
+
case '':
|
|
88
|
+
case 'none':
|
|
89
|
+
case 'identity':
|
|
90
|
+
return body;
|
|
91
|
+
case 'gzip':
|
|
92
|
+
case 'gz':
|
|
93
|
+
return zlib.gunzipSync(body, options);
|
|
94
|
+
case 'deflate':
|
|
95
|
+
return zlib.inflateSync(body, options);
|
|
96
|
+
case 'br':
|
|
97
|
+
case 'brotli':
|
|
98
|
+
return zlib.brotliDecompressSync(body, options);
|
|
99
|
+
case 'zstd': {
|
|
100
|
+
const zstdDecompressSync = zlib.zstdDecompressSync;
|
|
101
|
+
if (typeof zstdDecompressSync !== 'function') {
|
|
102
|
+
throw new Error('zstd webhook bodies require Node 22+; set the webhook content encoding to gzip or none');
|
|
103
|
+
}
|
|
104
|
+
return zstdDecompressSync(body, options);
|
|
105
|
+
}
|
|
106
|
+
default:
|
|
107
|
+
throw new Error(`Unsupported webhook content encoding: ${contentEncoding}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/** Raised when a body is larger than the configured cap, before or after inflation. */
|
|
111
|
+
class WebhookBodyTooLarge extends Error {
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Collect the request body without a body parser in front of it. body-parser
|
|
115
|
+
* answers 415 for any encoding it cannot inflate itself, and hands back a
|
|
116
|
+
* parsed object where the signature needs bytes.
|
|
117
|
+
*/
|
|
118
|
+
function readBody(req, limit) {
|
|
119
|
+
// express.raw() leaves the bytes on req.body.
|
|
120
|
+
if (Buffer.isBuffer(req.body)) {
|
|
121
|
+
return Promise.resolve(req.body);
|
|
122
|
+
}
|
|
123
|
+
// An object-producing parser (express.json()) already drained the stream, so
|
|
124
|
+
// 'data' and 'end' never fire again and awaiting them parks the request until the
|
|
125
|
+
// socket times out. Its output is unusable here: the signature covers bytes.
|
|
126
|
+
if (req.readableEnded || req.complete) {
|
|
127
|
+
return Promise.reject(new Error('the request body was already consumed by another body parser. Mount the Scrapfly ' +
|
|
128
|
+
'webhook route on an app without a global express.json()/urlencoded(), or register ' +
|
|
129
|
+
'those parsers after createServer(), so the raw bytes reach signature verification.'));
|
|
130
|
+
}
|
|
131
|
+
return new Promise((resolve, reject) => {
|
|
132
|
+
const chunks = [];
|
|
133
|
+
let size = 0;
|
|
134
|
+
req.on('data', (chunk) => {
|
|
135
|
+
size += chunk.length;
|
|
136
|
+
if (size > limit) {
|
|
137
|
+
reject(new WebhookBodyTooLarge(`webhook body exceeds ${limit} bytes`));
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
chunks.push(chunk);
|
|
141
|
+
});
|
|
142
|
+
req.on('end', () => resolve(Buffer.concat(chunks)));
|
|
143
|
+
req.on('error', reject);
|
|
144
|
+
});
|
|
32
145
|
}
|
|
33
146
|
/**
|
|
34
|
-
* Start a tiny Express server that receives Scrapfly webhook callbacks
|
|
35
|
-
* and dispatches them to the provided `callback`.
|
|
36
|
-
* integration and local development; production setups
|
|
37
|
-
* Scrapfly webhooks directly into their own HTTP stack via
|
|
147
|
+
* Start a tiny Express server that receives Scrapfly webhook callbacks,
|
|
148
|
+
* verifies their signature, and dispatches them to the provided `callback`.
|
|
149
|
+
* Intended for quick integration and local development; production setups
|
|
150
|
+
* should wire Scrapfly webhooks directly into their own HTTP stack via
|
|
38
151
|
* {@link verifySignature}.
|
|
39
152
|
*
|
|
153
|
+
* Every request must be signed. A body with no `X-Scrapfly-Webhook-Signature`
|
|
154
|
+
* is rejected with 401 rather than delivered.
|
|
155
|
+
*
|
|
40
156
|
* Requires the `express` package to be installed.
|
|
41
157
|
*
|
|
42
|
-
* @param
|
|
43
|
-
*
|
|
158
|
+
* @param signingSecrets Webhook signing secret(s) from your webhook settings.
|
|
159
|
+
* @param callback Invoked with the verified body, its resource type, and the
|
|
160
|
+
* raw Express request. Awaited, so an async callback's failure yields 500. Bodies are JSON-parsed when the content type says so;
|
|
161
|
+
* binary payloads (screenshot images) arrive as a Buffer.
|
|
44
162
|
* @param app Optional pre-configured Express app to mount on.
|
|
163
|
+
* @param maxBodyBytes Reject bodies larger than this, before any decompression.
|
|
45
164
|
* @returns The underlying Express app (for further configuration or `.listen()`).
|
|
46
165
|
*/
|
|
47
|
-
export async function createServer(callback, app) {
|
|
166
|
+
export async function createServer(signingSecrets, callback, app, maxBodyBytes = MAX_BODY_BYTES) {
|
|
167
|
+
if (!signingSecrets || (Array.isArray(signingSecrets) && signingSecrets.length === 0)) {
|
|
168
|
+
throw new Error('createServer requires the webhook signing secret(s) from your Scrapfly dashboard');
|
|
169
|
+
}
|
|
48
170
|
let express;
|
|
49
171
|
try {
|
|
50
172
|
const expressModule = await import('express');
|
|
@@ -56,31 +178,68 @@ export async function createServer(callback, app) {
|
|
|
56
178
|
if (!app) {
|
|
57
179
|
app = express();
|
|
58
180
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
181
|
+
app.post('/webhook', async (req, res) => {
|
|
182
|
+
const resourceType = req.headers['x-scrapfly-webhook-resource-type'];
|
|
183
|
+
// The digest is sent under both spellings; accept either.
|
|
184
|
+
const signature = req.headers['x-scrapfly-webhook-signature'] ||
|
|
185
|
+
req.headers['x-scrapfly-webhook-signature-lowercase'];
|
|
186
|
+
if (!RESOURCE_TYPES.includes(resourceType)) {
|
|
187
|
+
// Attacker-controlled and served as text/html: never echo it back.
|
|
188
|
+
log.error('Unsupported resource type:', JSON.stringify(resourceType));
|
|
189
|
+
return res.status(400).send('unsupported resource type');
|
|
190
|
+
}
|
|
191
|
+
// A ping sent while a webhook is being created carries no signature. Answer the
|
|
192
|
+
// reachability probe, but never hand an unverified body to the callback: the
|
|
193
|
+
// resource type is as unauthenticated as the signature.
|
|
194
|
+
if (resourceType === ResourceType.PING && !signature) {
|
|
195
|
+
return res.status(200).send('');
|
|
196
|
+
}
|
|
197
|
+
let body;
|
|
65
198
|
try {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
199
|
+
body = decompress(await readBody(req, maxBodyBytes), req.headers['content-encoding'], maxBodyBytes);
|
|
200
|
+
}
|
|
201
|
+
catch (e) {
|
|
202
|
+
log.error('Webhook decoding error:', e);
|
|
203
|
+
// Decoding runs before verification, so these failures are reachable unauthenticated.
|
|
204
|
+
return res.status(e instanceof WebhookBodyTooLarge ? 413 : 400).send('');
|
|
205
|
+
}
|
|
206
|
+
let signedMessage = body;
|
|
207
|
+
if (resourceType === ResourceType.ALERT) {
|
|
208
|
+
signedMessage = alertSignedMessage(req.headers['x-scrapfly-webhook-timestamp'], body);
|
|
209
|
+
if (signedMessage === null) {
|
|
210
|
+
log.error(`Rejected alert webhook with a missing or stale timestamp from ${req.ip}`);
|
|
211
|
+
return res.status(401).send('');
|
|
70
212
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
213
|
+
}
|
|
214
|
+
if (!verifySignature(signedMessage, signature, signingSecrets)) {
|
|
215
|
+
log.error(`Rejected unsigned or mis-signed ${resourceType} webhook from ${req.ip}`);
|
|
216
|
+
return res.status(401).send('');
|
|
217
|
+
}
|
|
218
|
+
let data = body;
|
|
219
|
+
const contentType = (req.headers['content-type'] || '');
|
|
220
|
+
try {
|
|
221
|
+
if (contentType.startsWith('application/json')) {
|
|
222
|
+
data = JSON.parse(body.toString('utf8'));
|
|
75
223
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
224
|
+
else if (contentType.startsWith('application/msgpack')) {
|
|
225
|
+
// application/msgpack is an offered webhook content type.
|
|
226
|
+
const { decode } = await import('@msgpack/msgpack');
|
|
227
|
+
data = decode(body);
|
|
79
228
|
}
|
|
80
229
|
}
|
|
81
230
|
catch (e) {
|
|
82
|
-
log.error('Webhook
|
|
83
|
-
res.status(400).send('');
|
|
231
|
+
log.error('Webhook body parse error:', e);
|
|
232
|
+
return res.status(400).send('');
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
// Awaited: an un-awaited async callback rejects into an unhandled rejection,
|
|
236
|
+
// which terminates the process on Node >= 15.
|
|
237
|
+
await callback(data, resourceType, req);
|
|
238
|
+
return res.status(200).send('');
|
|
239
|
+
}
|
|
240
|
+
catch (e) {
|
|
241
|
+
log.error('Callback error:', e);
|
|
242
|
+
return res.status(500).send('');
|
|
84
243
|
}
|
|
85
244
|
});
|
|
86
245
|
return app;
|
package/package.json
CHANGED
|
@@ -12,7 +12,13 @@ export declare enum OperatingSystem {
|
|
|
12
12
|
/** Emulate a Windows browser fingerprint. */
|
|
13
13
|
WINDOWS = "windows",
|
|
14
14
|
/** Emulate a macOS browser fingerprint. */
|
|
15
|
-
MACOS = "macos"
|
|
15
|
+
MACOS = "macos",
|
|
16
|
+
/** Emulate an Android phone browser fingerprint. */
|
|
17
|
+
ANDROID = "android",
|
|
18
|
+
/** Emulate an iPhone browser fingerprint. */
|
|
19
|
+
IPHONE = "iphone",
|
|
20
|
+
/** Emulate an iPad browser fingerprint. */
|
|
21
|
+
IPAD = "ipad"
|
|
16
22
|
}
|
|
17
23
|
/**
|
|
18
24
|
* User-facing options accepted by {@link BrowserConfig}. All fields are
|
|
@@ -28,6 +34,18 @@ export type BrowserConfigOptions = {
|
|
|
28
34
|
session?: string;
|
|
29
35
|
/** ISO country code for geo-targeting. */
|
|
30
36
|
country?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Browser UI language — the singular `navigator.language` base tag
|
|
39
|
+
* (e.g. "en"). When omitted, the server derives it from `country`.
|
|
40
|
+
*/
|
|
41
|
+
lang?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Ordered language preference list driving `navigator.languages` and the
|
|
44
|
+
* q-weighted `Accept-Language` header (e.g. ["fr-FR", "fr", "en-US"]).
|
|
45
|
+
* Pass a comma-separated string or an array; capped server-side at 3
|
|
46
|
+
* entries.
|
|
47
|
+
*/
|
|
48
|
+
languages?: string | string[];
|
|
31
49
|
/** Close the browser automatically when the connection drops. */
|
|
32
50
|
auto_close?: boolean;
|
|
33
51
|
/** Session timeout in seconds (max 1800 = 30 minutes). */
|
|
@@ -54,16 +72,24 @@ export type BrowserConfigOptions = {
|
|
|
54
72
|
cache?: boolean;
|
|
55
73
|
/** Apply Scrapfly's abuse blacklist. */
|
|
56
74
|
blacklist?: boolean;
|
|
57
|
-
/**
|
|
75
|
+
/**
|
|
76
|
+
* @deprecated The Cloud Browser WebSocket endpoint does not read this
|
|
77
|
+
* parameter; it is sent but has no effect. Use
|
|
78
|
+
* {@link ScrapflyClient.cloudBrowserUnblock} (the `/unblock` endpoint).
|
|
79
|
+
*/
|
|
58
80
|
unblock?: boolean;
|
|
59
|
-
/**
|
|
81
|
+
/**
|
|
82
|
+
* @deprecated The Cloud Browser WebSocket endpoint does not read this
|
|
83
|
+
* parameter; it is sent but has no effect. Use
|
|
84
|
+
* {@link ScrapflyClient.cloudBrowserUnblock}, which takes its own timeouts.
|
|
85
|
+
*/
|
|
60
86
|
unblock_timeout?: number;
|
|
61
87
|
/** Browser brand to emulate (e.g. "chrome", "firefox"). */
|
|
62
88
|
browser_brand?: string;
|
|
63
89
|
/** Enable the MCP (Model Context Protocol) bridge. */
|
|
64
90
|
enable_mcp?: boolean;
|
|
65
91
|
/**
|
|
66
|
-
* Arm
|
|
92
|
+
* Arm the Cloud Browser's built-in captcha detector + solver on the first page attach.
|
|
67
93
|
* Turnstile, DataDome slider, reCAPTCHA, GeeTest, PerimeterX hold, and
|
|
68
94
|
* puzzle captchas are handled automatically — no extra CDP calls from the
|
|
69
95
|
* client. Billed per solve; failures cost nothing.
|
|
@@ -99,6 +125,12 @@ export type BrowserConfigOptions = {
|
|
|
99
125
|
* comma-separated string or an array of bare IPs / CIDRs.
|
|
100
126
|
*/
|
|
101
127
|
hitl_allowed_networks?: string | string[];
|
|
128
|
+
/**
|
|
129
|
+
* Return large downloaded files from `ScrapiumBrowser.getDownloads` as IO stream handles instead of
|
|
130
|
+
* inline base64: read them with `CloudBrowserDownloads`.
|
|
131
|
+
* See https://scrapfly.io/docs/cloud-browser-api/file-downloads#large-downloads
|
|
132
|
+
*/
|
|
133
|
+
downloads_stream?: boolean;
|
|
102
134
|
};
|
|
103
135
|
/**
|
|
104
136
|
* Configuration for a Scrapfly Cloud Browser session. Builds the
|
|
@@ -114,6 +146,10 @@ export declare class BrowserConfig {
|
|
|
114
146
|
session?: string;
|
|
115
147
|
/** See {@link BrowserConfigOptions.country}. */
|
|
116
148
|
country?: string;
|
|
149
|
+
/** See {@link BrowserConfigOptions.lang}. */
|
|
150
|
+
lang?: string;
|
|
151
|
+
/** See {@link BrowserConfigOptions.languages}. Normalized to a comma-separated string. */
|
|
152
|
+
languages?: string;
|
|
117
153
|
/** See {@link BrowserConfigOptions.auto_close}. */
|
|
118
154
|
auto_close?: boolean;
|
|
119
155
|
/** See {@link BrowserConfigOptions.timeout}. */
|
|
@@ -140,9 +176,9 @@ export declare class BrowserConfig {
|
|
|
140
176
|
cache?: boolean;
|
|
141
177
|
/** See {@link BrowserConfigOptions.blacklist}. */
|
|
142
178
|
blacklist?: boolean;
|
|
143
|
-
/**
|
|
179
|
+
/** @deprecated Not read by the API, see {@link BrowserConfigOptions.unblock}. */
|
|
144
180
|
unblock?: boolean;
|
|
145
|
-
/**
|
|
181
|
+
/** @deprecated Not read by the API, see {@link BrowserConfigOptions.unblock_timeout}. */
|
|
146
182
|
unblock_timeout?: number;
|
|
147
183
|
/** See {@link BrowserConfigOptions.browser_brand}. */
|
|
148
184
|
browser_brand?: string;
|
|
@@ -166,6 +202,8 @@ export declare class BrowserConfig {
|
|
|
166
202
|
rtc_password?: string;
|
|
167
203
|
/** See {@link BrowserConfigOptions.hitl_allowed_networks}. */
|
|
168
204
|
hitl_allowed_networks?: string;
|
|
205
|
+
/** See {@link BrowserConfigOptions.downloads_stream}. */
|
|
206
|
+
downloads_stream?: boolean;
|
|
169
207
|
/**
|
|
170
208
|
* @param options Session options. See {@link BrowserConfigOptions}.
|
|
171
209
|
* @throws `Error` if `timeout` exceeds the upper bound of 1800 seconds.
|
|
@@ -173,13 +211,23 @@ export declare class BrowserConfig {
|
|
|
173
211
|
constructor(options?: BrowserConfigOptions);
|
|
174
212
|
/**
|
|
175
213
|
* Return the deterministic project salt for an api_key (`sha256(api_key)[:8]`).
|
|
176
|
-
* Matches the `X-Browser-Project-Salt` response header returned on
|
|
177
|
-
*
|
|
178
|
-
* an attach link belongs to
|
|
214
|
+
* Matches the `X-Browser-Project-Salt` response header returned on
|
|
215
|
+
* VNC-enabled Cloud Browser upgrades, where the salt is also the VNC
|
|
216
|
+
* password prefix. Useful for verifying that an attach link belongs to
|
|
217
|
+
* your project before sharing it.
|
|
179
218
|
*
|
|
180
219
|
* Uses the Web Crypto API where available (modern Node and browsers).
|
|
181
220
|
*/
|
|
182
221
|
static projectSalt(apiKey: string): Promise<string>;
|
|
222
|
+
/**
|
|
223
|
+
* Return the password a native VNC client must type to attach to a session
|
|
224
|
+
* created with this config: `<projectSalt>-<vnc_password>`.
|
|
225
|
+
*
|
|
226
|
+
* Required by the VNC TCP endpoint (port 5901), which the server salts at
|
|
227
|
+
* allocation. The WebSocket endpoint `/run/<run_id>/vnc` takes the raw
|
|
228
|
+
* `vnc_password` instead.
|
|
229
|
+
*/
|
|
230
|
+
vncClientPassword(apiKey: string): Promise<string>;
|
|
183
231
|
/**
|
|
184
232
|
* Build the WebSocket URL clients use to connect to the Cloud Browser
|
|
185
233
|
* session (compatible with Puppeteer `browserWSEndpoint` and the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browserconfig.d.ts","sourceRoot":"","sources":["../src/browserconfig.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,oBAAY,SAAS;IACnB,2EAA2E;IAC3E,UAAU,eAAe;IACzB,iFAAiF;IACjF,WAAW,gBAAgB;CAC5B;AAED,6EAA6E;AAC7E,oBAAY,eAAe;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MAAM,CAAC;IACnB,mDAAmD;IACnD,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,qDAAqD;IACrD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,mDAAmD;IACnD,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,qDAAqD;IACrD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,qDAAqD;IACrD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,8DAA8D;IAC9D,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,yDAAyD;IACzD,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAE3B;;;OAGG;gBACS,OAAO,GAAE,oBAAyB;IAmD9C;;;;;;;;OAQG;WACU,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAWzD;;;;;;;OAOG;IACG,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAQxD;;;;;;;;OAQG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM;CAyCpD"}
|
package/script/browserconfig.js
CHANGED
|
@@ -18,6 +18,12 @@ var OperatingSystem;
|
|
|
18
18
|
OperatingSystem["WINDOWS"] = "windows";
|
|
19
19
|
/** Emulate a macOS browser fingerprint. */
|
|
20
20
|
OperatingSystem["MACOS"] = "macos";
|
|
21
|
+
/** Emulate an Android phone browser fingerprint. */
|
|
22
|
+
OperatingSystem["ANDROID"] = "android";
|
|
23
|
+
/** Emulate an iPhone browser fingerprint. */
|
|
24
|
+
OperatingSystem["IPHONE"] = "iphone";
|
|
25
|
+
/** Emulate an iPad browser fingerprint. */
|
|
26
|
+
OperatingSystem["IPAD"] = "ipad";
|
|
21
27
|
})(OperatingSystem || (exports.OperatingSystem = OperatingSystem = {}));
|
|
22
28
|
/**
|
|
23
29
|
* Configuration for a Scrapfly Cloud Browser session. Builds the
|
|
@@ -58,6 +64,20 @@ class BrowserConfig {
|
|
|
58
64
|
writable: true,
|
|
59
65
|
value: void 0
|
|
60
66
|
});
|
|
67
|
+
/** See {@link BrowserConfigOptions.lang}. */
|
|
68
|
+
Object.defineProperty(this, "lang", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: void 0
|
|
73
|
+
});
|
|
74
|
+
/** See {@link BrowserConfigOptions.languages}. Normalized to a comma-separated string. */
|
|
75
|
+
Object.defineProperty(this, "languages", {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
configurable: true,
|
|
78
|
+
writable: true,
|
|
79
|
+
value: void 0
|
|
80
|
+
});
|
|
61
81
|
/** See {@link BrowserConfigOptions.auto_close}. */
|
|
62
82
|
Object.defineProperty(this, "auto_close", {
|
|
63
83
|
enumerable: true,
|
|
@@ -149,14 +169,14 @@ class BrowserConfig {
|
|
|
149
169
|
writable: true,
|
|
150
170
|
value: void 0
|
|
151
171
|
});
|
|
152
|
-
/**
|
|
172
|
+
/** @deprecated Not read by the API, see {@link BrowserConfigOptions.unblock}. */
|
|
153
173
|
Object.defineProperty(this, "unblock", {
|
|
154
174
|
enumerable: true,
|
|
155
175
|
configurable: true,
|
|
156
176
|
writable: true,
|
|
157
177
|
value: void 0
|
|
158
178
|
});
|
|
159
|
-
/**
|
|
179
|
+
/** @deprecated Not read by the API, see {@link BrowserConfigOptions.unblock_timeout}. */
|
|
160
180
|
Object.defineProperty(this, "unblock_timeout", {
|
|
161
181
|
enumerable: true,
|
|
162
182
|
configurable: true,
|
|
@@ -240,6 +260,13 @@ class BrowserConfig {
|
|
|
240
260
|
writable: true,
|
|
241
261
|
value: void 0
|
|
242
262
|
});
|
|
263
|
+
/** See {@link BrowserConfigOptions.downloads_stream}. */
|
|
264
|
+
Object.defineProperty(this, "downloads_stream", {
|
|
265
|
+
enumerable: true,
|
|
266
|
+
configurable: true,
|
|
267
|
+
writable: true,
|
|
268
|
+
value: void 0
|
|
269
|
+
});
|
|
243
270
|
if (options.timeout !== undefined && options.timeout > 1800) {
|
|
244
271
|
throw new Error('timeout cannot exceed 1800 seconds (30 minutes)');
|
|
245
272
|
}
|
|
@@ -247,6 +274,14 @@ class BrowserConfig {
|
|
|
247
274
|
this.os = options.os;
|
|
248
275
|
this.session = options.session;
|
|
249
276
|
this.country = options.country;
|
|
277
|
+
this.lang = options.lang;
|
|
278
|
+
if (Array.isArray(options.languages)) {
|
|
279
|
+
const cleaned = options.languages.map((s) => s.trim()).filter((s) => s.length > 0);
|
|
280
|
+
this.languages = cleaned.length > 0 ? cleaned.join(',') : undefined;
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
this.languages = options.languages || undefined;
|
|
284
|
+
}
|
|
250
285
|
this.auto_close = options.auto_close;
|
|
251
286
|
this.timeout = options.timeout;
|
|
252
287
|
this.debug = options.debug;
|
|
@@ -281,12 +316,14 @@ class BrowserConfig {
|
|
|
281
316
|
else {
|
|
282
317
|
this.hitl_allowed_networks = options.hitl_allowed_networks || undefined;
|
|
283
318
|
}
|
|
319
|
+
this.downloads_stream = options.downloads_stream;
|
|
284
320
|
}
|
|
285
321
|
/**
|
|
286
322
|
* Return the deterministic project salt for an api_key (`sha256(api_key)[:8]`).
|
|
287
|
-
* Matches the `X-Browser-Project-Salt` response header returned on
|
|
288
|
-
*
|
|
289
|
-
* an attach link belongs to
|
|
323
|
+
* Matches the `X-Browser-Project-Salt` response header returned on
|
|
324
|
+
* VNC-enabled Cloud Browser upgrades, where the salt is also the VNC
|
|
325
|
+
* password prefix. Useful for verifying that an attach link belongs to
|
|
326
|
+
* your project before sharing it.
|
|
290
327
|
*
|
|
291
328
|
* Uses the Web Crypto API where available (modern Node and browsers).
|
|
292
329
|
*/
|
|
@@ -300,6 +337,21 @@ class BrowserConfig {
|
|
|
300
337
|
}
|
|
301
338
|
return hex;
|
|
302
339
|
}
|
|
340
|
+
/**
|
|
341
|
+
* Return the password a native VNC client must type to attach to a session
|
|
342
|
+
* created with this config: `<projectSalt>-<vnc_password>`.
|
|
343
|
+
*
|
|
344
|
+
* Required by the VNC TCP endpoint (port 5901), which the server salts at
|
|
345
|
+
* allocation. The WebSocket endpoint `/run/<run_id>/vnc` takes the raw
|
|
346
|
+
* `vnc_password` instead.
|
|
347
|
+
*/
|
|
348
|
+
async vncClientPassword(apiKey) {
|
|
349
|
+
if (!this.enable_vnc || !this.vnc_password) {
|
|
350
|
+
throw new Error('enable_vnc and vnc_password must both be set on this BrowserConfig');
|
|
351
|
+
}
|
|
352
|
+
const salt = await BrowserConfig.projectSalt(apiKey);
|
|
353
|
+
return `${salt}-${this.vnc_password}`;
|
|
354
|
+
}
|
|
303
355
|
/**
|
|
304
356
|
* Build the WebSocket URL clients use to connect to the Cloud Browser
|
|
305
357
|
* session (compatible with Puppeteer `browserWSEndpoint` and the
|
|
@@ -320,6 +372,10 @@ class BrowserConfig {
|
|
|
320
372
|
params.set('session', this.session);
|
|
321
373
|
if (this.country !== undefined)
|
|
322
374
|
params.set('country', this.country);
|
|
375
|
+
if (this.lang !== undefined)
|
|
376
|
+
params.set('lang', this.lang);
|
|
377
|
+
if (this.languages !== undefined)
|
|
378
|
+
params.set('languages', this.languages);
|
|
323
379
|
if (this.auto_close !== undefined)
|
|
324
380
|
params.set('auto_close', String(this.auto_close));
|
|
325
381
|
if (this.timeout !== undefined)
|
|
@@ -372,6 +428,8 @@ class BrowserConfig {
|
|
|
372
428
|
params.set('rtc_password', this.rtc_password);
|
|
373
429
|
if (this.hitl_allowed_networks !== undefined)
|
|
374
430
|
params.set('hitl_allowed_networks', this.hitl_allowed_networks);
|
|
431
|
+
if (this.downloads_stream !== undefined)
|
|
432
|
+
params.set('downloads_stream', String(this.downloads_stream));
|
|
375
433
|
const baseHost = host || 'wss://browser.scrapfly.io';
|
|
376
434
|
return `${baseHost}?${params.toString()}`;
|
|
377
435
|
}
|