Skip to content

Commit

Permalink
Refactor encoding option
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 21, 2024
1 parent cf36c1d commit 8ac3eb9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
31 changes: 16 additions & 15 deletions lib/stdio/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import {isUint8Array} from './utils.js';
// Apply the `encoding` option using an implicit generator.
// This encodes the final output of `stdout`/`stderr`.
export const handleStreamsEncoding = (stdioStreams, {encoding}, isSync) => {
if (stdioStreams[0].direction === 'input' || IGNORED_ENCODINGS.has(encoding) || isSync) {
return stdioStreams.map(stdioStream => ({...stdioStream, encoding}));
}

const transform = encodingEndGenerator.bind(undefined, encoding);
const objectMode = stdioStreams.findLast(({type}) => type === 'generator')?.value.readableObjectMode === true;
return [
...stdioStreams,
{
...stdioStreams[0],
type: 'generator',
value: {transform, binary: true, readableObjectMode: objectMode, writableObjectMode: objectMode},
encoding: 'buffer',
},
];
const newStdioStreams = stdioStreams.map(stdioStream => ({...stdioStream, encoding}));
return newStdioStreams[0].direction === 'input' || IGNORED_ENCODINGS.has(encoding) || isSync
? newStdioStreams
: [
...newStdioStreams,
{
...newStdioStreams[0],
type: 'generator',
value: {
transform: encodingEndGenerator.bind(undefined, encoding),
binary: true,
objectMode: 'previous',
},
encoding: 'buffer',
},
];
};

// eslint-disable-next-line unicorn/text-encoding-identifier-case
Expand Down
2 changes: 1 addition & 1 deletion lib/stdio/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The same applies to the first output's generator's `writableObjectMode`.
*/
const getOutputObjectModes = (objectMode, index, newGenerators) => {
const writableObjectMode = index !== 0 && newGenerators[index - 1].value.readableObjectMode;
const readableObjectMode = objectMode;
const readableObjectMode = objectMode === 'previous' ? writableObjectMode : objectMode;
return {writableObjectMode, readableObjectMode};
};

Expand Down
2 changes: 1 addition & 1 deletion lib/stdio/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const handleInput = (addProperties, options, isSync) => {
const stdioStreamsGroups = [[...stdinStreams, ...handleInputOptions(options)], ...otherStreamsGroups]
.map(stdioStreams => validateStreams(stdioStreams))
.map(stdioStreams => addStreamDirection(stdioStreams))
.map(stdioStreams => normalizeGenerators(stdioStreams))
.map(stdioStreams => handleStreamsEncoding(stdioStreams, options, isSync))
.map(stdioStreams => normalizeGenerators(stdioStreams))
.map(stdioStreams => addStreamsProperties(stdioStreams, addProperties));
options.stdio = transformStdio(stdioStreamsGroups);
return stdioStreamsGroups;
Expand Down

0 comments on commit 8ac3eb9

Please # to comment.