encoding.js
This file
Path
lib/stdio/encoding.js
27 lines · 936 bytes · JavaScript
Last changed by
Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
History of this fileTake it
Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 1–27why, one step back1import {StringDecoder} from 'node:string_decoder';23// Apply the `encoding` option using an implicit generator.4export const handleStreamsEncoding = (stdioStreams, {encoding}, isSync) => {5 if (stdioStreams[0].direction === 'input' || IGNORED_ENCODINGS.has(encoding) || isSync) {6 return stdioStreams.map(stdioStream => ({...stdioStream, encoding}));7 }89 const value = encodingEndGenerator.bind(undefined, encoding);10 return [...stdioStreams, {...stdioStreams[0], type: 'generator', value, encoding: 'buffer'}];11};1213// eslint-disable-next-line unicorn/text-encoding-identifier-case14const IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);1516const encodingEndGenerator = async function * (encoding, chunks) {17 const stringDecoder = new StringDecoder(encoding);1819 for await (const chunk of chunks) {20 yield stringDecoder.write(chunk);21 }2223 const lastChunk = stringDecoder.end();24 if (lastChunk !== '') {25 yield lastChunk;26 }27};