Skip to content
Myr

encoding-transform.js

This file

Path

lib/transform/encoding-transform.js

66 lines · 2.8 KiB · JavaScript

Last changed by

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–2why, one step back
1import {Buffer} from 'node:buffer';
2import {StringDecoder} from 'node:string_decoder';

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

line 3why, one step back
3import {getStringEncoding} from '../arguments/encoding-option.js';

Rename files (#977)ehmickybefore adoption4b2e316

lines 4–6why, one step back
4import {isUint8Array, bufferToUint8Array} from '../utils/uint-array.js';
56/*

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 7why, one step back
7When using binary encodings, add an internal generator that converts chunks from `Buffer` to `string` or `Uint8Array`.

Rename files (#977)ehmickybefore adoption4b2e316

lines 8–43why, one step back
8Chunks might be Buffer, Uint8Array or strings since:
9- `subprocess.stdout|stderr` emits Buffers10- `subprocess.stdin.write()` accepts Buffer, Uint8Array or string11- 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*/17export const getEncodingTransformGenerator = (binary, encoding, skipped) => {18 if (skipped) {19 return;20 }2122 if (binary) {23 return {transform: encodingUint8ArrayGenerator.bind(undefined, new TextEncoder())};24 }2526 const stringDecoder = new StringDecoder(encoding);27 return {28 transform: encodingStringGenerator.bind(undefined, stringDecoder),29 final: encodingStringFinal.bind(undefined, stringDecoder),30 };31};3233const encodingUint8ArrayGenerator = function * (textEncoder, chunk) {34 if (Buffer.isBuffer(chunk)) {35 yield bufferToUint8Array(chunk);36 } else if (typeof chunk === 'string') {37 yield textEncoder.encode(chunk);38 } else {39 yield chunk;40 }41};4243const encodingStringGenerator = function * (stringDecoder, chunk) {

Allow passing `Buffer` to the `input` option (#1029)ehmickybefore adoption40fdc75

line 44why, one step back
44 yield isUint8Array(chunk) ? stringDecoder.write(chunk) : chunk;

Rename files (#977)ehmickybefore adoption4b2e316

lines 45–51why, one step back
45};
4647const encodingStringFinal = function * (stringDecoder) {48 const lastChunk = stringDecoder.end();49 if (lastChunk !== '') {50 yield lastChunk;51 }

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 52–65why, one step back
52};
5354/*55Once the user's transform has run, its string chunks are converted to bytes: by `Transform.push()` with the asynchronous methods, and by `joinToString()` with the synchronous ones. Both use UTF-8, yet the result is then decoded with the `encoding` option, which would corrupt the text when that option is a different text encoding.56Therefore, those chunks are converted using the `encoding` option beforehand.57With the `binary` transform option, they are converted with UTF-8 even when the `encoding` option is another text encoding. Otherwise, with synchronous methods, the next transform would receive the string itself, while with asynchronous methods it receives those UTF-8 bytes decoded with the `encoding` option.58When the `encoding` option is UTF-8, strings round-trip unchanged, so there is nothing to do.59*/60export const getStringToBytesGenerator = (binary, encoding, skipped) => skipped || encoding === 'utf8'61 ? undefined62 : {transform: stringToBytesGenerator.bind(undefined, getStringEncoding(binary, encoding))};6364const stringToBytesGenerator = function * (stringEncoding, chunk) {65 yield typeof chunk === 'string' ? bufferToUint8Array(Buffer.from(chunk, stringEncoding)) : chunk;

Rename files (#977)ehmickybefore adoption4b2e316

line 66why, one step back
66};