ipc.md
This file
Path
docs/ipc.md
228 lines · 8.4 KiB · Markdown
Last changed by
Document `stdio: [..., 'ipc']` support (#1221)ehmickybefore adoption1796805
History of this fileTake it
The walk stopped after 200 commits: lines older than ea889c7 point at it.
Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
lines 1–10why, one step back1<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 messages10Add `ipcInput` option (#1068)ehmickybefore adoption46cae30
line 11why, one step back11When 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).Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
lines 12–14why, one step back1213The `ipc` option defaults to `true` when using [`execaNode()`](node.md#run-nodejs-files) or the [`node`](node.md#run-nodejs-files) option.14Document `stdio: [..., 'ipc']` support (#1221)ehmickybefore adoption1796805
lines 15–16why, one step back15Please 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`.16Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 17why, one step back17The current process sends messages with [`subprocess.sendMessage(message)`](api.md#subprocesssendmessagemessage-sendmessageoptions) and receives them with [`subprocess.getOneMessage()`](api.md#subprocessgetonemessagegetonemessageoptions).Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
line 18why, one step back18Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 19why, one step back19The 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 back20Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 21–23why, one step back21```js22// parent.js23import {execaNode} from 'execa';Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
line 24why, one step back24Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 25why, one step back25const subprocess = execaNode`child.js`;Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 26–27why, one step back26await subprocess.sendMessage('Hello from parent');27const message = await subprocess.getOneMessage();Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 28why, one step back28console.log(message); // 'Hello from child'Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 29why, one step back29await subprocess;Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 30–31why, one step back30```31Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
line 32why, one step back32```jsAdd promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 33why, one step back33// child.jsAdd `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 34why, one step back34import {getOneMessage, sendMessage} from 'execa';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 35why, one step back35Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
lines 36–38why, one step back36const 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 back39```4041## Listening to messages42Fix broken Markdown links (#1119)ehmickybefore adoption8572eb1
line 43why, one step back43The 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 back44Fix broken Markdown links (#1119)ehmickybefore adoption8572eb1
line 45why, one step back45[`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 back4647```jsFix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
lines 48–51why, one step back48// parent.js49import {execaNode} from 'execa';5051const subprocess = execaNode`child.js`;Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 52–59why, one step back52await 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}Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
lines 60–63why, one step back60```6162```js63// child.jsAdd promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 64why, one step back64import {sendMessage, getEachMessage} from 'execa';Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
line 65why, one step back65Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 66–68why, one step back66// 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 back69 break;Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 70–74why, one step back70 }7172 console.log(message); // 0, 2, 4, 6, 873 await sendMessage(message + 1);74}Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
lines 75–76why, one step back75```76Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
lines 77–96why, one step back77## Filter messages7879```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```96Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 97–130why, one step back97## Ensure messages are received9899When 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```130Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 131–132why, one step back131## Retrieve all messages132Fix broken Markdown links (#1119)ehmickybefore adoption8572eb1
line 133why, one step back133The [`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 back134135```js136// main.js137import {execaNode} from 'execa';138Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
lines 139–141why, one step back139const {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 back142```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 back151```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 back173```174Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
lines 175–181why, one step back175## Message type176177By 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).178179To limit messages to JSON instead, the [`serialization`](api.md#optionsserialization) option can be set to `'json'`.180181```jsAdd `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
lines 182–183why, one step back182import {execaNode} from 'execa';183Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 184why, one step back184await execaNode({serialization: 'json'})`child.js`;Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
line 185why, one step back185```Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 186why, one step back186Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
lines 187–188why, one step back187## Messages order188Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 189why, one step back189The messages are always received in the same order they were sent. Even when sent all at once.Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 190why, one step back190Improve IPC documentation (#1079)ehmickybefore adoption4414737
line 191why, one step back191```jsDebounce `message` events (#1087)ehmickybefore adoptione8bab97
line 192why, one step back192import {sendMessage} from 'execa';Improve IPC documentation (#1079)ehmickybefore adoption4414737
line 193why, one step back193Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 194–198why, one step back194await Promise.all([195 sendMessage('first'),196 sendMessage('second'),197 sendMessage('third'),198]);Improve IPC documentation (#1079)ehmickybefore adoption4414737
lines 199–200why, one step back199```200Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6
lines 201–210why, one step back201## Keeping the subprocess alive202203By 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.204205However, 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.206207```js208import {getEachMessage} from 'execa';209210// {type: 'gracefulExit'} is sometimes received, but not alwaysFix typo in IPC documentation (#1121)ehmickybefore adoptione15e516
line 211why, one step back211for await (const message of getEachMessage({reference: false})) {Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6
line 212why, one step back212 if (message.type === 'gracefulExit') {Fix typo in IPC documentation (#1121)ehmickybefore adoptione15e516
line 213why, one step back213 gracefulExit();Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6
lines 214–217why, one step back214 }215}216```217Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 218why, one step back218## DebuggingImprove IPC documentation (#1079)ehmickybefore adoption4414737
line 219why, one step back219Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 220why, one step back220When 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 221why, one step back221Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 222why, one step back222Also, 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).Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
lines 223–228why, one step back223224<hr>225226[**Next**: 🐛 Debugging](debugging.md)\227[**Previous**: ⏳️ Streams](streams.md)\228[**Top**: Table of contents](../readme.md#documentation)