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.
Files changed (166) hide show
  1. package/esm/browserconfig.d.ts +70 -1
  2. package/esm/browserconfig.d.ts.map +1 -1
  3. package/esm/browserconfig.js +44 -1
  4. package/esm/client.d.ts +33 -0
  5. package/esm/client.d.ts.map +1 -1
  6. package/esm/client.js +33 -5
  7. package/esm/crawl.d.ts +4 -0
  8. package/esm/crawl.d.ts.map +1 -1
  9. package/esm/crawl.js +4 -0
  10. package/esm/crawlerresult.d.ts +7 -0
  11. package/esm/crawlerresult.d.ts.map +1 -1
  12. package/esm/errors.d.ts +43 -0
  13. package/esm/errors.d.ts.map +1 -1
  14. package/esm/errors.js +43 -16
  15. package/esm/extractionconfig.d.ts +54 -9
  16. package/esm/extractionconfig.d.ts.map +1 -1
  17. package/esm/extractionconfig.js +37 -13
  18. package/esm/logger.d.ts +1 -0
  19. package/esm/logger.d.ts.map +1 -1
  20. package/esm/logger.js +1 -0
  21. package/esm/main.d.ts +27 -0
  22. package/esm/main.d.ts.map +1 -1
  23. package/esm/main.js +27 -0
  24. package/esm/monitoringconfig.d.ts +48 -11
  25. package/esm/monitoringconfig.d.ts.map +1 -1
  26. package/esm/monitoringconfig.js +14 -0
  27. package/esm/result.d.ts +2 -0
  28. package/esm/result.d.ts.map +1 -1
  29. package/esm/scrapeconfig.d.ts +10 -8
  30. package/esm/scrapeconfig.d.ts.map +1 -1
  31. package/esm/scrapeconfig.js +10 -8
  32. package/esm/screenshotconfig.d.ts +59 -26
  33. package/esm/screenshotconfig.d.ts.map +1 -1
  34. package/esm/screenshotconfig.js +38 -26
  35. package/esm/webhookserver.d.ts +24 -0
  36. package/esm/webhookserver.d.ts.map +1 -1
  37. package/esm/webhookserver.js +24 -0
  38. package/package.json +1 -1
  39. package/script/browserconfig.d.ts +70 -1
  40. package/script/browserconfig.d.ts.map +1 -1
  41. package/script/browserconfig.js +44 -1
  42. package/script/client.d.ts +33 -0
  43. package/script/client.d.ts.map +1 -1
  44. package/script/client.js +33 -5
  45. package/script/crawl.d.ts +4 -0
  46. package/script/crawl.d.ts.map +1 -1
  47. package/script/crawl.js +4 -0
  48. package/script/crawlerresult.d.ts +7 -0
  49. package/script/crawlerresult.d.ts.map +1 -1
  50. package/script/errors.d.ts +43 -0
  51. package/script/errors.d.ts.map +1 -1
  52. package/script/errors.js +43 -16
  53. package/script/extractionconfig.d.ts +54 -9
  54. package/script/extractionconfig.d.ts.map +1 -1
  55. package/script/extractionconfig.js +37 -13
  56. package/script/logger.d.ts +1 -0
  57. package/script/logger.d.ts.map +1 -1
  58. package/script/logger.js +1 -0
  59. package/script/main.d.ts +27 -0
  60. package/script/main.d.ts.map +1 -1
  61. package/script/main.js +27 -0
  62. package/script/monitoringconfig.d.ts +48 -11
  63. package/script/monitoringconfig.d.ts.map +1 -1
  64. package/script/monitoringconfig.js +14 -0
  65. package/script/result.d.ts +2 -0
  66. package/script/result.d.ts.map +1 -1
  67. package/script/scrapeconfig.d.ts +10 -8
  68. package/script/scrapeconfig.d.ts.map +1 -1
  69. package/script/scrapeconfig.js +10 -8
  70. package/script/screenshotconfig.d.ts +59 -26
  71. package/script/screenshotconfig.d.ts.map +1 -1
  72. package/script/screenshotconfig.js +38 -26
  73. package/script/webhookserver.d.ts +24 -0
  74. package/script/webhookserver.d.ts.map +1 -1
  75. package/script/webhookserver.js +24 -0
  76. package/src/_dnt.shims.ts +64 -0
  77. package/src/batch.ts +272 -0
  78. package/src/browserconfig.ts +187 -0
  79. package/src/client.ts +1637 -0
  80. package/src/crawl.ts +255 -0
  81. package/src/crawlerconfig.ts +363 -0
  82. package/src/crawlerresult.ts +478 -0
  83. package/src/deps/jsr.io/@std/path/1.0.1/_common/assert_path.ts +10 -0
  84. package/src/deps/jsr.io/@std/path/1.0.1/_common/basename.ts +53 -0
  85. package/src/deps/jsr.io/@std/path/1.0.1/_common/common.ts +26 -0
  86. package/src/deps/jsr.io/@std/path/1.0.1/_common/constants.ts +49 -0
  87. package/src/deps/jsr.io/@std/path/1.0.1/_common/dirname.ts +9 -0
  88. package/src/deps/jsr.io/@std/path/1.0.1/_common/format.ts +25 -0
  89. package/src/deps/jsr.io/@std/path/1.0.1/_common/from_file_url.ts +10 -0
  90. package/src/deps/jsr.io/@std/path/1.0.1/_common/glob_to_reg_exp.ts +290 -0
  91. package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize.ts +9 -0
  92. package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize_string.ts +74 -0
  93. package/src/deps/jsr.io/@std/path/1.0.1/_common/relative.ts +10 -0
  94. package/src/deps/jsr.io/@std/path/1.0.1/_common/strip_trailing_separators.ts +25 -0
  95. package/src/deps/jsr.io/@std/path/1.0.1/_common/to_file_url.ts +17 -0
  96. package/src/deps/jsr.io/@std/path/1.0.1/_os.ts +31 -0
  97. package/src/deps/jsr.io/@std/path/1.0.1/basename.ts +35 -0
  98. package/src/deps/jsr.io/@std/path/1.0.1/common.ts +35 -0
  99. package/src/deps/jsr.io/@std/path/1.0.1/constants.ts +18 -0
  100. package/src/deps/jsr.io/@std/path/1.0.1/dirname.ts +28 -0
  101. package/src/deps/jsr.io/@std/path/1.0.1/extname.ts +27 -0
  102. package/src/deps/jsr.io/@std/path/1.0.1/format.ts +30 -0
  103. package/src/deps/jsr.io/@std/path/1.0.1/from_file_url.ts +30 -0
  104. package/src/deps/jsr.io/@std/path/1.0.1/glob_to_regexp.ts +94 -0
  105. package/src/deps/jsr.io/@std/path/1.0.1/is_absolute.ts +30 -0
  106. package/src/deps/jsr.io/@std/path/1.0.1/is_glob.ts +49 -0
  107. package/src/deps/jsr.io/@std/path/1.0.1/join.ts +28 -0
  108. package/src/deps/jsr.io/@std/path/1.0.1/join_globs.ts +42 -0
  109. package/src/deps/jsr.io/@std/path/1.0.1/mod.ts +55 -0
  110. package/src/deps/jsr.io/@std/path/1.0.1/normalize.ts +31 -0
  111. package/src/deps/jsr.io/@std/path/1.0.1/normalize_glob.ts +45 -0
  112. package/src/deps/jsr.io/@std/path/1.0.1/parse.ts +44 -0
  113. package/src/deps/jsr.io/@std/path/1.0.1/posix/_util.ts +10 -0
  114. package/src/deps/jsr.io/@std/path/1.0.1/posix/basename.ts +39 -0
  115. package/src/deps/jsr.io/@std/path/1.0.1/posix/constants.ts +15 -0
  116. package/src/deps/jsr.io/@std/path/1.0.1/posix/dirname.ts +55 -0
  117. package/src/deps/jsr.io/@std/path/1.0.1/posix/extname.ts +73 -0
  118. package/src/deps/jsr.io/@std/path/1.0.1/posix/format.ts +31 -0
  119. package/src/deps/jsr.io/@std/path/1.0.1/posix/from_file_url.ts +25 -0
  120. package/src/deps/jsr.io/@std/path/1.0.1/posix/glob_to_regexp.ts +94 -0
  121. package/src/deps/jsr.io/@std/path/1.0.1/posix/is_absolute.ts +25 -0
  122. package/src/deps/jsr.io/@std/path/1.0.1/posix/join.ts +27 -0
  123. package/src/deps/jsr.io/@std/path/1.0.1/posix/join_globs.ts +44 -0
  124. package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize.ts +41 -0
  125. package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize_glob.ts +42 -0
  126. package/src/deps/jsr.io/@std/path/1.0.1/posix/parse.ts +121 -0
  127. package/src/deps/jsr.io/@std/path/1.0.1/posix/relative.ts +103 -0
  128. package/src/deps/jsr.io/@std/path/1.0.1/posix/resolve.ts +69 -0
  129. package/src/deps/jsr.io/@std/path/1.0.1/posix/to_file_url.ts +32 -0
  130. package/src/deps/jsr.io/@std/path/1.0.1/posix/to_namespaced_path.ts +21 -0
  131. package/src/deps/jsr.io/@std/path/1.0.1/relative.ts +32 -0
  132. package/src/deps/jsr.io/@std/path/1.0.1/resolve.ts +32 -0
  133. package/src/deps/jsr.io/@std/path/1.0.1/to_file_url.ts +30 -0
  134. package/src/deps/jsr.io/@std/path/1.0.1/to_namespaced_path.ts +31 -0
  135. package/src/deps/jsr.io/@std/path/1.0.1/types.ts +40 -0
  136. package/src/deps/jsr.io/@std/path/1.0.1/windows/_util.ts +28 -0
  137. package/src/deps/jsr.io/@std/path/1.0.1/windows/basename.ts +48 -0
  138. package/src/deps/jsr.io/@std/path/1.0.1/windows/constants.ts +15 -0
  139. package/src/deps/jsr.io/@std/path/1.0.1/windows/dirname.ts +114 -0
  140. package/src/deps/jsr.io/@std/path/1.0.1/windows/extname.ts +86 -0
  141. package/src/deps/jsr.io/@std/path/1.0.1/windows/format.ts +31 -0
  142. package/src/deps/jsr.io/@std/path/1.0.1/windows/from_file_url.ts +34 -0
  143. package/src/deps/jsr.io/@std/path/1.0.1/windows/glob_to_regexp.ts +92 -0
  144. package/src/deps/jsr.io/@std/path/1.0.1/windows/is_absolute.ts +40 -0
  145. package/src/deps/jsr.io/@std/path/1.0.1/windows/join.ts +72 -0
  146. package/src/deps/jsr.io/@std/path/1.0.1/windows/join_globs.ts +45 -0
  147. package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize.ts +132 -0
  148. package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize_glob.ts +42 -0
  149. package/src/deps/jsr.io/@std/path/1.0.1/windows/parse.ts +184 -0
  150. package/src/deps/jsr.io/@std/path/1.0.1/windows/relative.ts +128 -0
  151. package/src/deps/jsr.io/@std/path/1.0.1/windows/resolve.ts +174 -0
  152. package/src/deps/jsr.io/@std/path/1.0.1/windows/to_file_url.ts +38 -0
  153. package/src/deps/jsr.io/@std/path/1.0.1/windows/to_namespaced_path.ts +60 -0
  154. package/src/deps.ts +4 -0
  155. package/src/errors.ts +82 -0
  156. package/src/extractionconfig.ts +217 -0
  157. package/src/logger.ts +53 -0
  158. package/src/main.ts +88 -0
  159. package/src/monitoringconfig.ts +88 -0
  160. package/src/polyfill.ts +37 -0
  161. package/src/result.ts +340 -0
  162. package/src/scrapeconfig.ts +450 -0
  163. package/src/screenshotconfig.ts +237 -0
  164. package/src/types.ts +2 -0
  165. package/src/utils.ts +86 -0
  166. 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
+ }