Skip to content

Commit

Permalink
fix(fetch): avoid sending body for null body reponses (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Aug 8, 2023
1 parent f4cba4e commit 815b65b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/runtime/fetch/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface CallContext {
body?: any;
}

const nullBodyResponses = new Set([101, 204, 205, 304]);

export function createCall(handle: Handle) {
return function callHandle(context: CallContext) {
const req = new IncomingMessage();
Expand Down Expand Up @@ -51,10 +53,20 @@ export function createCall(handle: Handle) {
req.__unenv__ = context.context;

return handle(req, res).then(() => {
// https://developer.mozilla.org/en-US/docs/Web/API/Response/body
// TODO: Ensure _data is either of BodyInit (or narrower) types
// Blob | ArrayBUffer | TypedArray | DataView | FormData | ReadableStream | URLSearchParams | String
// Blob | ArrayBuffer | TypedArray | DataView | FormData | ReadableStream | URLSearchParams | String
let body = res._data as BodyInit | null;
if (
nullBodyResponses.has(res.statusCode) ||
req.method.toUpperCase() === "HEAD"
) {
body = null;
delete res._headers["content-length"];
}

const r = {
body: (res._data as BodyInit) || "",
body,
headers: res._headers,
status: res.statusCode,
statusText: res.statusMessage,
Expand Down

0 comments on commit 815b65b

Please sign in to comment.