Skip to content
Myr

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

3 files+42 added−6 removed

modified:index.js+12 added-5 removed
42 unmodified lines
43 }4445 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 lines
117118 return fn(file, args, opts);119}120120function getStream(process, stream, encoding, maxBuffer) {121function getStream(process, stream, {encoding, buffer, maxBuffer}) {122 if (!process[stream]) {123 return null;124 }125126 let ret;127127 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 lines
196}197198module.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);202203 let spawned;204 try {
61 unmodified lines
266 }267268 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 lines
modified:readme.md+7 added
250 unmodified lines
251Default: `0`252253If 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.254255#### buffer256257Type: `boolean`<br>258Default: `true`259260Buffer 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.261262#### maxBuffer263264Type: `number`<br>265Default: `10000000` (10MB)
62 unmodified lines
modified:test.js+23 added-1 removed
237 unmodified lines
238 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});241242test('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 ]);248249 t.is(result.stdout, undefined);250 t.is(stdout, '.........\n');251});252253test('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 ]);259260 t.is(result.stderr, undefined);261 t.is(stderr, '.........\n');262});263264test('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 lines
379test('err.code is 3', code, 3);380test('err.code is 4', code, 4);381382test('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}));384385 t.true(err.timedOut);386 t.not(err.code, 22);387});
161 unmodified lines