@@ -35,15 +35,17 @@ async function validateWebhook(requestData, secret) {
35
35
const signingSecret = secret || requestData . secret ;
36
36
37
37
if ( requestData && requestData . headers && requestData . body ) {
38
- id =
39
- requestData . headers [ "webhook-id" ] ||
40
- requestData . headers . get ?. ( "webhook-id" ) ;
41
- timestamp =
42
- requestData . headers [ "webhook-timestamp" ] ||
43
- requestData . headers . get ?. ( "webhook-timestamp" ) ;
44
- signature =
45
- requestData . headers [ "webhook-signature" ] ||
46
- requestData . headers . get ?. ( "webhook-signature" ) ;
38
+ if ( typeof requestData . headers . get === "function" ) {
39
+ // Headers object (e.g. Fetch API Headers)
40
+ id = requestData . headers . get ( "webhook-id" ) ;
41
+ timestamp = requestData . headers . get ( "webhook-timestamp" ) ;
42
+ signature = requestData . headers . get ( "webhook-signature" ) ;
43
+ } else {
44
+ // Plain object with header key-value pairs
45
+ id = requestData . headers [ "webhook-id" ] ;
46
+ timestamp = requestData . headers [ "webhook-timestamp" ] ;
47
+ signature = requestData . headers [ "webhook-signature" ] ;
48
+ }
47
49
body = requestData . body ;
48
50
}
49
51
@@ -75,11 +77,18 @@ async function validateWebhook(requestData, secret) {
75
77
76
78
const signedContent = `${ id } .${ timestamp } .${ body } ` ;
77
79
78
- const computedSignature = await createHMACSHA256 ( signingSecret . split ( "_" ) . pop ( ) , signedContent ) ;
80
+ const computedSignature = await createHMACSHA256 (
81
+ signingSecret . split ( "_" ) . pop ( ) ,
82
+ signedContent
83
+ ) ;
79
84
80
- const expectedSignatures = signature . split ( " " ) . map ( ( sig ) => sig . split ( "," ) [ 1 ] ) ;
85
+ const expectedSignatures = signature
86
+ . split ( " " )
87
+ . map ( ( sig ) => sig . split ( "," ) [ 1 ] ) ;
81
88
82
- return expectedSignatures . some ( ( expectedSignature ) => expectedSignature === computedSignature ) ;
89
+ return expectedSignatures . some (
90
+ ( expectedSignature ) => expectedSignature === computedSignature
91
+ ) ;
83
92
}
84
93
85
94
/**
@@ -106,9 +115,13 @@ async function createHMACSHA256(secret, data) {
106
115
crypto = require . call ( null , "node:crypto" ) . webcrypto ;
107
116
}
108
117
109
- const key = await crypto . subtle . importKey ( "raw" , base64ToBytes ( secret ) , { name : "HMAC" , hash : "SHA-256" } , false , [
110
- "sign" ,
111
- ] ) ;
118
+ const key = await crypto . subtle . importKey (
119
+ "raw" ,
120
+ base64ToBytes ( secret ) ,
121
+ { name : "HMAC" , hash : "SHA-256" } ,
122
+ false ,
123
+ [ "sign" ]
124
+ ) ;
112
125
113
126
const signature = await crypto . subtle . sign ( "HMAC" , key , encoder . encode ( data ) ) ;
114
127
return bytesToBase64 ( signature ) ;
@@ -232,7 +245,11 @@ async function transformFileInputs(client, inputs, strategy) {
232
245
try {
233
246
return await transformFileInputsToReplicateFileURLs ( client , inputs ) ;
234
247
} catch ( error ) {
235
- if ( error instanceof ApiError && error . response . status >= 400 && error . response . status < 500 ) {
248
+ if (
249
+ error instanceof ApiError &&
250
+ error . response . status >= 400 &&
251
+ error . response . status < 500
252
+ ) {
236
253
throw error ;
237
254
}
238
255
return await transformFileInputsToBase64EncodedDataURIs ( inputs ) ;
@@ -296,7 +313,7 @@ async function transformFileInputsToBase64EncodedDataURIs(inputs) {
296
313
totalBytes += buffer . byteLength ;
297
314
if ( totalBytes > MAX_DATA_URI_SIZE ) {
298
315
throw new Error (
299
- `Combined filesize of prediction ${ totalBytes } bytes exceeds 10mb limit for inline encoding, please provide URLs instead` ,
316
+ `Combined filesize of prediction ${ totalBytes } bytes exceeds 10mb limit for inline encoding, please provide URLs instead`
300
317
) ;
301
318
}
302
319
@@ -354,11 +371,14 @@ function isPlainObject(value) {
354
371
if ( proto === null ) {
355
372
return true ;
356
373
}
357
- const Ctor = Object . prototype . hasOwnProperty . call ( proto , "constructor" ) && proto . constructor ;
374
+ const Ctor =
375
+ Object . prototype . hasOwnProperty . call ( proto , "constructor" ) &&
376
+ proto . constructor ;
358
377
return (
359
378
typeof Ctor === "function" &&
360
379
Ctor instanceof Ctor &&
361
- Function . prototype . toString . call ( Ctor ) === Function . prototype . toString . call ( Object )
380
+ Function . prototype . toString . call ( Ctor ) ===
381
+ Function . prototype . toString . call ( Object )
362
382
) ;
363
383
}
364
384
0 commit comments