From 233603a66e4ecfdb25d10c55db63b13a2f0a5c08 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 7 Feb 2024 19:33:00 +0100 Subject: [PATCH] fix: properly handle child process stdio chunking (#409) * fix: properly handle child process stdio chunking Converting individual chunks from UTF-8 to JS strings is problematic because it does not handle UTF-8 characters that are split across chunks properly. Use the proper way of reading string data from streams instead. * fix: update bufHandler after main merge --------- Co-authored-by: Keeley Hammond --- src/spawn-promise.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/spawn-promise.ts b/src/spawn-promise.ts index ea0a3ec..e125a86 100644 --- a/src/spawn-promise.ts +++ b/src/spawn-promise.ts @@ -33,14 +33,13 @@ export default function spawn(exe: string, params: string[], opts?: SpawnOptions if (--refCount <= 0 && !rejected) resolve(stdout); }; - const bufHandler = (b: Buffer): void => { - const chunk = b.toString(); + const bufHandler = (chunk: string): void => { stdout += chunk; }; - proc.stdout.on('data', bufHandler); + proc.stdout.setEncoding('utf8').on('data', bufHandler); proc.stdout.once('close', release); - proc.stderr.on('data', bufHandler); + proc.stderr.setEncoding('utf8').on('data', bufHandler); proc.stderr.once('close', release); proc.on('error', (e: Error): void => reject(e));