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 fileTake it
Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 1why, one step back1import {Buffer} from 'node:buffer';Improve `encoding` option (#958)ehmickybefore adoption507b09c
lines 2–3why, one step back2import {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 back45/*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 back9- `subprocess.stdout|stderr` emits Buffers10- `subprocess.stdin.write()` accepts Buffer, Uint8Array or stringRename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 11–16why, one step back11- Previous generators might return Uint8Array or string1213However, 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 back17export const getEncodingTransformGenerator = (binary, encoding, skipped) => {18 if (skipped) {Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 19–21why, one step back19 return;20 }21Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 22why, one step back22 if (binary) {Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 23why, one step back23 return {transform: encodingUint8ArrayGenerator.bind(undefined, new TextEncoder())};Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 24–25why, one step back24 }25Improve `encoding` option (#958)ehmickybefore adoption507b09c
line 26why, one step back26 const stringDecoder = new StringDecoder(encoding);Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 27why, one step back27 return {Improve `encoding` option (#958)ehmickybefore adoption507b09c
lines 28–29why, one step back28 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 back30 };31};32Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 33–34why, one step back33const encodingUint8ArrayGenerator = function * (textEncoder, chunk) {34 if (Buffer.isBuffer(chunk)) {Improve `encoding` option (#958)ehmickybefore adoption507b09c
line 35why, one step back35 yield bufferToUint8Array(chunk);Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 36–40why, one step back36 } 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 back41};42Improve `encoding` option (#958)ehmickybefore adoption507b09c
line 43why, one step back43const encodingStringGenerator = function * (stringDecoder, chunk) {Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 44why, one step back44 yield Buffer.isBuffer(chunk) || isUint8Array(chunk)Improve `encoding` option (#958)ehmickybefore adoption507b09c
line 45why, one step back45 ? stringDecoder.write(chunk)Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 46why, one step back46 : chunk;Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 47–48why, one step back47};48Improve `encoding` option (#958)ehmickybefore adoption507b09c
lines 49–50why, one step back49const encodingStringFinal = function * (stringDecoder) {50 const lastChunk = stringDecoder.end();Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 51–54why, one step back51 if (lastChunk !== '') {52 yield lastChunk;53 }54};