Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: update TextEncoderStream to align with the latest spec #44101

Merged
merged 3 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions lib/internal/webstreams/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const {
ObjectDefineProperties,
Symbol,
String,
StringPrototypeCharCodeAt,
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
Uint8Array,
} = primordials;

const {
Expand Down Expand Up @@ -31,6 +34,7 @@ const {
const kHandle = Symbol('kHandle');
const kTransform = Symbol('kTransform');
const kType = Symbol('kType');
const kPendingHighSurrogate = Symbol('kPendingHighSurrogate');

/**
* @typedef {import('./readablestream').ReadableStream} ReadableStream
Expand All @@ -49,19 +53,46 @@ function isTextDecoderStream(value) {

class TextEncoderStream {
constructor() {
this[kPendingHighSurrogate] = null;
this[kType] = 'TextEncoderStream';
this[kHandle] = new TextEncoder();
this[kTransform] = new TransformStream({
transform: (chunk, controller) => {
const value = this[kHandle].encode(chunk);
if (value)
// https://encoding.spec.whatwg.org/#encode-and-enqueue-a-chunk
chunk = String(chunk);
let finalChunk = '';
for (let i = 0; i < chunk.length; i++) {
const item = chunk[i];
const codeUnit = StringPrototypeCharCodeAt(item, 0);
if (this[kPendingHighSurrogate] !== null) {
const highSurrogate = this[kPendingHighSurrogate];
this[kPendingHighSurrogate] = null;
if (0xDC00 <= codeUnit && codeUnit <= 0xDFFF) {
finalChunk += highSurrogate + item;
continue;
}
finalChunk += '\uFFFD';
}
if (0xD800 <= codeUnit && codeUnit <= 0xDBFF) {
this[kPendingHighSurrogate] = item;
continue;
}
if (0xDC00 <= codeUnit && codeUnit <= 0xDFFF) {
finalChunk += '\uFFFD';
continue;
}
finalChunk += item;
}
if (finalChunk) {
const value = this[kHandle].encode(finalChunk);
controller.enqueue(value);
}
},
flush: (controller) => {
const value = this[kHandle].encode();
if (value.byteLength > 0)
controller.enqueue(value);
controller.terminate();
// https://encoding.spec.whatwg.org/#encode-and-flush
if (this[kPendingHighSurrogate] !== null) {
controller.enqueue(new Uint8Array([0xEF, 0xBF, 0xBD]));
}
},
});
}
Expand Down
31 changes: 29 additions & 2 deletions test/wpt/status/encoding.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,35 @@
"unsupported-encodings.any.js": {
"skip": "decoding-helpers.js needs XMLHttpRequest"
},
"streams/*.js": {
"fail": "No implementation of TextDecoderStream and TextEncoderStream"
"streams/decode-ignore-bom.any.js": {
"requires": ["small-icu"]
},
"streams/realms.window.js": {
"skip": "window is not defined"
},
"streams/decode-attributes.any.js": {
"requires": ["full-icu"]
},
"streams/decode-incomplete-input.any.js": {
"requires": ["small-icu"]
},
"streams/decode-utf8.any.js": {
"requires": ["small-icu"],
"fail": {
"unexpected": [
"promise_test: Unhandled rejection with value: object 'TypeError: Cannot perform Construct on a detached ArrayBuffer'"
]
}
},
"streams/decode-bad-chunks.any.js": {
"fail": {
"unexpected": [
"assert_unreached: Should have rejected: write should reject Reached unreachable code"
]
}
},
"streams/decode-non-utf8.any.js": {
"requires": ["full-icu"]
},
"encodeInto.any.js": {
"requires": ["small-icu"]
Expand Down