Mirror: A Node.js fetch shim using built-in Request, Response, and Headers (but without native fetch)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: Drop `setHeaders` call for consistent `setHeader` calls (#35)

authored by

Phil Pluckthun and committed by
GitHub
7f19790d f88e6eb9

+20 -4
+5
.changeset/loud-ideas-type.md
··· 1 + --- 2 + 'fetch-nodeshim': patch 3 + --- 4 + 5 + Avoid `setHeaders` to increase consistency, fix `Set-Cookie` case for older Node versions, and work around bug in Bun <=1.3.9
+15 -4
src/fetch.ts
··· 33 33 outgoing: http.OutgoingMessage, 34 34 headers: Headers 35 35 ) => { 36 - if (typeof outgoing.setHeaders === 'function') { 37 - outgoing.setHeaders(headers); 38 - } else { 39 - for (const [key, value] of headers) outgoing.setHeader(key, value); 36 + // Preassemble array headers, mostly only for Set-Cookie 37 + // We're avoiding `getSetCookie` since support is unclear in Node 18 38 + const collection: Record<string, string | string[]> = {}; 39 + for (const [key, value] of headers) { 40 + if (Array.isArray(collection[key])) { 41 + collection[key].push(value); 42 + } else if (collection[key] != undefined) { 43 + collection[key] = [collection[key], value]; 44 + } else { 45 + collection[key] = value; 46 + } 47 + } 48 + // We don't use `setHeaders` due to a Bun bug (Fix: https://github.com/oven-sh/bun/pull/27050) 49 + for (const key in collection) { 50 + outgoing.setHeader(key, collection[key]); 40 51 } 41 52 }; 42 53