encoding.js
This file
Path
lib/stdio/encoding.js
100 lines · 3.2 KiB · JavaScript
Last changed by
Fix `encoding` option not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169
History of this fileTake it
Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 1why, one step back1import {StringDecoder} from 'node:string_decoder';Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 2–3why, one step back2import {Buffer} from 'node:buffer';3import {isUint8Array} from './utils.js';Fix `encoding` option not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169
line 4why, one step back4import {willPipeStreams} from './pipe.js';Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 5–6why, one step back56// Apply the `encoding` option using an implicit generator.Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 7why, one step back7// This encodes the final output of `stdout`/`stderr`.Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 8why, one step back8export const handleStreamsEncoding = (stdioStreams, {encoding}, isSync) => {Refactor `encoding` option (#735)ehmickybefore adoption1907e7c
line 9why, one step back9 const newStdioStreams = stdioStreams.map(stdioStream => ({...stdioStream, encoding}));Fix `encoding` option not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169
line 10why, one step back10 if (!shouldEncodeOutput(newStdioStreams, encoding, isSync)) {Refactor `encoding` option (#735)ehmickybefore adoption1907e7c
line 11why, one step back11 return newStdioStreams;Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 12–13why, one step back12 }13Refactor `encoding` option (#735)ehmickybefore adoption1907e7c
line 14why, one step back14 const objectMode = newStdioStreams.findLast(({type}) => type === 'generator')?.value.objectMode === true;Fix using both `objectMode` and `encoding` options (#736)ehmickybefore adoption9f205f6
lines 15–16why, one step back15 const encodingEndGenerator = objectMode ? encodingEndObjectGenerator : encodingEndStringGenerator;16 const transform = encodingEndGenerator.bind(undefined, encoding);Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 17why, one step back17 return [Refactor `encoding` option (#735)ehmickybefore adoption1907e7c
line 18why, one step back18 ...newStdioStreams,Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 19why, one step back19 {Refactor `encoding` option (#735)ehmickybefore adoption1907e7c
line 20why, one step back20 ...newStdioStreams[0],Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 21why, one step back21 type: 'generator',Refactor `encoding` option (#735)ehmickybefore adoption1907e7c
line 22why, one step back22 value: {transform, binary: true, objectMode},Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 23–25why, one step back23 encoding: 'buffer',24 },25 ];Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 26why, one step back26};Fix `encoding` option not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169
lines 27–31why, one step back2728const shouldEncodeOutput = (stdioStreams, encoding, isSync) => stdioStreams[0].direction === 'output'29 && !IGNORED_ENCODINGS.has(encoding)30 && !isSync31 && willPipeStreams(stdioStreams);Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 32–35why, one step back3233// eslint-disable-next-line unicorn/text-encoding-identifier-case34const IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);35Fix using both `objectMode` and `encoding` options (#736)ehmickybefore adoption9f205f6
line 36why, one step back36const encodingEndStringGenerator = async function * (encoding, chunks) {Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 37–43why, one step back37 const stringDecoder = new StringDecoder(encoding);3839 for await (const chunk of chunks) {40 yield stringDecoder.write(chunk);41 }4243 const lastChunk = stringDecoder.end();Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 44–45why, one step back44 if (lastChunk !== '') {45 yield lastChunk;Fix using both `objectMode` and `encoding` options (#736)ehmickybefore adoption9f205f6
lines 46–53why, one step back46 }47};4849const encodingEndObjectGenerator = async function * (encoding, chunks) {50 const stringDecoder = new StringDecoder(encoding);5152 for await (const chunk of chunks) {53 yield isUint8Array(chunk) ? stringDecoder.end(chunk) : chunk;Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 54–73why, one step back54 }55};5657/*58When using generators, add an internal generator that converts chunks from `Buffer` to `string` or `Uint8Array`.59This allows generator functions to operate with those types instead.60Chunks might be Buffer, Uint8Array or strings since:61- `childProcess.stdout|stderr` emits Buffers62- `childProcess.stdin.write()` accepts Buffer, Uint8Array or string63- Previous generators might return Uint8Array or string6465However, those are converted to Buffer:66- on writes: `Duplex.writable` `decodeStrings: true` default option67- on reads: `Duplex.readable` `readableEncoding: null` default option68*/69export const getEncodingStartGenerator = encoding => encoding === 'buffer'70 ? encodingStartBufferGenerator71 : encodingStartStringGenerator;7273const encodingStartBufferGenerator = async function * (chunks) {Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 74–75why, one step back74 const textEncoder = new TextEncoder();75Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 76why, one step back76 for await (const chunk of chunks) {Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 77–83why, one step back77 if (Buffer.isBuffer(chunk)) {78 yield new Uint8Array(chunk);79 } else if (typeof chunk === 'string') {80 yield textEncoder.encode(chunk);81 } else {82 yield chunk;83 }Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 84–90why, one step back84 }85};8687const encodingStartStringGenerator = async function * (chunks) {88 const textDecoder = new TextDecoder();8990 for await (const chunk of chunks) {Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 91–93why, one step back91 yield Buffer.isBuffer(chunk) || isUint8Array(chunk)92 ? textDecoder.decode(chunk, {stream: true})93 : chunk;Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 94–96why, one step back94 }9596 const lastChunk = textDecoder.decode();Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 97–100why, one step back97 if (lastChunk !== '') {98 yield lastChunk;99 }100};