transform.md
This file
Path
docs/transform.md
203 lines · 5.9 KiB · Markdown
Last changed by
Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
History of this fileTake it
Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 1–4why, one step back1# Transforms23## Summary4Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 5why, one step back5Transforms map or filter the input or output of a subprocess. They are defined by passing a [generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*) to the [`stdin`](../readme.md#stdin), [`stdout`](../readme.md#stdout-1), [`stderr`](../readme.md#stderr-1) or [`stdio`](../readme.md#stdio-1) option. It can be [`async`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*).Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 6–9why, one step back67```js8import {execa} from 'execa';9Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 10–12why, one step back10const transform = function * (line) {11 const prefix = line.includes('error') ? 'ERROR' : 'INFO';12 yield `${prefix}: ${line}`;Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 13–14why, one step back13};14Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
line 15why, one step back15const {stdout} = await execa('./run.js', {stdout: transform});Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 16–20why, one step back16console.log(stdout); // HELLO17```1819## Encoding20Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
lines 21–22why, one step back21The `line` argument passed to the transform is a string by default.\22However, if either a `{transform, binary: true}` plain object is passed, or if the [`encoding`](../readme.md#encoding) option is `buffer`, it is an `Uint8Array` instead.Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 23why, one step back23Fix Markdown typos in documentation (#907)ehmickybefore adoption8ee5985
line 24why, one step back24The transform can `yield` either a `string` or an `Uint8Array`, regardless of the `line` argument's type.Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 25–27why, one step back2526## Filtering27Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 28why, one step back28`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 29–32why, one step back2930```js31import {execa} from 'execa';32Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 33–35why, one step back33const transform = function * (line) {34 if (!line.includes('secret')) {35 yield line;Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 36–41why, one step back36 }37};3839const {stdout} = await execa('echo', ['This is a secret.'], {stdout: transform});40console.log(stdout); // ''41```Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 42–45why, one step back4243## Binary data4445The transform iterates over lines by default.\Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 46why, one step back46However, if either a `{transform, binary: true}` plain object is passed, or if the [`encoding`](../readme.md#encoding) option is `buffer`, it iterates over arbitrary chunks of data instead.Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 47–52why, one step back4748```js49await execa('./binary.js', {stdout: {transform, binary: true}});50```5152This is more efficient and recommended if the data is either:Fix Markdown typos in documentation (#907)ehmickybefore adoption8ee5985
lines 53–54why, one step back53- Binary: Which does not have lines.54- 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 55why, one step back55Line-wise standard streams (#741)ehmickybefore adoption479d12e
lines 56–57why, one step back56Please note the [`lines`](../readme.md#lines) option is unrelated: it has no impact on transforms.57Strip newlines in transforms (#919)ehmickybefore adoption26e9150
lines 58–117why, one step back58## Newlines5960Unless [`{transform, binary: true}`](#binary-data) is used, the transform iterates over lines.61By default, newlines are stripped from each `line` argument.6263```js64// `line`'s value never ends with '\n'.65const transform = function * (line) { /* ... */ };6667await execa('./run.js', {stdout: transform});68```6970However, if a `{transform, preserveNewlines: true}` plain object is passed, newlines are kept.7172```js73// `line`'s value ends with '\n'.74// The output's last `line` might or might not end with '\n', depending on the output.75const transform = function * (line) { /* ... */ };7677await execa('./run.js', {stdout: {transform, preserveNewlines: true}});78```7980Each `yield` produces at least one line. Calling `yield` multiple times or calling `yield *` produces multiples lines.8182```js83const transform = function * (line) {84 yield 'Important note:';85 yield 'Read the comments below.';8687 // Or:88 yield * [89 'Important note:',90 'Read the comments below.',91 ];9293 // Is the same as:94 yield 'Important note:\nRead the comments below.\n';9596 yield line97};9899await execa('./run.js', {stdout: transform});100```101102However, if a `{transform, preserveNewlines: true}` plain object is passed, multiple `yield`s produce a single line instead.103104```js105const transform = function * (line) {106 yield 'Important note: ';107 yield 'Read the comments below.\n';108109 // Is the same as:110 yield 'Important note: Read the comments below.\n';111112 yield line113};114115await execa('./run.js', {stdout: {transform, preserveNewlines: true}});116```117Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 118–119why, one step back118## Object mode119Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
lines 120–121why, one step back120By default, `stdout` and `stderr`'s transforms must return a string or an `Uint8Array`.\121However, if a `{transform, objectMode: true}` plain object is passed, any type can be returned instead, except `null` or `undefined`. The subprocess' [`stdout`](../readme.md#stdout)/[`stderr`](../readme.md#stderr) will be an array of values.Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 122–123why, one step back122123```jsSimplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 124–126why, one step back124const transform = function * (line) {125 yield JSON.parse(line);126};Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 127–129why, one step back127128const {stdout} = await execa('./jsonlines-output.js', {stdout: {transform, objectMode: true}});129for (const data of stdout) {Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
line 130why, one step back130 console.log(stdout); // {...}Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 131–136why, one step back131}132```133134`stdin` can also use `objectMode: true`.135136```jsSimplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 137–139why, one step back137const transform = function * (line) {138 yield JSON.stringify(line);139};Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 140why, one step back140Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
line 141why, one step back141const input = [{event: 'example'}, {event: 'otherExample'}];Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 142why, one step back142await execa('./jsonlines-input.js', {stdin: [input, {transform, objectMode: true}]});Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 143–159why, one step back143```144145## Sharing state146147State can be shared between calls of the `transform` and [`final`](#finalizing) functions.148149```js150let count = 0151152// Prefix line number153const transform = function * (line) {154 yield `[${count++}] ${line}`;155};156```157158## Finalizing159Allow using a `final` function without a `transform` (#916)ehmickybefore adoptionf8af153
line 160why, one step back160To create additional lines after the last one, a `final` generator function can be used by passing a `{final}` or `{transform, final}` plain object.Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 161–175why, one step back161162```js163let count = 0;164165const transform = function * (line) {166 count += 1;167 yield line;168};169170const final = function * () {171 yield `Number of lines: ${count}`;172};173174const {stdout} = await execa('./command.js', {stdout: {transform, final}});175console.log(stdout); // Ends with: 'Number of lines: 54'Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 176–177why, one step back176```177Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 178–189why, one step back178## Combining179180The [`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`.181182```js183await execa('echo', ['hello'], {stdout: [transform, 'inherit']});184```185186This also allows using multiple transforms.187188```js189await execa('echo', ['hello'], {stdout: [transform, otherTransform]});Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 190–202why, one step back190```191192## Async iteration193194In some cases, [iterating](../readme.md#iterablereadableoptions) over the subprocess can be an alternative to transforms.195196```js197import {execa} from 'execa';198199for await (const line of execa('./run.js')) {200 const prefix = line.includes('error') ? 'ERROR' : 'INFO';201 console.log(`${prefix}: ${line}`);202}Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 203why, one step back203```