Skip to content

Commit aef314c

Browse files
authored
feat(webidl): better error message for ByteString converter (#1591)
1 parent 2f4b3b6 commit aef314c

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

lib/fetch/webidl.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,6 @@ webidl.converters.DOMString = function (V, opts = {}) {
388388
return String(V)
389389
}
390390

391-
// Check for 0 or more characters outside of the latin1 range.
392-
// eslint-disable-next-line no-control-regex
393-
const isLatin1 = /^[\u0000-\u00ff]{0,}$/
394-
395391
// https://webidl.spec.whatwg.org/#es-ByteString
396392
webidl.converters.ByteString = function (V) {
397393
// 1. Let x be ? ToString(V).
@@ -400,8 +396,15 @@ webidl.converters.ByteString = function (V) {
400396

401397
// 2. If the value of any element of x is greater than
402398
// 255, then throw a TypeError.
403-
if (!isLatin1.test(x)) {
404-
throw new TypeError('Argument is not a ByteString')
399+
for (let index = 0; index < x.length; index++) {
400+
const charCode = x.charCodeAt(index)
401+
402+
if (charCode > 255) {
403+
throw new TypeError(
404+
'Cannot convert argument to a ByteString because the character at' +
405+
`index ${index} has a value of ${charCode} which is greater than 255.`
406+
)
407+
}
405408
}
406409

407410
// 3. Return an IDL ByteString value whose length is the

test/webidl/converters.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,14 @@ test('ByteString', (t) => {
189189
webidl.converters.ByteString('')
190190
})
191191

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

0 commit comments

Comments
 (0)