etf_js
Types
An error that occured while decoding the ETF encoded binary
pub type EtfDecodeError {
NotEtf
Invalid
AtomLengthInvalid
Compressed
PakoNotInstalled
Unknown(Dynamic)
DecoderError(List(decode.DecodeError))
}
Constructors
-
NotEtf
The given binary is not an ETF binary
-
Invalid
The given ETF binary has some invalidity
-
AtomLengthInvalid
An atom parsed from the binary has an invalid length (>255)
-
Compressed
Binary is compressed but the no_compression method was called
-
PakoNotInstalled
The
pako
library is not installed (from npm). This error occurs if both DecompressionStream and node:zlib are not available, usually in older browsers (<May 2023). -
Unknown(Dynamic)
An unknown error occured while parsing the binary. The enum argument is the error passed.
-
DecoderError(List(decode.DecodeError))
Only raised by the
decode*
functions – an error occured while decoding the returned object.
Functions
pub fn decode(
bits: BitArray,
decoder: Decoder(a),
) -> Promise(Result(a, EtfDecodeError))
Converts an erlang ETF encoded bitarray and decodes it using decoder
.
This function can also convert compressed bitarrays, and hence is asynchronous.
pub fn decode_non_compressed(
bits: BitArray,
decoder: Decoder(a),
) -> Result(a, EtfDecodeError)
Converts an erlang ETF encoded bitarray and decodes it using decoder
.
This function does not convert compressed bitarrays, and hence is synchronous.
pub fn loose_string() -> Decoder(String)
Decodes both JS strings and utf-8 BitArray
s.
This is required because in Gleam’s Erlang target, strings are represented as
BitArray
s, but in the JavaScript target, they are regular JS strings.
Since charlists are also returned as bit arrays, this will also convert charlists to strings (if they are valid strings).
pub fn to_dynamic(
bits: BitArray,
) -> Promise(Result(Dynamic, EtfDecodeError))
Converts an erlang ETF encoded bitarray into a JS object returned as a Dynamic.
pub fn to_dynamic_no_compression(
bits: BitArray,
) -> Result(Dynamic, EtfDecodeError)
Converts an erlang ETF encoded bitarray into a JS object returned as a Dynamic.
This method is synchronous and does not support compressed ETF binaries.