Skip to content
Myr

encoding-transform.js

This file

Path

lib/stdio/encoding-transform.js

53 lines · 1.6 KiB · JavaScript

Last changed by

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

History of this file

Take it

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

line 1why, one step back
1import {Buffer} from 'node:buffer';

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 2why, one step back
2import {isUint8Array} from './uint-array.js';

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 3–7why, one step back
3
4/*5When using generators, add an internal generator that converts chunks from `Buffer` to `string` or `Uint8Array`.6This allows generator functions to operate with those types instead.7Chunks might be Buffer, Uint8Array or strings since:

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 8–9why, one step back
8- `subprocess.stdout|stderr` emits Buffers
9- `subprocess.stdin.write()` accepts Buffer, Uint8Array or string

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 10–15why, one step back
10- Previous generators might return Uint8Array or string
1112However, those are converted to Buffer:13- on writes: `Duplex.writable` `decodeStrings: true` default option14- on reads: `Duplex.readable` `readableEncoding: null` default option15*/

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 16why, one step back
16export const getEncodingTransformGenerator = (binary, writableObjectMode, forceEncoding) => {

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 17–20why, one step back
17 if (writableObjectMode && !forceEncoding) {
18 return;19 }20

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 21why, one step back
21 if (binary) {

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 22why, one step back
22 return {transform: encodingUint8ArrayGenerator.bind(undefined, new TextEncoder())};

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 23–31why, one step back
23 }
2425 const textDecoder = new TextDecoder();26 return {27 transform: encodingStringGenerator.bind(undefined, textDecoder),28 final: encodingStringFinal.bind(undefined, textDecoder),29 };30};31

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 32–39why, one step back
32const encodingUint8ArrayGenerator = function * (textEncoder, chunk) {
33 if (Buffer.isBuffer(chunk)) {34 yield new Uint8Array(chunk);35 } else if (typeof chunk === 'string') {36 yield textEncoder.encode(chunk);37 } else {38 yield chunk;39 }

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 40–42why, one step back
40};
4142const encodingStringGenerator = function * (textDecoder, chunk) {

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

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

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 46–53why, one step back
46};
4748const encodingStringFinal = function * (textDecoder) {49 const lastChunk = textDecoder.decode();50 if (lastChunk !== '') {51 yield lastChunk;52 }53};