npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bert-elixir

v1.0.4

Published

BERT (Binary ERlang Term) serialization library for Javascript

Downloads

10

Readme

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