Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -113,11 +114,13 @@ function combineSignals(signals: Iterable<AbortSignal>): AbortSignal {

async function readStream(stream: Readable): Promise<string> {
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
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down