encoding.js
This file
Path
lib/stdio/encoding.js
103 lines · 3.3 KiB · JavaScript
Last changed by
Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
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;Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 15–23why, one step back15 const stringDecoder = new StringDecoder(encoding);16 const generator = objectMode17 ? {18 transform: encodingEndObjectGenerator.bind(undefined, stringDecoder),19 }20 : {21 transform: encodingEndStringGenerator.bind(undefined, stringDecoder),22 final: encodingEndStringFinal.bind(undefined, stringDecoder),23 };Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 24why, one step back24 return [Refactor `encoding` option (#735)ehmickybefore adoption1907e7c
line 25why, one step back25 ...newStdioStreams,Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 26why, one step back26 {Refactor `encoding` option (#735)ehmickybefore adoption1907e7c
line 27why, one step back27 ...newStdioStreams[0],Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 28why, one step back28 type: 'generator',Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
line 29why, one step back29 value: {...generator, binary: true, objectMode},Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 30–32why, one step back30 encoding: 'buffer',31 },32 ];Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 33why, one step back33};Fix `encoding` option not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169
lines 34–38why, one step back3435const shouldEncodeOutput = (stdioStreams, encoding, isSync) => stdioStreams[0].direction === 'output'36 && !IGNORED_ENCODINGS.has(encoding)37 && !isSync38 && willPipeStreams(stdioStreams);Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 39–42why, one step back3940// eslint-disable-next-line unicorn/text-encoding-identifier-case41const IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);42Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 43–45why, one step back43const encodingEndStringGenerator = function * (stringDecoder, chunk) {44 yield stringDecoder.write(chunk);45};Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 46why, one step back46Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
line 47why, one step back47const encodingEndStringFinal = function * (stringDecoder) {Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 48why, one step back48 const lastChunk = stringDecoder.end();Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 49–50why, one step back49 if (lastChunk !== '') {50 yield lastChunk;Fix using both `objectMode` and `encoding` options (#736)ehmickybefore adoption9f205f6
lines 51–53why, one step back51 }52};53Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 54–55why, one step back54const encodingEndObjectGenerator = function * (stringDecoder, chunk) {55 yield isUint8Array(chunk) ? stringDecoder.end(chunk) : chunk;Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 56–69why, one step back56};5758/*59When using generators, add an internal generator that converts chunks from `Buffer` to `string` or `Uint8Array`.60This allows generator functions to operate with those types instead.61Chunks might be Buffer, Uint8Array or strings since:62- `childProcess.stdout|stderr` emits Buffers63- `childProcess.stdin.write()` accepts Buffer, Uint8Array or string64- Previous generators might return Uint8Array or string6566However, those are converted to Buffer:67- on writes: `Duplex.writable` `decodeStrings: true` default option68- on reads: `Duplex.readable` `readableEncoding: null` default option69*/Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 70–72why, one step back70export const getEncodingStartGenerator = encoding => {71 if (encoding === 'buffer') {72 return {transform: encodingStartBufferGenerator.bind(undefined, new TextEncoder())};Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 73–75why, one step back73 }7475 const textDecoder = new TextDecoder();Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 76–80why, one step back76 return {77 transform: encodingStartStringGenerator.bind(undefined, textDecoder),78 final: encodingStartStringFinal.bind(undefined, textDecoder),79 };80};Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 81why, one step back81Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 82–88why, one step back82const encodingStartBufferGenerator = function * (textEncoder, chunk) {83 if (Buffer.isBuffer(chunk)) {84 yield new Uint8Array(chunk);85 } else if (typeof chunk === 'string') {86 yield textEncoder.encode(chunk);87 } else {88 yield chunk;Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 89why, one step back89 }Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
line 90why, one step back90};Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 91why, one step back91Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 92–98why, one step back92const encodingStartStringGenerator = function * (textDecoder, chunk) {93 yield Buffer.isBuffer(chunk) || isUint8Array(chunk)94 ? textDecoder.decode(chunk, {stream: true})95 : chunk;96};9798const encodingStartStringFinal = function * (textDecoder) {Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 99why, one step back99 const lastChunk = textDecoder.decode();Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 100–103why, one step back100 if (lastChunk !== '') {101 yield lastChunk;102 }103};