From d453823bfc2fc9d2ccaf185debaedac12d6010e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20=C5=BBelazko?= Date: Thu, 12 Jun 2025 22:10:23 +0200 Subject: [PATCH] fix: ensure transcript decoding supports unicode --- src/transcript.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/transcript.ts b/src/transcript.ts index 57e787e..6e0189c 100644 --- a/src/transcript.ts +++ b/src/transcript.ts @@ -17,19 +17,11 @@ export class Transcript { } recv(redactedSymbol = '*') { - return this.#recv.reduce((recv: string, num) => { - recv = - recv + (num === 0 ? redactedSymbol : Buffer.from([num]).toString()); - return recv; - }, ''); + return bytesToUtf8(substituteRedactions(this.#recv, redactedSymbol)); } sent(redactedSymbol = '*') { - return this.#sent.reduce((sent: string, num) => { - sent = - sent + (num === 0 ? redactedSymbol : Buffer.from([num]).toString()); - return sent; - }, ''); + return bytesToUtf8(substituteRedactions(this.#sent, redactedSymbol)); } text = (redactedSymbol = '*') => { @@ -101,3 +93,15 @@ function indexOfString(str: string, substr: string): number { function bytesSize(str: string): number { return Buffer.from(str).byteLength; } + +function bytesToUtf8(array: number[]): string { + return Buffer.from(array).toString("utf8"); +} + +function substituteRedactions( + array: number[], + redactedSymbol: string = "*", +): number[] { + const replaceCharByte = redactedSymbol.charCodeAt(0); + return array.map((byte) => (byte === 0 ? replaceCharByte : byte)); +}