diff --git a/lib/fetch/formdata.js b/lib/fetch/formdata.js index add64aa9226..80df2b8f399 100644 --- a/lib/fetch/formdata.js +++ b/lib/fetch/formdata.js @@ -1,6 +1,6 @@ 'use strict' -const { isBlobLike, makeIterator } = require('./util') +const { isBlobLike, iteratorMixin } = require('./util') const { kState } = require('./symbols') const { kEnumerableProperty } = require('../core/util') const { File: UndiciFile, FileLike, isFileLike } = require('./file') @@ -154,62 +154,9 @@ class FormData { this[kState].push(entry) } } - - entries () { - webidl.brandCheck(this, FormData) - - return makeIterator( - () => this[kState], - 'FormData', - 'key+value', - 'name', 'value' - ) - } - - keys () { - webidl.brandCheck(this, FormData) - - return makeIterator( - () => this[kState], - 'FormData', - 'key', - 'name', 'value' - ) - } - - values () { - webidl.brandCheck(this, FormData) - - return makeIterator( - () => this[kState], - 'FormData', - 'value', - 'name', '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 this) { - 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, @@ -218,11 +165,6 @@ Object.defineProperties(FormData.prototype, { getAll: kEnumerableProperty, has: kEnumerableProperty, set: kEnumerableProperty, - entries: kEnumerableProperty, - keys: kEnumerableProperty, - values: kEnumerableProperty, - forEach: kEnumerableProperty, - [Symbol.iterator]: { enumerable: false }, [Symbol.toStringTag]: { value: 'FormData', configurable: true diff --git a/lib/fetch/headers.js b/lib/fetch/headers.js index 504942edc6e..43860c5d98a 100644 --- a/lib/fetch/headers.js +++ b/lib/fetch/headers.js @@ -6,7 +6,7 @@ const { kHeadersList, kConstruct } = require('../core/symbols') const { kGuard } = require('./symbols') const { kEnumerableProperty } = require('../core/util') const { - makeIterator, + iteratorMixin, isValidHeaderName, isValidHeaderValue } = require('./util') @@ -504,59 +504,6 @@ class Headers { return headers } - keys () { - webidl.brandCheck(this, Headers) - - return makeIterator( - () => this[kHeadersSortedMap], - 'Headers', - 'key', - 0, 1 - ) - } - - values () { - webidl.brandCheck(this, Headers) - - return makeIterator( - () => this[kHeadersSortedMap], - 'Headers', - 'value', - 0, 1 - ) - } - - entries () { - webidl.brandCheck(this, Headers) - - return makeIterator( - () => this[kHeadersSortedMap], - 'Headers', - 'key+value', - 0, 1 - ) - } - - /** - * @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 this) { - callbackFn.call(thisArg, value, key, this) - } - } - [Symbol.for('nodejs.util.inspect.custom')] () { webidl.brandCheck(this, Headers) @@ -564,7 +511,7 @@ class Headers { } } -Headers.prototype[Symbol.iterator] = Headers.prototype.entries +iteratorMixin('Headers', Headers, kHeadersSortedMap, 0, 1) Object.defineProperties(Headers.prototype, { append: kEnumerableProperty, @@ -573,11 +520,6 @@ Object.defineProperties(Headers.prototype, { has: kEnumerableProperty, set: kEnumerableProperty, getSetCookie: kEnumerableProperty, - keys: kEnumerableProperty, - values: kEnumerableProperty, - entries: kEnumerableProperty, - forEach: kEnumerableProperty, - [Symbol.iterator]: { enumerable: false }, [Symbol.toStringTag]: { value: 'Headers', configurable: true diff --git a/lib/fetch/util.js b/lib/fetch/util.js index c5a6b46b170..82e96ec9acd 100644 --- a/lib/fetch/util.js +++ b/lib/fetch/util.js @@ -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} */ @@ -739,35 +740,40 @@ const esIteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbo /** * @see https://webidl.spec.whatwg.org/#dfn-iterator-prototype-object - * @param {() => unknown} iterator * @param {string} name name of the instance - * @param {'key'|'value'|'key+value'} kind + * @param {symbol} kInternalIterator * @param {string | number} [keyIndex] * @param {string | number} [valueIndex] */ -function makeIterator (iterator, name, kind, keyIndex = 0, valueIndex = 1) { - const object = { - index: 0, - kind, - target: iterator - } - // The [[Prototype]] internal slot of an iterator prototype object must be %IteratorPrototype%. - const iteratorObject = Object.create(esIteratorPrototype) +function createIterator (name, kInternalIterator, keyIndex = 0, valueIndex = 1) { + class FastIterableIterator { + /** @type {any} */ + #target + /** @type {'key' | 'value' | 'key+value'} */ + #kind + /** @type {number} */ + #index + + /** + * @see https://webidl.spec.whatwg.org/#dfn-default-iterator-object + * @param {unknown} target + * @param {'key' | 'value' | 'key+value'} kind + */ + constructor (target, kind) { + this.#target = target + this.#kind = kind + this.#index = 0 + } - Object.defineProperty(iteratorObject, 'next', { - value: function next () { + next () { // 1. Let interface be the interface for which the iterator prototype object exists. - // 2. Let thisValue be the this value. - // 3. Let object be ? ToObject(thisValue). - // 4. If object is a platform object, then perform a security // check, passing: - // 5. If object is not a default iterator object for interface, // then throw a TypeError. - if (Object.getPrototypeOf(this) !== iteratorObject) { + if (typeof this !== 'object' || this === null || !(#target in this)) { throw new TypeError( `'next' called on an object that does not implement interface ${name} Iterator.` ) @@ -776,8 +782,8 @@ function makeIterator (iterator, name, kind, keyIndex = 0, valueIndex = 1) { // 6. Let index be object’s index. // 7. Let kind be object’s kind. // 8. Let values be object’s target's value pairs to iterate over. - const { index, kind, target } = object - const values = target() + const index = this.#index + const values = this.#target[kInternalIterator] // 9. Let len be the length of values. const len = values.length @@ -785,17 +791,25 @@ function makeIterator (iterator, name, kind, keyIndex = 0, valueIndex = 1) { // 10. If index is greater than or equal to len, then return // CreateIterResultObject(undefined, true). if (index >= len) { - return { value: undefined, done: true } + return { + value: undefined, + done: true + } } + // 11. Let pair be the entry in values at index index. const { [keyIndex]: key, [valueIndex]: value } = values[index] + // 12. Set object’s index to index + 1. - object.index = index + 1 + this.#index = index + 1 + // 13. Return the iterator result for pair and kind. + // https://webidl.spec.whatwg.org/#iterator-result + // 1. Let result be a value determined by the value of kind: let result - switch (kind) { + switch (this.#kind) { case 'key': // 1. Let idlKey be pair’s key. // 2. Let key be the result of converting idlKey to an @@ -824,29 +838,108 @@ function makeIterator (iterator, name, kind, keyIndex = 0, valueIndex = 1) { result = [key, value] break } + // 2. Return CreateIterResultObject(result, false). return { value: result, done: false } + } + } + + // https://webidl.spec.whatwg.org/#dfn-iterator-prototype-object + // @ts-ignore + delete FastIterableIterator.prototype.constructor + + Object.setPrototypeOf(FastIterableIterator.prototype, esIteratorPrototype) + + Object.defineProperties(FastIterableIterator.prototype, { + [Symbol.toStringTag]: { + writable: false, + enumerable: false, + configurable: true, + value: `${name} Iterator` }, - writable: true, - enumerable: true, - configurable: true + next: { writable: true, enumerable: true, configurable: true } }) - // The class string of an iterator prototype object for a given interface is the - // result of concatenating the identifier of the interface and the string " Iterator". - Object.defineProperty(iteratorObject, Symbol.toStringTag, { - value: `${name} Iterator`, - writable: false, - enumerable: false, - configurable: true - }) + /** + * @param {unknown} target + * @param {'key' | 'value' | 'key+value'} kind + * @returns {IterableIterator} + */ + return function (target, kind) { + return new FastIterableIterator(target, kind) + } +} - // esIteratorPrototype needs to be the prototype of iteratorObject - // which is the prototype of an empty object. Yes, it's confusing. - return Object.create(iteratorObject) +/** + * @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 = globalThis) { + 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 makeIterator(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 + } + }) } /** @@ -1340,7 +1433,6 @@ module.exports = { isCancelled, createDeferredPromise, ReadableStreamFrom, - toUSVString, tryUpgradeRequestToAPotentiallyTrustworthyURL, clampAndCoarsenConnectionTimingInfo, coarsenedSharedCurrentTime, @@ -1365,7 +1457,8 @@ module.exports = { sameOrigin, normalizeMethod, serializeJavascriptValueToJSONString, - makeIterator, + iteratorMixin, + createIterator, isValidHeaderName, isValidHeaderValue, isErrorLike, diff --git a/lib/fetch/webidl.js b/lib/fetch/webidl.js index d639b8b7668..a93a25505fc 100644 --- a/lib/fetch/webidl.js +++ b/lib/fetch/webidl.js @@ -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 = {} diff --git a/test/fetch/headers.js b/test/fetch/headers.js index b61d8b612d2..fcdf4b7a820 100644 --- a/test/fetch/headers.js +++ b/test/fetch/headers.js @@ -472,6 +472,21 @@ test('Headers as Iterable', async (t) => { deepStrictEqual([...headers], expected) }) + + await t.test('always use the same prototype Iterator', (t) => { + const HeadersIteratorNext = Function.call.bind(new Headers()[Symbol.iterator]().next) + + const init = [ + ['a', '1'], + ['b', '2'] + ] + + const headers = new Headers(init) + const iterator = headers[Symbol.iterator]() + assert.deepStrictEqual(HeadersIteratorNext(iterator), { value: init[0], done: false }) + assert.deepStrictEqual(HeadersIteratorNext(iterator), { value: init[1], done: false }) + assert.deepStrictEqual(HeadersIteratorNext(iterator), { value: undefined, done: true }) + }) }) test('arg validation', () => {