From 2ac34a4ce75a1fc9c728faf7d3288e24e76df427 Mon Sep 17 00:00:00 2001 From: jdude700 Date: Fri, 13 Sep 2024 17:53:36 +1000 Subject: [PATCH] fix http response on null body erroring --- apps/desktop/src/http.ts | 103 ++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 49 deletions(-) diff --git a/apps/desktop/src/http.ts b/apps/desktop/src/http.ts index 5fd6a523..e800c059 100644 --- a/apps/desktop/src/http.ts +++ b/apps/desktop/src/http.ts @@ -1,57 +1,62 @@ import * as commands from "./commands"; export interface ClientOptions { - /** - * Defines the maximum number of redirects the client should follow. - * If set to 0, no redirects will be followed. - */ - maxRedirections?: number; - /** Timeout in milliseconds */ - connectTimeout?: number; + /** + * Defines the maximum number of redirects the client should follow. + * If set to 0, no redirects will be followed. + */ + maxRedirections?: number; + /** Timeout in milliseconds */ + connectTimeout?: number; } export async function fetch( - input: URL | Request | string, - init?: RequestInit & ClientOptions, + input: URL | Request | string, + init?: RequestInit & ClientOptions, ): Promise { - const maxRedirections = init?.maxRedirections; - const connectTimeout = init?.maxRedirections; - - // Remove these fields before creating the request - if (init) { - init.maxRedirections = undefined; - init.connectTimeout = undefined; - } - - const req = new Request(input, init); - const buffer = await req.arrayBuffer(); - const reqData = buffer.byteLength ? Array.from(new Uint8Array(buffer)) : null; - - const rid = await commands.fetch( - req.method, - req.url, - Array.from(req.headers.entries()), - reqData, - maxRedirections ?? null, - connectTimeout ?? null, - ); - - req.signal.addEventListener("abort", () => { - commands.fetchCancel(rid); - }); - - const { status, statusText, url, headers } = await commands.fetchSend(rid); - - const body = await commands.fetchReadBody(rid); - - const res = new Response(new Uint8Array(body), { - headers, - status, - statusText, - }); - - // url is read only but seems like we can do this - Object.defineProperty(res, "url", { value: url }); - - return res; + const maxRedirections = init?.maxRedirections; + const connectTimeout = init?.maxRedirections; + + // Remove these fields before creating the request + if (init) { + init.maxRedirections = undefined; + init.connectTimeout = undefined; + } + + const req = new Request(input, init); + const buffer = await req.arrayBuffer(); + const reqData = buffer.byteLength ? Array.from(new Uint8Array(buffer)) : null; + + const rid = await commands.fetch( + req.method, + req.url, + Array.from(req.headers.entries()), + reqData, + maxRedirections ?? null, + connectTimeout ?? null, + ); + + req.signal.addEventListener("abort", () => { + commands.fetchCancel(rid); + }); + + const { status, statusText, url, headers } = await commands.fetchSend(rid); + + const body = await commands.fetchReadBody(rid); + + const res = + body.length === 0 + ? new Response(new Uint8Array(body)) + : new Response(new Uint8Array(body), { + headers, + status, + statusText, + }); + + console.log(res); + + // url is read only but seems like we can do this + Object.defineProperty(res, "url", { value: url }); + + return res; }