Fix OpenScript.fetch JSON IPC serialization for text and binary

This commit is contained in:
2026-09-13 14:03:29 -07:00
parent da135d506a
commit a0f8160302
7 changed files with 102 additions and 13 deletions

View File

@@ -42,7 +42,17 @@ ${code}
type: '${FETCH_MESSAGE}', url: url.toString(), options: { ...rest, headers, body },
});
if (!response?.ok) throw new TypeError(response?.error || 'OpenScript fetch failed');
const resBody = [101, 204, 205, 304].includes(response.status) ? null : response.body;
let resBody = null;
if (![101, 204, 205, 304].includes(response.status)) {
if (response.base64 !== undefined) {
const bin = atob(response.base64);
const bytes = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
resBody = bytes.buffer;
} else {
resBody = response.body ?? null;
}
}
const res = new Response(resBody, {
status: response.status, statusText: response.statusText, headers: response.headers,
});