Skip to content
Myr

binary.md

This file

Path

docs/binary.md

82 lines · 4.0 KiB · Markdown

Last changed by

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 1–32why, 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 input1011There 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).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](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).2526```js27const {stdout} = await execa({encoding: 'buffer'})`zip -r - input.txt`;28console.log(stdout.byteLength);29```3031## Encoding32

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

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#RFC_4648). The output will be a string then.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 34–63why, one step back
34
35```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`](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.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`](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.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```63

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 64–66why, one step back
64> [!NOTE]
65> With the [`binary`](api.md#transformoptionsbinary) transform option, a `string` a transform yields is always converted to bytes using UTF-8, since the transform deals with raw bytes. Therefore, if the [`encoding`](api.md#optionsencoding) option is a different text encoding like `'utf16le'`, those bytes are then decoded with that encoding, which corrupts the text. Either `yield` an `Uint8Array` instead, or do not use the `binary` transform option.66

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 67–82why, one step back
67## Streams
6869[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).7071```js72const readable = execa`npm run build`.readable({binary: false});73readable.on('data', lineString => {74 /* ... */75});76```7778<hr>7980[**Next**: 🧙 Transforms](transform.md)\81[**Previous**: 📃 Text lines](lines.md)\82[**Top**: Table of contents](../readme.md#documentation)