From bfb468d97a444e24dd82bb26784c7e04871c9567 Mon Sep 17 00:00:00 2001 From: adrieljss Date: Wed, 12 Mar 2025 09:55:36 +0800 Subject: [PATCH] fix: use responseRid in invoke and build js --- plugins/http/guest-js/index.ts | 61 +++++++++++++++++----------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/plugins/http/guest-js/index.ts b/plugins/http/guest-js/index.ts index 770cbc5f..623781a4 100644 --- a/plugins/http/guest-js/index.ts +++ b/plugins/http/guest-js/index.ts @@ -199,6 +199,36 @@ export async function fetch( } }) + const abort = () => invoke('plugin:http|fetch_cancel', { rid }) + + // abort early here if needed + if (signal?.aborted) { + // we don't care about the result of this proimse + // eslint-disable-next-line @typescript-eslint/no-floating-promises + abort() + throw new Error(ERROR_REQUEST_CANCELLED) + } + + signal?.addEventListener('abort', () => void abort()) + + interface FetchSendResponse { + status: number + statusText: string + headers: [[string, string]] + url: string + rid: number + } + + const { + status, + statusText, + url, + headers: responseHeaders, + rid: responseRid + } = await invoke('plugin:http|fetch_send', { + rid + }) + const readableStreamBody = new ReadableStream({ start: (controller) => { const streamChannel = new Channel() @@ -226,7 +256,7 @@ export async function fetch( // run a non-blocking body stream fetch invoke('plugin:http|fetch_read_body', { - rid, + rid: responseRid, streamChannel }).catch((e) => { controller.error(e) @@ -234,35 +264,6 @@ export async function fetch( } }) - const abort = () => invoke('plugin:http|fetch_cancel', { rid }) - - // abort early here if needed - if (signal?.aborted) { - // we don't care about the result of this proimse - // eslint-disable-next-line @typescript-eslint/no-floating-promises - abort() - throw new Error(ERROR_REQUEST_CANCELLED) - } - - signal?.addEventListener('abort', () => void abort()) - - interface FetchSendResponse { - status: number - statusText: string - headers: [[string, string]] - url: string - rid: number - } - - const { - status, - statusText, - url, - headers: responseHeaders - } = await invoke('plugin:http|fetch_send', { - rid - }) - const res = new Response(readableStreamBody, { status, statusText