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

perf: always use the same prototype Iterator #2743

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add iteratorMixin
  • Loading branch information
tsctx committed Feb 12, 2024
commit 8a2300a1eba67fccda73e5e79c972d7f4943bab2
52 changes: 6 additions & 46 deletions lib/fetch/formdata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { isBlobLike, createIterator } = require('./util')
const { isBlobLike, iteratorMixin } = require('./util')
const { kState } = require('./symbols')
const { kEnumerableProperty } = require('../core/util')
const { File: UndiciFile, FileLike, isFileLike } = require('./file')
Expand All @@ -10,8 +10,6 @@ const { File: NativeFile } = require('node:buffer')
/** @type {globalThis['File']} */
const File = NativeFile ?? UndiciFile

const makeIterator = createIterator('FormData', kState, 'name', 'value')

// https://xhr.spec.whatwg.org/#formdata
class FormData {
constructor (form) {
Expand Down Expand Up @@ -156,47 +154,9 @@ class FormData {
this[kState].push(entry)
}
}

entries () {
webidl.brandCheck(this, FormData)

return makeIterator(this, 'key+value')
}

keys () {
webidl.brandCheck(this, FormData)

return makeIterator(this, 'key')
}

values () {
webidl.brandCheck(this, FormData)

return makeIterator(this, 'value')
}

/**
* @param {(value: string, key: string, self: FormData) => void} callbackFn
* @param {unknown} thisArg
*/
forEach (callbackFn, thisArg = globalThis) {
webidl.brandCheck(this, FormData)

webidl.argumentLengthCheck(arguments, 1, { header: 'FormData.forEach' })

if (typeof callbackFn !== 'function') {
throw new TypeError(
"Failed to execute 'forEach' on 'FormData': parameter 1 is not of type 'Function'."
)
}

for (const [key, value] of makeIterator(this, 'key+value')) {
callbackFn.call(thisArg, value, key, this)
}
}
}

FormData.prototype[Symbol.iterator] = FormData.prototype.entries
iteratorMixin('FormData', FormData, kState, 'name', 'value')

Object.defineProperties(FormData.prototype, {
append: kEnumerableProperty,
Expand All @@ -205,11 +165,11 @@ Object.defineProperties(FormData.prototype, {
getAll: kEnumerableProperty,
has: kEnumerableProperty,
set: kEnumerableProperty,
entries: kEnumerableProperty,
keys: kEnumerableProperty,
values: kEnumerableProperty,
// entries: kEnumerableProperty,
// keys: kEnumerableProperty,
// values: kEnumerableProperty,
forEach: kEnumerableProperty,
[Symbol.iterator]: { enumerable: false },
// [Symbol.iterator]: { enumerable: false },
[Symbol.toStringTag]: {
value: 'FormData',
configurable: true
Expand Down
52 changes: 6 additions & 46 deletions lib/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { kHeadersList, kConstruct } = require('../core/symbols')
const { kGuard } = require('./symbols')
const { kEnumerableProperty } = require('../core/util')
const {
createIterator,
iteratorMixin,
isValidHeaderName,
isValidHeaderValue
} = require('./util')
Expand All @@ -17,8 +17,6 @@ const assert = require('node:assert')
const kHeadersMap = Symbol('headers map')
const kHeadersSortedMap = Symbol('headers map sorted')

const makeIterator = createIterator('Headers', kHeadersSortedMap, 0, 1)

/**
* @param {number} code
*/
Expand Down Expand Up @@ -507,52 +505,14 @@ class Headers {
return headers
}

keys () {
webidl.brandCheck(this, Headers)

return makeIterator(this, 'key')
}

values () {
webidl.brandCheck(this, Headers)

return makeIterator(this, 'value')
}

entries () {
webidl.brandCheck(this, Headers)

return makeIterator(this, 'key+value')
}

/**
* @param {(value: string, key: string, self: Headers) => void} callbackFn
* @param {unknown} thisArg
*/
forEach (callbackFn, thisArg = globalThis) {
webidl.brandCheck(this, Headers)

webidl.argumentLengthCheck(arguments, 1, { header: 'Headers.forEach' })

if (typeof callbackFn !== 'function') {
throw new TypeError(
"Failed to execute 'forEach' on 'Headers': parameter 1 is not of type 'Function'."
)
}

for (const [key, value] of makeIterator(this, 'key+value')) {
callbackFn.call(thisArg, value, key, this)
}
}

[Symbol.for('nodejs.util.inspect.custom')] () {
webidl.brandCheck(this, Headers)

return this[kHeadersList]
}
}

Headers.prototype[Symbol.iterator] = Headers.prototype.entries
iteratorMixin('Headers', Headers, kHeadersSortedMap, 0, 1)

Object.defineProperties(Headers.prototype, {
append: kEnumerableProperty,
Expand All @@ -561,11 +521,11 @@ Object.defineProperties(Headers.prototype, {
has: kEnumerableProperty,
set: kEnumerableProperty,
getSetCookie: kEnumerableProperty,
keys: kEnumerableProperty,
values: kEnumerableProperty,
entries: kEnumerableProperty,
// keys: kEnumerableProperty,
// values: kEnumerableProperty,
// entries: kEnumerableProperty,
forEach: kEnumerableProperty,
[Symbol.iterator]: { enumerable: false },
// [Symbol.iterator]: { enumerable: false },
[Symbol.toStringTag]: {
value: 'Headers',
configurable: true
Expand Down
74 changes: 72 additions & 2 deletions lib/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const { redirectStatusSet, referrerPolicySet: referrerPolicyTokens, badPortsSet
const { getGlobalOrigin } = require('./global')
const { collectASequenceOfCodePoints, collectAnHTTPQuotedString, removeChars, parseMIMEType } = require('./dataURL')
const { performance } = require('node:perf_hooks')
const { isBlobLike, toUSVString, ReadableStreamFrom, isValidHTTPToken } = require('../core/util')
const { isBlobLike, ReadableStreamFrom, isValidHTTPToken } = require('../core/util')
const assert = require('node:assert')
const { isUint8Array } = require('util/types')
const { webidl } = require('./webidl')

// https://nodejs.org/api/crypto.html#determining-if-crypto-support-is-unavailable
/** @type {import('crypto')|undefined} */
Expand Down Expand Up @@ -863,6 +864,75 @@ function createIterator (name, kInternalIterator, keyIndex = 0, valueIndex = 1)
}
}

/**
* @see https://webidl.spec.whatwg.org/#dfn-iterator-prototype-object
* @param {string} name name of the instance
* @param {any} object class
* @param {symbol} kInternalIterator
* @param {string | number} [keyIndex]
* @param {string | number} [valueIndex]
*/
function iteratorMixin (name, object, kInternalIterator, keyIndex = 0, valueIndex = 1) {
const makeIterator = createIterator(name, kInternalIterator, keyIndex, valueIndex)

const properties = {
keys: {
writable: true,
enumerable: true,
configurable: true,
value: function keys () {
webidl.brandCheck(this, object)
return makeIterator(this, 'key')
}
},
values: {
writable: true,
enumerable: true,
configurable: true,
value: function values () {
webidl.brandCheck(this, object)
return makeIterator(this, 'value')
}
},
entries: {
writable: true,
enumerable: true,
configurable: true,
value: function entries () {
webidl.brandCheck(this, object)
return makeIterator(this, 'key+value')
}
},
forEach: {
writable: true,
enumerable: true,
configurable: true,
value: function forEach (callbackfn, thisArg = undefined) {
webidl.brandCheck(this, object)
webidl.argumentLengthCheck(arguments, 1, { header: `${name}.forEach` })
if (typeof callbackfn !== 'function') {
throw new TypeError(
`Failed to execute 'forEach' on '${name}': parameter 1 is not of type 'Function'.`
)
}
for (const { 0: key, 1: value } of createIterator(this, 'key+value')) {
callbackfn.call(thisArg, value, key, this)
}
}
}
}

return Object.defineProperties(object.prototype, {
...properties,
[Symbol.iterator]: {
writable: true,
enumerable: false,
configurable: true,
value: properties.entries.value
}
})
}

/**
* @see https://fetch.spec.whatwg.org/#body-fully-read
*/
Expand Down Expand Up @@ -1354,7 +1424,6 @@ module.exports = {
isCancelled,
createDeferredPromise,
ReadableStreamFrom,
toUSVString,
tryUpgradeRequestToAPotentiallyTrustworthyURL,
clampAndCoarsenConnectionTimingInfo,
coarsenedSharedCurrentTime,
Expand All @@ -1380,6 +1449,7 @@ module.exports = {
normalizeMethod,
serializeJavascriptValueToJSONString,
createIterator,
iteratorMixin,
isValidHeaderName,
isValidHeaderValue,
isErrorLike,
Expand Down
2 changes: 1 addition & 1 deletion lib/fetch/webidl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { types } = require('node:util')
const { toUSVString } = require('./util')
const { toUSVString } = require('../core/util')

/** @type {import('../../types/webidl').Webidl} */
const webidl = {}
Expand Down
Loading