Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't use internal header state for cookies #3295

Merged
merged 2 commits into from
May 22, 2024
Merged
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
7 changes: 3 additions & 4 deletions lib/web/cookies/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { parseSetCookie } = require('./parse')
const { stringify, getHeadersList } = require('./util')
const { stringify } = require('./util')
const { webidl } = require('../fetch/webidl')
const { Headers } = require('../fetch/headers')

Expand Down Expand Up @@ -78,14 +78,13 @@ function getSetCookies (headers) {

webidl.brandCheck(headers, Headers, { strict: false })

const cookies = getHeadersList(headers).cookies
const cookies = headers.getSetCookie()

if (!cookies) {
return []
}

// In older versions of undici, cookies is a list of name:value.
return cookies.map((pair) => parseSetCookie(Array.isArray(pair) ? pair[1] : pair))
return cookies.map((pair) => parseSetCookie(pair))
}

/**
Expand Down
29 changes: 1 addition & 28 deletions lib/web/cookies/util.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict'

const assert = require('node:assert')
const { getHeadersList: internalGetHeadersList } = require('../fetch/headers')

/**
* @param {string} value
* @returns {boolean}
Expand Down Expand Up @@ -275,35 +272,11 @@ function stringify (cookie) {
return out.join('; ')
}

let kHeadersListNode

function getHeadersList (headers) {
try {
return internalGetHeadersList(headers)
} catch {
// fall-through
}

if (!kHeadersListNode) {
kHeadersListNode = Object.getOwnPropertySymbols(headers).find(
(symbol) => symbol.description === 'headers list'
)

assert(kHeadersListNode, 'Headers cannot be parsed')
}

const headersList = headers[kHeadersListNode]
assert(headersList)

return headersList
}

module.exports = {
isCTLExcludingHtab,
validateCookieName,
validateCookiePath,
validateCookieValue,
toIMFDate,
stringify,
getHeadersList
stringify
}
8 changes: 0 additions & 8 deletions lib/web/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,14 +641,6 @@ Object.defineProperties(Headers.prototype, {
},
[util.inspect.custom]: {
enumerable: false
},
// Compatibility for global headers
[Symbol('headers list')]: {
configurable: false,
enumerable: false,
get: function () {
return getHeadersList(this)
}
}
})

Expand Down
3 changes: 1 addition & 2 deletions test/cookie/global-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const {
getSetCookies,
setCookie
} = require('../..')
const { getHeadersList } = require('../../lib/web/cookies/util')

describe('Using global Headers', async () => {
test('deleteCookies', () => {
Expand All @@ -32,7 +31,7 @@ describe('Using global Headers', async () => {
'set-cookie': 'undici=getSetCookies; Secure'
})

const supportsCookies = getHeadersList(headers).cookies
const supportsCookies = headers.getSetCookie()

if (!supportsCookies) {
assert.deepEqual(getSetCookies(headers), [])
Expand Down
Loading