Skip to content

fix(parse): support legacy fetch headers - #93

Open
awais-ur-rehman wants to merge 1 commit into
nfriedly:masterfrom
awais-ur-rehman:feat/legacy-fetch-headers
Open

awais-ur-rehman wants to merge 1 commit into
nfriedly:masterfrom
awais-ur-rehman:feat/legacy-fetch-headers

Conversation

@awais-ur-rehman

Copy link
Copy Markdown

Summary

A fetch Response whose Headers supports get() but lacks getSetCookie() currently produces an empty cookie list. Read its combined Set-Cookie value through get('set-cookie') and pass it through the existing splitter; keep getSetCookie() preferred when available. Update the input declaration to accept these Headers implementations.

Breaking Changes

None.

Testing

  • Added regressions using real Response/Headers objects with only the newer getSetCookie capability disabled, covering combined cookies with Expires commas, map/decoding options, and absent headers.
  • Confirmed two runtime failures and one type overload failure before the fix.
  • npm test passes build, lint, 75 Mocha tests, and tsd typecheck on Node24/macOS; git diff --check passes.
  • Other platform/runtime combinations were not exercised locally.

Manual check: pass a Response-like object with headers.get("set-cookie") returning foo=bar; parse() now returns that cookie.

AI assistance

OpenAI Codex assisted with investigation, implementation, regression tests, review, and validation.

@nfriedly

nfriedly commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Hi, do you have a real-world example of where this fails, or is this more of a theoretical fix?

I ask because I'm fairly certain I remember this library working with fetch before the advent of getSetCookie() - I think it took the input.headers['set-cookie'] path then. (This would have been tested on react native and node.js; browser implementations have never allowed access to the set-cookie header.) Is there a variant of fetch where that doesn't work?

Comment thread lib/set-cookie.js
Comment on lines +158 to 162
} else if (typeof input.headers.get === "function") {
input = input.headers.get("set-cookie");
} else if (input.headers["set-cookie"]) {
// fast-path for node.js (which automatically normalizes header names to lower-case)
input = input.headers["set-cookie"];

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do add get() support, it should probably come after the node.js fast-path.

(getSetCookie() comes first because of some environments (mostly React Native) where it provides a pre-split array but input.headers["set-cookie"] provides a combined string that takes extra work to process.)

@awais-ur-rehman

Copy link
Copy Markdown
Author

Found by code reading, not from a production report. I reproduced it with an unmodified node-fetch@2.7.0 Response:

const { Response } = require('node-fetch');
const response = new Response(null, {
  headers: { 'set-cookie': 'foo=bar' }
});

Here response.headers.getSetCookie and response.headers['set-cookie'] are both undefined, while response.headers.get('set-cookie') returns foo=bar. On the base commit, parse(response) returns []; with this patch it returns the cookie.

I haven't verified React Native. Browser restrictions still apply; this fallback only reads a header the fetch implementation already exposes.

@nfriedly

nfriedly commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Found by code reading, not from a production report. I reproduced it with an unmodified node-fetch@2.7.0 Response:

const { Response } = require('node-fetch');
const response = new Response(null, {
  headers: { 'set-cookie': 'foo=bar' }
});

Here response.headers.getSetCookie and response.headers['set-cookie'] are both undefined, while response.headers.get('set-cookie') returns foo=bar. On the base commit, parse(response) returns []; with this patch it returns the cookie.

Ok, let's add a unit test like that (add node-fetch as a dev dependency), and also make the other change I suggested where .get('set-cookie') comes after .headers['set-cookie'], and then I think we can go ahead and release this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants