Skip to content

Commit

Permalink
Add buffer option to be able to disable buffering (#133)
Browse files Browse the repository at this point in the history
Fixes #127
  • Loading branch information
Spencer authored and sindresorhus committed Aug 20, 2018
1 parent d1f20ec commit 50384aa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function handleArgs(cmd, args, opts) {

opts = Object.assign({
maxBuffer: TEN_MEGABYTES,
buffer: true,
stripEof: true,
preferLocal: true,
localDir: parsed.options.cwd || process.cwd(),
Expand Down Expand Up @@ -117,14 +118,20 @@ function handleShell(fn, cmd, opts) {
return fn(file, args, opts);
}

function getStream(process, stream, encoding, maxBuffer) {
function getStream(process, stream, {encoding, buffer, maxBuffer}) {
if (!process[stream]) {
return null;
}

let ret;

if (encoding) {
if (!buffer) {
ret = new Promise((resolve, reject) => {
process[stream]
.once('end', resolve)
.once('error', reject);
});
} else if (encoding) {
ret = _getStream(process[stream], {
encoding,
maxBuffer
Expand Down Expand Up @@ -190,7 +197,7 @@ function joinCmd(cmd, args) {

module.exports = (cmd, args, opts) => {
const parsed = handleArgs(cmd, args, opts);
const {encoding, maxBuffer} = parsed.opts;
const {encoding, buffer, maxBuffer} = parsed.opts;
const joinedCmd = joinCmd(cmd, args);

let spawned;
Expand Down Expand Up @@ -260,8 +267,8 @@ module.exports = (cmd, args, opts) => {

const handlePromise = () => pFinally(Promise.all([
processDone,
getStream(spawned, 'stdout', encoding, maxBuffer),
getStream(spawned, 'stderr', encoding, maxBuffer)
getStream(spawned, 'stdout', {encoding, buffer, maxBuffer}),
getStream(spawned, 'stderr', {encoding, buffer, maxBuffer})
]).then(arr => {
const result = arr[0];
result.stdout = arr[1];
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ Default: `0`

If timeout is greater than `0`, the parent will send the signal identified by the `killSignal` property (the default is `SIGTERM`) if the child runs longer than timeout milliseconds.

#### buffer

Type: `boolean`<br>
Default: `true`

Buffer the output from the spawned process. When buffering is disabled you must consume the output of the `stdout` and `stderr` streams because the promise will not be resolved/rejected until they have completed.

#### maxBuffer

Type: `number`<br>
Expand Down
24 changes: 23 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,28 @@ test('maxBuffer affects stderr', async t => {
await t.notThrows(m('max-buffer', ['stderr', '12'], {maxBuffer: 12}));
});

test('do not buffer stdout when `buffer` set to `false`', async t => {
const promise = m('max-buffer', ['stdout', '10'], {buffer: false});
const [result, stdout] = await Promise.all([
promise,
getStream(promise.stdout)
]);

t.is(result.stdout, undefined);
t.is(stdout, '.........\n');
});

test('do not buffer stderr when `buffer` set to `false`', async t => {
const promise = m('max-buffer', ['stderr', '10'], {buffer: false});
const [result, stderr] = await Promise.all([
promise,
getStream(promise.stderr)
]);

t.is(result.stderr, undefined);
t.is(stderr, '.........\n');
});

test('skip throwing when using reject option', async t => {
const err = await m('exit', ['2'], {reject: false});
t.is(typeof err.stdout, 'string');
Expand Down Expand Up @@ -358,7 +380,7 @@ test('err.code is 3', code, 3);
test('err.code is 4', code, 4);

test('timeout will kill the process early', async t => {
const err = await t.throws(m('delay', ['3000', '0'], {timeout: 1500}));
const err = await t.throws(m('delay', ['60000', '0'], {timeout: 1500}));

t.true(err.timedOut);
t.not(err.code, 22);
Expand Down

0 comments on commit 50384aa

Please sign in to comment.