writable.js
This file
Path
lib/convert/writable.js
85 lines · 3.5 KiB · JavaScript
Last changed by
Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
History of this fileTake it
Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
lines 1–85why, one step back1import {Writable} from 'node:stream';2import {callbackify} from 'node:util';3import {getWritable} from '../pipe/validate.js';4import {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),17 destroy: callbackify(onWritableDestroy.bind(undefined, {subprocessStdin, subprocess, waitWritableFinal, waitWritableDestroy})),18 highWaterMark: subprocessStdin.writableHighWaterMark,19 objectMode: subprocessStdin.writableObjectMode,20 });21 onStdinFinished(subprocessStdin, writable);22 return writable;23};2425// Retrieve `stdin` (or other stream depending on `to`)26export const getSubprocessStdin = (subprocess, to, concurrentStreams) => {27 const subprocessStdin = getWritable(subprocess, to);28 const waitWritableFinal = addConcurrentStream(concurrentStreams, subprocessStdin, 'writableFinal');29 const waitWritableDestroy = addConcurrentStream(concurrentStreams, subprocessStdin, 'writableDestroy');30 return {subprocessStdin, waitWritableFinal, waitWritableDestroy};31};3233export const getWritableMethods = (subprocessStdin, subprocess, waitWritableFinal) => ({34 write: onWrite.bind(undefined, subprocessStdin),35 final: callbackify(onWritableFinal.bind(undefined, subprocessStdin, subprocess, waitWritableFinal)),36});3738// Forwards data from `writable` to `stdin`39const onWrite = (subprocessStdin, chunk, encoding, done) => {40 if (subprocessStdin.write(chunk, encoding)) {41 done();42 } else {43 subprocessStdin.once('drain', done);44 }45};4647// Ensures that the writable `final` and readable `end` events awaits the subprocess.48// Like this, any subprocess failure is propagated as a stream `error` event, instead of being lost.49// The user does not need to `await` the subprocess anymore, but now needs to await the stream completion or error.50// When multiple writables are targeting the same stream, they wait for each other, unless the subprocess ends first.51const onWritableFinal = async (subprocessStdin, subprocess, waitWritableFinal) => {52 if (await waitForConcurrentStreams(waitWritableFinal, subprocess)) {53 if (subprocessStdin.writable) {54 subprocessStdin.end();55 }5657 await subprocess;58 }59};6061// When `subprocess.stdin` ends/aborts/errors, do the same on `writable`.62export const onStdinFinished = async (subprocessStdin, writable, subprocessStdout) => {63 try {64 await waitForSubprocessStdin(subprocessStdin);65 if (writable.writable) {66 writable.end();67 }68 } catch (error) {69 await safeWaitForSubprocessStdout(subprocessStdout);70 destroyOtherWritable(writable, error);71 }72};7374// When `writable` aborts/errors, do the same on `subprocess.stdin`75export const onWritableDestroy = async ({subprocessStdin, subprocess, waitWritableFinal, waitWritableDestroy}, error) => {76 await waitForConcurrentStreams(waitWritableFinal, subprocess);77 if (await waitForConcurrentStreams(waitWritableDestroy, subprocess)) {78 destroyOtherWritable(subprocessStdin, error);79 await waitForSubprocess(subprocess, error);80 }81};8283const destroyOtherWritable = (stream, error) => {84 destroyOtherStream(stream, stream.writable, error);85};