Skip to content
Myr

ipc.md

This file

Path

docs/ipc.md

156 lines · 5.4 KiB · Markdown

Last changed by

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

History of this file

Take it

Add user guides (#989)ehmickybefore adoptionbe91ecf

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

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

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

line 15why, one step back
15The current process sends messages with [`subprocess.sendMessage(message)`](api.md#subprocesssendmessagemessage) and receives them with [`subprocess.getOneMessage()`](api.md#subprocessgetonemessagegetonemessageoptions). The subprocess uses [`sendMessage(message)`](api.md#sendmessagemessage) and [`getOneMessage()`](api.md#getonemessagegetonemessageoptions) instead.

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 16why, one step back
16

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 17–19why, one step back
17```js
18// parent.js19import {execaNode} from 'execa';

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 20why, one step back
20

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 21–25why, one step back
21const subprocess = execaNode`child.js`;
22console.log(await subprocess.getOneMessage()); // 'Hello from child'23await subprocess.sendMessage('Hello from parent');24```25

Add user guides (#989)ehmickybefore adoptionbe91ecf

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 27–35why, one step back
27// child.js
28import {sendMessage, getOneMessage} from 'execa';2930await sendMessage('Hello from child');31console.log(await getOneMessage()); // 'Hello from parent'32```3334## Listening to messages35

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

line 36why, one step back
36[`subprocess.getOneMessage()`](api.md#subprocessgetonemessagegetonemessageoptions) and [`getOneMessage()`](api.md#getonemessagegetonemessageoptions) read a single message. To listen to multiple messages in a row, [`subprocess.getEachMessage()`](api.md#subprocessgeteachmessage) and [`getEachMessage()`](api.md#geteachmessage) should be used instead.

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 37–40why, one step back
37
38[`subprocess.getEachMessage()`](api.md#subprocessgeteachmessage) 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).3940```js

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 41–44why, one step back
41// parent.js
42import {execaNode} from 'execa';4344const subprocess = execaNode`child.js`;

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 45–52why, one step back
45await subprocess.sendMessage(0);
4647// This loop ends when the subprocess exits.48// It throws if the subprocess fails.49for await (const message of subprocess.getEachMessage()) {50 console.log(message); // 1, 3, 5, 7, 951 await subprocess.sendMessage(message + 1);52}

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 53–56why, one step back
53```
5455```js56// child.js

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 58why, one step back
58

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 59–67why, one step back
59// The subprocess exits when hitting `break`
60for await (const message of getEachMessage()) {61 if (message === 10) {62 break63 }6465 console.log(message); // 0, 2, 4, 6, 866 await sendMessage(message + 1);67}

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 68–69why, one step back
68```
69

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

lines 70–89why, one step back
70## Filter messages
7172```js73import {getOneMessage} from 'execa';7475const startMessage = await getOneMessage({76 filter: message => message.type === 'start',77});78```7980```js81import {getEachMessage} from 'execa';8283for await (const message of getEachMessage()) {84 if (message.type === 'start') {85 // ...86 }87}88```89

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

lines 90–91why, one step back
90## Retrieve all messages
91

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

line 92why, one step back
92The [`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#subprocessgeteachmessage).

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

lines 93–97why, one step back
93
94```js95// main.js96import {execaNode} from 'execa';97

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

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

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

lines 101–109why, one step back
101```
102103```js104// build.js105import {sendMessage} from 'execa';106107await sendMessage({kind: 'start', timestamp: new Date()});108await runBuild();109await sendMessage({kind: 'stop', timestamp: new Date()});

Add `ipcInput` option (#1068)ehmickybefore adoption46cae30

lines 110–131why, one step back
110```
111112## Send an initial message113114The [`ipcInput`](api.md#optionsipcinput) option sends a message to the [Node.js subprocess](node.md) when it starts.115116```js117// main.js118import {execaNode} from 'execa';119120const ipcInput = [121 {task: 'lint', ignore: /test\.js/},122 {task: 'copy', files: new Set(['main.js', 'index.js']),123}];124await execaNode({ipcInput})`build.js`;125```126127```js128// build.js129import {getOneMessage} from 'execa';130131const ipcInput = await getOneMessage();

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

lines 132–133why, one step back
132```
133

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 134–137why, one step back
134## Message type
135136By 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`, `bigint`, `Uint8Array`, and circular references. This throws when passing functions, symbols or promises (including inside an object or array).137

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

line 138why, one step back
138To limit messages to JSON instead, the [`serialization`](api.md#optionsserialization) option can be set to `'json'`.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 139–140why, one step back
139
140```js

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

lines 141–142why, one step back
141import {execaNode} from 'execa';
142

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 143–144why, one step back
143const subprocess = execaNode({serialization: 'json'})`child.js`;
144```

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

lines 145–149why, one step back
145
146## Debugging147148When 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).149

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

line 150why, one step back
150Also, 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).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 151–156why, one step back
151
152<hr>153154[**Next**: 🐛 Debugging](debugging.md)\155[**Previous**: ⏳️ Streams](streams.md)\156[**Top**: Table of contents](../readme.md#documentation)