@@ -42,16 +42,12 @@ async function validateWebhook(requestData, secret) {
4242
4343 if ( body instanceof ReadableStream || body . readable ) {
4444 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 ( ) ;
5046 } catch ( err ) {
5147 throw new Error ( `Error reading body: ${ err . message } ` ) ;
5248 }
53- } else if ( body instanceof Buffer ) {
54- body = body . toString ( "utf8" ) ;
49+ } else if ( isTypedArray ( body ) ) {
50+ body = await new Blob ( [ body ] ) . text ( ) ;
5551 } else if ( typeof body !== "string" ) {
5652 throw new Error ( "Invalid body type" ) ;
5753 }
@@ -231,9 +227,9 @@ async function transformFileInputs(inputs) {
231227 // a JavaScript implenentation like base64-js.
232228 // See: https://developer.mozilla.org/en-US/docs/Glossary/Base64
233229 // See: https://github.com/beatgammit/base64-js
234- buffer = Buffer . from ( await value . arrayBuffer ( ) ) ;
230+ buffer = await value . arrayBuffer ( ) ;
235231 mime = value . type ;
236- } else if ( Buffer . isBuffer ( value ) ) {
232+ } else if ( isTypedArray ( value ) ) {
237233 buffer = value ;
238234 } else {
239235 return value ;
@@ -246,7 +242,7 @@ async function transformFileInputs(inputs) {
246242 ) ;
247243 }
248244
249- const data = buffer . toString ( "base64" ) ;
245+ const data = bytesToBase64 ( buffer ) ;
250246 mime = mime ?? "application/octet-stream" ;
251247
252248 return `data:${ mime } ;base64,${ data } ` ;
@@ -276,6 +272,20 @@ async function transform(value, mapper) {
276272 return await mapper ( value ) ;
277273}
278274
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+
279289// Test for a plain JS object.
280290// Source: lodash.isPlainObject
281291function isPlainObject ( value ) {
0 commit comments