erlb.js

Javascript binary support of Erlang External Term Format

BSD-3-CLAUSE License

Stars
7

erlb.js

Javascript binary support of Erlang External Term Format (ETF)

Installation

git clone https://github.com/saleyn/erlb.js.git

Include erlb.js to your project.

Interface

  • Erl.encode(obj, opts) - Encode a Javascript object into ETF, return ArrayBuffer suitable for sending via websocket. The object can be a Boolean, Integer, Float, String, Array, Object (treated as a proplist) or an Atom, Binary, or Tuple (with the help of Erl.atom(), Erl.binary(), or Erl.tuple(), respectively). The opts argument is optional and may contain the following settings:

    Value Values Default Description
    mapKeyType 'atom'/'binary'/'string' 'binary' Encode object's map keys using this data type
  • Erl.decode(arrayBuffer) - Decode a binary ArrayBuffer into a Javascript object.

  • Erl.equals(obj1, obj2) - Compare Obj1 and Obj2 for equality.

  • Erl.toString(obj, opts) - Print a Javascript object in the Erlang notation. The opts argument is optional and may contain the following settings:

    Value Values Default Description
    compact true/false false Print binaries in compact form (using backticks instead of <<"">> enclosures
  • Erl.atom(string) - Create a Javascript object that will be encoded to an Atom.

  • Erl.binary(uInt8Array) - Create a Javascript object that will be encoded to an Binary.

  • Erl.tuple(array) - Create a Javascript object that will be encoded to a Tuple.

  • Erl.pid(node, id, ser, creation) - Create a Javascript object that will be encoded to a Pid.

  • Erl.ref(node, creation, ids) - Create a Javascript object that will be encoded to a Ref.

The library natively understands the following Javascript types:

  • Number (integer, float)
  • String
  • Boolean
  • Array (Erlang list)
  • Object (Erlang proplists)

Utility Functions

  • Erl.toArrayBuffer(array) - Convert array of bytes to a binary buffer.
  • Erl.bufferToArray(arrayBuffer) - Convert a binary buffer to an array of bytes.
  • Erl.timestampToTuple(int) - Convert a timestamp (number of ms since epoch) to
    {megaSec, sec, microSec} tuple.
  • Erl.dateToTuple(date) - Convert a Javascript Date to
    {megaSec, sec, microSec} tuple.

Testing

You can use provided bin/simple_http_server to serve the content of the current directory to a web browser:

$ bin/simple_http_server 8000

Point your browser to http://localhost:8000/erlb-test.html. The page automatically runs all unit tests defined in the erlb-test.js script.

Author

Serge Aleynikov <saleyn at gmail dot com>

License

BSD License (see included LICENSE file)