Skip to content
Myr

encoding.js

This file

Path

lib/stdio/encoding.js

85 lines · 2.6 KiB · JavaScript

Last changed by

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

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

line 7why, one step back
7export const handleStreamsEncoding = (stdioStreams, {encoding}, isSync) => {

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

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

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

lines 11–12why, one step back
11 }
12

Line-wise transforms (#699)ehmickybefore adoption6dd804f

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

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

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

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 15why, one step back
15 return [

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

line 16why, one step back
16 ...newStdioStreams,

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 17why, one step back
17 {

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

line 18why, one step back
18 ...newStdioStreams[0],

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 19why, one step back
19 type: 'generator',

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

line 20why, one step back
20 value: {transform, binary: true, objectMode},

Line-wise transforms (#699)ehmickybefore adoption6dd804f

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

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

lines 24–36why, one step back
24};
2526// eslint-disable-next-line unicorn/text-encoding-identifier-case27const IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);2829const encodingEndGenerator = async function * (encoding, chunks) {30 const stringDecoder = new StringDecoder(encoding);3132 for await (const chunk of chunks) {33 yield stringDecoder.write(chunk);34 }3536 const lastChunk = stringDecoder.end();

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

lines 37–58why, one step back
37 if (lastChunk !== '') {
38 yield lastChunk;39 }40};4142/*43When using generators, add an internal generator that converts chunks from `Buffer` to `string` or `Uint8Array`.44This allows generator functions to operate with those types instead.45Chunks might be Buffer, Uint8Array or strings since:46- `childProcess.stdout|stderr` emits Buffers47- `childProcess.stdin.write()` accepts Buffer, Uint8Array or string48- Previous generators might return Uint8Array or string4950However, those are converted to Buffer:51- on writes: `Duplex.writable` `decodeStrings: true` default option52- on reads: `Duplex.readable` `readableEncoding: null` default option53*/54export const getEncodingStartGenerator = encoding => encoding === 'buffer'55 ? encodingStartBufferGenerator56 : encodingStartStringGenerator;5758const encodingStartBufferGenerator = async function * (chunks) {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 59–60why, one step back
59 const textEncoder = new TextEncoder();
60

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

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

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 62–68why, one step back
62 if (Buffer.isBuffer(chunk)) {
63 yield new Uint8Array(chunk);64 } else if (typeof chunk === 'string') {65 yield textEncoder.encode(chunk);66 } else {67 yield chunk;68 }

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

lines 69–75why, one step back
69 }
70};7172const encodingStartStringGenerator = async function * (chunks) {73 const textDecoder = new TextDecoder();7475 for await (const chunk of chunks) {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 76–78why, one step back
76 yield Buffer.isBuffer(chunk) || isUint8Array(chunk)
77 ? textDecoder.decode(chunk, {stream: true})78 : chunk;

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

lines 79–81why, one step back
79 }
8081 const lastChunk = textDecoder.decode();

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

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