Skip to content

Commit 6639bec

Browse files
committed
lib: fix TypeError when converting a detached buffer source
1 parent 60898ee commit 6639bec

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
@@ -206,3 +206,10 @@ if (common.hasIntl) {
206206
const str = decoder.decode(chunk);
207207
assert.strictEqual(str, '\ufffd');
208208
}
209+
210+
{
211+
const buffer = new ArrayBuffer(1);
212+
new MessageChannel().port1.postMessage(buffer, [buffer]); // buffer is detached
213+
const decoder = new TextDecoder();
214+
assert.strictEqual(decoder.decode(buffer), '');
215+
}

0 commit comments

Comments
 (0)