Skip to content

Commit

Permalink
fix: convert HeadersInit to sequence/dictionary correctly (nodejs#2784)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent 311a471 commit 3451a06
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,10 @@ Object.defineProperties(Headers.prototype, {

webidl.converters.HeadersInit = function (V) {
if (webidl.util.Type(V) === 'Object') {
if (V[Symbol.iterator]) {
return webidl.converters['sequence<sequence<ByteString>>'](V)
const iterator = Reflect.get(V, Symbol.iterator)

if (typeof iterator === 'function') {
return webidl.converters['sequence<sequence<ByteString>>'](V, iterator.bind(V))
}

return webidl.converters['record<ByteString, ByteString>'](V)
Expand Down
8 changes: 4 additions & 4 deletions lib/fetch/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ webidl.util.IntegerPart = function (n) {

// https://webidl.spec.whatwg.org/#es-sequence
webidl.sequenceConverter = function (converter) {
return (V) => {
return (V, Iterable) => {
// 1. If Type(V) is not Object, throw a TypeError.
if (webidl.util.Type(V) !== 'Object') {
throw webidl.errors.exception({
Expand All @@ -229,7 +229,7 @@ webidl.sequenceConverter = function (converter) {

// 2. Let method be ? GetMethod(V, @@iterator).
/** @type {Generator} */
const method = V?.[Symbol.iterator]?.()
const method = typeof Iterable === 'function' ? Iterable() : V?.[Symbol.iterator]?.()
const seq = []

// 3. If method is undefined, throw a TypeError.
Expand Down Expand Up @@ -273,8 +273,8 @@ webidl.recordConverter = function (keyConverter, valueConverter) {
const result = {}

if (!types.isProxy(O)) {
// Object.keys only returns enumerable properties
const keys = Object.keys(O)
// 1. Let desc be ? O.[[GetOwnProperty]](key).
const keys = [...Object.getOwnPropertyNames(O), ...Object.getOwnPropertySymbols(O)]

for (const key of keys) {
// 1. Let typedKey be key converted to an IDL value of type K.
Expand Down
27 changes: 27 additions & 0 deletions test/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,30 @@ test('When the value is updated, update the cache', (t) => {
headers.append('d', 'd')
deepStrictEqual([...headers], [...expected, ['d', 'd']])
})

test('Symbol.iterator is only accessed once', (t) => {
const { ok } = tspl(t, { plan: 1 })

const dict = new Proxy({}, {
get () {
ok(true)

return function * () {}
}
})

new Headers(dict) // eslint-disable-line no-new
})

test('Invalid Symbol.iterators', (t) => {
const { throws } = tspl(t, { plan: 3 })

throws(() => new Headers({ [Symbol.iterator]: null }), TypeError)
throws(() => new Headers({ [Symbol.iterator]: undefined }), TypeError)
throws(() => {
const obj = { [Symbol.iterator]: null }
Object.defineProperty(obj, Symbol.iterator, { enumerable: false })

new Headers(obj) // eslint-disable-line no-new
}, TypeError)
})
2 changes: 1 addition & 1 deletion types/webidl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
type Converter<T> = (object: unknown) => T

type SequenceConverter<T> = (object: unknown) => T[]
type SequenceConverter<T> = (object: unknown, iterable?: IterableIterator<T>) => T[]

type RecordConverter<K extends string, V> = (object: unknown) => Record<K, V>

Expand Down

0 comments on commit 3451a06

Please sign in to comment.