Skip to content

Commit

Permalink
fix http response on null body erroring
Browse files Browse the repository at this point in the history
  • Loading branch information
jdudetv committed Sep 13, 2024
1 parent 5d018e1 commit 2ac34a4
Showing 1 changed file with 54 additions and 49 deletions.
103 changes: 54 additions & 49 deletions apps/desktop/src/http.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
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;
}

0 comments on commit 2ac34a4

Please sign in to comment.