INTRO
The bert.js module provides JavaScript encoder/decoder for External Term Format used in Erlang distribution protocol. That means your JavaScript applications talk to Erlang natively. This module uses DataView and getUint8, getUint16, getUint32 word accessors which is the fastest way of dealing with binaries in JavaScript. The library is used in Erang and Haskell N2O versions. The size of bert.js is 4654 bytes.
Note that this library is low-level, fast version of BERT encoder/decoder. If you want to generate JavaScript SDK with field names as in Erlang include files, please take RPC as high-level counterpart library that generates wrappers around bert.js and provides more slick JavaScript experience.
Erlang structure that compiles to BERT format:
Target structure of bert.js — fast low-level handcoded parser from BERT format.
Target structure of generated parser with RPC parse transform:
The following practical subset of BERT format is supported:
70
IEEE-754 float encoding. A float is stored as 8 bytes in big-endian IEEE format. This term is used in minor version 1 of the external format.
97
Byte encoding. Unsigned 8-bit integer.
98
Integer encoding. Signed 32-bit integer in big-endian format.
99
Float POSIX encoding. A float is stored in string format. The format used in sprintf to format the float is "%.20e" (there are more bytes allocated than necessary). To unpack the float, use sscanf with format "%lf".
100
Atom encoding. An atom is stored with a 2 byte unsigned length in big-endian order, followed by N numbers of 8-bit Latin-1 characters that forms the name. The maximum allowed value for Len is 255.
104
Tuple encoding. The Arity field is an unsigned byte N that determines how many elements that follows in section Elements.
105
Large tuple encoding. Same as 104 except that N is an unsigned 4 byte integer in big-endian format.
106
Nil.
107
String encoding. As field N is an unsigned 2 byte integer (big-endian), implementations must ensure that lists longer than 65535 elements are encoded as 108.
108
List encoding. N is the number of elements that follows in section Characters. Tail is the final tail of the list; it is 106 for a proper list, but can be any type if the list is improper (for example, [a|b]).
109
Binary encoding. Binaries are generated with bit syntax expression or with erlang:list_to_binary/1, erlang:term_to_binary/1, or as input from binary ports. The N length field is an unsigned 4 byte integer (big-endian).
110
Small bignum encoding. Bignums are stored in unary form with a Sign byte, that is, 0 if the binum is positive and 1 if it is negative. The digits are stored with the least significant byte stored first. To calculate the integer, the following formula can be used:
B = 256,
d0*B0
+ d1*B1
+ d2*B2
+ ... dN-1*B(n-1)
111
Large bignum encoding. Same as 110 except that the length field is an unsigned 4 byte integer.
115
Small atom encoding. An atom is stored with a 1 byte unsigned length, followed by N numbers of 8-bit Latin-1 characters that forms the Characters.
116
Maps encoding. The N field is an unsigned 4 byte integer in big-endian format that determines the number of key-value pairs in the map. Key and value pairs (Ki => Vi) are encoded in section Pairs in the following order: K1, V1, K2, V2,..., Kn, Vn. Duplicate keys are not allowed within the same map. Requires OTP 17.
118
UTF8 atom encoding. An atom is stored with a 2 byte unsigned length in big-endian order, followed by N bytes containing the Characters encoded in UTF-8.
119
UTF8 small atom encoding. An atom is stored with a 1 byte unsigned length, followed by N bytes containing the Characters encoded in UTF-8. Longer atoms encoded in UTF-8 can be represented using 118.
API
enc(json)
Encodes internal JSON to binary buffer.
dec(buffer)
Decodes binary buffer to internal JSON.
bin(x)
Creates JSON for binary encoding.
atom(x)
Creates JSON for Latin-1 atom encoding.
string(x)
Creates JSON for string encoding.
float(x)
Creates JSON for IEEE-754 float encoding.
number(x)
Creates JSON for integers and GMP big numbers encoding.
list(x,...)
Creates JSON for list encoding.
tuple(x,...)
Creates JSON for tuple encoding.
map(x,...)
Creates JSON for map encoding.
You may also want to read: utf8.js, ieee754.js, heart.js, nitro.js, mq.js, n2o.js.