Skip to content

Commit

Permalink
don't use internal header state for cookies (#3295)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored May 22, 2024
1 parent eed423a commit a4879b4
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 42 deletions.
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

0 comments on commit a4879b4

Please sign in to comment.