Skip to content

Commit

Permalink
fix(http): handle UInt8Array on body (#7546) (#7558)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Jul 9, 2024
1 parent 8e15a3e commit 39bbc02
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 44 deletions.
35 changes: 21 additions & 14 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 22 additions & 16 deletions core/native-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,30 @@ const convertBody = async (
body: Document | XMLHttpRequestBodyInit | ReadableStream<any> | undefined,
contentType?: string,
): Promise<any> => {
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 {
Expand Down
35 changes: 21 additions & 14 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 39bbc02

Please sign in to comment.