Skip to content
Myr

writable.js

This file

Path

lib/convert/writable.js

115 lines · 4.0 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

The walk stopped after 200 commits: lines older than c8a6ed7 point at it.

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 1–3why, one step back
1import {Writable} from 'node:stream';
2import {callbackify} from 'node:util';3import {getToStream} from '../arguments/fd-options.js';

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 4why, one step back
4import {isStreamAbort} from '../resolve/wait-stream.js';

TweaksSindre Sorhusbefore adoption698a1b8

line 5why, one step back
5import {copyOptions} from '../utils/options.js';

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 6–14why, one step back
6import {addConcurrentStream, waitForConcurrentStreams} from './concurrent.js';
7import {8 safeWaitForSubprocessStdout,9 waitForSubprocessStdin,10 waitForSubprocess,11 destroyOtherStream,12} from './shared.js';1314// Create a `Writable` stream that forwards to `stdin` and awaits the subprocess

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 15why, one step back
15export const createWritable = ({subprocess, concurrentStreams}, options) => {

TweaksSindre Sorhusbefore adoption698a1b8

line 16why, one step back
16 const {to} = copyOptions(options);

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 17–28why, one step back
17 const {subprocessStdin, waitWritableFinal, waitWritableDestroy} = getSubprocessStdin(subprocess, to, concurrentStreams);
18 const writable = new Writable({19 ...getWritableMethods(subprocessStdin, subprocess, waitWritableFinal),20 destroy: callbackify(onWritableDestroy.bind(undefined, {21 subprocessStdin,22 subprocess,23 waitWritableFinal,24 waitWritableDestroy,25 })),26 highWaterMark: subprocessStdin.writableHighWaterMark,27 objectMode: subprocessStdin.writableObjectMode,28 });

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 29why, one step back
29 onStdinFinished(subprocessStdin, writable, undefined, subprocess);

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 30–59why, one step back
30 return writable;
31};3233// Retrieve `stdin` (or other stream depending on `to`)34export const getSubprocessStdin = (subprocess, to, concurrentStreams) => {35 const subprocessStdin = getToStream(subprocess, to);36 const waitWritableFinal = addConcurrentStream(concurrentStreams, subprocessStdin, 'writableFinal');37 const waitWritableDestroy = addConcurrentStream(concurrentStreams, subprocessStdin, 'writableDestroy');38 return {subprocessStdin, waitWritableFinal, waitWritableDestroy};39};4041export const getWritableMethods = (subprocessStdin, subprocess, waitWritableFinal) => ({42 write: onWrite.bind(undefined, subprocessStdin),43 final: callbackify(onWritableFinal.bind(undefined, subprocessStdin, subprocess, waitWritableFinal)),44});4546// Forwards data from `writable` to `stdin`47const onWrite = (subprocessStdin, chunk, encoding, done) => {48 if (subprocessStdin.write(chunk, encoding)) {49 done();50 } else {51 subprocessStdin.once('drain', done);52 }53};5455// Ensures that the writable `final` and readable `end` events awaits the subprocess.56// Like this, any subprocess failure is propagated as a stream `error` event, instead of being lost.57// The user does not need to `await` the subprocess anymore, but now needs to await the stream completion or error.58// When multiple writables are targeting the same stream, they wait for each other, unless the subprocess ends first.59const onWritableFinal = async (subprocessStdin, subprocess, waitWritableFinal) => {

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

lines 60–62why, one step back
60 if (!await waitForConcurrentStreams(waitWritableFinal, subprocess)) {
61 return;62 }

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

line 63why, one step back
63

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

lines 64–65why, one step back
64 if (subprocessStdin.writable) {
65 subprocessStdin.end();

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

line 66why, one step back
66 }

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

lines 67–68why, one step back
67
68 await subprocess;

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 69–71why, one step back
69};
7071// When `subprocess.stdin` ends/aborts/errors, do the same on `writable`.

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 72why, one step back
72export const onStdinFinished = async (subprocessStdin, writable, subprocessStdout, subprocess) => {

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 73–74why, one step back
73 try {
74 await waitForSubprocessStdin(subprocessStdin);

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 75why, one step back
75 await subprocess;

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 76–80why, one step back
76 if (writable.writable) {
77 writable.end();78 }79 } catch (error) {80 await safeWaitForSubprocessStdout(subprocessStdout);

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 81why, one step back
81 destroyOtherWritable(writable, await getSubprocessError(subprocess, error));

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 82–83why, one step back
82 }
83};

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

lines 84–100why, one step back
84
85const getSubprocessError = async (subprocess, error) => {86 if (!shouldUseSubprocessError(error)) {87 return error;88 }8990 try {91 await subprocess;92 } catch (subprocessError) {93 return subprocessError;94 }9596 return error;97};9899const shouldUseSubprocessError = error => error === undefined100 || isStreamAbort(error);

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 101–104why, one step back
101
102// When `writable` aborts/errors, do the same on `subprocess.stdin`103export const onWritableDestroy = async ({subprocessStdin, subprocess, waitWritableFinal, waitWritableDestroy}, error) => {104 await waitForConcurrentStreams(waitWritableFinal, subprocess);

TweaksSindre Sorhusbefore adoption698a1b8

lines 105–106why, one step back
105 if (!await waitForConcurrentStreams(waitWritableDestroy, subprocess)) {
106 return;

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

line 107why, one step back
107 }

TweaksSindre Sorhusbefore adoption698a1b8

lines 108–110why, one step back
108
109 destroyOtherWritable(subprocessStdin, error);110 await waitForSubprocess(subprocess, error);

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 111–115why, one step back
111};
112113const destroyOtherWritable = (stream, error) => {114 destroyOtherStream(stream, stream.writable, error);115};