scrapfly-sdk 0.11.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -3
- package/esm/browserconfig.d.ts +37 -5
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +51 -3
- package/esm/client.d.ts +118 -5
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +339 -38
- package/esm/crawl.d.ts +58 -1
- package/esm/crawl.d.ts.map +1 -1
- package/esm/crawl.js +55 -0
- package/esm/crawlerconfig.d.ts +41 -3
- package/esm/crawlerconfig.d.ts.map +1 -1
- package/esm/crawlerconfig.js +93 -2
- package/esm/crawlerresult.d.ts +335 -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 +4 -4
- package/esm/main.d.ts.map +1 -1
- package/esm/main.js +3 -3
- package/esm/scrapeconfig.d.ts +36 -0
- package/esm/scrapeconfig.d.ts.map +1 -1
- package/esm/scrapeconfig.js +44 -2
- package/esm/types.d.ts.map +1 -1
- 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 +37 -5
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +51 -3
- package/script/client.d.ts +118 -5
- package/script/client.d.ts.map +1 -1
- package/script/client.js +338 -37
- package/script/crawl.d.ts +58 -1
- package/script/crawl.d.ts.map +1 -1
- package/script/crawl.js +55 -0
- package/script/crawlerconfig.d.ts +41 -3
- package/script/crawlerconfig.d.ts.map +1 -1
- package/script/crawlerconfig.js +94 -3
- package/script/crawlerresult.d.ts +335 -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 +4 -4
- package/script/main.d.ts.map +1 -1
- package/script/main.js +9 -2
- package/script/scrapeconfig.d.ts +36 -0
- package/script/scrapeconfig.d.ts.map +1 -1
- package/script/scrapeconfig.js +44 -2
- package/script/types.d.ts.map +1 -1
- 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 +52 -4
- package/src/client.ts +406 -38
- package/src/crawl.ts +74 -0
- package/src/crawlerconfig.ts +115 -5
- package/src/crawlerresult.ts +566 -3
- package/src/errors.ts +12 -0
- package/src/main.ts +25 -2
- package/src/scrapeconfig.ts +54 -2
- package/src/types.ts +0 -1
- package/src/webhookserver.ts +235 -39
package/esm/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,CAAC,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;AACvC,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,GAAG,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,CAAC,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;AACvC,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,GAAG,MAAM,CAAC;AAWnE,kEAAkE;AAClE,MAAM,MAAM,KAAK,GAAG;IAClB,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,C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,OAAO,CAAC;CACtB,CAAC;AAEF,yDAAyD;AACzD,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,aAAa,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,WAAW,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC,CAAC"}
|
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). */
|
|
@@ -63,7 +81,7 @@ export type BrowserConfigOptions = {
|
|
|
63
81
|
/** Enable the MCP (Model Context Protocol) bridge. */
|
|
64
82
|
enable_mcp?: boolean;
|
|
65
83
|
/**
|
|
66
|
-
* Arm
|
|
84
|
+
* Arm the Cloud Browser's built-in captcha detector + solver on the first page attach.
|
|
67
85
|
* Turnstile, DataDome slider, reCAPTCHA, GeeTest, PerimeterX hold, and
|
|
68
86
|
* puzzle captchas are handled automatically — no extra CDP calls from the
|
|
69
87
|
* client. Billed per solve; failures cost nothing.
|
|
@@ -114,6 +132,10 @@ export declare class BrowserConfig {
|
|
|
114
132
|
session?: string;
|
|
115
133
|
/** See {@link BrowserConfigOptions.country}. */
|
|
116
134
|
country?: string;
|
|
135
|
+
/** See {@link BrowserConfigOptions.lang}. */
|
|
136
|
+
lang?: string;
|
|
137
|
+
/** See {@link BrowserConfigOptions.languages}. Normalized to a comma-separated string. */
|
|
138
|
+
languages?: string;
|
|
117
139
|
/** See {@link BrowserConfigOptions.auto_close}. */
|
|
118
140
|
auto_close?: boolean;
|
|
119
141
|
/** See {@link BrowserConfigOptions.timeout}. */
|
|
@@ -173,13 +195,23 @@ export declare class BrowserConfig {
|
|
|
173
195
|
constructor(options?: BrowserConfigOptions);
|
|
174
196
|
/**
|
|
175
197
|
* 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
|
|
198
|
+
* Matches the `X-Browser-Project-Salt` response header returned on
|
|
199
|
+
* VNC-enabled Cloud Browser upgrades, where the salt is also the VNC
|
|
200
|
+
* password prefix. Useful for verifying that an attach link belongs to
|
|
201
|
+
* your project before sharing it.
|
|
179
202
|
*
|
|
180
203
|
* Uses the Web Crypto API where available (modern Node and browsers).
|
|
181
204
|
*/
|
|
182
205
|
static projectSalt(apiKey: string): Promise<string>;
|
|
206
|
+
/**
|
|
207
|
+
* Return the password a native VNC client must type to attach to a session
|
|
208
|
+
* created with this config: `<projectSalt>-<vnc_password>`.
|
|
209
|
+
*
|
|
210
|
+
* Required by the VNC TCP endpoint (port 5901), which the server salts at
|
|
211
|
+
* allocation. The WebSocket endpoint `/run/<run_id>/vnc` takes the raw
|
|
212
|
+
* `vnc_password` instead.
|
|
213
|
+
*/
|
|
214
|
+
vncClientPassword(apiKey: string): Promise<string>;
|
|
183
215
|
/**
|
|
184
216
|
* Build the WebSocket URL clients use to connect to the Cloud Browser
|
|
185
217
|
* 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;IACzB,2CAA2C;IAC3C,KAAK,UAAU;IACf,6CAA6C;IAC7C,OAAO,YAAY;IACnB,2CAA2C;IAC3C,KAAK,UAAU;
|
|
1
|
+
{"version":3,"file":"browserconfig.d.ts","sourceRoot":"","sources":["../src/browserconfig.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,oBAAY,SAAS;IACn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DAA8D;IAC9D,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAE/B;;;OAGG;gBACS,OAAO,GAAE,oBAAyB;IAkD9C;;;;;;;;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;CAwCpD"}
|
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,
|
|
@@ -247,6 +267,14 @@ class BrowserConfig {
|
|
|
247
267
|
this.os = options.os;
|
|
248
268
|
this.session = options.session;
|
|
249
269
|
this.country = options.country;
|
|
270
|
+
this.lang = options.lang;
|
|
271
|
+
if (Array.isArray(options.languages)) {
|
|
272
|
+
const cleaned = options.languages.map((s) => s.trim()).filter((s) => s.length > 0);
|
|
273
|
+
this.languages = cleaned.length > 0 ? cleaned.join(',') : undefined;
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
this.languages = options.languages || undefined;
|
|
277
|
+
}
|
|
250
278
|
this.auto_close = options.auto_close;
|
|
251
279
|
this.timeout = options.timeout;
|
|
252
280
|
this.debug = options.debug;
|
|
@@ -284,9 +312,10 @@ class BrowserConfig {
|
|
|
284
312
|
}
|
|
285
313
|
/**
|
|
286
314
|
* 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
|
|
315
|
+
* Matches the `X-Browser-Project-Salt` response header returned on
|
|
316
|
+
* VNC-enabled Cloud Browser upgrades, where the salt is also the VNC
|
|
317
|
+
* password prefix. Useful for verifying that an attach link belongs to
|
|
318
|
+
* your project before sharing it.
|
|
290
319
|
*
|
|
291
320
|
* Uses the Web Crypto API where available (modern Node and browsers).
|
|
292
321
|
*/
|
|
@@ -300,6 +329,21 @@ class BrowserConfig {
|
|
|
300
329
|
}
|
|
301
330
|
return hex;
|
|
302
331
|
}
|
|
332
|
+
/**
|
|
333
|
+
* Return the password a native VNC client must type to attach to a session
|
|
334
|
+
* created with this config: `<projectSalt>-<vnc_password>`.
|
|
335
|
+
*
|
|
336
|
+
* Required by the VNC TCP endpoint (port 5901), which the server salts at
|
|
337
|
+
* allocation. The WebSocket endpoint `/run/<run_id>/vnc` takes the raw
|
|
338
|
+
* `vnc_password` instead.
|
|
339
|
+
*/
|
|
340
|
+
async vncClientPassword(apiKey) {
|
|
341
|
+
if (!this.enable_vnc || !this.vnc_password) {
|
|
342
|
+
throw new Error('enable_vnc and vnc_password must both be set on this BrowserConfig');
|
|
343
|
+
}
|
|
344
|
+
const salt = await BrowserConfig.projectSalt(apiKey);
|
|
345
|
+
return `${salt}-${this.vnc_password}`;
|
|
346
|
+
}
|
|
303
347
|
/**
|
|
304
348
|
* Build the WebSocket URL clients use to connect to the Cloud Browser
|
|
305
349
|
* session (compatible with Puppeteer `browserWSEndpoint` and the
|
|
@@ -320,6 +364,10 @@ class BrowserConfig {
|
|
|
320
364
|
params.set('session', this.session);
|
|
321
365
|
if (this.country !== undefined)
|
|
322
366
|
params.set('country', this.country);
|
|
367
|
+
if (this.lang !== undefined)
|
|
368
|
+
params.set('lang', this.lang);
|
|
369
|
+
if (this.languages !== undefined)
|
|
370
|
+
params.set('languages', this.languages);
|
|
323
371
|
if (this.auto_close !== undefined)
|
|
324
372
|
params.set('auto_close', String(this.auto_close));
|
|
325
373
|
if (this.timeout !== undefined)
|