Skip to content

Commit ad6a134

Browse files
committed
lib: fix TypeError when converting a detached buffer source
1 parent abddacb commit ad6a134

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/internal/encoding.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,13 @@ function makeTextDecoderICU() {
412412
decode(input = empty, options = kEmptyObject) {
413413
validateDecoder(this);
414414
if (isAnyArrayBuffer(input)) {
415-
input = lazyBuffer().from(input);
415+
try {
416+
input = lazyBuffer().from(input);
417+
} catch {
418+
// If the buffer is detached,
419+
// use an empty Uint8Array to avoid TypeError
420+
input = empty;
421+
}
416422
} else if (!isArrayBufferView(input)) {
417423
throw new ERR_INVALID_ARG_TYPE('input',
418424
['ArrayBuffer', 'ArrayBufferView'],

test/parallel/test-whatwg-encoding-custom-textdecoder.js

+7
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,10 @@ if (common.hasIntl) {
199199
const str = decoder.decode(chunk);
200200
assert.strictEqual(str, 'foo\ufffd');
201201
}
202+
203+
{
204+
const buffer = new ArrayBuffer(1);
205+
new MessageChannel().port1.postMessage(buffer, [buffer]); // buffer is detached
206+
const decoder = new TextDecoder();
207+
assert.strictEqual(decoder.decode(buffer), '');
208+
}

0 commit comments

Comments
 (0)