Skip to content

Commit

Permalink
fix: properly handle child process stdio chunking (#409)
Browse files Browse the repository at this point in the history
* 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 <vertedinde@electronjs.org>
  • Loading branch information
addaleax and VerteDinde authored Feb 7, 2024
1 parent 0ad8e20 commit 233603a
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/spawn-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit 233603a

Please sign in to comment.