Skip to content
Myr

encoding-transform.js

This file

Path

lib/stdio/encoding-transform.js

44 lines · 1.4 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

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

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

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

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

lines 9–14why, one step back
9- Previous generators might return Uint8Array or string
1011However, those are converted to Buffer:12- on writes: `Duplex.writable` `decodeStrings: true` default option13- on reads: `Duplex.readable` `readableEncoding: null` default option14*/

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

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

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

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

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

lines 20–21why, one step back
20 if (binary) {
21 return {transform: encodingUint8ArrayGenerator};

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

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

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

line 31why, one step back
31const encodingUint8ArrayGenerator = function * (chunk) {

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

line 32why, one step back
32 yield Buffer.isBuffer(chunk) ? new Uint8Array(chunk) : chunk;

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

lines 33–35why, one step back
33};
3435const encodingStringGenerator = function * (textDecoder, chunk) {

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

line 36why, one step back
36 yield Buffer.isBuffer(chunk) ? textDecoder.decode(chunk, {stream: true}) : chunk;

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

lines 37–44why, one step back
37};
3839const encodingStringFinal = function * (textDecoder) {40 const lastChunk = textDecoder.decode();41 if (lastChunk !== '') {42 yield lastChunk;43 }44};