未验证 提交 fc8e4779 编写于 作者: B Benjamin Pasero 提交者: GitHub

Use TextDecoder when decoding UTF-8 (#102369)

* textfiles - always use TextDecoder when reading UTF-8

* fix tests

* do not share a text decoder when using streams

* jsdoc
上级 42d10d8c
......@@ -49,18 +49,39 @@ class DecoderStream implements IDecoderStream {
* This stream will only load iconv-lite lazily if the encoding
* is not UTF-8. This ensures that for most common cases we do
* not pay the price of loading the module from disk.
*
* We still need to be careful when converting UTF-8 to a string
* though because we read the file in chunks of Buffer and thus
* need to decode it via TextDecoder helper that is available
* in browser and node.js environments.
*/
static async create(encoding: string): Promise<DecoderStream> {
let decoder: IDecoderStream | undefined = undefined;
if (encoding !== UTF8) {
const iconv = await import('iconv-lite-umd');
decoder = iconv.getDecoder(toNodeEncoding(encoding));
} else {
const utf8TextDecoder = new TextDecoder();
decoder = {
write(buffer: Uint8Array): string {
return utf8TextDecoder.decode(buffer, {
// Signal to TextDecoder that potentially more data is coming
// and that we are calling `decode` in the end to consume any
// remainders
stream: true
});
},
end(): string | undefined {
return utf8TextDecoder.decode();
}
};
}
return new DecoderStream(decoder);
}
private constructor(private iconvLiteDecoder: IDecoderStream | undefined) { }
private constructor(private iconvLiteDecoder: IDecoderStream) { }
write(buffer: Uint8Array): string {
if (this.iconvLiteDecoder) {
......
......@@ -335,7 +335,7 @@ suite('Encoding', () => {
const source = newTestReadableStream(buffers);
const { stream } = await encoding.toDecodeStream(source, { minBytesRequiredForDetection: 4, guessEncoding: false, overwriteEncoding: async detected => detected || encoding.UTF8 });
const expected = incompleteEmojis.toString(encoding.UTF8);
const expected = new TextDecoder().decode(incompleteEmojis);
const actual = await readAllAsString(stream);
assert.equal(actual, expected);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册