Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 5 additions & 4 deletions src/bun.js/bindings/CookieMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,17 @@ JSC::JSValue CookieMap::toJSON(JSC::JSGlobalObject* globalObject) const
// Add modified cookies to the object
for (const auto& cookie : m_modifiedCookies) {
if (!cookie->value().isEmpty()) {
object->putDirect(vm, JSC::Identifier::fromString(vm, cookie->name()), JSC::jsString(vm, cookie->value()));
object->putDirectMayBeIndex(globalObject, JSC::Identifier::fromString(vm, cookie->name()), JSC::jsString(vm, cookie->value()));
RETURN_IF_EXCEPTION(scope, {});
}
}

// Add original cookies to the object
for (const auto& cookie : m_originalCookies) {
// Skip if this cookie name was already added from modified cookies
if (!object->hasProperty(globalObject, JSC::Identifier::fromString(vm, cookie.key))) {
object->putDirect(vm, JSC::Identifier::fromString(vm, cookie.key), JSC::jsString(vm, cookie.value));
// Skip if this cookie name was already added from modified cookies (own-property check only)
auto ident = JSC::Identifier::fromString(vm, cookie.key);
if (object->getDirectOffset(vm, ident) == JSC::invalidOffset) {
object->putDirectMayBeIndex(globalObject, ident, JSC::jsString(vm, cookie.value));
RETURN_IF_EXCEPTION(scope, {});
}
Comment thread
claude[bot] marked this conversation as resolved.
Outdated
}
Expand Down
18 changes: 18 additions & 0 deletions test/js/bun/cookie/cookie-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,24 @@ describe("Bun.Cookie and Bun.CookieMap", () => {
`);
});

test("CookieMap.toJSON() handles numeric cookie names", () => {
const map = new Bun.CookieMap("0=first; 1=second; 42=answer");
expect(map.toJSON()).toEqual({
"0": "first",
"1": "second",
"42": "answer",
});
});

test("CookieMap.toJSON() handles cookie names matching Object.prototype properties", () => {
const map = new Bun.CookieMap("toString=hello; constructor=world; valueOf=test");
expect(map.toJSON()).toEqual({
"toString": "hello",
"constructor": "world",
"valueOf": "test",
});
});

test("CookieMap works with cookies with advanced attributes", () => {
const map = new Bun.CookieMap();

Expand Down
Loading