Skip to content
Myr

streams.md

This file

Path

docs/streams.md

117 lines · 5.9 KiB · Markdown

Last changed by

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

History of this file

Take it

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 1–17why, one step back
1<picture>
2 <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg">3 <img alt="execa logo" src="../media/logo.svg" width="400">4</picture>5<br>67# ⏳️ Streams89## Node.js streams1011### Input1213```js14import {createReadStream} from 'node:fs';15import {once} from 'node:events';16import {execa} from 'execa';17

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

line 18why, one step back
18const readable = createReadStream('input.txt');

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 19–29why, one step back
19await once(readable, 'open');
20await execa({stdin: readable})`npm run scaffold`;21```2223### Output2425```js26import {createWriteStream} from 'node:fs';27import {once} from 'node:events';28import {execa} from 'execa';29

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

line 30why, one step back
30const writable = createWriteStream('output.txt');

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 31–36why, one step back
31await once(writable, 'open');
32await execa({stdout: writable})`npm run build`;33```3435### File descriptors36

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 37why, one step back
37When passing a Node.js stream to the [`stdin`](api.md#optionsstdin), [`stdout`](api.md#optionsstdout) or [`stderr`](api.md#optionsstderr) option, that stream must have an underlying file or socket, such as the streams created by the [`fs`](https://nodejs.org/api/fs.html#filehandlecreatereadstreamoptions), [`net`](https://nodejs.org/api/net.html#new-netsocketoptions) or [`http`](https://nodejs.org/api/http.html#class-httpincomingmessage) core modules. Otherwise the following error is thrown.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 38–43why, one step back
38
39```40TypeError [ERR_INVALID_ARG_VALUE]: The argument 'stdio' is invalid.41```4243This limitation can be worked around by either:

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 44why, one step back
44- Using the [`input`](api.md#optionsinput) option instead of the [`stdin`](api.md#optionsstdin) option.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 45–70why, one step back
45- Passing a [web stream](#web-streams).
46- Passing [`[nodeStream, 'pipe']`](output.md#multiple-targets) instead of `nodeStream`.4748## Web streams4950[Web streams](https://nodejs.org/api/webstreams.html) ([`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) or [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream)) can be used instead of [Node.js streams](https://nodejs.org/api/stream.html).5152```js53const response = await fetch('https://example.com');54await execa({stdin: response.body})`npm run build`;55```5657## Iterables as input5859```js60const getReplInput = async function * () {61 for await (const replLine of getReplLines()) {62 yield replLine;63 }64};6566await execa({stdin: getReplInput()})`npm run scaffold`;67```6869## Manual streaming70

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 71why, one step back
71[`subprocess.stdin`](api.md#subprocessstdin) is a Node.js [`Readable`](https://nodejs.org/api/stream.html#class-streamreadable) stream and [`subprocess.stdout`](api.md#subprocessstdout)/[`subprocess.stderr`](api.md#subprocessstderr)/[`subprocess.all`](api.md#subprocessall) are Node.js [`Writable`](https://nodejs.org/api/stream.html#class-streamwritable) streams.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 72–74why, one step back
72
73They can be used to stream input/output manually. This is intended for advanced situations. In most cases, the following simpler solutions can be used instead:74- [`result.stdout`](output.md#stdout-and-stderr), [`result.stderr`](output.md#stdout-and-stderr) or [`result.stdio`](output.md#additional-file-descriptors).

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 75why, one step back
75- The [`stdin`](api.md#optionsstdin), [`stdout`](api.md#optionsstdout), [`stderr`](api.md#optionsstderr) or [`stdio`](api.md#optionsstdio) options.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 76–82why, one step back
76- [`subprocess.iterable()`](lines.md#progressive-splitting).
77- [`subprocess.pipe()`](pipe.md).7879## Converting a subprocess to a stream8081### Convert82

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 83why, one step back
83The [`subprocess.readable()`](api.md#subprocessreadablereadableoptions), [`subprocess.writable()`](api.md#subprocesswritablewritableoptions) and [`subprocess.duplex()`](api.md#subprocessduplexduplexoptions) methods convert the subprocess to a Node.js [`Readable`](https://nodejs.org/api/stream.html#class-streamreadable), [`Writable`](https://nodejs.org/api/stream.html#class-streamwritable) and [`Duplex`](https://nodejs.org/api/stream.html#class-streamduplex) stream.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 84–96why, one step back
84
85This is useful when using a library or API that expects Node.js streams as arguments. In every other situation, the simpler solutions described [above](#manual-streaming) can be used instead.8687```js88const readable = execa`npm run scaffold`.readable();8990const writable = execa`npm run scaffold`.writable();9192const duplex = execa`npm run scaffold`.duplex();93```9495### Different file descriptor96

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 97why, one step back
97By default, [`subprocess.readable()`](api.md#subprocessreadablereadableoptions), [`subprocess.writable()`](api.md#subprocesswritablewritableoptions) and [`subprocess.duplex()`](api.md#subprocessduplexduplexoptions) methods use [`stdin`](api.md#subprocessstdin) and [`stdout`](api.md#subprocessstdout). This can be changed using the [`from`](api.md#readableoptionsfrom) and [`to`](api.md#writableoptionsto) options.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 98–108why, one step back
98
99```js100const readable = execa`npm run scaffold`.readable({from: 'stderr'});101102const writable = execa`npm run scaffold`.writable({to: 'fd3'});103104const duplex = execa`npm run scaffold`.duplex({from: 'stderr', to: 'fd3'});105```106107### Error handling108

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 109why, one step back
109When using [`subprocess.readable()`](api.md#subprocessreadablereadableoptions), [`subprocess.writable()`](api.md#subprocesswritablewritableoptions) or [`subprocess.duplex()`](api.md#subprocessduplexduplexoptions), the stream waits for the subprocess to end, and emits an [`error`](https://nodejs.org/api/stream.html#event-error) event if the subprocess [fails](errors.md). This differs from [`subprocess.stdin`](api.md#subprocessstdin), [`subprocess.stdout`](api.md#subprocessstdout) and [`subprocess.stderr`](api.md#subprocessstderr)'s behavior.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 110–117why, one step back
110
111This means you do not need to `await` the subprocess' [promise](execution.md#result). On the other hand, you (or the library using the stream) do need to both consume the stream, and handle its `error` event. This can be done by using [`await finished(stream)`](https://nodejs.org/api/stream.html#streamfinishedstream-options), [`await pipeline(..., stream, ...)`](https://nodejs.org/api/stream.html#streampipelinesource-transforms-destination-options) or [`await text(stream)`](https://nodejs.org/api/webstreams.html#streamconsumerstextstream) which throw an exception when the stream errors.112113<hr>114115[**Next**: 📞 Inter-process communication](ipc.md)\116[**Previous**: 🔀 Piping multiple subprocesses](pipe.md)\117[**Top**: Table of contents](../readme.md#documentation)