Skip to content
Myr

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 file

Take it

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 1why, one step back
1import {StringDecoder} from 'node:string_decoder';

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 2–3why, one step back
2import {Buffer} from 'node:buffer';
3import {isUint8Array} from './utils.js';

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

lines 4–5why, one step back
4
5// Apply the `encoding` option using an implicit generator.

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

line 6why, one step back
6// This encodes the final output of `stdout`/`stderr`.

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

lines 7–11why, one step back
7export const handleStreamsEncoding = (stdioStreams, {encoding}, isSync) => {
8 if (stdioStreams[0].direction === 'input' || IGNORED_ENCODINGS.has(encoding) || isSync) {9 return stdioStreams.map(stdioStream => ({...stdioStream, encoding}));10 }11

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 12why, one step back
12 const transform = encodingEndGenerator.bind(undefined, encoding);

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 13why, one step back
13 const objectMode = stdioStreams.findLast(({type}) => type === 'generator')?.value.readableObjectMode === true;

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 14–18why, one step back
14 return [
15 ...stdioStreams,16 {17 ...stdioStreams[0],18 type: 'generator',

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 19why, one step back
19 value: {transform, binary: true, readableObjectMode: objectMode, writableObjectMode: objectMode},

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 20–22why, one step back
20 encoding: 'buffer',
21 },22 ];

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

lines 23–35why, one step back
23};
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 back
36 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 back
58 const textEncoder = new TextEncoder();
59

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

line 60why, one step back
60 for await (const chunk of chunks) {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 61–67why, one step back
61 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 back
68 }
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 back
75 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 back
78 }
7980 const lastChunk = textDecoder.decode();

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

lines 81–84why, one step back
81 if (lastChunk !== '') {
82 yield lastChunk;83 }84};