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)) } } })