Skip to content
Myr

writable.js

This file

Path

lib/convert/writable.js

118 lines · 4.3 KiB · JavaScript

Last changed by

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

History of this file

Take 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 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';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 6–9why, one step back
6import {addConcurrentStream, waitForConcurrentStreams} from './concurrent.js';
7import {8 safeWaitForSubprocessStdout,9 waitForSubprocessStdin,

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

line 10why, one step back
10 throwDestroyError,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 11–14why, one step back
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);

100% test coverage (#1018)ehmickybefore adoption9f65b86

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);

100% test coverage (#1018)ehmickybefore adoption9f65b86

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 }

100% test coverage (#1018)ehmickybefore adoption9f65b86

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();

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 66why, one step back
66 }

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

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

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) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

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;

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

lines 81–82why, one step back
81 // `writable.writable` is already `false` once `writable.end()` is called, although its pending writes still wait for `stdin`
82 destroyOtherStream(writable, !writable.writableFinished, await getSubprocessError(subprocess, error));

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

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

lines 85–99why, one step back
85
86const getSubprocessError = async (subprocess, error) => {87 if (!shouldUseSubprocessError(error)) {88 return error;89 }9091 try {92 await subprocess;93 } catch (subprocessError) {94 return subprocessError;95 }9697 return error;98};99

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

line 100why, one step back
100// `EPIPE` happens when the subprocess fails without reading its `stdin`, e.g. when it fails to spawn

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

line 101why, one step back
101const shouldUseSubprocessError = error => error === undefined

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

lines 102–103why, one step back
102 || isStreamAbort(error)
103 || error.code === 'EPIPE';

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

lines 108–110why, one step back
108 if (await waitForConcurrentStreams(waitWritableDestroy, subprocess)) {
109 destroyOtherWritable(subprocessStdin, error);110 await subprocess;

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 111why, one step back
111 }

TweaksSindre Sorhusbefore adoption698a1b8

line 112why, one step back
112

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

line 113why, one step back
113 throwDestroyError(error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 114–118why, one step back
114};
115116const destroyOtherWritable = (stream, error) => {117 destroyOtherStream(stream, stream.writable, error);118};