Skip to content

Commit

Permalink
lib: fix TypeError when converting a detached buffer source
Browse files Browse the repository at this point in the history
  • Loading branch information
cola119 committed Oct 30, 2022
1 parent e43ecd5 commit a154022
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
8 changes: 7 additions & 1 deletion lib/internal/encoding.js
Expand Up @@ -412,7 +412,13 @@ function makeTextDecoderICU() {
decode(input = empty, options = kEmptyObject) {
validateDecoder(this);
if (isAnyArrayBuffer(input)) {
input = lazyBuffer().from(input);
try {
input = lazyBuffer().from(input);
} catch {
// If the buffer is detached,
// use an empty Uint8Array to avoid TypeError
input = empty;
}
} else if (!isArrayBufferView(input)) {
throw new ERR_INVALID_ARG_TYPE('input',
['ArrayBuffer', 'ArrayBufferView'],
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-encoding-custom-textdecoder.js
Expand Up @@ -211,3 +211,10 @@ if (common.hasIntl) {
assert.strictEqual(e.code, 'ERR_ENCODING_NOT_SUPPORTED');
}
}

{
const buffer = new ArrayBuffer(1);
new MessageChannel().port1.postMessage(buffer, [buffer]); // buffer is detached
const decoder = new TextDecoder();
assert.strictEqual(decoder.decode(buffer), '');
}
7 changes: 1 addition & 6 deletions test/wpt/status/encoding.json
Expand Up @@ -61,12 +61,7 @@
"requires": ["small-icu"]
},
"streams/decode-utf8.any.js": {
"requires": ["small-icu"],
"fail": {
"expected": [
"decoding a transferred ArrayBuffer chunk should give no output"
]
}
"requires": ["small-icu"]
},
"streams/decode-bad-chunks.any.js": {
"fail": {
Expand Down

0 comments on commit a154022

Please sign in to comment.