Add `buffer` option to be able to disable buffering (#133)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #127
authored by Spencer · committed by Sindre Sorhus · parent d1f20ec · Browse files · 3 files·+42 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+42 added−6 removed
- other:+42 added−6 removed
Provenance
- Commit
50384aa- Tree
17920c8- Author
Spencer <email@spalger.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
d1f20ec
3 files changed+42 added−6 removed
modified:index.js+12 added-5 removed
42 unmodified lines43 }44 45 opts = Object.assign({46 maxBuffer: TEN_MEGABYTES,47+ buffer: true,48 stripEof: true,49 preferLocal: true,50 localDir: parsed.options.cwd || process.cwd(),51 encoding: 'utf8',65 unmodified lines117 118 return fn(file, args, opts);119 }120 120-function getStream(process, stream, encoding, maxBuffer) {121+function getStream(process, stream, {encoding, buffer, maxBuffer}) {122 if (!process[stream]) {123 return null;124 }125 126 let ret;127 127- if (encoding) {128+ if (!buffer) {129+ ret = new Promise((resolve, reject) => {130+ process[stream]131+ .once('end', resolve)132+ .once('error', reject);133+ });134+ } else if (encoding) {135 ret = _getStream(process[stream], {136 encoding,137 maxBuffer138 });57 unmodified lines196 }197 198 module.exports = (cmd, args, opts) => {199 const parsed = handleArgs(cmd, args, opts);193- const {encoding, maxBuffer} = parsed.opts;200+ const {encoding, buffer, maxBuffer} = parsed.opts;201 const joinedCmd = joinCmd(cmd, args);202 203 let spawned;204 try {61 unmodified lines266 }267 268 const handlePromise = () => pFinally(Promise.all([269 processDone,263- getStream(spawned, 'stdout', encoding, maxBuffer),264- getStream(spawned, 'stderr', encoding, maxBuffer)270+ getStream(spawned, 'stdout', {encoding, buffer, maxBuffer}),271+ getStream(spawned, 'stderr', {encoding, buffer, maxBuffer})272 ]).then(arr => {273 const result = arr[0];274 result.stdout = arr[1];275 result.stderr = arr[2];88 unmodified linesmodified:readme.md+7 added
250 unmodified lines251 Default: `0`252 253 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.254 255+#### buffer256+257+Type: `boolean`<br>258+Default: `true`259+260+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.261+262 #### maxBuffer263 264 Type: `number`<br>265 Default: `10000000` (10MB)62 unmodified linesmodified:test.js+23 added-1 removed
237 unmodified lines238 await t.throws(m('max-buffer', ['stderr', '13'], {maxBuffer: 12}), /stderr maxBuffer exceeded/);239 await t.notThrows(m('max-buffer', ['stderr', '12'], {maxBuffer: 12}));240 });241 242+test('do not buffer stdout when `buffer` set to `false`', async t => {243+ const promise = m('max-buffer', ['stdout', '10'], {buffer: false});244+ const [result, stdout] = await Promise.all([245+ promise,246+ getStream(promise.stdout)247+ ]);248+249+ t.is(result.stdout, undefined);250+ t.is(stdout, '.........\n');251+});252+253+test('do not buffer stderr when `buffer` set to `false`', async t => {254+ const promise = m('max-buffer', ['stderr', '10'], {buffer: false});255+ const [result, stderr] = await Promise.all([256+ promise,257+ getStream(promise.stderr)258+ ]);259+260+ t.is(result.stderr, undefined);261+ t.is(stderr, '.........\n');262+});263+264 test('skip throwing when using reject option', async t => {265 const err = await m('exit', ['2'], {reject: false});266 t.is(typeof err.stdout, 'string');267 t.is(typeof err.stderr, 'string');111 unmodified lines379 test('err.code is 3', code, 3);380 test('err.code is 4', code, 4);381 382 test('timeout will kill the process early', async t => {361- const err = await t.throws(m('delay', ['3000', '0'], {timeout: 1500}));383+ const err = await t.throws(m('delay', ['60000', '0'], {timeout: 1500}));384 385 t.true(err.timedOut);386 t.not(err.code, 22);387 });161 unmodified lines