encoding.js
This file
Path
lib/stdio/encoding.js
84 lines · 2.6 KiB · JavaScript
Last changed by
Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
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 (#696)ehmickybefore adoptionb6f9fa2
lines 4–5why, one step back45// Apply the `encoding` option using an implicit generator.Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 6why, one step back6// This encodes the final output of `stdout`/`stderr`.Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 7–11why, one step back7export const handleStreamsEncoding = (stdioStreams, {encoding}, isSync) => {8 if (stdioStreams[0].direction === 'input' || IGNORED_ENCODINGS.has(encoding) || isSync) {9 return stdioStreams.map(stdioStream => ({...stdioStream, encoding}));10 }11Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 12why, one step back12 const transform = encodingEndGenerator.bind(undefined, encoding);Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 13why, one step back13 const objectMode = stdioStreams.findLast(({type}) => type === 'generator')?.value.readableObjectMode === true;Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 14–18why, one step back14 return [15 ...stdioStreams,16 {17 ...stdioStreams[0],18 type: 'generator',Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 19why, one step back19 value: {transform, binary: true, readableObjectMode: objectMode, writableObjectMode: objectMode},Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 20–22why, one step back20 encoding: 'buffer',21 },22 ];Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 23–35why, one step back23};2425// eslint-disable-next-line unicorn/text-encoding-identifier-case26const IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);2728const encodingEndGenerator = async function * (encoding, chunks) {29 const stringDecoder = new StringDecoder(encoding);3031 for await (const chunk of chunks) {32 yield stringDecoder.write(chunk);33 }3435 const lastChunk = stringDecoder.end();Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 36–57why, one step back36 if (lastChunk !== '') {37 yield lastChunk;38 }39};4041/*42When using generators, add an internal generator that converts chunks from `Buffer` to `string` or `Uint8Array`.43This allows generator functions to operate with those types instead.44Chunks might be Buffer, Uint8Array or strings since:45- `childProcess.stdout|stderr` emits Buffers46- `childProcess.stdin.write()` accepts Buffer, Uint8Array or string47- Previous generators might return Uint8Array or string4849However, those are converted to Buffer:50- on writes: `Duplex.writable` `decodeStrings: true` default option51- on reads: `Duplex.readable` `readableEncoding: null` default option52*/53export const getEncodingStartGenerator = encoding => encoding === 'buffer'54 ? encodingStartBufferGenerator55 : encodingStartStringGenerator;5657const encodingStartBufferGenerator = async function * (chunks) {Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 58–59why, one step back58 const textEncoder = new TextEncoder();59Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 60why, one step back60 for await (const chunk of chunks) {Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 61–67why, one step back61 if (Buffer.isBuffer(chunk)) {62 yield new Uint8Array(chunk);63 } else if (typeof chunk === 'string') {64 yield textEncoder.encode(chunk);65 } else {66 yield chunk;67 }Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 68–74why, one step back68 }69};7071const encodingStartStringGenerator = async function * (chunks) {72 const textDecoder = new TextDecoder();7374 for await (const chunk of chunks) {Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 75–77why, one step back75 yield Buffer.isBuffer(chunk) || isUint8Array(chunk)76 ? textDecoder.decode(chunk, {stream: true})77 : chunk;Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 78–80why, one step back78 }7980 const lastChunk = textDecoder.decode();Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 81–84why, one step back81 if (lastChunk !== '') {82 yield lastChunk;83 }84};