Skip to content

Commit

Permalink
Headers webidl errors (#3833)
Browse files Browse the repository at this point in the history
* fixup! contructor

* better error message

* fixup! rm DOMString converter from ByteString

* fixup
  • Loading branch information
KhafraDev authored Nov 14, 2024
1 parent 28b10fa commit 216ec7b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/web/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ class Headers {

// 2. If init is given, then fill this with init.
if (init !== undefined) {
init = webidl.converters.HeadersInit(init, 'Headers contructor', 'init')
init = webidl.converters.HeadersInit(init, 'Headers constructor', 'init')
fill(this, init)
}
}
Expand Down
16 changes: 12 additions & 4 deletions lib/web/fetch/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,14 @@ webidl.recordConverter = function (keyConverter, valueConverter) {
const keys = [...Object.getOwnPropertyNames(O), ...Object.getOwnPropertySymbols(O)]

for (const key of keys) {
const keyName = webidl.util.Stringify(key)

// 1. Let typedKey be key converted to an IDL value of type K.
const typedKey = keyConverter(key, prefix, argument)
const typedKey = keyConverter(key, prefix, `Key ${keyName} in ${argument}`)

// 2. Let value be ? Get(O, key).
// 3. Let typedValue be value converted to an IDL value of type V.
const typedValue = valueConverter(O[key], prefix, argument)
const typedValue = valueConverter(O[key], prefix, `${argument}[${keyName}]`)

// 4. Set result[typedKey] to typedValue.
result[typedKey] = typedValue
Expand Down Expand Up @@ -501,8 +503,14 @@ webidl.converters.DOMString = function (V, prefix, argument, opts) {
// https://webidl.spec.whatwg.org/#es-ByteString
webidl.converters.ByteString = function (V, prefix, argument) {
// 1. Let x be ? ToString(V).
// Note: DOMString converter perform ? ToString(V)
const x = webidl.converters.DOMString(V, prefix, argument)
if (typeof V === 'symbol') {
throw webidl.errors.exception({
header: prefix,
message: `${argument} is a symbol, which cannot be converted to a ByteString.`
})
}

const x = String(V)

// 2. If the value of any element of x is greater than
// 255, then throw a TypeError.
Expand Down
17 changes: 15 additions & 2 deletions test/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('Headers initialization', async (t) => {
throws(() => new Headers(['undici', 'fetch', 'fetch']), TypeError)
throws(
() => new Headers([0, 1, 2]),
TypeError('Headers contructor: init[0] (0) is not iterable.')
TypeError('Headers constructor: init[0] (0) is not iterable.')
)
})

Expand All @@ -41,7 +41,7 @@ test('Headers initialization', async (t) => {
const init = ['undici', 'fetch', 'fetch', 'undici']
throws(
() => new Headers(init),
TypeError('Headers contructor: init[0] ("undici") is not iterable.')
TypeError('Headers constructor: init[0] ("undici") is not iterable.')
)
})
})
Expand Down Expand Up @@ -767,3 +767,16 @@ test('Invalid Symbol.iterators', (t) => {
new Headers(obj) // eslint-disable-line no-new
}, TypeError)
})

// https://github.com/nodejs/undici/issues/3829
test('Invalid key/value records passed to constructor (issue #3829)', (t) => {
assert.throws(
() => new Headers({ [Symbol('x-fake-header')]: '??' }),
new TypeError('Headers constructor: Key Symbol(x-fake-header) in init is a symbol, which cannot be converted to a ByteString.')
)

assert.throws(
() => new Headers({ 'x-fake-header': Symbol('why is this here?') }),
new TypeError('Headers constructor: init["x-fake-header"] is a symbol, which cannot be converted to a ByteString.')
)
})
2 changes: 1 addition & 1 deletion test/webidl/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('ByteString', (t) => {
]) {
assert.throws(
() => new Headers()[method](name, value),
new TypeError(`Headers.${method}: name is a symbol, which cannot be converted to a DOMString.`)
new TypeError(`Headers.${method}: name is a symbol, which cannot be converted to a ByteString.`)
)
}
})
Expand Down

0 comments on commit 216ec7b

Please sign in to comment.