scrapfly-sdk 0.10.5 → 0.11.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/esm/batch.d.ts.map +1 -1
- package/esm/batch.js +6 -0
- package/esm/browserconfig.d.ts +64 -0
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +116 -0
- package/esm/client.d.ts +135 -2
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +454 -12
- package/esm/crawlerconfig.d.ts +6 -2
- package/esm/crawlerconfig.d.ts.map +1 -1
- package/esm/crawlerconfig.js +33 -4
- package/esm/extractionconfig.js +1 -1
- package/esm/main.d.ts +3 -0
- package/esm/main.d.ts.map +1 -1
- package/esm/main.js +1 -0
- package/esm/schedule.d.ts +93 -0
- package/esm/schedule.d.ts.map +1 -0
- package/esm/schedule.js +40 -0
- package/esm/scrapeconfig.d.ts +1 -0
- package/esm/scrapeconfig.d.ts.map +1 -1
- package/esm/scrapeconfig.js +9 -5
- package/esm/types.d.ts +94 -0
- package/esm/types.d.ts.map +1 -1
- package/esm/utils.d.ts +1 -1
- package/esm/utils.d.ts.map +1 -1
- package/esm/webhookserver.d.ts +7 -3
- package/esm/webhookserver.d.ts.map +1 -1
- package/esm/webhookserver.js +13 -6
- package/package.json +1 -1
- package/script/batch.d.ts.map +1 -1
- package/script/batch.js +6 -0
- package/script/browserconfig.d.ts +64 -0
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +116 -0
- package/script/client.d.ts +135 -2
- package/script/client.d.ts.map +1 -1
- package/script/client.js +454 -12
- package/script/crawlerconfig.d.ts +6 -2
- package/script/crawlerconfig.d.ts.map +1 -1
- package/script/crawlerconfig.js +33 -4
- package/script/extractionconfig.js +1 -1
- package/script/main.d.ts +3 -0
- package/script/main.d.ts.map +1 -1
- package/script/main.js +3 -1
- package/script/schedule.d.ts +93 -0
- package/script/schedule.d.ts.map +1 -0
- package/script/schedule.js +44 -0
- package/script/scrapeconfig.d.ts +1 -0
- package/script/scrapeconfig.d.ts.map +1 -1
- package/script/scrapeconfig.js +9 -5
- package/script/types.d.ts +94 -0
- package/script/types.d.ts.map +1 -1
- package/script/utils.d.ts +1 -1
- package/script/utils.d.ts.map +1 -1
- package/script/webhookserver.d.ts +7 -3
- package/script/webhookserver.d.ts.map +1 -1
- package/script/webhookserver.js +13 -6
- package/src/batch.ts +7 -1
- package/src/browserconfig.ts +99 -0
- package/src/client.ts +592 -13
- package/src/crawlerconfig.ts +27 -6
- package/src/extractionconfig.ts +1 -1
- package/src/main.ts +11 -0
- package/src/schedule.ts +101 -0
- package/src/scrapeconfig.ts +4 -5
- package/src/types.ts +115 -0
- package/src/utils.ts +6 -1
- package/src/webhookserver.ts +13 -6
package/src/crawlerconfig.ts
CHANGED
|
@@ -65,7 +65,12 @@ const VALID_WEBHOOK_EVENTS: ReadonlySet<string> = new Set([
|
|
|
65
65
|
* which lets the server apply its documented defaults (e.g. `respect_robots_txt=true`).
|
|
66
66
|
*/
|
|
67
67
|
export type CrawlerConfigOptions = {
|
|
68
|
-
url
|
|
68
|
+
// URL source — exactly one of url, url_list, remote_url_list must be set.
|
|
69
|
+
// url enables discovery; url_list and remote_url_list crawl an explicit set
|
|
70
|
+
// with discovery off.
|
|
71
|
+
url?: string;
|
|
72
|
+
url_list?: string[];
|
|
73
|
+
remote_url_list?: string;
|
|
69
74
|
|
|
70
75
|
// Crawl limits
|
|
71
76
|
page_limit?: number;
|
|
@@ -131,7 +136,9 @@ export type CrawlerConfigOptions = {
|
|
|
131
136
|
* ```
|
|
132
137
|
*/
|
|
133
138
|
export class CrawlerConfig {
|
|
134
|
-
url
|
|
139
|
+
url?: string;
|
|
140
|
+
url_list?: string[];
|
|
141
|
+
remote_url_list?: string;
|
|
135
142
|
|
|
136
143
|
page_limit?: number;
|
|
137
144
|
max_depth?: number;
|
|
@@ -174,10 +181,19 @@ export class CrawlerConfig {
|
|
|
174
181
|
constructor(options: CrawlerConfigOptions) {
|
|
175
182
|
this.validateOptions(options);
|
|
176
183
|
|
|
177
|
-
|
|
178
|
-
|
|
184
|
+
const hasSeed = typeof options.url === 'string' && options.url.trim() !== '';
|
|
185
|
+
const hasList = Array.isArray(options.url_list) && options.url_list.length > 0;
|
|
186
|
+
const hasRemote = typeof options.remote_url_list === 'string' && options.remote_url_list.trim() !== '';
|
|
187
|
+
const sourceCount = (hasSeed ? 1 : 0) + (hasList ? 1 : 0) + (hasRemote ? 1 : 0);
|
|
188
|
+
if (sourceCount === 0) {
|
|
189
|
+
throw new CrawlerConfigError('Provide one of: url, url_list, remote_url_list');
|
|
179
190
|
}
|
|
180
|
-
|
|
191
|
+
if (sourceCount > 1) {
|
|
192
|
+
throw new CrawlerConfigError('Only one of url, url_list, remote_url_list can be set');
|
|
193
|
+
}
|
|
194
|
+
if (hasSeed) this.url = options.url;
|
|
195
|
+
if (hasList) this.url_list = options.url_list;
|
|
196
|
+
if (hasRemote) this.remote_url_list = options.remote_url_list;
|
|
181
197
|
|
|
182
198
|
// Mutually exclusive path filters
|
|
183
199
|
if (options.exclude_paths && options.include_only_paths) {
|
|
@@ -272,6 +288,8 @@ export class CrawlerConfig {
|
|
|
272
288
|
private validateOptions(options: Partial<CrawlerConfigOptions>) {
|
|
273
289
|
const validKeys = new Set<string>([
|
|
274
290
|
'url',
|
|
291
|
+
'url_list',
|
|
292
|
+
'remote_url_list',
|
|
275
293
|
'page_limit',
|
|
276
294
|
'max_depth',
|
|
277
295
|
'max_duration',
|
|
@@ -320,8 +338,11 @@ export class CrawlerConfig {
|
|
|
320
338
|
* defaults (e.g. `respect_robots_txt=true`).
|
|
321
339
|
*/
|
|
322
340
|
toApiParams(): Rec<any> {
|
|
323
|
-
const params: Rec<any> = {
|
|
341
|
+
const params: Rec<any> = {};
|
|
324
342
|
const fields: Array<keyof CrawlerConfig> = [
|
|
343
|
+
'url',
|
|
344
|
+
'url_list',
|
|
345
|
+
'remote_url_list',
|
|
325
346
|
'page_limit',
|
|
326
347
|
'max_depth',
|
|
327
348
|
'max_duration',
|
package/src/extractionconfig.ts
CHANGED
|
@@ -168,7 +168,7 @@ export class ExtractionConfig {
|
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
if (this.extraction_template) {
|
|
171
|
-
params.extraction_template = this.extraction_template;
|
|
171
|
+
params.extraction_template = 'persistent:' + this.extraction_template;
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
if (this.extraction_ephemeral_template) {
|
package/src/main.ts
CHANGED
|
@@ -40,6 +40,7 @@ export {
|
|
|
40
40
|
export { ExtractionConfig } from './extractionconfig.js';
|
|
41
41
|
export { BrowserConfig, ProxyPool, OperatingSystem } from './browserconfig.js';
|
|
42
42
|
export type { BrowserConfigOptions } from './browserconfig.js';
|
|
43
|
+
export type { Vault, VaultItem, VaultItemCreate, VaultItemType, VaultSecret } from './types.js';
|
|
43
44
|
/** Namespace of every error class the SDK can throw. See {@link ScrapflyError}. */
|
|
44
45
|
export * as errors from './errors.js';
|
|
45
46
|
|
|
@@ -83,6 +84,16 @@ export type {
|
|
|
83
84
|
MonitoringTargetMetricsOptions,
|
|
84
85
|
} from './monitoringconfig.js';
|
|
85
86
|
|
|
87
|
+
export { ScheduleAPIError } from './schedule.js';
|
|
88
|
+
export type {
|
|
89
|
+
CreateScheduleRequest,
|
|
90
|
+
ListSchedulesOptions,
|
|
91
|
+
Schedule,
|
|
92
|
+
ScheduleEnd,
|
|
93
|
+
ScheduleRecurrence,
|
|
94
|
+
UpdateScheduleRequest,
|
|
95
|
+
} from './schedule.js';
|
|
96
|
+
|
|
86
97
|
export { log } from './logger.js';
|
|
87
98
|
|
|
88
99
|
export { createServer, verifySignature } from './webhookserver.js';
|
package/src/schedule.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public schedule client for the Scrapfly API.
|
|
3
|
+
*
|
|
4
|
+
* Wraps `/scrape/schedules`, `/screenshot/schedules`, `/crawl/schedules`
|
|
5
|
+
* and the cross-kind `/schedules` endpoints. The server returns fully-formed
|
|
6
|
+
* Schedule objects; we surface them as-is so callers always see the live
|
|
7
|
+
* server shape.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Bounds a recurring schedule by either a date or a fire count. */
|
|
11
|
+
export interface ScheduleEnd {
|
|
12
|
+
type: 'date' | 'count';
|
|
13
|
+
date?: string;
|
|
14
|
+
count?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* When a schedule fires next. Cron mode wins when `cron` is set; otherwise
|
|
19
|
+
* `interval` + `unit` drive the cadence.
|
|
20
|
+
*/
|
|
21
|
+
export interface ScheduleRecurrence {
|
|
22
|
+
cron?: string;
|
|
23
|
+
interval?: number;
|
|
24
|
+
unit?: 'minute' | 'hour' | 'day' | 'week' | 'month';
|
|
25
|
+
days?: string[];
|
|
26
|
+
ends?: ScheduleEnd;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Public-facing request envelope for creating a schedule. The kind-specific
|
|
31
|
+
* config (`scrape_config` / `screenshot_config` / `crawler_config`) is
|
|
32
|
+
* supplied as a separate argument by the matching `create*Schedule` method.
|
|
33
|
+
*/
|
|
34
|
+
export interface CreateScheduleRequest {
|
|
35
|
+
webhook_name: string;
|
|
36
|
+
recurrence?: ScheduleRecurrence;
|
|
37
|
+
scheduled_date?: string;
|
|
38
|
+
allow_concurrency?: boolean;
|
|
39
|
+
retry_on_failure?: boolean;
|
|
40
|
+
max_retries?: number;
|
|
41
|
+
notes?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** PATCH payload. Only fields explicitly set are forwarded. */
|
|
45
|
+
export interface UpdateScheduleRequest {
|
|
46
|
+
recurrence?: ScheduleRecurrence;
|
|
47
|
+
scheduled_date?: string;
|
|
48
|
+
allow_concurrency?: boolean;
|
|
49
|
+
retry_on_failure?: boolean;
|
|
50
|
+
max_retries?: number;
|
|
51
|
+
notes?: string;
|
|
52
|
+
scrape_config?: Record<string, unknown>;
|
|
53
|
+
screenshot_config?: Record<string, unknown>;
|
|
54
|
+
crawler_config?: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Server-side schedule record. Returned by every read or mutation endpoint. */
|
|
58
|
+
export interface Schedule {
|
|
59
|
+
id: string;
|
|
60
|
+
kind: string;
|
|
61
|
+
status: string;
|
|
62
|
+
next_scheduled_date?: string;
|
|
63
|
+
scheduled_date?: string;
|
|
64
|
+
recurrence?: ScheduleRecurrence;
|
|
65
|
+
metadata?: Record<string, unknown>;
|
|
66
|
+
notes?: string | null;
|
|
67
|
+
created_by?: string | null;
|
|
68
|
+
created_at: string;
|
|
69
|
+
updated_at: string;
|
|
70
|
+
cancelled_at?: string | null;
|
|
71
|
+
allow_concurrency: boolean;
|
|
72
|
+
retry_on_failure: boolean;
|
|
73
|
+
max_retries: number;
|
|
74
|
+
webhook_uuid?: string | null;
|
|
75
|
+
user_uuid?: string | null;
|
|
76
|
+
consecutive_failures?: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface ListSchedulesOptions {
|
|
80
|
+
status?: string;
|
|
81
|
+
kind?: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Raised on any non-2xx response from a `/schedules/*` endpoint. The
|
|
86
|
+
* `code` property carries the public `ERR::SCHEDULER::*` identifier so
|
|
87
|
+
* callers can branch on it without parsing the message string.
|
|
88
|
+
*/
|
|
89
|
+
export class ScheduleAPIError extends Error {
|
|
90
|
+
code: string;
|
|
91
|
+
httpStatusCode: number;
|
|
92
|
+
details: unknown;
|
|
93
|
+
|
|
94
|
+
constructor(args: { code: string; message: string; httpStatusCode: number; details?: unknown }) {
|
|
95
|
+
super(args.message);
|
|
96
|
+
this.name = 'ScheduleAPIError';
|
|
97
|
+
this.code = args.code;
|
|
98
|
+
this.httpStatusCode = args.httpStatusCode;
|
|
99
|
+
this.details = args.details;
|
|
100
|
+
}
|
|
101
|
+
}
|
package/src/scrapeconfig.ts
CHANGED
|
@@ -113,6 +113,7 @@ type ScrapeConfigOptions = {
|
|
|
113
113
|
export class ScrapeConfig {
|
|
114
114
|
static PUBLIC_DATACENTER_POOL = 'public_datacenter_pool';
|
|
115
115
|
static PUBLIC_RESIDENTIAL_POOL = 'public_residential_pool';
|
|
116
|
+
static PUBLIC_TOR_POOL = 'public_tor_pool';
|
|
116
117
|
|
|
117
118
|
url: string;
|
|
118
119
|
retry = true;
|
|
@@ -145,7 +146,7 @@ export class ScrapeConfig {
|
|
|
145
146
|
js?: string;
|
|
146
147
|
rendering_wait?: number;
|
|
147
148
|
wait_for_selector?: string;
|
|
148
|
-
session_sticky_proxy =
|
|
149
|
+
session_sticky_proxy = true;
|
|
149
150
|
screenshots?: Rec<any>;
|
|
150
151
|
screenshot_flags?:
|
|
151
152
|
('load_images' | 'dark_mode' | 'block_banners' | 'print_media_format' | 'high_quality' | ScreenshotFlags)[];
|
|
@@ -402,7 +403,7 @@ export class ScrapeConfig {
|
|
|
402
403
|
}
|
|
403
404
|
|
|
404
405
|
if (this.extraction_template) {
|
|
405
|
-
params.extraction_template = this.extraction_template;
|
|
406
|
+
params.extraction_template = 'persistent:' + this.extraction_template;
|
|
406
407
|
}
|
|
407
408
|
|
|
408
409
|
if (this.extraction_ephemeral_template) {
|
|
@@ -421,9 +422,7 @@ export class ScrapeConfig {
|
|
|
421
422
|
}
|
|
422
423
|
if (this.session) {
|
|
423
424
|
params.session = this.session;
|
|
424
|
-
|
|
425
|
-
params.session_sticky_proxy = true;
|
|
426
|
-
}
|
|
425
|
+
params.session_sticky_proxy = this.session_sticky_proxy;
|
|
427
426
|
} else {
|
|
428
427
|
if (this.session_sticky_proxy) {
|
|
429
428
|
log.warn('Params "session_sticky_proxy" is ignored. Works only if session is enabled');
|
package/src/types.ts
CHANGED
|
@@ -1,2 +1,117 @@
|
|
|
1
1
|
export type Rec<T> = Record<string, T>;
|
|
2
2
|
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'HEAD';
|
|
3
|
+
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Cloud Browser Credential Vault types.
|
|
6
|
+
//
|
|
7
|
+
// Vaults store browser credentials (passwords, passkeys, cookies, TOTP seeds)
|
|
8
|
+
// encrypted server-side under a customer-held vault key. The server emits the
|
|
9
|
+
// raw vault key exactly once on POST /vault and POST /vault/{id}/rotate; the
|
|
10
|
+
// SDK MUST never log, print, or include it in error messages.
|
|
11
|
+
// See compliance memory at agent_secret_tokenization_boundary.md.
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
|
|
14
|
+
/** Vault metadata returned by the server (no secret material). */
|
|
15
|
+
export type Vault = {
|
|
16
|
+
id: string;
|
|
17
|
+
user_uuid?: string;
|
|
18
|
+
project?: string;
|
|
19
|
+
env?: string;
|
|
20
|
+
name: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
item_count?: number;
|
|
23
|
+
created_at?: string;
|
|
24
|
+
updated_at?: string;
|
|
25
|
+
// Allow forward-compat fields the server may add.
|
|
26
|
+
[k: string]: unknown;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Discriminator for the secret payload of a {@link VaultItem}. */
|
|
30
|
+
export type VaultItemType = 'password' | 'passkey' | 'cookie' | 'totp';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Plaintext secret payload used when creating or rotating an item.
|
|
34
|
+
*
|
|
35
|
+
* Only one variant should be set, matching the item's `type`. The flat shape
|
|
36
|
+
* mirrors the dashboard form ergonomics; the API also accepts a typed form
|
|
37
|
+
* (e.g. `{ password: { password: 'hunter2' } }`).
|
|
38
|
+
*/
|
|
39
|
+
export type VaultSecret = {
|
|
40
|
+
// password
|
|
41
|
+
password?: string | { password: string };
|
|
42
|
+
// passkey (flat)
|
|
43
|
+
credentialId?: string;
|
|
44
|
+
privateKey?: string;
|
|
45
|
+
userHandle?: string;
|
|
46
|
+
signCount?: number;
|
|
47
|
+
// passkey (typed)
|
|
48
|
+
passkey?: {
|
|
49
|
+
credentialId: string;
|
|
50
|
+
privateKey: string;
|
|
51
|
+
userHandle?: string;
|
|
52
|
+
signCount?: number;
|
|
53
|
+
};
|
|
54
|
+
// cookie (flat)
|
|
55
|
+
name?: string;
|
|
56
|
+
value?: string;
|
|
57
|
+
domain?: string;
|
|
58
|
+
path?: string;
|
|
59
|
+
secure?: boolean;
|
|
60
|
+
httpOnly?: boolean;
|
|
61
|
+
sameSite?: 'Strict' | 'Lax' | 'None' | string;
|
|
62
|
+
expires?: number;
|
|
63
|
+
// cookie (typed)
|
|
64
|
+
cookie?: {
|
|
65
|
+
name: string;
|
|
66
|
+
value?: string;
|
|
67
|
+
domain: string;
|
|
68
|
+
path?: string;
|
|
69
|
+
secure?: boolean;
|
|
70
|
+
httpOnly?: boolean;
|
|
71
|
+
sameSite?: 'Strict' | 'Lax' | 'None' | string;
|
|
72
|
+
expires?: number;
|
|
73
|
+
};
|
|
74
|
+
// totp (flat — `seed` to avoid colliding with the envelope's `secret` key)
|
|
75
|
+
seed?: string;
|
|
76
|
+
issuer?: string;
|
|
77
|
+
account?: string;
|
|
78
|
+
algorithm?: 'SHA1' | 'SHA256' | 'SHA512' | string;
|
|
79
|
+
digits?: number;
|
|
80
|
+
period?: number;
|
|
81
|
+
// totp (typed)
|
|
82
|
+
totp?: {
|
|
83
|
+
secret: string;
|
|
84
|
+
issuer?: string;
|
|
85
|
+
account?: string;
|
|
86
|
+
algorithm?: 'SHA1' | 'SHA256' | 'SHA512' | string;
|
|
87
|
+
digits?: number;
|
|
88
|
+
period?: number;
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/** Server-stored vault item (secret_blob is opaque ciphertext). */
|
|
93
|
+
export type VaultItem = {
|
|
94
|
+
id: string;
|
|
95
|
+
vault_id: string;
|
|
96
|
+
type: VaultItemType;
|
|
97
|
+
label: string;
|
|
98
|
+
origin: string;
|
|
99
|
+
username?: string;
|
|
100
|
+
/** Base64-encoded envelope ciphertext; useless without the vault key. */
|
|
101
|
+
secret_blob?: string;
|
|
102
|
+
master_key_version?: number;
|
|
103
|
+
metadata?: Record<string, unknown>;
|
|
104
|
+
created_at?: string;
|
|
105
|
+
updated_at?: string;
|
|
106
|
+
[k: string]: unknown;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** Payload accepted by `cloudBrowserVaultItemCreate`. */
|
|
110
|
+
export type VaultItemCreate = {
|
|
111
|
+
type: VaultItemType;
|
|
112
|
+
label: string;
|
|
113
|
+
origin: string;
|
|
114
|
+
username?: string;
|
|
115
|
+
secret: VaultSecret;
|
|
116
|
+
metadata?: Record<string, unknown>;
|
|
117
|
+
};
|
package/src/utils.ts
CHANGED
|
@@ -15,7 +15,12 @@ export type RequestOptions = {
|
|
|
15
15
|
url: string;
|
|
16
16
|
method?: string;
|
|
17
17
|
headers?: any;
|
|
18
|
-
|
|
18
|
+
// Body accepts strings, raw bytes, or any FormData-like value. The latter
|
|
19
|
+
// is used by the Crawler client to upload an in-memory URL list as a
|
|
20
|
+
// multipart 'urls' part; fetch() fills in the multipart boundary header
|
|
21
|
+
// automatically when given a FormData instance.
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
|
+
body?: string | Uint8Array | any;
|
|
19
24
|
};
|
|
20
25
|
|
|
21
26
|
export async function fetchRetry(
|
package/src/webhookserver.ts
CHANGED
|
@@ -12,17 +12,24 @@ export enum ResourceType {
|
|
|
12
12
|
* HMAC-SHA256 signature on the request body against the configured
|
|
13
13
|
* signing secret.
|
|
14
14
|
*
|
|
15
|
-
* @param body Raw request body as received (
|
|
16
|
-
*
|
|
17
|
-
*
|
|
15
|
+
* @param body Raw request body as received (verify BEFORE any decompression
|
|
16
|
+
* or JSON parsing — the signature is computed on the bytes
|
|
17
|
+
* Scrapfly sent on the wire).
|
|
18
|
+
* @param signature Value of the `X-Scrapfly-Webhook-Signature` header
|
|
19
|
+
* (uppercase hex digest).
|
|
20
|
+
* @param signingSecret Webhook signing secret from the dashboard, passed as-is
|
|
21
|
+
* (UTF-8 string; not hex-encoded).
|
|
18
22
|
* @returns `true` if the signature matches.
|
|
19
23
|
*/
|
|
20
24
|
export function verifySignature(body: Buffer, signature: string, signingSecret: string): boolean {
|
|
21
|
-
const
|
|
22
|
-
const hmac = crypto.createHmac('sha256', secret);
|
|
25
|
+
const hmac = crypto.createHmac('sha256', Buffer.from(signingSecret, 'utf8'));
|
|
23
26
|
hmac.update(body);
|
|
24
27
|
const computed = hmac.digest('hex').toUpperCase();
|
|
25
|
-
|
|
28
|
+
const received = signature.toUpperCase();
|
|
29
|
+
if (computed.length !== received.length) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
return crypto.timingSafeEqual(Buffer.from(computed), Buffer.from(received));
|
|
26
33
|
}
|
|
27
34
|
|
|
28
35
|
|