Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(http): parse readablestream data on fetch request objects #6919

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,52 @@ var nativeBridge = (function (exports) {
}
return newFormData;
};
const convertBody = async (body) => {
if (body instanceof FormData) {
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);
}
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;
}
let data = new TextDecoder().decode(concatenated);
let type;
if (contentType === 'application/json') {
try {
data = JSON.parse(data);
}
catch (ignored) {
// ignore
}
type = 'json';
}
else if (contentType === 'multipart/form-data') {
type = 'formData';
}
else if (contentType === null || contentType === void 0 ? void 0 : contentType.startsWith('image')) {
type = 'image';
}
else if (contentType === 'application/octet-stream') {
type = 'binary';
}
else {
type = 'text';
}
return {
data,
type,
headers: { 'Content-Type': contentType || 'application/octet-stream' },
};
}
else if (body instanceof FormData) {
const formData = await convertFormData(body);
const boundary = `${Date.now()}`;
return {
Expand Down Expand Up @@ -432,8 +476,8 @@ var nativeBridge = (function (exports) {
console.time(tag);
try {
const { body, method } = request;
const { data: requestData, type, headers, } = await convertBody(body || undefined);
const optionHeaders = Object.fromEntries(request.headers.entries());
const { data: requestData, type, headers, } = await convertBody((options === null || options === void 0 ? void 0 : options.body) || body || undefined, optionHeaders['Content-Type'] || optionHeaders['content-type']);
const nativeResponse = await cap.nativePromise('CapacitorHttp', 'request', {
url: request.url,
method: method,
Expand Down
51 changes: 48 additions & 3 deletions core/native-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,50 @@ const convertFormData = async (formData: FormData): Promise<any> => {

const convertBody = async (
body: Document | XMLHttpRequestBodyInit | ReadableStream<any> | undefined,
contentType?: string,
): Promise<any> => {
if (body instanceof FormData) {
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;
}

let data = new TextDecoder().decode(concatenated);
let type;
if (contentType === 'application/json') {
try {
data = JSON.parse(data);
} catch (ignored) {
// ignore
}
type = 'json';
} else if (contentType === 'multipart/form-data') {
type = 'formData';
} else if (contentType?.startsWith('image')) {
type = 'image';
} else if (contentType === 'application/octet-stream') {
type = 'binary';
} else {
type = 'text';
}

return {
data,
type,
headers: { 'Content-Type': contentType || 'application/octet-stream' },
};
} else if (body instanceof FormData) {
const formData = await convertFormData(body);
const boundary = `${Date.now()}`;
return {
Expand Down Expand Up @@ -482,13 +524,16 @@ const initBridge = (w: any): void => {

try {
const { body, method } = request;
const optionHeaders = Object.fromEntries(request.headers.entries());
const {
data: requestData,
type,
headers,
} = await convertBody(body || undefined);
} = await convertBody(
options?.body || body || undefined,
optionHeaders['Content-Type'] || optionHeaders['content-type'],
);

const optionHeaders = Object.fromEntries(request.headers.entries());
const nativeResponse: HttpResponse = await cap.nativePromise(
'CapacitorHttp',
'request',
Expand Down
50 changes: 47 additions & 3 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,52 @@ var nativeBridge = (function (exports) {
}
return newFormData;
};
const convertBody = async (body) => {
if (body instanceof FormData) {
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);
}
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;
}
let data = new TextDecoder().decode(concatenated);
let type;
if (contentType === 'application/json') {
try {
data = JSON.parse(data);
}
catch (ignored) {
// ignore
}
type = 'json';
}
else if (contentType === 'multipart/form-data') {
type = 'formData';
}
else if (contentType === null || contentType === void 0 ? void 0 : contentType.startsWith('image')) {
type = 'image';
}
else if (contentType === 'application/octet-stream') {
type = 'binary';
}
else {
type = 'text';
}
return {
data,
type,
headers: { 'Content-Type': contentType || 'application/octet-stream' },
};
}
else if (body instanceof FormData) {
const formData = await convertFormData(body);
const boundary = `${Date.now()}`;
return {
Expand Down Expand Up @@ -432,8 +476,8 @@ var nativeBridge = (function (exports) {
console.time(tag);
try {
const { body, method } = request;
const { data: requestData, type, headers, } = await convertBody(body || undefined);
const optionHeaders = Object.fromEntries(request.headers.entries());
const { data: requestData, type, headers, } = await convertBody((options === null || options === void 0 ? void 0 : options.body) || body || undefined, optionHeaders['Content-Type'] || optionHeaders['content-type']);
const nativeResponse = await cap.nativePromise('CapacitorHttp', 'request', {
url: request.url,
method: method,
Expand Down