diff --git a/lib/internal/blob.js b/lib/internal/blob.js index 13c84e192f834a..5fb25e04fe1abe 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -42,7 +42,6 @@ const { } = require('internal/util/types'); const { - toUSVString, createDeferredPromise, customInspectSymbol: kInspect, emitExperimentalWarning, @@ -317,8 +316,7 @@ class Blob { throw new ERR_INVALID_THIS('Blob'); const dec = new TextDecoder(); - const str = dec.decode(await this.arrayBuffer()); - return toUSVString(str); + return dec.decode(await this.arrayBuffer()); } /** diff --git a/lib/stream/consumers.js b/lib/stream/consumers.js index 3bcdcbf2ec3456..4566eff6a30f7f 100644 --- a/lib/stream/consumers.js +++ b/lib/stream/consumers.js @@ -16,10 +16,6 @@ const { Buffer, } = require('buffer'); -const { - toUSVString -} = require('internal/util'); - /** * @typedef {import('../internal/webstreams/readablestream').ReadableStream * } ReadableStream @@ -70,7 +66,7 @@ async function text(stream) { // Flush the streaming TextDecoder so that any pending // incomplete multibyte characters are handled. str += dec.decode(undefined, { stream: false }); - return toUSVString(str); + return str; } /** diff --git a/test/parallel/test-blob.js b/test/parallel/test-blob.js index e3d09fb348d0ee..f710b26e0ebdca 100644 --- a/test/parallel/test-blob.js +++ b/test/parallel/test-blob.js @@ -88,11 +88,11 @@ assert.throws(() => new Blob({}), { } { - const b = new Blob(['hello', Buffer.from('world\ud801')]); - assert.strictEqual(b.size, 13); + const b = new Blob(['hello', new Uint8Array([0xed,0xa0,0x88])]); + assert.strictEqual(b.size, 8); b.text().then(common.mustCall((text) => { - assert.strictEqual(text, 'helloworld\ufffd'); - assert.strictEqual(text.length, 11); + assert.strictEqual(text, 'hello\ufffd\ufffd\ufffd'); + assert.strictEqual(text.length, 8); })); } diff --git a/test/parallel/test-whatwg-encoding-custom-textdecoder.js b/test/parallel/test-whatwg-encoding-custom-textdecoder.js index 877cd43734451b..2f09883fc61b85 100644 --- a/test/parallel/test-whatwg-encoding-custom-textdecoder.js +++ b/test/parallel/test-whatwg-encoding-custom-textdecoder.js @@ -191,3 +191,18 @@ if (common.hasIntl) { } ); } + +// Test TextDecoder for surrogate code points +{ + const decoder = new TextDecoder(); + const chunk = Buffer.from([0x66, 0x6f, 0x6f, 0xed, 0xa0, 0x80]); // foo + U+D800 + const str = decoder.decode(chunk); + assert.strictEqual(str, 'foo\ufffd\ufffd\ufffd'); +} + +{ + const decoder = new TextDecoder(); + const chunk = Buffer.from('\ud807'); + const str = decoder.decode(chunk); + assert.strictEqual(str, '\ufffd'); +} \ No newline at end of file