From a6939524a6b0a0a6cc65a41f4fe04dc1f4674da0 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 5 Jul 2024 17:44:53 +0200 Subject: [PATCH] fix(http): handle UInt8Array on body --- .../src/main/assets/native-bridge.js | 35 ++++++++++------- core/native-bridge.ts | 38 +++++++++++-------- .../Capacitor/assets/native-bridge.js | 35 ++++++++++------- 3 files changed, 64 insertions(+), 44 deletions(-) diff --git a/android/capacitor/src/main/assets/native-bridge.js b/android/capacitor/src/main/assets/native-bridge.js index b0f7581a42..33b7a33c53 100644 --- a/android/capacitor/src/main/assets/native-bridge.js +++ b/android/capacitor/src/main/assets/native-bridge.js @@ -65,22 +65,29 @@ var nativeBridge = (function (exports) { return newFormData; }; const convertBody = async (body, contentType) => { - if (body instanceof ReadableStream) { - const reader = body.getReader(); - const chunks = []; - while (true) { - const { done, value } = await reader.read(); - if (done) - break; - chunks.push(value); + if (body instanceof ReadableStream || body instanceof Uint8Array) { + let encodedData; + if (body instanceof ReadableStream) { + const reader = body.getReader(); + const chunks = []; + while (true) { + const { done, value } = await reader.read(); + if (done) + break; + chunks.push(value); + } + const concatenated = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0)); + let position = 0; + for (const chunk of chunks) { + concatenated.set(chunk, position); + position += chunk.length; + } + encodedData = concatenated; } - const concatenated = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0)); - let position = 0; - for (const chunk of chunks) { - concatenated.set(chunk, position); - position += chunk.length; + else { + encodedData = body; } - let data = new TextDecoder().decode(concatenated); + let data = new TextDecoder().decode(encodedData); let type; if (contentType === 'application/json') { try { diff --git a/core/native-bridge.ts b/core/native-bridge.ts index b0e638d949..0040a43bcc 100644 --- a/core/native-bridge.ts +++ b/core/native-bridge.ts @@ -55,24 +55,30 @@ const convertBody = async ( body: Document | XMLHttpRequestBodyInit | ReadableStream | undefined, contentType?: string, ): Promise => { - if (body instanceof ReadableStream) { - const reader = body.getReader(); - const chunks: any[] = []; - while (true) { - const { done, value } = await reader.read(); - if (done) break; - chunks.push(value); - } - const concatenated = new Uint8Array( - chunks.reduce((acc, chunk) => acc + chunk.length, 0), - ); - let position = 0; - for (const chunk of chunks) { - concatenated.set(chunk, position); - position += chunk.length; + if (body instanceof ReadableStream || body instanceof Uint8Array) { + let encodedData; + if (body instanceof ReadableStream) { + const reader = body.getReader(); + const chunks: any[] = []; + while (true) { + const { done, value } = await reader.read(); + if (done) break; + chunks.push(value); + } + const concatenated = new Uint8Array( + chunks.reduce((acc, chunk) => acc + chunk.length, 0), + ); + let position = 0; + for (const chunk of chunks) { + concatenated.set(chunk, position); + position += chunk.length; + } + encodedData = concatenated; + } else { + encodedData = body; } - let data = new TextDecoder().decode(concatenated); + let data = new TextDecoder().decode(encodedData); let type; if (contentType === 'application/json') { try { diff --git a/ios/Capacitor/Capacitor/assets/native-bridge.js b/ios/Capacitor/Capacitor/assets/native-bridge.js index b0f7581a42..33b7a33c53 100644 --- a/ios/Capacitor/Capacitor/assets/native-bridge.js +++ b/ios/Capacitor/Capacitor/assets/native-bridge.js @@ -65,22 +65,29 @@ var nativeBridge = (function (exports) { return newFormData; }; const convertBody = async (body, contentType) => { - if (body instanceof ReadableStream) { - const reader = body.getReader(); - const chunks = []; - while (true) { - const { done, value } = await reader.read(); - if (done) - break; - chunks.push(value); + if (body instanceof ReadableStream || body instanceof Uint8Array) { + let encodedData; + if (body instanceof ReadableStream) { + const reader = body.getReader(); + const chunks = []; + while (true) { + const { done, value } = await reader.read(); + if (done) + break; + chunks.push(value); + } + const concatenated = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0)); + let position = 0; + for (const chunk of chunks) { + concatenated.set(chunk, position); + position += chunk.length; + } + encodedData = concatenated; } - const concatenated = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0)); - let position = 0; - for (const chunk of chunks) { - concatenated.set(chunk, position); - position += chunk.length; + else { + encodedData = body; } - let data = new TextDecoder().decode(concatenated); + let data = new TextDecoder().decode(encodedData); let type; if (contentType === 'application/json') { try {