Skip to content
Merged
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
16 changes: 1 addition & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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';
Expand Down Expand Up @@ -111,20 +111,6 @@ function combineSignals(signals: Iterable<AbortSignal>): AbortSignal {
return controller.signal;
}

async function readStream(stream: Readable): Promise<string> {
let output = '';

try {
for await (const chunk of stream) {
output += chunk.toString();
}
} catch {
// suppress errors, child process probably failed to spawn
}

return output;
}

export class ExecProcess implements Result {
protected _process?: ChildProcess;
protected _aborted: boolean = false;
Expand Down
17 changes: 17 additions & 0 deletions src/stream.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -28,3 +29,19 @@ export const combineStreams = (streams: Readable[]): Readable => {

return combined;
};

export async function readStream(stream: Readable): Promise<string> {
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;
}
12 changes: 11 additions & 1 deletion src/test/stream_test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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('🚀');
});
});