From 2564860a17bb0d1eaef0d4c46e12c0186147febe Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 2 Jun 2024 19:54:27 +0800 Subject: [PATCH] Fix: proper `TextDecoderStream` implementation --- Build/lib/text-decoder-stream.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Build/lib/text-decoder-stream.ts b/Build/lib/text-decoder-stream.ts index b99d948b..251c135e 100644 --- a/Build/lib/text-decoder-stream.ts +++ b/Build/lib/text-decoder-stream.ts @@ -16,18 +16,23 @@ // Modified by Sukka (https://skk.moe) to increase compatibility and performance with Bun. export class PolyfillTextDecoderStream extends TransformStream { - readonly encoding: string; readonly fatal: boolean; readonly ignoreBOM: boolean; constructor( - encoding: Bun.Encoding = 'utf-8', - { fatal = false, ignoreBOM = false }: ConstructorParameters[1] = {} + public readonly encoding: Bun.Encoding = 'utf-8', + { + fatal = false, + ignoreBOM = false + }: ConstructorParameters[1] = {} ) { const decoder = new TextDecoder(encoding, { fatal, ignoreBOM }); + + const nonLastChunkDecoderOpt: TextDecodeOptions = { stream: true }; + super({ transform(chunk: Uint8Array, controller: TransformStreamDefaultController) { - const decoded = decoder.decode(chunk); + const decoded = decoder.decode(chunk, nonLastChunkDecoderOpt); controller.enqueue(decoded); }, flush(controller: TransformStreamDefaultController) { @@ -43,7 +48,6 @@ export class PolyfillTextDecoderStream extends TransformStream