fix(streams): toText() incorrect with multibyte strings in different chunks (#5381)

This commit is contained in:
liang 2024-07-10 18:52:35 +08:00 committed by GitHub
parent d134a2d9ae
commit fd9286e10e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 4 deletions

View File

@ -1,8 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
const textDecoder = new TextDecoder();
/**
* Converts a {@linkcode ReadableSteam} of strings or {@linkcode Uint8Array}s
* to a single string. Works the same as {@linkcode Response.text}.
@ -22,6 +20,7 @@ const textDecoder = new TextDecoder();
export async function toText(
readableStream: ReadableStream,
): Promise<string> {
const textDecoder = new TextDecoder();
const reader = readableStream.getReader();
let result = "";
@ -32,8 +31,10 @@ export async function toText(
break;
}
result += typeof value === "string" ? value : textDecoder.decode(value);
result += typeof value === "string"
? value
: textDecoder.decode(value, { stream: true });
}
result += textDecoder.decode();
return result;
}

View File

@ -12,4 +12,14 @@ Deno.test("toText()", async () => {
const stringStream = ReadableStream.from(["hello", " deno ", "world"]);
assertEquals(await toText(stringStream), "hello deno world");
const utf8ByteStream = new ReadableStream({
start(controller) {
controller.enqueue(new Uint8Array([228, 184, 173, 230, 150]));
controller.enqueue(new Uint8Array([135]));
controller.close();
},
});
assertEquals(await toText(utf8ByteStream), "中文");
});