// Apply the `encoding` option using an implicit generator.
// This encodes the final output of `stdout`/`stderr`.
export const handleStreamsEncoding = (stdioStreams, {encoding}, isSync) => {
const newStdioStreams = stdioStreams.map(stdioStream => ({...stdioStream, encoding}));
if (stdioStreams[0].direction === 'input' || IGNORED_ENCODINGS.has(encoding) || isSync) {
return stdioStreams.map(stdioStream => ({...stdioStream, encoding}));
if (newStdioStreams[0].direction === 'input' || IGNORED_ENCODINGS.has(encoding) || isSync) {
return newStdioStreams;
}
const transform = encodingEndGenerator.bind(undefined, encoding);
const objectMode = stdioStreams.findLast(({type}) => type === 'generator')?.value.readableObjectMode === true;
const objectMode = newStdioStreams.findLast(({type}) => type === 'generator')?.value.objectMode === true;
return [
...stdioStreams,
...newStdioStreams,
{
...stdioStreams[0],
...newStdioStreams[0],
type: 'generator',
value: {transform, binary: true, readableObjectMode: objectMode, writableObjectMode: objectMode},
value: {transform, binary: true, objectMode},
encoding: 'buffer',
},
];
};