Skip to content
Myr

encoding.js

This file

Path

lib/stdio/encoding.js

100 lines · 3.2 KiB · JavaScript

Last changed by

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

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;

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

lines 15–16why, one step back
15 const encodingEndGenerator = objectMode ? encodingEndObjectGenerator : encodingEndStringGenerator;
16 const transform = encodingEndGenerator.bind(undefined, encoding);

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 17why, one step back
17 return [

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

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

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 19why, one step back
19 {

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

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

Line-wise transforms (#699)ehmickybefore adoption6dd804f

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

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

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

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 23–25why, one step back
23 encoding: 'buffer',
24 },25 ];

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 26why, one step back
26};

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

lines 27–31why, one step back
27
28const shouldEncodeOutput = (stdioStreams, encoding, isSync) => stdioStreams[0].direction === 'output'29 && !IGNORED_ENCODINGS.has(encoding)30 && !isSync31 && willPipeStreams(stdioStreams);

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

lines 32–35why, one step back
32
33// eslint-disable-next-line unicorn/text-encoding-identifier-case34const IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);35

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

line 36why, one step back
36const encodingEndStringGenerator = async function * (encoding, chunks) {

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

lines 37–43why, one step back
37 const stringDecoder = new StringDecoder(encoding);
3839 for await (const chunk of chunks) {40 yield stringDecoder.write(chunk);41 }4243 const lastChunk = stringDecoder.end();

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

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

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

lines 46–53why, one step back
46 }
47};4849const encodingEndObjectGenerator = async function * (encoding, chunks) {50 const stringDecoder = new StringDecoder(encoding);5152 for await (const chunk of chunks) {53 yield isUint8Array(chunk) ? stringDecoder.end(chunk) : chunk;

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

lines 54–73why, one step back
54 }
55};5657/*58When using generators, add an internal generator that converts chunks from `Buffer` to `string` or `Uint8Array`.59This allows generator functions to operate with those types instead.60Chunks might be Buffer, Uint8Array or strings since:61- `childProcess.stdout|stderr` emits Buffers62- `childProcess.stdin.write()` accepts Buffer, Uint8Array or string63- Previous generators might return Uint8Array or string6465However, those are converted to Buffer:66- on writes: `Duplex.writable` `decodeStrings: true` default option67- on reads: `Duplex.readable` `readableEncoding: null` default option68*/69export const getEncodingStartGenerator = encoding => encoding === 'buffer'70 ? encodingStartBufferGenerator71 : encodingStartStringGenerator;7273const encodingStartBufferGenerator = async function * (chunks) {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 74–75why, one step back
74 const textEncoder = new TextEncoder();
75

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

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

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 77–83why, one step back
77 if (Buffer.isBuffer(chunk)) {
78 yield new Uint8Array(chunk);79 } else if (typeof chunk === 'string') {80 yield textEncoder.encode(chunk);81 } else {82 yield chunk;83 }

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

lines 84–90why, one step back
84 }
85};8687const encodingStartStringGenerator = async function * (chunks) {88 const textDecoder = new TextDecoder();8990 for await (const chunk of chunks) {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

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

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

lines 94–96why, one step back
94 }
9596 const lastChunk = textDecoder.decode();

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

lines 97–100why, one step back
97 if (lastChunk !== '') {
98 yield lastChunk;99 }100};