Skip to content
Myr

encoding-transform.js

This file

Path

lib/stdio/encoding-transform.js

54 lines · 1.7 KiB · JavaScript

Last changed by

Improve `encoding` option (#958)ehmickybefore adoption507b09c

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';

Improve `encoding` option (#958)ehmickybefore adoption507b09c

lines 2–3why, one step back
2import {StringDecoder} from 'node:string_decoder';
3import {isUint8Array, bufferToUint8Array} from './uint-array.js';

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

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

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

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

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

lines 11–16why, one step back
11- Previous generators might return Uint8Array or string
1213However, those are converted to Buffer:14- on writes: `Duplex.writable` `decodeStrings: true` default option15- on reads: `Duplex.readable` `readableEncoding: null` default option16*/

Improve `encoding` option (#958)ehmickybefore adoption507b09c

lines 17–18why, one step back
17export const getEncodingTransformGenerator = (binary, encoding, skipped) => {
18 if (skipped) {

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

lines 19–21why, one step back
19 return;
20 }21

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

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

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

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

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

lines 24–25why, one step back
24 }
25

Improve `encoding` option (#958)ehmickybefore adoption507b09c

line 26why, one step back
26 const stringDecoder = new StringDecoder(encoding);

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

line 27why, one step back
27 return {

Improve `encoding` option (#958)ehmickybefore adoption507b09c

lines 28–29why, one step back
28 transform: encodingStringGenerator.bind(undefined, stringDecoder),
29 final: encodingStringFinal.bind(undefined, stringDecoder),

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

lines 30–32why, one step back
30 };
31};32

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

lines 33–34why, one step back
33const encodingUint8ArrayGenerator = function * (textEncoder, chunk) {
34 if (Buffer.isBuffer(chunk)) {

Improve `encoding` option (#958)ehmickybefore adoption507b09c

line 35why, one step back
35 yield bufferToUint8Array(chunk);

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

lines 36–40why, one step back
36 } else if (typeof chunk === 'string') {
37 yield textEncoder.encode(chunk);38 } else {39 yield chunk;40 }

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

lines 41–42why, one step back
41};
42

Improve `encoding` option (#958)ehmickybefore adoption507b09c

line 43why, one step back
43const encodingStringGenerator = function * (stringDecoder, chunk) {

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

line 44why, one step back
44 yield Buffer.isBuffer(chunk) || isUint8Array(chunk)

Improve `encoding` option (#958)ehmickybefore adoption507b09c

line 45why, one step back
45 ? stringDecoder.write(chunk)

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

line 46why, one step back
46 : chunk;

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

lines 47–48why, one step back
47};
48

Improve `encoding` option (#958)ehmickybefore adoption507b09c

lines 49–50why, one step back
49const encodingStringFinal = function * (stringDecoder) {
50 const lastChunk = stringDecoder.end();

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

lines 51–54why, one step back
51 if (lastChunk !== '') {
52 yield lastChunk;53 }54};