From 379052e9bed4123732a7ba20056d7cc8d10c28bf Mon Sep 17 00:00:00 2001 From: Ferit Date: Sat, 24 Oct 2026 22:46:15 +0300 Subject: [PATCH 1/2] fix: decode stream chunks with StringDecoder to prevent multibyte corruption --- src/main.ts | 5 ++++- src/test/main_test.ts | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 424d1e7..5d35f5c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,6 +12,7 @@ import {combineStreams} from './stream.js'; import readline from 'node:readline'; import {normalizeSpawnCommand} from './normalize.js'; import {NonZeroExitError} from './non-zero-exit-error.js'; +import {StringDecoder} from 'node:string_decoder'; export {NonZeroExitError, normalizeSpawnCommand}; @@ -113,11 +114,13 @@ function combineSignals(signals: Iterable): AbortSignal { async function readStream(stream: Readable): Promise { let output = ''; + const decoder = new StringDecoder(); try { for await (const chunk of stream) { - output += chunk.toString(); + output += typeof chunk === 'string' ? chunk : decoder.write(chunk); } + output += decoder.end(); } catch { // suppress errors, child process probably failed to spawn } diff --git a/src/test/main_test.ts b/src/test/main_test.ts index ecc757c..7ee800d 100644 --- a/src/test/main_test.ts +++ b/src/test/main_test.ts @@ -52,6 +52,14 @@ describe.for(variants)('exec ($name)', ({x, isAsync}) => { expect(result.stderr).toBe(''); }); + test('handles multibyte UTF-8 characters split across chunks', async () => { + const result = await x('node', [ + '-e', + "process.stdout.write(Buffer.from([0xF0, 0x9F])); setTimeout(() => process.stdout.write(Buffer.from([0x9A, 0x80])), 20);" + ]); + expect(result.stdout).toBe('🚀'); + }); + test('captures stderr', async () => { const result = await x('node', ['-e', "console.error('some error')"]); expect(result.stderr).toBe('some error\n'); From 7259f72047dded42604395be5cbe3a1db52bdd30 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Tue, 24 Nov 2026 21:25:47 +0000 Subject: [PATCH 2/2] chore: move readStream to stream utilities --- src/main.ts | 19 +------------------ src/stream.ts | 17 +++++++++++++++++ src/test/main_test.ts | 8 -------- src/test/stream_test.ts | 12 +++++++++++- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/main.ts b/src/main.ts index 5d35f5c..03795d6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,11 +8,10 @@ import { import {type Readable} from 'node:stream'; import {cwd as getCwd} from 'node:process'; import {computeEnv} from './env.js'; -import {combineStreams} from './stream.js'; +import {combineStreams, readStream} from './stream.js'; import readline from 'node:readline'; import {normalizeSpawnCommand} from './normalize.js'; import {NonZeroExitError} from './non-zero-exit-error.js'; -import {StringDecoder} from 'node:string_decoder'; export {NonZeroExitError, normalizeSpawnCommand}; @@ -112,22 +111,6 @@ function combineSignals(signals: Iterable): AbortSignal { return controller.signal; } -async function readStream(stream: Readable): Promise { - let output = ''; - const decoder = new StringDecoder(); - - try { - for await (const chunk of stream) { - output += typeof chunk === 'string' ? chunk : decoder.write(chunk); - } - output += decoder.end(); - } catch { - // suppress errors, child process probably failed to spawn - } - - return output; -} - export class ExecProcess implements Result { protected _process?: ChildProcess; protected _aborted: boolean = false; diff --git a/src/stream.ts b/src/stream.ts index 6750b09..7476af0 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1,6 +1,7 @@ import {type EventEmitter} from 'node:events'; import {pipeline} from 'node:stream/promises'; import {type Readable, PassThrough} from 'node:stream'; +import {StringDecoder} from 'node:string_decoder'; export const waitForEvent = ( emitter: EventEmitter, @@ -28,3 +29,19 @@ export const combineStreams = (streams: Readable[]): Readable => { return combined; }; + +export async function readStream(stream: Readable): Promise { + let output = ''; + const decoder = new StringDecoder(); + + try { + for await (const chunk of stream) { + output += typeof chunk === 'string' ? chunk : decoder.write(chunk); + } + output += decoder.end(); + } catch { + // suppress errors, child process probably failed to spawn + } + + return output; +} diff --git a/src/test/main_test.ts b/src/test/main_test.ts index 7ee800d..ecc757c 100644 --- a/src/test/main_test.ts +++ b/src/test/main_test.ts @@ -52,14 +52,6 @@ describe.for(variants)('exec ($name)', ({x, isAsync}) => { expect(result.stderr).toBe(''); }); - test('handles multibyte UTF-8 characters split across chunks', async () => { - const result = await x('node', [ - '-e', - "process.stdout.write(Buffer.from([0xF0, 0x9F])); setTimeout(() => process.stdout.write(Buffer.from([0x9A, 0x80])), 20);" - ]); - expect(result.stdout).toBe('🚀'); - }); - test('captures stderr', async () => { const result = await x('node', ['-e', "console.error('some error')"]); expect(result.stderr).toBe('some error\n'); diff --git a/src/test/stream_test.ts b/src/test/stream_test.ts index 79f9c7d..8d8c9e8 100644 --- a/src/test/stream_test.ts +++ b/src/test/stream_test.ts @@ -1,4 +1,4 @@ -import {combineStreams, waitForEvent} from '../stream.js'; +import {combineStreams, readStream, waitForEvent} from '../stream.js'; import {describe, test, expect} from 'vitest'; import {EventEmitter} from 'node:events'; import {Readable} from 'node:stream'; @@ -70,3 +70,13 @@ describe('combineStreams', async () => { expect(chunks).toEqual(['foo', 'bar']); // no time for 'baz' before stream0 was destroyed }); }); + +describe('readStream', async () => { + test('decodes multibyte characters split across chunks', async () => { + const stream = Readable.from([ + Buffer.from([0xf0, 0x9f]), + Buffer.from([0x9a, 0x80]) + ]); + expect(await readStream(stream)).toBe('🚀'); + }); +});