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

@otpjs/serializer-ertf

v0.17.0

Published

Erlang External Term Format serializer for otpjs

Downloads

37

Readme

Open Telecom Platform on JS

ERTF Serialization

This module provides serialization/deserialization functionality for Erlang's External Term Format.

Installation

npm i @otpjs/serializer-ertf

Usage

Initializing

In order to be able to properly serialize/deserialize Pids and Refs, you must initialize @otpjs/serializer-ertf with an instance of Node from @otpjs/core.

import { Node } from '@otpjs/core';
import makeERTF from '@otpjs/serializer-ertf';

const node = new Node();
const ERTF = makeERTF(node);

The options second options parameter provides customization of the behavior.

Options
stringsAsBinaries (default: false)

When encoding/serializing, encode strings as binaries instead of lists.

binariesAsStrings (default: false)

When parsing/deserializing, convert binaries to UTF-8 strings.

serialize/encode

Encodes a JavaScript value as an Erlang term stored in a Buffer. Understands types from @otpjs/types as well.

import { t, l } from '@otpjs/types';

// Integers will serialize into the fewest number of bytes. BigInts are also supported.
ERTF.serialize(1); // <Buffer 83 61 01>
ERTF.serialize(100000000000); // <Buffer 83 6e 08 00 00 00 00 00 17 48 76 e8>
ERTF.serialize(100000000000000000000n); // <Buffer 83 6e 0a 00 00 00 05 6b c7 5e 2d 63 10 00>

// Floats will serialize into doubles.
ERTF.serialize(1.33333); // <Buffer 83 46 3f f5 55 51 d6 8c 69 2f>

// By default, strings will serialize into LISTS, which will become evident during deserialization
const buff = ERTF.serialize('hello, world!'); // <Buffer 83 6b 00 0d 68 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21>
ERTF.deserialize(buff); // [ 104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33 ]

// ...with the `stringsAsBinaries` option set to `true`, you will receive a buffer instead
const buff = ERTF.serialize('hello, world!'); // <Buffer 83 6d 00 00 00 0d 68 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21>
ERTF.deserialize(buff); // <Buffer 68 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21>

// ...with the 'binariesAsStrings' option set to also `true`, you will receive a UTF-8 string
const buff = ERTF.serialize('hello, world!'); // <Buffer 83 6d 00 00 00 0d 68 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21>
ERTF.deserialize(buff); // 'hello, world!'

deserialize/parse

Decodes a Buffer containing an ERTF-encoded value into native Javascript types.

import { t, l } from '@otpjs/types';

ERTF.deserialize(Buffer.from([0x83, 0x61, 0x01])); // 0x1
ERTF.deserialize(
    Buffer.from(
        [0x83, 0x6e, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x48, 0x76, 0xe8]
            .map(String.fromCharCode)
            .join('')
    )
); // 100000000000n

Complex Type Support

Current support for complex Javascript types is limited to the types published in @otpjs/types, including Tuple and List.

Support for maps (raw Object and Map instances) is forthcoming. Seeking input on how to treat Array: is it a tuple or a list? Neither is a perfect fit.