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 fileTake it
Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 1–6why, one step back1import {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 back7- `subprocess.stdout|stderr` emits Buffers8- `subprocess.stdin.write()` accepts Buffer, Uint8Array or stringRename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 9–14why, one step back9- Previous generators might return Uint8Array or string1011However, 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 back15export const getEncodingTransformGenerator = (binary, writableObjectMode, forceEncoding) => {Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 16–19why, one step back16 if (writableObjectMode && !forceEncoding) {17 return;18 }19Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
lines 20–21why, one step back20 if (binary) {21 return {transform: encodingUint8ArrayGenerator};Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 22–30why, one step back22 }2324 const textDecoder = new TextDecoder();25 return {26 transform: encodingStringGenerator.bind(undefined, textDecoder),27 final: encodingStringFinal.bind(undefined, textDecoder),28 };29};30Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 31why, one step back31const encodingUint8ArrayGenerator = function * (chunk) {Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
line 32why, one step back32 yield Buffer.isBuffer(chunk) ? new Uint8Array(chunk) : chunk;Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 33–35why, one step back33};3435const encodingStringGenerator = function * (textDecoder, chunk) {Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
line 36why, one step back36 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 back37};3839const encodingStringFinal = function * (textDecoder) {40 const lastChunk = textDecoder.decode();41 if (lastChunk !== '') {42 yield lastChunk;43 }44};