@@ -42,16 +42,12 @@ async function validateWebhook(requestData, secret) {
42
42
43
43
if ( body instanceof ReadableStream || body . readable ) {
44
44
try {
45
- const chunks = [ ] ;
46
- for await ( const chunk of body ) {
47
- chunks . push ( Buffer . from ( chunk ) ) ;
48
- }
49
- body = Buffer . concat ( chunks ) . toString ( "utf8" ) ;
45
+ body = await new Response ( body ) . text ( ) ;
50
46
} catch ( err ) {
51
47
throw new Error ( `Error reading body: ${ err . message } ` ) ;
52
48
}
53
- } else if ( body instanceof Buffer ) {
54
- body = body . toString ( "utf8" ) ;
49
+ } else if ( isTypedArray ( body ) ) {
50
+ body = await new Blob ( [ body ] ) . text ( ) ;
55
51
} else if ( typeof body !== "string" ) {
56
52
throw new Error ( "Invalid body type" ) ;
57
53
}
@@ -231,9 +227,9 @@ async function transformFileInputs(inputs) {
231
227
// a JavaScript implenentation like base64-js.
232
228
// See: https://developer.mozilla.org/en-US/docs/Glossary/Base64
233
229
// See: https://github.com/beatgammit/base64-js
234
- buffer = Buffer . from ( await value . arrayBuffer ( ) ) ;
230
+ buffer = await value . arrayBuffer ( ) ;
235
231
mime = value . type ;
236
- } else if ( Buffer . isBuffer ( value ) ) {
232
+ } else if ( isTypedArray ( value ) ) {
237
233
buffer = value ;
238
234
} else {
239
235
return value ;
@@ -246,7 +242,7 @@ async function transformFileInputs(inputs) {
246
242
) ;
247
243
}
248
244
249
- const data = buffer . toString ( "base64" ) ;
245
+ const data = bytesToBase64 ( buffer ) ;
250
246
mime = mime ?? "application/octet-stream" ;
251
247
252
248
return `data:${ mime } ;base64,${ data } ` ;
@@ -276,6 +272,20 @@ async function transform(value, mapper) {
276
272
return await mapper ( value ) ;
277
273
}
278
274
275
+ function isTypedArray ( arr ) {
276
+ return (
277
+ arr instanceof Int8Array ||
278
+ arr instanceof Int16Array ||
279
+ arr instanceof Int32Array ||
280
+ arr instanceof Uint8Array ||
281
+ arr instanceof Uint8ClampedArray ||
282
+ arr instanceof Uint16Array ||
283
+ arr instanceof Uint32Array ||
284
+ arr instanceof Float32Array ||
285
+ arr instanceof Float64Array
286
+ ) ;
287
+ }
288
+
279
289
// Test for a plain JS object.
280
290
// Source: lodash.isPlainObject
281
291
function isPlainObject ( value ) {
0 commit comments