writable.js
This file
Path
lib/convert/writable.js
90 lines · 3.5 KiB · JavaScript
Last changed by
Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
History of this fileTake it
Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
lines 1–2why, one step back1import {Writable} from 'node:stream';2import {callbackify} from 'node:util';Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
line 3why, one step back3import {getToStream} from '../arguments/fd-options.js';Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
lines 4–16why, one step back4import {addConcurrentStream, waitForConcurrentStreams} from './concurrent.js';5import {6 safeWaitForSubprocessStdout,7 waitForSubprocessStdin,8 waitForSubprocess,9 destroyOtherStream,10} from './shared.js';1112// Create a `Writable` stream that forwards to `stdin` and awaits the subprocess13export const createWritable = ({subprocess, concurrentStreams}, {to} = {}) => {14 const {subprocessStdin, waitWritableFinal, waitWritableDestroy} = getSubprocessStdin(subprocess, to, concurrentStreams);15 const writable = new Writable({16 ...getWritableMethods(subprocessStdin, subprocess, waitWritableFinal),Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 17–22why, one step back17 destroy: callbackify(onWritableDestroy.bind(undefined, {18 subprocessStdin,19 subprocess,20 waitWritableFinal,21 waitWritableDestroy,22 })),Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
lines 23–31why, one step back23 highWaterMark: subprocessStdin.writableHighWaterMark,24 objectMode: subprocessStdin.writableObjectMode,25 });26 onStdinFinished(subprocessStdin, writable);27 return writable;28};2930// Retrieve `stdin` (or other stream depending on `to`)31export const getSubprocessStdin = (subprocess, to, concurrentStreams) => {Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
line 32why, one step back32 const subprocessStdin = getToStream(subprocess, to);Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
lines 33–90why, one step back33 const waitWritableFinal = addConcurrentStream(concurrentStreams, subprocessStdin, 'writableFinal');34 const waitWritableDestroy = addConcurrentStream(concurrentStreams, subprocessStdin, 'writableDestroy');35 return {subprocessStdin, waitWritableFinal, waitWritableDestroy};36};3738export const getWritableMethods = (subprocessStdin, subprocess, waitWritableFinal) => ({39 write: onWrite.bind(undefined, subprocessStdin),40 final: callbackify(onWritableFinal.bind(undefined, subprocessStdin, subprocess, waitWritableFinal)),41});4243// Forwards data from `writable` to `stdin`44const onWrite = (subprocessStdin, chunk, encoding, done) => {45 if (subprocessStdin.write(chunk, encoding)) {46 done();47 } else {48 subprocessStdin.once('drain', done);49 }50};5152// Ensures that the writable `final` and readable `end` events awaits the subprocess.53// Like this, any subprocess failure is propagated as a stream `error` event, instead of being lost.54// The user does not need to `await` the subprocess anymore, but now needs to await the stream completion or error.55// When multiple writables are targeting the same stream, they wait for each other, unless the subprocess ends first.56const onWritableFinal = async (subprocessStdin, subprocess, waitWritableFinal) => {57 if (await waitForConcurrentStreams(waitWritableFinal, subprocess)) {58 if (subprocessStdin.writable) {59 subprocessStdin.end();60 }6162 await subprocess;63 }64};6566// When `subprocess.stdin` ends/aborts/errors, do the same on `writable`.67export const onStdinFinished = async (subprocessStdin, writable, subprocessStdout) => {68 try {69 await waitForSubprocessStdin(subprocessStdin);70 if (writable.writable) {71 writable.end();72 }73 } catch (error) {74 await safeWaitForSubprocessStdout(subprocessStdout);75 destroyOtherWritable(writable, error);76 }77};7879// When `writable` aborts/errors, do the same on `subprocess.stdin`80export const onWritableDestroy = async ({subprocessStdin, subprocess, waitWritableFinal, waitWritableDestroy}, error) => {81 await waitForConcurrentStreams(waitWritableFinal, subprocess);82 if (await waitForConcurrentStreams(waitWritableDestroy, subprocess)) {83 destroyOtherWritable(subprocessStdin, error);84 await waitForSubprocess(subprocess, error);85 }86};8788const destroyOtherWritable = (stream, error) => {89 destroyOtherStream(stream, stream.writable, error);90};