Skip to content

Commit c4b22ef

Browse files
KhafraDevronag
authored andcommitted
fix: convert HeadersInit to sequence/dictionary correctly (#2784)
1 parent b798f30 commit c4b22ef

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

lib/fetch/headers.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,10 @@ Object.defineProperties(Headers.prototype, {
528528

529529
webidl.converters.HeadersInit = function (V) {
530530
if (webidl.util.Type(V) === 'Object') {
531-
if (V[Symbol.iterator]) {
532-
return webidl.converters['sequence<sequence<ByteString>>'](V)
531+
const iterator = Reflect.get(V, Symbol.iterator)
532+
533+
if (typeof iterator === 'function') {
534+
return webidl.converters['sequence<sequence<ByteString>>'](V, iterator.bind(V))
533535
}
534536

535537
return webidl.converters['record<ByteString, ByteString>'](V)

lib/fetch/webidl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ webidl.util.IntegerPart = function (n) {
218218

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

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

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

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

279279
for (const key of keys) {
280280
// 1. Let typedKey be key converted to an IDL value of type K.

test/fetch/headers.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,3 +719,30 @@ test('When the value is updated, update the cache', (t) => {
719719
headers.append('d', 'd')
720720
deepStrictEqual([...headers], [...expected, ['d', 'd']])
721721
})
722+
723+
test('Symbol.iterator is only accessed once', (t) => {
724+
const { ok } = tspl(t, { plan: 1 })
725+
726+
const dict = new Proxy({}, {
727+
get () {
728+
ok(true)
729+
730+
return function * () {}
731+
}
732+
})
733+
734+
new Headers(dict) // eslint-disable-line no-new
735+
})
736+
737+
test('Invalid Symbol.iterators', (t) => {
738+
const { throws } = tspl(t, { plan: 3 })
739+
740+
throws(() => new Headers({ [Symbol.iterator]: null }), TypeError)
741+
throws(() => new Headers({ [Symbol.iterator]: undefined }), TypeError)
742+
throws(() => {
743+
const obj = { [Symbol.iterator]: null }
744+
Object.defineProperty(obj, Symbol.iterator, { enumerable: false })
745+
746+
new Headers(obj) // eslint-disable-line no-new
747+
}, TypeError)
748+
})

types/webidl.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
type Converter<T> = (object: unknown) => T
77

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

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

0 commit comments

Comments
 (0)