binary.md
This file
Path
docs/binary.md
79 lines · 3.6 KiB · Markdown
Last changed by
Add user guides (#989)ehmickybefore adoptionbe91ecf
History of this fileTake it
Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 1–79why, 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# 🤖 Binary data89## Binary input1011There are multiple ways to pass binary input using the [`stdin`](../readme.md#optionsstdin), [`input`](../readme.md#optionsinput) or [`inputFile`](../readme.md#optionsinputfile) options: `Uint8Array`s, [files](input.md#file-input), [streams](streams.md) or [other subprocesses](pipe.md).1213This is required if the subprocess input includes [null bytes](https://en.wikipedia.org/wiki/Null_character).1415```js16import {execa} from 'execa';1718const binaryData = new Uint8Array([/* ... */]);19await execa({stdin: binaryData})`hexdump`;20```2122## Binary output2324By default, the subprocess [output](../readme.md#resultstdout) is a [UTF8](https://en.wikipedia.org/wiki/UTF-8) string. If it is binary, the [`encoding`](../readme.md#optionsencoding) option should be set to `'buffer'` instead. The output will be an [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).2526```js27const {stdout} = await execa({encoding: 'buffer'})`zip -r - input.txt`;28console.log(stdout.byteLength);29```3031## Encoding3233When the output is binary, the [`encoding`](../readme.md#optionsencoding) option can also be set to [`'hex'`](https://en.wikipedia.org/wiki/Hexadecimal), [`'base64'`](https://en.wikipedia.org/wiki/Base64) or [`'base64url'`](https://en.wikipedia.org/wiki/Base64#URL_applications). The output will be a string then.3435```js36const {stdout} = await execa({encoding: 'hex'})`zip -r - input.txt`;37console.log(stdout); // Hexadecimal string38```3940## Iterable4142By default, the subprocess [iterates](lines.md#progressive-splitting) over line strings. However, if the [`encoding`](../readme.md#optionsencoding) subprocess option is binary, or if the [`binary`](../readme.md#readableoptionsbinary) iterable option is `true`, it iterates over arbitrary chunks of [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) data instead.4344```js45for await (const data of execa({encoding: 'buffer'})`zip -r - input.txt`) {46 /* ... */47}48```4950## Transforms5152The same applies to transforms. When the [`encoding`](../readme.md#optionsencoding) subprocess option is binary, or when the [`binary`](../readme.md#transformoptionsbinary) transform option is `true`, it iterates over arbitrary chunks of [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) data instead.5354However, transforms can always `yield` either a `string` or an `Uint8Array`, regardless of whether the output is binary or not.5556```js57const transform = function * (data) {58 /* ... */59}6061await execa({stdout: {transform, binary: true}})`zip -r - input.txt`;62```6364## Streams6566[Streams produced](streams.md#converting-a-subprocess-to-a-stream) by [`subprocess.readable()`](../readme.md#subprocessreadablereadableoptions) and [`subprocess.duplex()`](../readme.md#subprocessduplexduplexoptions) are binary by default, which means they iterate over arbitrary [`Buffer`](https://nodejs.org/api/buffer.html#class-buffer) chunks. However, if the [`binary`](../readme.md#readableoptionsbinary) option is `false`, they iterate over line strings instead, and the stream is [in object mode](https://nodejs.org/api/stream.html#object-mode).6768```js69const readable = execa`npm run build`.readable({binary: false});70readable.on('data', lineString => {71 /* ... */72});73```7475<hr>7677[**Next**: 🧙 Transforms](transform.md)\78[**Previous**: 📃 Text lines](lines.md)\79[**Top**: Table of contents](../readme.md#documentation)