From fd9286e10e863e05a5b3a1ccc81c0a0836ca8c89 Mon Sep 17 00:00:00 2001 From: liang <836792+leonco@users.noreply.github.com> Date: Wed, 10 Jul 2024 18:52:35 +0800 Subject: [PATCH] fix(streams): toText() incorrect with multibyte strings in different chunks (#5381) --- streams/to_text.ts | 9 +++++---- streams/to_text_test.ts | 10 ++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/streams/to_text.ts b/streams/to_text.ts index 9cd1af9d1..4839743ef 100644 --- a/streams/to_text.ts +++ b/streams/to_text.ts @@ -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 { + 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; } diff --git a/streams/to_text_test.ts b/streams/to_text_test.ts index 5bbd916b0..f45725f9e 100644 --- a/streams/to_text_test.ts +++ b/streams/to_text_test.ts @@ -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), "中文"); });