scrapfly-sdk 0.10.2 → 0.10.4

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 (100) hide show
  1. package/esm/batch.d.ts.map +1 -1
  2. package/esm/batch.js +4 -0
  3. package/esm/client.d.ts.map +1 -1
  4. package/esm/client.js +17 -8
  5. package/package.json +1 -1
  6. package/script/batch.d.ts.map +1 -1
  7. package/script/batch.js +4 -0
  8. package/script/client.d.ts.map +1 -1
  9. package/script/client.js +17 -8
  10. package/src/_dnt.shims.ts +64 -0
  11. package/src/batch.ts +272 -0
  12. package/src/browserconfig.ts +118 -0
  13. package/src/client.ts +1609 -0
  14. package/src/crawl.ts +251 -0
  15. package/src/crawlerconfig.ts +363 -0
  16. package/src/crawlerresult.ts +471 -0
  17. package/src/deps/jsr.io/@std/path/1.0.1/_common/assert_path.ts +10 -0
  18. package/src/deps/jsr.io/@std/path/1.0.1/_common/basename.ts +53 -0
  19. package/src/deps/jsr.io/@std/path/1.0.1/_common/common.ts +26 -0
  20. package/src/deps/jsr.io/@std/path/1.0.1/_common/constants.ts +49 -0
  21. package/src/deps/jsr.io/@std/path/1.0.1/_common/dirname.ts +9 -0
  22. package/src/deps/jsr.io/@std/path/1.0.1/_common/format.ts +25 -0
  23. package/src/deps/jsr.io/@std/path/1.0.1/_common/from_file_url.ts +10 -0
  24. package/src/deps/jsr.io/@std/path/1.0.1/_common/glob_to_reg_exp.ts +290 -0
  25. package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize.ts +9 -0
  26. package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize_string.ts +74 -0
  27. package/src/deps/jsr.io/@std/path/1.0.1/_common/relative.ts +10 -0
  28. package/src/deps/jsr.io/@std/path/1.0.1/_common/strip_trailing_separators.ts +25 -0
  29. package/src/deps/jsr.io/@std/path/1.0.1/_common/to_file_url.ts +17 -0
  30. package/src/deps/jsr.io/@std/path/1.0.1/_os.ts +31 -0
  31. package/src/deps/jsr.io/@std/path/1.0.1/basename.ts +35 -0
  32. package/src/deps/jsr.io/@std/path/1.0.1/common.ts +35 -0
  33. package/src/deps/jsr.io/@std/path/1.0.1/constants.ts +18 -0
  34. package/src/deps/jsr.io/@std/path/1.0.1/dirname.ts +28 -0
  35. package/src/deps/jsr.io/@std/path/1.0.1/extname.ts +27 -0
  36. package/src/deps/jsr.io/@std/path/1.0.1/format.ts +30 -0
  37. package/src/deps/jsr.io/@std/path/1.0.1/from_file_url.ts +30 -0
  38. package/src/deps/jsr.io/@std/path/1.0.1/glob_to_regexp.ts +94 -0
  39. package/src/deps/jsr.io/@std/path/1.0.1/is_absolute.ts +30 -0
  40. package/src/deps/jsr.io/@std/path/1.0.1/is_glob.ts +49 -0
  41. package/src/deps/jsr.io/@std/path/1.0.1/join.ts +28 -0
  42. package/src/deps/jsr.io/@std/path/1.0.1/join_globs.ts +42 -0
  43. package/src/deps/jsr.io/@std/path/1.0.1/mod.ts +55 -0
  44. package/src/deps/jsr.io/@std/path/1.0.1/normalize.ts +31 -0
  45. package/src/deps/jsr.io/@std/path/1.0.1/normalize_glob.ts +45 -0
  46. package/src/deps/jsr.io/@std/path/1.0.1/parse.ts +44 -0
  47. package/src/deps/jsr.io/@std/path/1.0.1/posix/_util.ts +10 -0
  48. package/src/deps/jsr.io/@std/path/1.0.1/posix/basename.ts +39 -0
  49. package/src/deps/jsr.io/@std/path/1.0.1/posix/constants.ts +15 -0
  50. package/src/deps/jsr.io/@std/path/1.0.1/posix/dirname.ts +55 -0
  51. package/src/deps/jsr.io/@std/path/1.0.1/posix/extname.ts +73 -0
  52. package/src/deps/jsr.io/@std/path/1.0.1/posix/format.ts +31 -0
  53. package/src/deps/jsr.io/@std/path/1.0.1/posix/from_file_url.ts +25 -0
  54. package/src/deps/jsr.io/@std/path/1.0.1/posix/glob_to_regexp.ts +94 -0
  55. package/src/deps/jsr.io/@std/path/1.0.1/posix/is_absolute.ts +25 -0
  56. package/src/deps/jsr.io/@std/path/1.0.1/posix/join.ts +27 -0
  57. package/src/deps/jsr.io/@std/path/1.0.1/posix/join_globs.ts +44 -0
  58. package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize.ts +41 -0
  59. package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize_glob.ts +42 -0
  60. package/src/deps/jsr.io/@std/path/1.0.1/posix/parse.ts +121 -0
  61. package/src/deps/jsr.io/@std/path/1.0.1/posix/relative.ts +103 -0
  62. package/src/deps/jsr.io/@std/path/1.0.1/posix/resolve.ts +69 -0
  63. package/src/deps/jsr.io/@std/path/1.0.1/posix/to_file_url.ts +32 -0
  64. package/src/deps/jsr.io/@std/path/1.0.1/posix/to_namespaced_path.ts +21 -0
  65. package/src/deps/jsr.io/@std/path/1.0.1/relative.ts +32 -0
  66. package/src/deps/jsr.io/@std/path/1.0.1/resolve.ts +32 -0
  67. package/src/deps/jsr.io/@std/path/1.0.1/to_file_url.ts +30 -0
  68. package/src/deps/jsr.io/@std/path/1.0.1/to_namespaced_path.ts +31 -0
  69. package/src/deps/jsr.io/@std/path/1.0.1/types.ts +40 -0
  70. package/src/deps/jsr.io/@std/path/1.0.1/windows/_util.ts +28 -0
  71. package/src/deps/jsr.io/@std/path/1.0.1/windows/basename.ts +48 -0
  72. package/src/deps/jsr.io/@std/path/1.0.1/windows/constants.ts +15 -0
  73. package/src/deps/jsr.io/@std/path/1.0.1/windows/dirname.ts +114 -0
  74. package/src/deps/jsr.io/@std/path/1.0.1/windows/extname.ts +86 -0
  75. package/src/deps/jsr.io/@std/path/1.0.1/windows/format.ts +31 -0
  76. package/src/deps/jsr.io/@std/path/1.0.1/windows/from_file_url.ts +34 -0
  77. package/src/deps/jsr.io/@std/path/1.0.1/windows/glob_to_regexp.ts +92 -0
  78. package/src/deps/jsr.io/@std/path/1.0.1/windows/is_absolute.ts +40 -0
  79. package/src/deps/jsr.io/@std/path/1.0.1/windows/join.ts +72 -0
  80. package/src/deps/jsr.io/@std/path/1.0.1/windows/join_globs.ts +45 -0
  81. package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize.ts +132 -0
  82. package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize_glob.ts +42 -0
  83. package/src/deps/jsr.io/@std/path/1.0.1/windows/parse.ts +184 -0
  84. package/src/deps/jsr.io/@std/path/1.0.1/windows/relative.ts +128 -0
  85. package/src/deps/jsr.io/@std/path/1.0.1/windows/resolve.ts +174 -0
  86. package/src/deps/jsr.io/@std/path/1.0.1/windows/to_file_url.ts +38 -0
  87. package/src/deps/jsr.io/@std/path/1.0.1/windows/to_namespaced_path.ts +60 -0
  88. package/src/deps.ts +4 -0
  89. package/src/errors.ts +55 -0
  90. package/src/extractionconfig.ts +175 -0
  91. package/src/logger.ts +51 -0
  92. package/src/main.ts +61 -0
  93. package/src/monitoringconfig.ts +50 -0
  94. package/src/polyfill.ts +37 -0
  95. package/src/result.ts +338 -0
  96. package/src/scrapeconfig.ts +448 -0
  97. package/src/screenshotconfig.ts +204 -0
  98. package/src/types.ts +2 -0
  99. package/src/utils.ts +86 -0
  100. package/src/webhookserver.ts +66 -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,66 @@
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
+ export function verifySignature(body: Buffer, signature: string, signingSecret: string): boolean {
11
+ const secret = Buffer.from(signingSecret, 'hex');
12
+ const hmac = crypto.createHmac('sha256', secret);
13
+ hmac.update(body);
14
+ const computed = hmac.digest('hex').toUpperCase();
15
+ return computed === signature;
16
+ }
17
+
18
+
19
+ export async function createServer(
20
+ callback: (data: any, resourceType: string, request: any) => void,
21
+ app?: any,
22
+ ): Promise<any> {
23
+ let express: any;
24
+ try {
25
+ const expressModule = await import('express');
26
+ express = expressModule.default || expressModule;
27
+ } catch (e) {
28
+ throw new Error('express is not installed, please install it with `npm install express`');
29
+ }
30
+
31
+ if (!app) {
32
+ app = express();
33
+ }
34
+
35
+ const jsonMiddleware = express.json({
36
+ verify: (req: any, _res: any, buf: Buffer) => {
37
+ req.rawBody = buf;
38
+ },
39
+ });
40
+
41
+ app.post('/webhook', jsonMiddleware, (req: any, res: any) => {
42
+ try {
43
+ const resourceType = req.headers['x-scrapfly-webhook-resource-type'];
44
+
45
+ if (resourceType !== 'scrape' && resourceType !== 'ping') {
46
+ log.error(`Unsupported resource type: ${resourceType}`);
47
+ return res.status(400).send(`this webhook server only supports scrape and ping calls`);
48
+ }
49
+
50
+ const data: any = req.body;
51
+
52
+ try {
53
+ callback(data, resourceType, req);
54
+ res.status(200).send('');
55
+ } catch (e) {
56
+ log.error('Callback error:', e);
57
+ res.status(500).send('');
58
+ }
59
+ } catch (e) {
60
+ log.error('Webhook processing error:', e);
61
+ res.status(400).send('');
62
+ }
63
+ });
64
+
65
+ return app;
66
+ }