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

erlang

v1.0.0

Published

Erlang interoperability with Javascript

Downloads

16

Readme

erlang.js

Basic Erlang and Node.js interoperability

Erlang.js is a simple JavaScript library to support the standard Erlang data encoding format (term_to_binary).

If you send data to an Erlang program, and that program will run binary_to_term(), then you need Erlang.js.

Erlang.js was spun off from production code used at Iris Couch. Erlang.js is available as an npm module.

npm install erlang

Examples

So far, only term_to_binary is implemented. Pass a data structure to term_to_binary() and it will return a Node.js buffer of that data, encoded.

Data Types

Some data types encode as you would expect: numbers, arrays, and booleans.

But Erlang has foreign data types. Erlang.js supports a simple mechanism to represent those in JavaScript.

Strings

JavaScript strings are converted to Erlang strings (i.e. lists of integers).

term_to_binary("I am a string") // Encodes the Erlang string "I am a string"

Atoms

{atom:"Foo"} encodes as Erlang 'Foo'. You can also use a shorthand, {a:"Foo"}.

term_to_binary({a:"POST"}) // Encodes the atom 'POST'

Binaries

{binary:"Foo"} encodes as Erlang <<"Foo">>. You can also use a shorthand, {b:"Foo"}.

term_to_binary({b:"POST"}) // Encodes the binary <<"POST">>

Tuples

{tuple:["foo", "bar"]} encodes as Erlang {"foo", "bar"}. You can also use a shorthand, {t:["foo", "bar"]}.

var foo = {a:"foo"} // an atom
term_to_binary({t:[foo, "bar"]}) // Encodes the tuple {foo, "bar"}

Optlists

In JavaScript, if an API has tons of options, it tends to use an object.

// An API with so many options it uses a single object argument:
request({url:"http://jsonip.com", json:true}, my_callback)

Erlang does not have this (for one thing, all data is always immutable). Instead it uses option lists (optlists) of mostly atoms and tagged tuples.

Options = [set, public, {keypos, 1}, {write_concurrency, true}, compressed],
ets:new(my_table, Options).

Optlists are common in Erlang. You can build them manually with term_to_binary(), but the data structure is messy and distracting. Erlang.js provides an alternative, optlist_to_binary() which encodes more readable and obvious JavaScript data structures.

optlist_to_binary() encodes its arguments as an array. Each argument must be:

  1. A short string (all-lowercase, up to 255 characters): converted to an atom
  2. A 2-array where the first element is a short string: converted to a tagged tuple
  3. An object with only one key/val pair: converted to a tagged tuple

To encode the optlist from the above Erlang example:

optlist_to_binary('set', 'public', ['keypos', 1], {write_concurrency:true}, 'compressed')

/* Same as
term_to_binary([ { atom: 'set' }
               , { atom: 'public' }
               , { tuple: [ { atom: 'keypos' }, 1 ] }
               , { tuple: [ { atom: 'write_concurrency' }, true ] }
               , { atom: 'compressed' }
               ])
*/

License

Apache 2.0

See the Apache 2.0 license.