Giter Site home page Giter Site logo

bert-elixir's Introduction

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:

// ... Code that receives binary data from erlang/elixir and stores it
// to a variable, personData

const Bert = require('bert-elixir')

const decodedPerson = Bert.decode(personData)
/*
  => { age: 32,
      eye_color: 'Brown',
      name: 'Bob',
      personality_traits: [ 'Funny', 'Inquisitive' ]
    }
*/

Modifying this data and sending it back to Erlang/Elixir would be as simple as:

Javascript:

// ... Assuming we have a decodedPerson object

decodedPerson.age = 38
decodedPerson.name = 'Robert'

const reEncodedPerson = Bert.encode(decodedPerson)

// ... Send the binary

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')

// To encode a javascript object to an elixir map:
const mapToEncode = { a: 1, b: "hello!", c: [1, 2, 3] }
const encodedMap = Bert.encode(mapToEncode)

// BinaryToList shows individual bytes as a javascript array
console.log(Bert.binaryToList(encodedMap))
// => [ 131, 116, 0, 0, 0, 3, 100, 0, 1, 97, 97, 1, 100, 0, 1, 98, 109, 0, 0, 0, 6, 104, 101, 108, 108, 111, 33, 100, 0, 1, 99, 108, 0, 0, 0, 3, 97, 1, 97, 2, 97, 3, 106 ]

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:

  • Write docs for rest of data types
  • Return nil as null instead of 'nil'
  • Add support for NEW_FLOAT_EXT

Decoding

Decoding is typically much simpler than encoding. Just pass the given Binary Erlang Term:

const Bert = require('bert-elixir')

// We're showing the term as an array of bytes here for clarity.
// You'll more likely have a string
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('') // Convert the array to a string

const decoded = Bert.decode(erlangTerm)

console.log(decoded)
// => { a: 1, b: 2, c: { woah: 'a string' } }

bert-elixir's People

Contributors

alexdovzhanyn avatar alexkwolfe avatar bettio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

bert-elixir's Issues

"Unexpected BERT type: 0"

I am getting the error:
Unexpected BERT type: 0

this is the buffer data:
[
131,
80,
0,
0,
2,
21,
120,
156,
149,
81,
75,
78,
195,
48,
16,
13,
81,
43,
96,
1,
103,
136,
44,
150,
53,
74,
27,
39,
78,
188,
130,
84,
137,
96,
195,
29,
156,
140,
243,
147,
227,
84,
113,
160,
173,
170,
30,
134,
155,
98,
183,
217,
192,
142,
213,
188,
153,
55,
191,
55,
211,
44,
192,
89,
30,
122,
41,
100,
239,
56,
206,
109,
47,
180,
230,
181,
144,
6,
47,
27,
215,
134,
238,
12,
201,
36,
87,
181,
117,
92,
161,
174,
81,
91,
162,
180,
69,
15,
29,
47,
10,
49,
178,
82,
182,
66,
77,
87,
214,
109,
193,
154,
167,
10,
40,
4,
156,
199,
56,
78,
226,
24,
147,
4,
8,
142,
33,
160,
24,
194,
96,
93,
144,
16,
40,
161,
209,
92,
49,
13,
214,
60,
175,
105,
226,
39,
180,
72,
112,
80,
209,
16,
147,
40,
137,
112,
17,
198,
27,
204,
41,
248,
126,
92,
110,
170,
10,
200,
139,
28,
74,
46,
155,
65,
207,
211,
22,
211,
113,
39,
46,
160,
108,
248,
212,
217,
221,
221,
230,
151,
172,
251,
97,
108,
235,
86,
225,
22,
174,
228,
31,
9,
143,
159,
163,
98,
135,
126,
183,
99,
186,
5,
230,
255,
95,
67,
215,
253,
158,
183,
40,
6,
56,
94,
22,
185,
105,
92,
184,
156,
176,
4,
62,
113,
203,
125,
159,
144,
22,
10,
196,
248,
193,
123,
129,
24,
122,
227,
163,
110,
188,
215,
154,
143,
123,
46,
209,
10,
125,
137,
17,
177,
245,
10,
181,
170,
26,
16,
59,
161,
249,
35,
38,
115,
70,
222,
187,
242,
6,
9,
94,
53,
140,
61,
159,
208,
217,
164,
130,
97,
67,
18,
6,
209,
54,
219,
166,
73,
22,
103,
36,
35,
52,
220,
68,
81,
30,
69,
105,
230,
167,
89,
158,
167,
166,
181,
189,
147,
201,
156,
196,
97,
50,
158,
253,
169,
241,
132,
50,
184,
213,
249,
222,
52,
169,
184,
212,
226,
220,
117,
63,
207,
174,
142,
147
]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.