Skip to content
Myr

encoding-final.js

This file

Path

lib/stdio/encoding-final.js

50 lines · 1.6 KiB · JavaScript

Last changed by

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

History of this file

Take it

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

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

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

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

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 13–17why, one step back
13 const writableObjectMode = lastObjectStdioStream !== undefined && lastObjectStdioStream.value.objectMode;
14 if (writableObjectMode) {15 return newStdioStreams;16 }17

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

lines 18–23why, one step back
18 const stringDecoder = new StringDecoder(encoding);
19 return [20 ...newStdioStreams,21 {22 ...newStdioStreams[0],23 type: 'generator',

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 24–28why, one step back
24 value: {
25 transform: encodingStringGenerator.bind(undefined, stringDecoder),26 final: encodingStringFinal.bind(undefined, stringDecoder),27 binary: true,28 },

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

lines 29–50why, one step back
29 },
30 ];31};3233const shouldEncodeOutput = (stdioStreams, encoding, isSync) => stdioStreams[0].direction === 'output'34 && !IGNORED_ENCODINGS.has(encoding)35 && !isSync36 && willPipeStreams(stdioStreams);3738// eslint-disable-next-line unicorn/text-encoding-identifier-case39const IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);4041const encodingStringGenerator = function * (stringDecoder, chunk) {42 yield stringDecoder.write(chunk);43};4445const encodingStringFinal = function * (stringDecoder) {46 const lastChunk = stringDecoder.end();47 if (lastChunk !== '') {48 yield lastChunk;49 }50};