encoding-transform.js
This file
Path
lib/stdio/encoding-transform.js
53 lines · 1.6 KiB · JavaScript
Last changed by
Fix `lines` option (#957)ehmickybefore adoption15ab4e6
History of this fileTake it
Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 1why, one step back1import {Buffer} from 'node:buffer';Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 2why, one step back2import {isUint8Array} from './uint-array.js';Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 3–7why, one step back34/*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 back8- `subprocess.stdout|stderr` emits Buffers9- `subprocess.stdin.write()` accepts Buffer, Uint8Array or stringRename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 10–15why, one step back10- Previous generators might return Uint8Array or string1112However, those are converted to Buffer:13- on writes: `Duplex.writable` `decodeStrings: true` default option14- on reads: `Duplex.readable` `readableEncoding: null` default option15*/Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 16–17why, one step back16export const getEncodingTransformGenerator = (binary, writableObjectMode) => {17 if (writableObjectMode) {Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 18–20why, one step back18 return;19 }20Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 21why, one step back21 if (binary) {Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 22why, one step back22 return {transform: encodingUint8ArrayGenerator.bind(undefined, new TextEncoder())};Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 23–31why, one step back23 }2425 const textDecoder = new TextDecoder();26 return {27 transform: encodingStringGenerator.bind(undefined, textDecoder),28 final: encodingStringFinal.bind(undefined, textDecoder),29 };30};31Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 32–39why, one step back32const 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 back40};4142const encodingStringGenerator = function * (textDecoder, chunk) {Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 43–45why, one step back43 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 back46};4748const encodingStringFinal = function * (textDecoder) {49 const lastChunk = textDecoder.decode();50 if (lastChunk !== '') {51 yield lastChunk;52 }53};