BERT
Binary ERlang Term serialization library for Javascript. (An updated version of this repo )
Usage
Example Usage
When needing to consume data in Javascript from an Erlang system, the Erlang
system can simply send encoded binary data:
Elixir/Erlang:
# This is Elixir code, but function calls will be very similar in Erlang
personData = %{
name: "Bob",
age: 32,
eye_color: "Brown",
personality_traits: [
"Funny",
"Inquisitive"
]
}
# Convert to binary
:erlang.term_to_binary(personData)
# .... Code that sends binary data to javascript
Javascript:
const Bert = require('bert-elixir')
const decodedPerson = Bert.decode(personData)
Modifying this data and sending it back to Erlang/Elixir would be as simple as:
Javascript:
decodedPerson.age = 38
decodedPerson.name = 'Robert'
const reEncodedPerson = Bert.encode(decodedPerson)
Elixir/Erlang:
# ... After having received binary data and setting it to variable modifiedPersonData:
decodedPerson = :erlang.binary_to_term(modifiedPersonData, [:safe])
# => %{ age: 38, eye_color: "Brown", name: "Robert", personality_traits: ["Funny", "Inquisitive"] }
safe option should be always used when decoding an untrusted input, make also sure to have already all required atoms in the atoms table.
Encoding
Maps (Elixir)
Javascript objects map directly to Maps in Erlang
const Bert = require('bert-elixir')
const mapToEncode = { a: 1, b: "hello!", c: [1, 2, 3] }
const encodedMap = Bert.encode(mapToEncode)
console.log(Bert.binaryToList(encodedMap))
Lists
Javascript arrays map to Erlang Lists
const Bert = require('bert-elixir')
const arrayToEncode = ['hello', 'world', 32, [{ key: "value" }]]
const encodedArray = Bert.encode(arrayToEncode)
console.log(Bert.binaryToList(encodedArray))
Todo:
Decoding
Decoding is typically much simpler than encoding. Just pass the given Binary Erlang Term:
const Bert = require('bert-elixir')
const erlangTerm = [131, 116, 0, 0, 0, 3, 100, 0, 1, 97, 97, 1, 100, 0, 1, 98, 97, 2, 100, 0, 1, 99, 116, 0, 0, 0, 1, 100, 0, 4, 119, 111, 97, 104, 109, 0, 0, 0, 8, 97, 32, 115, 116, 114, 105, 110, 103]
.map(x => String.fromCharCode(x)).join('')
const decoded = Bert.decode(erlangTerm)
console.log(decoded)