scrapfly-sdk 0.10.3 → 0.10.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/browserconfig.d.ts +70 -1
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +44 -1
- package/esm/client.d.ts +33 -0
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +33 -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 +27 -0
- package/esm/main.d.ts.map +1 -1
- package/esm/main.js +27 -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/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 +24 -0
- package/esm/webhookserver.d.ts.map +1 -1
- package/esm/webhookserver.js +24 -0
- package/package.json +1 -1
- package/script/browserconfig.d.ts +70 -1
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +44 -1
- package/script/client.d.ts +33 -0
- package/script/client.d.ts.map +1 -1
- package/script/client.js +33 -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 +27 -0
- package/script/main.d.ts.map +1 -1
- package/script/main.js +27 -0
- 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/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 +24 -0
- package/script/webhookserver.d.ts.map +1 -1
- package/script/webhookserver.js +24 -0
- package/src/_dnt.shims.ts +64 -0
- package/src/batch.ts +272 -0
- package/src/browserconfig.ts +187 -0
- package/src/client.ts +1637 -0
- package/src/crawl.ts +255 -0
- package/src/crawlerconfig.ts +363 -0
- package/src/crawlerresult.ts +478 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/assert_path.ts +10 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/basename.ts +53 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/common.ts +26 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/constants.ts +49 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/dirname.ts +9 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/format.ts +25 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/from_file_url.ts +10 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/glob_to_reg_exp.ts +290 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize.ts +9 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize_string.ts +74 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/relative.ts +10 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/strip_trailing_separators.ts +25 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/to_file_url.ts +17 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_os.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/basename.ts +35 -0
- package/src/deps/jsr.io/@std/path/1.0.1/common.ts +35 -0
- package/src/deps/jsr.io/@std/path/1.0.1/constants.ts +18 -0
- package/src/deps/jsr.io/@std/path/1.0.1/dirname.ts +28 -0
- package/src/deps/jsr.io/@std/path/1.0.1/extname.ts +27 -0
- package/src/deps/jsr.io/@std/path/1.0.1/format.ts +30 -0
- package/src/deps/jsr.io/@std/path/1.0.1/from_file_url.ts +30 -0
- package/src/deps/jsr.io/@std/path/1.0.1/glob_to_regexp.ts +94 -0
- package/src/deps/jsr.io/@std/path/1.0.1/is_absolute.ts +30 -0
- package/src/deps/jsr.io/@std/path/1.0.1/is_glob.ts +49 -0
- package/src/deps/jsr.io/@std/path/1.0.1/join.ts +28 -0
- package/src/deps/jsr.io/@std/path/1.0.1/join_globs.ts +42 -0
- package/src/deps/jsr.io/@std/path/1.0.1/mod.ts +55 -0
- package/src/deps/jsr.io/@std/path/1.0.1/normalize.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/normalize_glob.ts +45 -0
- package/src/deps/jsr.io/@std/path/1.0.1/parse.ts +44 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/_util.ts +10 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/basename.ts +39 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/constants.ts +15 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/dirname.ts +55 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/extname.ts +73 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/format.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/from_file_url.ts +25 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/glob_to_regexp.ts +94 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/is_absolute.ts +25 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/join.ts +27 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/join_globs.ts +44 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize.ts +41 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize_glob.ts +42 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/parse.ts +121 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/relative.ts +103 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/resolve.ts +69 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/to_file_url.ts +32 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/to_namespaced_path.ts +21 -0
- package/src/deps/jsr.io/@std/path/1.0.1/relative.ts +32 -0
- package/src/deps/jsr.io/@std/path/1.0.1/resolve.ts +32 -0
- package/src/deps/jsr.io/@std/path/1.0.1/to_file_url.ts +30 -0
- package/src/deps/jsr.io/@std/path/1.0.1/to_namespaced_path.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/types.ts +40 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/_util.ts +28 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/basename.ts +48 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/constants.ts +15 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/dirname.ts +114 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/extname.ts +86 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/format.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/from_file_url.ts +34 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/glob_to_regexp.ts +92 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/is_absolute.ts +40 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/join.ts +72 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/join_globs.ts +45 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize.ts +132 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize_glob.ts +42 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/parse.ts +184 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/relative.ts +128 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/resolve.ts +174 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/to_file_url.ts +38 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/to_namespaced_path.ts +60 -0
- package/src/deps.ts +4 -0
- package/src/errors.ts +82 -0
- package/src/extractionconfig.ts +217 -0
- package/src/logger.ts +53 -0
- package/src/main.ts +88 -0
- package/src/monitoringconfig.ts +88 -0
- package/src/polyfill.ts +37 -0
- package/src/result.ts +340 -0
- package/src/scrapeconfig.ts +450 -0
- package/src/screenshotconfig.ts +237 -0
- package/src/types.ts +2 -0
- package/src/utils.ts +86 -0
- package/src/webhookserver.ts +90 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { log } from './logger.js';
|
|
2
|
+
import type { Rec } from './types.js';
|
|
3
|
+
|
|
4
|
+
export function urlsafe_b64encode(data: string): string {
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
const encoded = encoder.encode(data);
|
|
7
|
+
const base64 = btoa(String.fromCharCode(...encoded))
|
|
8
|
+
.replace(/\+/g, '-')
|
|
9
|
+
.replace(/\//g, '_')
|
|
10
|
+
.replace(/=+$/, '');
|
|
11
|
+
return base64;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type RequestOptions = {
|
|
15
|
+
url: string;
|
|
16
|
+
method?: string;
|
|
17
|
+
headers?: any;
|
|
18
|
+
body?: string | Uint8Array;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export async function fetchRetry(
|
|
22
|
+
config: RequestOptions,
|
|
23
|
+
retries: number = 3,
|
|
24
|
+
retryDelay: number = 1000,
|
|
25
|
+
): Promise<Response> {
|
|
26
|
+
let lastError: unknown = null;
|
|
27
|
+
|
|
28
|
+
for (let attempt = 1; attempt <= retries; attempt++) {
|
|
29
|
+
try {
|
|
30
|
+
// XXX: note that cloudflare workers don't support init options
|
|
31
|
+
const { url, ...reqInit } = config;
|
|
32
|
+
// The `body` field on RequestOptions is typed as string | Uint8Array for
|
|
33
|
+
// historical reasons. The dnt (deno-to-node) build does not expose the
|
|
34
|
+
// DOM `BodyInit` alias in every lib target, so we cast to `any` here to
|
|
35
|
+
// stay portable across Deno, Node, Bun and Cloudflare Workers. The
|
|
36
|
+
// runtime values (string | Uint8Array) are all valid fetch body inputs.
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
|
+
const init: RequestInit = {
|
|
39
|
+
method: reqInit.method,
|
|
40
|
+
headers: reqInit.headers,
|
|
41
|
+
body: reqInit.body as any,
|
|
42
|
+
};
|
|
43
|
+
const response = await fetch(new Request(url, init));
|
|
44
|
+
// retry 5xx status codes
|
|
45
|
+
if (response.status >= 500 && response.status < 600) {
|
|
46
|
+
const _text = await response.text(); // consume response to prevent leak
|
|
47
|
+
lastError = new Error(`Fetch failed with status: ${response.status}`);
|
|
48
|
+
if (attempt < retries) {
|
|
49
|
+
log.debug(`request failed ${response.status} (${_text}): retry ${attempt}/${retries} after ${retryDelay}ms`);
|
|
50
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
return response;
|
|
54
|
+
}
|
|
55
|
+
} catch (error) {
|
|
56
|
+
lastError = error;
|
|
57
|
+
|
|
58
|
+
// AbortError should never be retried: it means the caller explicitly
|
|
59
|
+
// gave up (cancel token, timeout, etc.) and we shouldn't paper over it.
|
|
60
|
+
const isAbort = error instanceof Error && error.name === 'AbortError';
|
|
61
|
+
if (attempt === retries || isAbort) {
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
throw lastError;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function normalizeHeaders(headers: Rec<string> | Headers): Rec<string> {
|
|
73
|
+
const normalizedHeaders: Rec<string> = {};
|
|
74
|
+
|
|
75
|
+
if (headers instanceof Headers) {
|
|
76
|
+
headers.forEach((value, key) => {
|
|
77
|
+
normalizedHeaders[key.toLowerCase()] = value;
|
|
78
|
+
});
|
|
79
|
+
} else {
|
|
80
|
+
Object.keys(headers).forEach((key) => {
|
|
81
|
+
normalizedHeaders[key.toLowerCase()] = headers[key];
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return normalizedHeaders;
|
|
86
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import crypto from 'node:crypto';
|
|
3
|
+
import { log } from './logger.js';
|
|
4
|
+
|
|
5
|
+
export enum ResourceType {
|
|
6
|
+
SCRAPE = 'scrape',
|
|
7
|
+
PING = 'ping',
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Verify that a webhook request was sent by Scrapfly by comparing the
|
|
12
|
+
* HMAC-SHA256 signature on the request body against the configured
|
|
13
|
+
* signing secret.
|
|
14
|
+
*
|
|
15
|
+
* @param body Raw request body as received (do NOT decode).
|
|
16
|
+
* @param signature Value of the `Scrapfly-Signature` header (hex).
|
|
17
|
+
* @param signingSecret Webhook signing secret configured in the dashboard (hex).
|
|
18
|
+
* @returns `true` if the signature matches.
|
|
19
|
+
*/
|
|
20
|
+
export function verifySignature(body: Buffer, signature: string, signingSecret: string): boolean {
|
|
21
|
+
const secret = Buffer.from(signingSecret, 'hex');
|
|
22
|
+
const hmac = crypto.createHmac('sha256', secret);
|
|
23
|
+
hmac.update(body);
|
|
24
|
+
const computed = hmac.digest('hex').toUpperCase();
|
|
25
|
+
return computed === signature;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Start a tiny Express server that receives Scrapfly webhook callbacks
|
|
31
|
+
* and dispatches them to the provided `callback`. Intended for quick
|
|
32
|
+
* integration and local development; production setups should wire
|
|
33
|
+
* Scrapfly webhooks directly into their own HTTP stack via
|
|
34
|
+
* {@link verifySignature}.
|
|
35
|
+
*
|
|
36
|
+
* Requires the `express` package to be installed.
|
|
37
|
+
*
|
|
38
|
+
* @param callback Invoked with the parsed webhook body, its resource
|
|
39
|
+
* type (`scrape` or `ping`), and the raw Express request.
|
|
40
|
+
* @param app Optional pre-configured Express app to mount on.
|
|
41
|
+
* @returns The underlying Express app (for further configuration or `.listen()`).
|
|
42
|
+
*/
|
|
43
|
+
export async function createServer(
|
|
44
|
+
callback: (data: any, resourceType: string, request: any) => void,
|
|
45
|
+
app?: any,
|
|
46
|
+
): Promise<any> {
|
|
47
|
+
let express: any;
|
|
48
|
+
try {
|
|
49
|
+
const expressModule = await import('express');
|
|
50
|
+
express = expressModule.default || expressModule;
|
|
51
|
+
} catch (e) {
|
|
52
|
+
throw new Error('express is not installed, please install it with `npm install express`');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!app) {
|
|
56
|
+
app = express();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const jsonMiddleware = express.json({
|
|
60
|
+
verify: (req: any, _res: any, buf: Buffer) => {
|
|
61
|
+
req.rawBody = buf;
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
app.post('/webhook', jsonMiddleware, (req: any, res: any) => {
|
|
66
|
+
try {
|
|
67
|
+
const resourceType = req.headers['x-scrapfly-webhook-resource-type'];
|
|
68
|
+
|
|
69
|
+
if (resourceType !== 'scrape' && resourceType !== 'ping') {
|
|
70
|
+
log.error(`Unsupported resource type: ${resourceType}`);
|
|
71
|
+
return res.status(400).send(`this webhook server only supports scrape and ping calls`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const data: any = req.body;
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
callback(data, resourceType, req);
|
|
78
|
+
res.status(200).send('');
|
|
79
|
+
} catch (e) {
|
|
80
|
+
log.error('Callback error:', e);
|
|
81
|
+
res.status(500).send('');
|
|
82
|
+
}
|
|
83
|
+
} catch (e) {
|
|
84
|
+
log.error('Webhook processing error:', e);
|
|
85
|
+
res.status(400).send('');
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
return app;
|
|
90
|
+
}
|