From 815b65b22272ab49bd8292f8f576184b208cb2a6 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Tue, 8 Aug 2023 10:29:44 +0200 Subject: [PATCH] fix(fetch): avoid sending body for null body reponses (#124) --- src/runtime/fetch/call.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/runtime/fetch/call.ts b/src/runtime/fetch/call.ts index bd03807b..199bdded 100644 --- a/src/runtime/fetch/call.ts +++ b/src/runtime/fetch/call.ts @@ -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(); @@ -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,