Skip to content

Commit

Permalink
test: refactor testcases for surrogate codes
Browse files Browse the repository at this point in the history
Since the readable web streams  already returns
USVString
The changes made to blob.js and consumers.js  are reverted
Test cases are added to check  surrogate to USVString conversion for
ReadablewebStreams Textdecoder and Blob

PR-URL: nodejs#40351
Fixes: nodejs#39804
  • Loading branch information
git-srinivas committed Oct 13, 2021
1 parent 442f969 commit 5fa492a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
4 changes: 1 addition & 3 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const {
} = require('internal/util/types');

const {
toUSVString,
createDeferredPromise,
customInspectSymbol: kInspect,
emitExperimentalWarning,
Expand Down Expand Up @@ -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());
}

/**
Expand Down
6 changes: 1 addition & 5 deletions lib/stream/consumers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ const {
Buffer,
} = require('buffer');

const {
toUSVString
} = require('internal/util');

/**
* @typedef {import('../internal/webstreams/readablestream').ReadableStream
* } ReadableStream
Expand Down Expand Up @@ -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;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
}

Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-whatwg-encoding-custom-textdecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

0 comments on commit 5fa492a

Please sign in to comment.