-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix http response on null body erroring
- Loading branch information
Showing
1 changed file
with
54 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |