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

feat(webidl): better error message for ByteString converter #1591

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 9 additions & 6 deletions lib/fetch/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,6 @@ webidl.converters.DOMString = function (V, opts = {}) {
return String(V)
}

// Check for 0 or more characters outside of the latin1 range.
// eslint-disable-next-line no-control-regex
const isLatin1 = /^[\u0000-\u00ff]{0,}$/

// https://webidl.spec.whatwg.org/#es-ByteString
webidl.converters.ByteString = function (V) {
// 1. Let x be ? ToString(V).
Expand All @@ -400,8 +396,15 @@ webidl.converters.ByteString = function (V) {

// 2. If the value of any element of x is greater than
// 255, then throw a TypeError.
if (!isLatin1.test(x)) {
throw new TypeError('Argument is not a ByteString')
for (let index = 0; index < x.length; index++) {
const charCode = x.charCodeAt(index)

if (charCode > 255) {
throw new TypeError(
'Cannot convert argument to a ByteString because the character at' +
`index ${index} has a value of ${charCode} which is greater than 255.`
)
}
}

// 3. Return an IDL ByteString value whose length is the
Expand Down
9 changes: 9 additions & 0 deletions test/webidl/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,14 @@ test('ByteString', (t) => {
webidl.converters.ByteString('')
})

// https://github.com/nodejs/undici/issues/1590
t.throws(() => {
const char = String.fromCharCode(256)
webidl.converters.ByteString(`invalid${char}char`)
}, {
message: 'Cannot convert argument to a ByteString because the character at' +
'index 7 has a value of 256 which is greater than 255.'
})

t.end()
})