Skip to content
Myr

ipc.md

This file

Path

docs/ipc.md

230 lines · 8.5 KiB · Markdown

Last changed by

Fix documentation and types mistakesSindre Sorhusbefore adoption74b854a

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–10why, 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# 📞 Inter-process communication89## Exchanging messages10

Add `ipcInput` option (#1068)ehmickybefore adoption46cae30

line 11why, one step back
11When the [`ipc`](api.md#optionsipc) option is `true`, the current process and subprocess can exchange messages. This only works if the subprocess is a [Node.js file](node.md).

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 12–14why, one step back
12
13The `ipc` option defaults to `true` when using [`execaNode()`](node.md#run-nodejs-files) or the [`node`](node.md#run-nodejs-files) option.14

Document `stdio: [..., 'ipc']` support (#1221)ehmickybefore adoption1796805

lines 15–16why, one step back
15Please note that unlike `node:child_process` [`spawn()`](https://nodejs.org/api/child_process.html#optionsstdio), the [`stdio` option](api.md#optionsstdio) does not support the `'ipc'` value. Instead, the [`ipc`](api.md#optionsipc) option must be set to `true`.
16

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 17why, one step back
17The current process sends messages with [`subprocess.sendMessage(message)`](api.md#subprocesssendmessagemessage-sendmessageoptions) and receives them with [`subprocess.getOneMessage()`](api.md#subprocessgetonemessagegetonemessageoptions).

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 18why, one step back
18

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 19why, one step back
19The subprocess uses [`sendMessage(message)`](api.md#sendmessagemessage-sendmessageoptions) and [`getOneMessage()`](api.md#getonemessagegetonemessageoptions). Those are the same methods, but imported directly from the `'execa'` module.

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 20why, one step back
20

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 21–23why, one step back
21```js
22// parent.js23import {execaNode} from 'execa';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 24why, one step back
24

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 25why, one step back
25const subprocess = execaNode`child.js`;

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 26–27why, one step back
26await subprocess.sendMessage('Hello from parent');
27const message = await subprocess.getOneMessage();

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 28why, one step back
28console.log(message); // 'Hello from child'

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 29why, one step back
29await subprocess;

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 30–31why, one step back
30```
31

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 32why, one step back
32```js

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 33why, one step back
33// child.js

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 34why, one step back
34import {getOneMessage, sendMessage} from 'execa';

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 35why, one step back
35

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

lines 36–38why, one step back
36const message = await getOneMessage(); // 'Hello from parent'
37const newMessage = message.replace('parent', 'child'); // 'Hello from child'38await sendMessage(newMessage);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 39–42why, one step back
39```
4041## Listening to messages42

Fix broken Markdown links (#1119)ehmickybefore adoption8572eb1

line 43why, one step back
43The methods described above read a single message. On the other hand, [`subprocess.getEachMessage()`](api.md#subprocessgeteachmessagegeteachmessageoptions) and [`getEachMessage()`](api.md#geteachmessagegeteachmessageoptions) return an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols). This should be preferred when listening to multiple messages.

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 44why, one step back
44

Fix broken Markdown links (#1119)ehmickybefore adoption8572eb1

line 45why, one step back
45[`subprocess.getEachMessage()`](api.md#subprocessgeteachmessagegeteachmessageoptions) waits for the subprocess to end (even when using [`break`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break) or [`return`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)). It throws if the subprocess [fails](api.md#result). This means you do not need to `await` the subprocess' [promise](execution.md#result).

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 46–47why, one step back
46
47```js

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 48–51why, one step back
48// parent.js
49import {execaNode} from 'execa';5051const subprocess = execaNode`child.js`;

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 52–59why, one step back
52await subprocess.sendMessage(0);
5354// This loop ends when the subprocess exits.55// It throws if the subprocess fails.56for await (const message of subprocess.getEachMessage()) {57 console.log(message); // 1, 3, 5, 7, 958 await subprocess.sendMessage(message + 1);59}

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 60–63why, one step back
60```
6162```js63// child.js

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 64why, one step back
64import {sendMessage, getEachMessage} from 'execa';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 65why, one step back
65

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 66–68why, one step back
66// The subprocess exits when hitting `break`
67for await (const message of getEachMessage()) {68 if (message === 10) {

Improve IPC documentation (#1079)ehmickybefore adoption4414737

line 69why, one step back
69 break;

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 70–74why, one step back
70 }
7172 console.log(message); // 0, 2, 4, 6, 873 await sendMessage(message + 1);74}

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 75–76why, one step back
75```
76

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

lines 77–96why, one step back
77## Filter messages
7879```js80import {getOneMessage} from 'execa';8182const startMessage = await getOneMessage({83 filter: message => message.type === 'start',84});85```8687```js88import {getEachMessage} from 'execa';8990for await (const message of getEachMessage()) {91 if (message.type === 'start') {92 // ...93 }94}95```96

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 97–130why, one step back
97## Ensure messages are received
9899When a message is sent by one process, the other process must receive it using [`getOneMessage()`](#exchanging-messages), [`getEachMessage()`](#listening-to-messages), or automatically with [`result.ipcOutput`](api.md#resultipcoutput). If not, that message is silently discarded.100101If the [`strict: true`](api.md#sendmessageoptionsstrict) option is passed to [`subprocess.sendMessage(message)`](api.md#subprocesssendmessagemessage-sendmessageoptions) or [`sendMessage(message)`](api.md#sendmessagemessage-sendmessageoptions), an error is thrown instead. This helps identifying subtle race conditions like the following example.102103```js104// main.js105import {execaNode} from 'execa';106107const subprocess = execaNode`build.js`;108// This `build` message is received109await subprocess.sendMessage('build', {strict: true});110// This `lint` message is not received, so it throws111await subprocess.sendMessage('lint', {strict: true});112await subprocess;113```114115```js116// build.js117import {getOneMessage} from 'execa';118119// Receives the 'build' message120const task = await getOneMessage();121// The `lint` message is sent while `runTask()` is ongoing122// Therefore the `lint` message is discarded123await runTask(task);124125// Does not receive the `lint` message126// Without `strict`, this would wait forever127const secondTask = await getOneMessage();128await runTask(secondTask);129```130

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 131–132why, one step back
131## Retrieve all messages
132

Fix broken Markdown links (#1119)ehmickybefore adoption8572eb1

line 133why, one step back
133The [`result.ipcOutput`](api.md#resultipcoutput) array contains all the messages sent by the subprocess. In many situations, this is simpler than using [`subprocess.getOneMessage()`](api.md#subprocessgetonemessagegetonemessageoptions) and [`subprocess.getEachMessage()`](api.md#subprocessgeteachmessagegeteachmessageoptions).

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 134–138why, one step back
134
135```js136// main.js137import {execaNode} from 'execa';138

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

lines 139–141why, one step back
139const {ipcOutput} = await execaNode`build.js`;
140console.log(ipcOutput[0]); // {kind: 'start', timestamp: date}141console.log(ipcOutput[1]); // {kind: 'stop', timestamp: date}

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 142–150why, one step back
142```
143144```js145// build.js146import {sendMessage} from 'execa';147148await sendMessage({kind: 'start', timestamp: new Date()});149await runBuild();150await sendMessage({kind: 'stop', timestamp: new Date()});

Add `ipcInput` option (#1068)ehmickybefore adoption46cae30

lines 151–172why, one step back
151```
152153## Send an initial message154155The [`ipcInput`](api.md#optionsipcinput) option sends a message to the [Node.js subprocess](node.md) when it starts.156157```js158// main.js159import {execaNode} from 'execa';160161const ipcInput = [162 {task: 'lint', ignore: /test\.js/},163 {task: 'copy', files: new Set(['main.js', 'index.js']),164}];165await execaNode({ipcInput})`build.js`;166```167168```js169// build.js170import {getOneMessage} from 'execa';171172const ipcInput = await getOneMessage();

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 173–174why, one step back
173```
174

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 175–176why, one step back
175## Message type
176

Fix documentation and types mistakesSindre Sorhusbefore adoption74b854a

line 177why, one step back
177By default, messages are serialized using [`structuredClone()`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm). This supports most types including objects, arrays, `Error`, `Date`, `RegExp`, `Map`, `Set`, `Uint8Array`, and circular references. This throws when passing functions, symbols or promises (including inside an object or array).

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 178–181why, one step back
178
179To limit messages to JSON instead, the [`serialization`](api.md#optionsserialization) option can be set to `'json'`.180181```js

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

lines 182–183why, one step back
182import {execaNode} from 'execa';
183

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 184why, one step back
184await execaNode({serialization: 'json'})`child.js`;

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 185why, one step back
185```

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 186why, one step back
186

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 187–188why, one step back
187Objects with a `type` property starting with `execa:ipc:` are reserved for Execa's internal messages, and should not be sent.
188

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

lines 189–190why, one step back
189## Messages order
190

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 191why, one step back
191The messages are always received in the same order they were sent. Even when sent all at once.

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 192why, one step back
192

Improve IPC documentation (#1079)ehmickybefore adoption4414737

line 193why, one step back
193```js

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 194why, one step back
194import {sendMessage} from 'execa';

Improve IPC documentation (#1079)ehmickybefore adoption4414737

line 195why, one step back
195

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 196–200why, one step back
196await Promise.all([
197 sendMessage('first'),198 sendMessage('second'),199 sendMessage('third'),200]);

Improve IPC documentation (#1079)ehmickybefore adoption4414737

lines 201–202why, one step back
201```
202

Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6

lines 203–212why, one step back
203## Keeping the subprocess alive
204205By default, the subprocess is kept alive as long as [`getOneMessage()`](api.md#getonemessagegetonemessageoptions) or [`getEachMessage()`](api.md#geteachmessagegeteachmessageoptions) is waiting. This is recommended if you're sure the current process will send a message, as this prevents the subprocess from exiting too early.206207However, if you don't know whether a message will be sent, this can leave the subprocess hanging forever. In that case, the [`reference: false`](api.md#geteachmessageoptionsreference) option can be set.208209```js210import {getEachMessage} from 'execa';211212// {type: 'gracefulExit'} is sometimes received, but not always

Fix typo in IPC documentation (#1121)ehmickybefore adoptione15e516

line 213why, one step back
213for await (const message of getEachMessage({reference: false})) {

Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6

line 214why, one step back
214 if (message.type === 'gracefulExit') {

Fix typo in IPC documentation (#1121)ehmickybefore adoptione15e516

line 215why, one step back
215 gracefulExit();

Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6

lines 216–219why, one step back
216 }
217}218```219

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 220why, one step back
220## Debugging

Improve IPC documentation (#1079)ehmickybefore adoption4414737

line 221why, one step back
221

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 222why, one step back
222When the [`verbose`](api.md#optionsverbose) option is `'full'`, the IPC messages sent by the subprocess to the current process are [printed on the console](debugging.md#full-mode).

Improve IPC documentation (#1079)ehmickybefore adoption4414737

line 223why, one step back
223

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 224why, one step back
224Also, when the subprocess [failed](errors.md#subprocess-failure), [`error.ipcOutput`](api.md) contains all the messages sent by the subprocess. Those are also shown at the end of the [error message](errors.md#error-message).

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 225–230why, one step back
225
226<hr>227228[**Next**: 🐛 Debugging](debugging.md)\229[**Previous**: ⏳️ Streams](streams.md)\230[**Top**: Table of contents](../readme.md#documentation)