Skip to main content

bytes_like

Function bytes_like 

Source
pub fn bytes_like(v: &Value) -> Option<Vec<u8>>
Expand description

The bytes of any byte-like source: a JS array of byte values, another Buffer, ANY typed array, or an ArrayBuffer. None for anything else (a string, which every caller handles with its own encoding rules).

A Buffer is a Uint8Array subclass, so every place that accepts a Buffer accepts a typed array too. Each of Buffer.from, Buffer.concat, buf.equals and buf.indexOf had its own notion of “byte source” and all four understood @@bytes only: passing a Uint8Array made from fall through to the STRING path and produce the bytes of "[object Object]", made concat contribute nothing, and made equals/indexOf silently miss. Routing them all through one helper is what keeps them from drifting again.

Element values are truncated to a byte each, which is what Node does: Buffer.from(new Int32Array([1, 2, 300])) is <Buffer 01 02 2c>.