Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/set-cookie.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface Options {
type ResponseLike = {
headers:
| { getSetCookie(): string[] }
| { get(name: string): string | null }
| { "set-cookie"?: string | readonly string[] }
| Record<string, string | string[] | undefined>;
};
Expand Down
2 changes: 2 additions & 0 deletions lib/set-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ function parseSetCookie(input, options) {
// for fetch responses - they combine headers of the same type in the headers array,
// but getSetCookie returns an uncombined array
input = input.headers.getSetCookie();
} 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"];
Comment on lines +158 to 162

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.)

Expand Down
5 changes: 5 additions & 0 deletions test-d/set-cookie.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,8 @@ setCookieParser.parseSetCookie("foo=bar");
setCookieParser.parse("foo=bar");
setCookieParser.parseString("foo=bar");
setCookieParser.splitCookiesString("foo=bar");

// Headers implementations that predate getSetCookie expose only get().
parseSetCookie({
headers: { get: (_name: string): string | null => "foo=bar" },
});
41 changes: 41 additions & 0 deletions test/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,45 @@ describe("fetch", () => {
var expected = [{ name: "foo", value: "bar" }];
assert.deepEqual(actual, expected);
});

describe("Headers without getSetCookie", () => {
function legacyResponse(value) {
const response = new Response(null, {
headers: value === null ? {} : { "set-cookie": value },
});
// Older fetch implementations expose get(), but not getSetCookie().
Object.defineProperty(response.headers, "getSetCookie", {
value: undefined,
});
return response;
}

it("should parse a combined header without splitting the Expires date", () => {
const response = legacyResponse(
"foo=bar; Expires=Tue, 01 Jul 2025 10:01:11 GMT, baz=qux; Secure"
);
assert.deepEqual(setCookie.parse(response), [
{
name: "foo",
value: "bar",
expires: new Date("2025-07-01T10:01:11Z"),
},
{ name: "baz", value: "qux", secure: true },
]);
});

it("should honor map and decoding options", () => {
const response = legacyResponse("foo=hello%20world");
assert.deepEqual(
setCookie.parse(response, { map: true, decodeValues: false }),
{
foo: { name: "foo", value: "hello%20world" },
}
);
});

it("should return an empty array when the header is absent", () => {
assert.deepEqual(setCookie.parse(legacyResponse(null)), []);
});
});
});
Loading