duplex.js
This file
Path
lib/convert/duplex.js
73 lines · 2.4 KiB · JavaScript
Last changed by
Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–3why, one step back1import {Duplex} from 'node:stream';2import {callbackify} from 'node:util';3import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 4why, one step back4import {copyOptions, checkBooleanOption} from '../utils/options.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 5–19why, one step back5import {6 getSubprocessStdout,7 getReadableOptions,8 getReadableMethods,9 onStdoutFinished,10 onReadableDestroy,11} from './readable.js';12import {13 getSubprocessStdin,14 getWritableMethods,15 onStdinFinished,16 onWritableDestroy,17} from './writable.js';1819// Create a `Duplex` stream combining both `subprocess.readable()` and `subprocess.writable()`Harden some thingsSindre Sorhusbefore adoption4a0a121
line 20why, one step back20export const createDuplex = ({subprocess, concurrentStreams, encoding}, options) => {TweaksSindre Sorhusbefore adoption698a1b8
line 21why, one step back21 const {from, to, binary: binaryOption = true, preserveNewlines = true} = copyOptions(options);Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
lines 22–23why, one step back22 checkBooleanOption(binaryOption, 'binary');23 checkBooleanOption(preserveNewlines, 'preserveNewlines');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 24–58why, one step back24 const binary = binaryOption || BINARY_ENCODINGS.has(encoding);25 const {subprocessStdout, waitReadableDestroy} = getSubprocessStdout(subprocess, from, concurrentStreams);26 const {subprocessStdin, waitWritableFinal, waitWritableDestroy} = getSubprocessStdin(subprocess, to, concurrentStreams);27 const {readableEncoding, readableObjectMode, readableHighWaterMark} = getReadableOptions(subprocessStdout, binary);28 const {read, onStdoutDataDone} = getReadableMethods({29 subprocessStdout,30 subprocess,31 binary,32 encoding,33 preserveNewlines,34 });35 const duplex = new Duplex({36 read,37 ...getWritableMethods(subprocessStdin, subprocess, waitWritableFinal),38 destroy: callbackify(onDuplexDestroy.bind(undefined, {39 subprocessStdout,40 subprocessStdin,41 subprocess,42 waitReadableDestroy,43 waitWritableFinal,44 waitWritableDestroy,45 })),46 readableHighWaterMark,47 writableHighWaterMark: subprocessStdin.writableHighWaterMark,48 readableObjectMode,49 writableObjectMode: subprocessStdin.writableObjectMode,50 encoding: readableEncoding,51 });52 onStdoutFinished({53 subprocessStdout,54 onStdoutDataDone,55 readable: duplex,56 subprocess,57 subprocessStdin,58 });Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 59why, one step back59 onStdinFinished(subprocessStdin, duplex, subprocessStdout, subprocess);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 60–73why, one step back60 return duplex;61};6263const onDuplexDestroy = async ({subprocessStdout, subprocessStdin, subprocess, waitReadableDestroy, waitWritableFinal, waitWritableDestroy}, error) => {64 await Promise.all([65 onReadableDestroy({subprocessStdout, subprocess, waitReadableDestroy}, error),66 onWritableDestroy({67 subprocessStdin,68 subprocess,69 waitWritableFinal,70 waitWritableDestroy,71 }, error),72 ]);73};