From 57eb09b35074af384b6ed0997b6b026d808b61d7 Mon Sep 17 00:00:00 2001 From: adrieljss Date: Mon, 3 Mar 2025 20:38:20 +0800 Subject: [PATCH] fix: content conversion bug --- plugins/http/guest-js/index.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/plugins/http/guest-js/index.ts b/plugins/http/guest-js/index.ts index 760f0648..d0e14d42 100644 --- a/plugins/http/guest-js/index.ts +++ b/plugins/http/guest-js/index.ts @@ -186,23 +186,31 @@ export async function fetch( throw new Error(ERROR_REQUEST_CANCELLED) } - const streamChannel = new Channel() + const streamChannel = new Channel() const readableStreamBody = new ReadableStream({ start: (controller) => { - streamChannel.onmessage = (res: Uint8Array) => { + streamChannel.onmessage = (res: ArrayBuffer | number[]) => { // close early if aborted if (signal?.aborted) { controller.error(ERROR_REQUEST_CANCELLED) + controller.close() return } - if (!res.length) { + // close when the signal to close (an empty chunk) + // is sent from the IPC. + if (res instanceof ArrayBuffer + ? res.byteLength == 0 + : res.length == 0) { controller.close() return } - - controller.enqueue(res) + + // the content conversion (like .text(), .json(), etc.) in Response + // must have Uint8Array as its content, else it will + // have untraceable error that's hard to debug. + controller.enqueue(new Uint8Array(res)) } } })