Skip to content
Myr

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 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 not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169

line 4why, one step back
4import {willPipeStreams} from './pipe.js';

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

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

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

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

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

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

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

line 9why, one step back
9 const newStdioStreams = stdioStreams.map(stdioStream => ({...stdioStream, encoding}));

Fix `encoding` option not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169

line 10why, one step back
10 if (!shouldEncodeOutput(newStdioStreams, encoding, isSync)) {

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

line 11why, one step back
11 return newStdioStreams;

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

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

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

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

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 15–23why, one step back
15 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 back
24 return [

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

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

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 26why, one step back
26 {

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

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

Line-wise transforms (#699)ehmickybefore adoption6dd804f

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

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

line 29why, one step back
29 value: {...generator, binary: true, objectMode},

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 30–32why, one step back
30 encoding: 'buffer',
31 },32 ];

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 33why, one step back
33};

Fix `encoding` option not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169

lines 34–38why, one step back
34
35const 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 back
39
40// eslint-disable-next-line unicorn/text-encoding-identifier-case41const IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);42

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 43–45why, one step back
43const encodingEndStringGenerator = function * (stringDecoder, chunk) {
44 yield stringDecoder.write(chunk);45};

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 46why, one step back
46

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

line 47why, one step back
47const encodingEndStringFinal = function * (stringDecoder) {

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 48why, one step back
48 const lastChunk = stringDecoder.end();

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

lines 49–50why, one step back
49 if (lastChunk !== '') {
50 yield lastChunk;

Fix using both `objectMode` and `encoding` options (#736)ehmickybefore adoption9f205f6

lines 51–53why, one step back
51 }
52};53

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 54–55why, one step back
54const encodingEndObjectGenerator = function * (stringDecoder, chunk) {
55 yield isUint8Array(chunk) ? stringDecoder.end(chunk) : chunk;

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

lines 56–69why, one step back
56};
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 back
70export 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 back
73 }
7475 const textDecoder = new TextDecoder();

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 76–80why, one step back
76 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 back
81

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 82–88why, one step back
82const 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 back
89 }

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

line 90why, one step back
90};

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

line 91why, one step back
91

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 92–98why, one step back
92const 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 back
99 const lastChunk = textDecoder.decode();

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

lines 100–103why, one step back
100 if (lastChunk !== '') {
101 yield lastChunk;102 }103};