From 1b0e2873ccaef5d6d1d104b72a5fbbe21e59b1a9 Mon Sep 17 00:00:00 2001 From: Mohamed Gad <59421219+mjad218@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:23:01 +0300 Subject: [PATCH 1/4] fix: TypeError: headers is not iterable At runtime headers may not be an `instanceof` Headers. This causes a lot of bugs. the function may get a js object as an argument. Instead of looping directly through `headers`, I get the entries then loop through them. --- src/utils.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 8117fbb..1120dec 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -41,11 +41,13 @@ export function writeFromReadableStream(stream: ReadableStream, writ } } -export const buildOutgoingHttpHeaders = (headers: Headers): OutgoingHttpHeaders => { +export const buildOutgoingHttpHeaders = (headers: Headers | Record): OutgoingHttpHeaders => { const res: OutgoingHttpHeaders = {} const cookies = [] - for (const [k, v] of headers) { + const entries = headers instanceof Headers ? headers.entries : Object.entries(headers); + + for (const [k, v] of entries) { if (k === 'set-cookie') { cookies.push(v) } else { From 4303748577ddd7bd5fd62a7c617cc995f18a3894 Mon Sep 17 00:00:00 2001 From: Gad Date: Thu, 31 Oct 2024 10:31:22 +0300 Subject: [PATCH 2/4] fix: add missing function call --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 1120dec..38e77ec 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -45,7 +45,7 @@ export const buildOutgoingHttpHeaders = (headers: Headers | Record Date: Thu, 31 Oct 2024 10:57:01 +0300 Subject: [PATCH 3/4] fix: format document utils.ts --- src/utils.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 38e77ec..ef8f263 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -41,12 +41,14 @@ export function writeFromReadableStream(stream: ReadableStream, writ } } -export const buildOutgoingHttpHeaders = (headers: Headers | Record): OutgoingHttpHeaders => { +export const buildOutgoingHttpHeaders = ( + headers: Headers | Record +): OutgoingHttpHeaders => { const res: OutgoingHttpHeaders = {} const cookies = [] - const entries = headers instanceof Headers ? headers.entries() : Object.entries(headers); - + const entries = headers instanceof Headers ? headers.entries() : Object.entries(headers) + for (const [k, v] of entries) { if (k === 'set-cookie') { cookies.push(v) From 6a6be4f25fac02129a356b2436bcfc6b4d656c7e Mon Sep 17 00:00:00 2001 From: Gad Date: Thu, 31 Oct 2024 11:01:10 +0300 Subject: [PATCH 4/4] fix: remove undefined values from headers --- src/utils.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index ef8f263..d73e6fb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -47,7 +47,10 @@ export const buildOutgoingHttpHeaders = ( const res: OutgoingHttpHeaders = {} const cookies = [] - const entries = headers instanceof Headers ? headers.entries() : Object.entries(headers) + const entries = + headers instanceof Headers + ? headers.entries() + : Object.entries(headers).filter(([, value]) => value) for (const [k, v] of entries) { if (k === 'set-cookie') { @@ -56,6 +59,7 @@ export const buildOutgoingHttpHeaders = ( res[k] = v } } + if (cookies.length > 0) { res['set-cookie'] = cookies }