Skip to content
Myr

encoding-final.js

This file

Path

lib/stdio/encoding-final.js

56 lines · 1.9 KiB · JavaScript

Last changed by

Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b

History of this file

Take it

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 1–12why, one step back
1import {StringDecoder} from 'node:string_decoder';
2import {isUint8Array} from '../utils.js';3import {willPipeStreams} from './forward.js';45// Apply the `encoding` option using an implicit generator.6// This encodes the final output of `stdout`/`stderr`.7export const handleStreamsEncoding = (stdioStreams, {encoding}, isSync) => {8 const newStdioStreams = stdioStreams.map(stdioStream => ({...stdioStream, encoding}));9 if (!shouldEncodeOutput(newStdioStreams, encoding, isSync)) {10 return newStdioStreams;11 }12

Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b

lines 13–14why, one step back
13 const lastObjectStdioStream = newStdioStreams.findLast(({type, value}) => type === 'generator' && value.objectMode !== undefined);
14 const objectMode = lastObjectStdioStream !== undefined && lastObjectStdioStream.value.objectMode;

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 15–28why, one step back
15 const stringDecoder = new StringDecoder(encoding);
16 const generator = objectMode17 ? {18 transform: encodingObjectGenerator.bind(undefined, stringDecoder),19 }20 : {21 transform: encodingStringGenerator.bind(undefined, stringDecoder),22 final: encodingStringFinal.bind(undefined, stringDecoder),23 };24 return [25 ...newStdioStreams,26 {27 ...newStdioStreams[0],28 type: 'generator',

Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b

line 29why, one step back
29 value: {...generator, binary: true},

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 30–56why, one step back
30 encoding: 'buffer',
31 },32 ];33};3435const shouldEncodeOutput = (stdioStreams, encoding, isSync) => stdioStreams[0].direction === 'output'36 && !IGNORED_ENCODINGS.has(encoding)37 && !isSync38 && willPipeStreams(stdioStreams);3940// eslint-disable-next-line unicorn/text-encoding-identifier-case41const IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);4243const encodingStringGenerator = function * (stringDecoder, chunk) {44 yield stringDecoder.write(chunk);45};4647const encodingStringFinal = function * (stringDecoder) {48 const lastChunk = stringDecoder.end();49 if (lastChunk !== '') {50 yield lastChunk;51 }52};5354const encodingObjectGenerator = function * (stringDecoder, chunk) {55 yield isUint8Array(chunk) ? stringDecoder.end(chunk) : chunk;56};