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

fetch: replace Proxy objects #1299

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions lib/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

'use strict'

const assert = require('assert')
const { validateHeaderName, validateHeaderValue } = require('http')
const { kHeadersList } = require('../core/symbols')
const { kGuard } = require('./symbols')
Expand Down Expand Up @@ -149,6 +150,60 @@ class HeadersList extends Array {
}
}

class FilteredHeadersList extends HeadersList {
/** @type {(name: string) => boolean} */
filter = () => true;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed babel-eslint for standard because it couldn't parse this properly. There's an issue on github with this solution I can pull up if you need.


/**
* @param {HeadersList} headersList
* @param {typeof this['filter'] | undefined} filter
*/
constructor (headersList, filter) {
super()
this.push(...headersList) // copy values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can’t copy.


if (filter) {
this.filter = filter
}
}

/**
* @param {never[]} args
*/
slice (...args) {
assert(args.length === 0)
const arr = []
for (let index = 0; index < this.length; index += 2) {
if (this.filter(this[index])) {
arr.push(this[index], this[index + 1])
}
}
return arr
}

/**
* @param {string} name
*/
get (name) {
if (this.filter(name)) {
return super.get(name)
} else {
return undefined
}
}

/**
* @param {string} name
*/
has (name) {
if (this.filter(name)) {
return super.has(name)
} else {
return undefined
}
}
}

class Headers {
constructor (...args) {
if (
Expand Down Expand Up @@ -385,6 +440,7 @@ module.exports = {
Headers,
HeadersList,
binarySearch,
FilteredHeadersList,
normalizeAndValidateHeaderName,
normalizeAndValidateHeaderValue
}
61 changes: 18 additions & 43 deletions lib/fetch/response.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { Headers, HeadersList, fill } = require('./headers')
const { Headers, HeadersList, fill, FilteredHeadersList } = require('./headers')
const { AbortError } = require('../core/errors')
const { extractBody, cloneBody, mixinBody } = require('./body')
const util = require('../core/util')
Expand Down Expand Up @@ -371,45 +371,14 @@ function makeNetworkError (reason) {
}

function makeFilteredResponse (response, state) {
state = {
internalResponse: response,
...state
}

return new Proxy(response, {
get (target, p) {
return p in state ? state[p] : target[p]
},
set (target, p, value) {
assert(!(p in state))
target[p] = value
return true
}
})
}

function makeFilteredHeadersList (headersList, filter) {
return new Proxy(headersList, {
get (target, prop) {
// Override methods used by Headers class.
if (prop === 'get' || prop === 'has') {
return (name) => filter(name) ? target[prop](name) : undefined
} else if (prop === 'slice') {
return (...args) => {
assert(args.length === 0)
const arr = []
for (let index = 0; index < target.length; index += 2) {
if (filter(target[index])) {
arr.push(target[index], target[index + 1])
}
}
return arr
}
} else {
return target[prop]
}
}
})
// https://fetch.spec.whatwg.org/#concept-filtered-response
// A filtered response has a corresponding internalResponse, which is its
// response. However, a key accessed in the response object must also be
// available in its internal response (and vice versa).
const cloned = cloneResponse(response)
Object.assign(response, { internalResponse: cloned }, state)

return response
}

// https://fetch.spec.whatwg.org/#concept-filtered-response
Expand All @@ -423,7 +392,10 @@ function filterResponse (response, type) {

return makeFilteredResponse(response, {
type: 'basic',
headersList: makeFilteredHeadersList(response.headersList, (name) => !forbiddenResponseHeaderNames.includes(name))
headersList: new FilteredHeadersList(
response.headersList,
(name) => !forbiddenResponseHeaderNames.includes(name)
)
})
} else if (type === 'cors') {
// A CORS filtered response is a filtered response whose type is "cors"
Expand All @@ -433,7 +405,10 @@ function filterResponse (response, type) {

return makeFilteredResponse(response, {
type: 'cors',
headersList: makeFilteredHeadersList(response.headersList, (name) => !corsSafeListedResponseHeaderNames.includes(name))
headersList: new FilteredHeadersList(
response.headersList,
(name) => !corsSafeListedResponseHeaderNames.includes(name)
)
})
} else if (type === 'opaque') {
// An opaque filtered response is a filtered response whose type is
Expand All @@ -456,7 +431,7 @@ function filterResponse (response, type) {
type: 'opaqueredirect',
status: 0,
statusText: '',
headersList: makeFilteredHeadersList(response.headersList, () => false),
headersList: new FilteredHeadersList([], () => false),
body: null
})
} else {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@sinonjs/fake-timers": "^7.0.5",
"@types/node": "^16.9.1",
"abort-controller": "^3.0.0",
"babel-eslint": "^10.1.0",
"busboy": "^0.3.1",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
Expand Down Expand Up @@ -103,7 +104,8 @@
"ignore": [
"lib/llhttp/constants.js",
"lib/llhttp/utils.js"
]
],
"parser": "babel-eslint"
},
"tsd": {
"directory": "test/types",
Expand Down