transform.md
This file
Path
docs/transform.md
101 lines · 3.4 KiB · Markdown
Last changed by
Document returning `null` in transforms (#740)ehmickybefore adoptiondd9c884
History of this fileTake it
Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 1–9why, one step back1# Transforms23## Summary45Transforms map or filter the input or output of a child process. They are defined by passing an [async generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) to the [`stdin`](../readme.md#stdin), [`stdout`](../readme.md#stdout-1), [`stderr`](../readme.md#stderr-1) or [`stdio`](../readme.md#stdio-1) option.67```js8import {execa} from 'execa';9Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 10–13why, one step back10const transform = async function * (lines) {11 for await (const line of lines) {12 const prefix = line.includes('error') ? 'ERROR' : 'INFO'13 yield `${prefix}: ${line}`Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 14–22why, one step back14 }15};1617const {stdout} = await execa('echo', ['hello'], {stdout: transform});18console.log(stdout); // HELLO19```2021## Encoding22Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 23why, one step back23The `lines` argument passed to the transform is an [`AsyncIterable<string>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols). If the [`encoding`](../readme.md#encoding) option is `buffer`, it is an `AsyncIterable<Uint8Array>` instead.Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 24why, one step back24Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 25why, one step back25The transform can `yield` either a `string` or an `Uint8Array`, regardless of the `lines` argument's type.Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 26–28why, one step back2627## Filtering28Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 29why, one step back29`yield` can be called 0, 1 or multiple times. Not calling `yield` enables filtering a specific line.Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 30–33why, one step back3031```js32import {execa} from 'execa';33Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 34–37why, one step back34const transform = async function * (lines) {35 for await (const line of lines) {36 if (!line.includes('secret')) {37 yield line;Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 38–44why, one step back38 }39 }40};4142const {stdout} = await execa('echo', ['This is a secret.'], {stdout: transform});43console.log(stdout); // ''44```Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 45–57why, one step back4546## Binary data4748The transform iterates over lines by default.\49However, if a `{transform, binary: true}` plain object is passed, it iterates over arbitrary chunks of data instead.5051```js52await execa('./binary.js', {stdout: {transform, binary: true}});53```5455This is more efficient and recommended if the data is either:56 - Binary: Which does not have lines.57 - Text: But the transform works even if a line or word is split across multiple chunks.Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 58why, one step back58Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 59–60why, one step back59## Object mode60Document returning `null` in transforms (#740)ehmickybefore adoptiondd9c884
line 61why, one step back61By default, `stdout` and `stderr`'s transforms must return a string or an `Uint8Array`. However, if a `{transform, objectMode: true}` plain object is passed, any type can be returned instead, except `null`. The process' [`stdout`](../readme.md#stdout)/[`stderr`](../readme.md#stderr) will be an array of values.Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 62–88why, one step back6263```js64const transform = async function * (lines) {65 for await (const line of lines) {66 yield JSON.parse(line)67 }68}6970const {stdout} = await execa('./jsonlines-output.js', {stdout: {transform, objectMode: true}});71for (const data of stdout) {72 console.log(stdout) // {...}73}74```7576`stdin` can also use `objectMode: true`.7778```js79const transform = async function * (lines) {80 for await (const line of lines) {81 yield JSON.stringify(line)82 }83}8485const input = [{event: 'example'}, {event: 'otherExample'}]86await execa('./jsonlines-input.js', {stdin: [input, {transform, objectMode: true}]});87```88Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 89–101why, one step back89## Combining9091The [`stdin`](../readme.md#stdin), [`stdout`](../readme.md#stdout-1), [`stderr`](../readme.md#stderr-1) and [`stdio`](../readme.md#stdio-1) options can accept an array of values. While this is not specific to transforms, this can be useful with them too. For example, the following transform impacts the value printed by `inherit`.9293```js94await execa('echo', ['hello'], {stdout: [transform, 'inherit']});95```9697This also allows using multiple transforms.9899```js100await execa('echo', ['hello'], {stdout: [transform, otherTransform]});101```