Skip to content
Myr

binary.md

This file

Path

docs/binary.md

79 lines · 3.5 KiB · Markdown

Last changed by

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

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# 🤖 Binary data89## Binary input10

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

line 11why, one step back
11There are multiple ways to pass binary input using the [`stdin`](api.md#optionsstdin), [`input`](api.md#optionsinput) or [`inputFile`](api.md#optionsinputfile) options: `Uint8Array`s, [files](input.md#file-input), [streams](streams.md) or [other subprocesses](pipe.md).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 12–23why, one step back
12
13This 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 output23

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

line 24why, one step back
24By default, the subprocess [output](api.md#resultstdout) is a [UTF8](https://en.wikipedia.org/wiki/UTF-8) string. If it is binary, the [`encoding`](api.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).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 25–32why, one step back
25
26```js27const {stdout} = await execa({encoding: 'buffer'})`zip -r - input.txt`;28console.log(stdout.byteLength);29```3031## Encoding32

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

line 33why, one step back
33When the output is binary, the [`encoding`](api.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.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 34–41why, one step back
34
35```js36const {stdout} = await execa({encoding: 'hex'})`zip -r - input.txt`;37console.log(stdout); // Hexadecimal string38```3940## Iterable41

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

line 42why, one step back
42By default, the subprocess [iterates](lines.md#progressive-splitting) over line strings. However, if the [`encoding`](api.md#optionsencoding) subprocess option is binary, or if the [`binary`](api.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.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 43–51why, one step back
43
44```js45for await (const data of execa({encoding: 'buffer'})`zip -r - input.txt`) {46 /* ... */47}48```4950## Transforms51

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

line 52why, one step back
52The same applies to transforms. When the [`encoding`](api.md#optionsencoding) subprocess option is binary, or when the [`binary`](api.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.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 53–65why, one step back
53
54However, 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## Streams65

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

line 66why, one step back
66[Streams produced](streams.md#converting-a-subprocess-to-a-stream) by [`subprocess.readable()`](api.md#subprocessreadablereadableoptions) and [`subprocess.duplex()`](api.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`](api.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).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 67–79why, one step back
67
68```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)