Skip to content

Commit

Permalink
fix: clear cache (nodejs#2519)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx authored and crysmags committed Feb 27, 2024
1 parent 5b61b9a commit 575460b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 132 deletions.
103 changes: 21 additions & 82 deletions lib/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function appendHeader (headers, name, value) {
// forbidden response-header name, return.

// 7. Append (name, value) to headers’s header list.
return headers[kHeadersList].append(name, value)
return headers[kHeadersList].append(name, value, false)

// 8. If headers’s guard is "request-no-cors", then remove
// privileged no-CORS request headers from headers
Expand All @@ -138,20 +138,14 @@ class HeadersList {
/**
* @see https://fetch.spec.whatwg.org/#header-list-contains
* @param {string} name
* @param {boolean} isLowerCase
*/
contains (name) {
contains (name, isLowerCase) {
// A header list list contains a header name name if list
// contains a header whose name is a byte-case-insensitive
// match for name.

return this[kHeadersMap].has(name.toLowerCase())
}

/**
* @param {string} name
*/
lowerCaseContains (name) {
return this[kHeadersMap].has(name)
return this[kHeadersMap].has(isLowerCase ? name : name.toLowerCase())
}

clear () {
Expand All @@ -164,13 +158,14 @@ class HeadersList {
* @see https://fetch.spec.whatwg.org/#concept-header-list-append
* @param {string} name
* @param {string} value
* @param {boolean} isLowerCase
*/
append (name, value) {
append (name, value, isLowerCase) {
this[kHeadersSortedMap] = null

// 1. If list contains name, then set name to the first such
// header’s name.
const lowercaseName = name.toLowerCase()
const lowercaseName = isLowerCase ? name : name.toLowerCase()
const exists = this[kHeadersMap].get(lowercaseName)

// 2. Append (name, value) to list.
Expand All @@ -189,41 +184,15 @@ class HeadersList {
}
}

/**
* @param {string} name
* @param {string} value
*/
lowerCaseAppend (name, value) {
this[kHeadersSortedMap] = null

// 1. If list contains name, then set name to the first such
// header’s name.
const exists = this[kHeadersMap].get(name)

// 2. Append (name, value) to list.
if (exists) {
const delimiter = name === 'cookie' ? '; ' : ', '
this[kHeadersMap].set(name, {
name: exists.name,
value: `${exists.value}${delimiter}${value}`
})
} else {
this[kHeadersMap].set(name, { name, value })
}

if (name === 'set-cookie') {
(this.cookies ??= []).push(value)
}
}

/**
* @see https://fetch.spec.whatwg.org/#concept-header-list-set
* @param {string} name
* @param {string} value
* @param {boolean} isLowerCase
*/
set (name, value) {
set (name, value, isLowerCase) {
this[kHeadersSortedMap] = null
const lowercaseName = name.toLowerCase()
const lowercaseName = isLowerCase ? name : name.toLowerCase()

if (lowercaseName === 'set-cookie') {
this.cookies = [value]
Expand All @@ -236,35 +205,14 @@ class HeadersList {
this[kHeadersMap].set(lowercaseName, { name, value })
}

/**
* @param {string} name
* @param {string} value
*/
lowerCaseSet (name, value) {
if (name === 'set-cookie') {
this.cookies = [value]
}

// 1. If list contains name, then set the value of
// the first such header to value and remove the
// others.
// 2. Otherwise, append header (name, value) to list.
this[kHeadersMap].set(name, { name, value })
}

/**
* @see https://fetch.spec.whatwg.org/#concept-header-list-delete
* @param {string} name
* @param {boolean} isLowerCase
*/
delete (name) {
return this.lowerCaseDelete(name.toLowerCase())
}

/**
* @param {string} name
*/
lowerCaseDelete (name) {
delete (name, isLowerCase) {
this[kHeadersSortedMap] = null
if (!isLowerCase) name = name.toLowerCase()

if (name === 'set-cookie') {
this.cookies = null
Expand All @@ -276,24 +224,15 @@ class HeadersList {
/**
* @see https://fetch.spec.whatwg.org/#concept-header-list-get
* @param {string} name
* @param {boolean} isLowerCase
* @returns {string | null}
*/
get (name) {
const value = this[kHeadersMap].get(name.toLowerCase())

get (name, isLowerCase) {
// 1. If list does not contain name, then return null.
// 2. Return the values of all headers in list whose name
// is a byte-case-insensitive match for name,
// separated from each other by 0x2C 0x20, in order.
return value === undefined ? null : value.value
}

/**
* @param {string} name
* @returns {string | null}
*/
lowerCaseGet (name) {
return this[kHeadersMap].get(name)?.value ?? null
return this[kHeadersMap].get(isLowerCase ? name : name.toLowerCase())?.value ?? null
}

* [Symbol.iterator] () {
Expand Down Expand Up @@ -383,14 +322,14 @@ class Headers {

// 6. If this’s header list does not contain name, then
// return.
if (!this[kHeadersList].contains(name)) {
if (!this[kHeadersList].contains(name, false)) {
return
}

// 7. Delete name from this’s header list.
// 8. If this’s guard is "request-no-cors", then remove
// privileged no-CORS request headers from this.
this[kHeadersList].delete(name)
this[kHeadersList].delete(name, false)
}

// https://fetch.spec.whatwg.org/#dom-headers-get
Expand All @@ -412,7 +351,7 @@ class Headers {

// 2. Return the result of getting name from this’s header
// list.
return this[kHeadersList].get(name)
return this[kHeadersList].get(name, false)
}

// https://fetch.spec.whatwg.org/#dom-headers-has
Expand All @@ -434,7 +373,7 @@ class Headers {

// 2. Return true if this’s header list contains name;
// otherwise false.
return this[kHeadersList].contains(name)
return this[kHeadersList].contains(name, false)
}

// https://fetch.spec.whatwg.org/#dom-headers-set
Expand Down Expand Up @@ -483,7 +422,7 @@ class Headers {
// 7. Set (name, value) in this’s header list.
// 8. If this’s guard is "request-no-cors", then remove
// privileged no-CORS request headers from this
this[kHeadersList].set(name, value)
this[kHeadersList].set(name, value, false)
}

// https://fetch.spec.whatwg.org/#dom-headers-getsetcookie
Expand Down
Loading

0 comments on commit 575460b

Please sign in to comment.