|
'use strict'; |
|
const isStream = require('is-stream'); |
|
const getStream = require('get-stream'); |
|
const mergeStream = require('merge-stream'); |
|
|
|
|
|
const handleInput = (spawned, input) => { |
|
|
|
|
|
if (input === undefined || spawned.stdin === undefined) { |
|
return; |
|
} |
|
|
|
if (isStream(input)) { |
|
input.pipe(spawned.stdin); |
|
} else { |
|
spawned.stdin.end(input); |
|
} |
|
}; |
|
|
|
|
|
const makeAllStream = (spawned, {all}) => { |
|
if (!all || (!spawned.stdout && !spawned.stderr)) { |
|
return; |
|
} |
|
|
|
const mixed = mergeStream(); |
|
|
|
if (spawned.stdout) { |
|
mixed.add(spawned.stdout); |
|
} |
|
|
|
if (spawned.stderr) { |
|
mixed.add(spawned.stderr); |
|
} |
|
|
|
return mixed; |
|
}; |
|
|
|
|
|
const getBufferedData = async (stream, streamPromise) => { |
|
if (!stream) { |
|
return; |
|
} |
|
|
|
stream.destroy(); |
|
|
|
try { |
|
return await streamPromise; |
|
} catch (error) { |
|
return error.bufferedData; |
|
} |
|
}; |
|
|
|
const getStreamPromise = (stream, {encoding, buffer, maxBuffer}) => { |
|
if (!stream || !buffer) { |
|
return; |
|
} |
|
|
|
if (encoding) { |
|
return getStream(stream, {encoding, maxBuffer}); |
|
} |
|
|
|
return getStream.buffer(stream, {maxBuffer}); |
|
}; |
|
|
|
|
|
const getSpawnedResult = async ({stdout, stderr, all}, {encoding, buffer, maxBuffer}, processDone) => { |
|
const stdoutPromise = getStreamPromise(stdout, {encoding, buffer, maxBuffer}); |
|
const stderrPromise = getStreamPromise(stderr, {encoding, buffer, maxBuffer}); |
|
const allPromise = getStreamPromise(all, {encoding, buffer, maxBuffer: maxBuffer * 2}); |
|
|
|
try { |
|
return await Promise.all([processDone, stdoutPromise, stderrPromise, allPromise]); |
|
} catch (error) { |
|
return Promise.all([ |
|
{error, signal: error.signal, timedOut: error.timedOut}, |
|
getBufferedData(stdout, stdoutPromise), |
|
getBufferedData(stderr, stderrPromise), |
|
getBufferedData(all, allPromise) |
|
]); |
|
} |
|
}; |
|
|
|
const validateInputSync = ({input}) => { |
|
if (isStream(input)) { |
|
throw new TypeError('The `input` option cannot be a stream in sync mode'); |
|
} |
|
}; |
|
|
|
module.exports = { |
|
handleInput, |
|
makeAllStream, |
|
getSpawnedResult, |
|
validateInputSync |
|
}; |
|
|
|
|