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

beson

v3.1.5

Published

Yet an another binary representation of json format

Downloads

116

Readme

BESON - Binary Extended JSON

Beson library is an es6 compatible library that allows users to encode and transfer javascript data with binary format. Beson library is similar to BSON format used in mongodb. The major difference between beson and bson is that beson allows primitive data to be encoded directly. Beson is designed to transfer or store data in a binary format, not specialized for database storage.

How to use

For Browser

Just use the following es6 module import statement.

<script type='module'>
    import {Serialize, Deserialize} from "https://cdn.jsdelivr.net/gh/jcloudyu/beson/beson.esm.js";
    ...
</script>

For NodeJS

Beson contains two different versions, 0.9.X and 1.x or greater. [email protected] is completely written in es module and 0.9 is written using CommonJS modules.

CommonJS Module ([email protected])

You can simply use the following statement in NodesJS to use beson.

const { Deserialize, Serialize } = require('beson');
...

Run Unit Test

node --experimental-modules --loader ./.loader.mjs unit-test.mjs

ES Module ( [email protected] )

Since that the module is completely written in es module, NodeJS requires more steps to make beson work! NodeJS requires ESM Resolve Hook to load modules ended with .esm.js. This repo contains the predefined loader script for NodeJS developers who doesn't want to write one...

Assume you have have the following loader script and store at "[PATH]/loader.mjs"

// This file is to provide simple straitforawrd logic that makes
// NodeJS process modules ended with .js. Using this loader the 
// modules ended with .esm.js will be treated as es modules.
import path from 'path';
import process from 'process';

const BaseURL = new URL("file://");
BaseURL.pathname = `${process.cwd()}/`;

export function resolve(specifier, parentModuleURL, defaultResolve) {
	if (specifier.substr(-7) === ".esm.js") {
		return {
			url: new URL(specifier, parentModuleURL||BaseURL).href,
			format: 'esm'
		};
	}
	
	return defaultResolve(specifier, parentModuleURL);
}

And you have to use the following command line options to execute your script.

node --experimental-modules --loader [PATH]/loader.mjs [PATH TO YOUR VERY OWN SCRIPT]

Once you're prepared to run the script, you can use the following statement to use beson.

import {Deserialize, Serialize} from "beson";
...

Basic Usage

Beson is shipped with Serialize and Deserialize apis for developers who wants to encode and decode data.

Serialize

Just call Serialize with any data you want to encode, and an ArrayBuffer object contains the encoded data is returned...

import { Serialize } from 'beson';

// Encode primitive data
let buf_null = Serialize(null);                                 // null
let buf_bool = Serialize(false);                                // boolean
let buf_num  = Serialize(123456);                               // js number (double)
let buf_num2 = Serialize(Math.PI);                              // js number (double)
let buf_str  = Serialize('Hello World');                        // string ( encoded in UTF8 )

// Encode objects
let buf_date = Serialize(new Date());                           // Date
let buf_obj  = Serialize({                                      // Arbitrary object
    a:1, b:2, c:3, d:false, "5":"string"
});
let buf_arry = Serialize([                                      // Arbitrary array
    null, 1, false, Math.E, 'HI', [ 1, 2, 3 ]
]);

let buf_ab   = Serialize(new ArrayBuffer(24));                  // ArrayBuffer
let buf_ta1  = Serialize(new Uint8Array(24));                   // UInt8Array
let buf_ta2  = Serialize(new Uint16Array(24));                  // UInt16Array
let buf_ta3  = Serialize(new Uint32Array(24));                  // UInt32Array
let buf_ta4  = Serialize(new Int8Array(24));                    // Int8Array
let buf_ta5  = Serialize(new Int16Array(24));                   // Int16Array
let buf_ta6  = Serialize(new Int32Array(24));                   // Int32Array
let buf_ta7  = Serialize(new Float32Array(24));                 // Float32Array
let buf_ta8  = Serialize(new Float64Array(24));                 // Float64Array
let buf_dv   = Serialize(new DataView(new ArrayBuffer(30)));    // DataView Object

Deserialize

Just call Deserialize with any beson encoded ArrayBuffer data, the corresponding value is returned...

import { Deserialize } from "beson";

// Do the encode...
let buf = Serialize('Hello World');
let data = Deserialize(buf);
console.log(data); // Hello World

Some Extended Usage

Beson also provides other types that for developers who want to specifically controls the data they want to encode, such as uint32 or uint64. Following list are types that are provided by beson. ( These types can be found in Beson exports )

Int32 / UInt32
Int64 / UInt64
Int128 / UInt128
ObjectId
Binary

Friendly Reminders

  • This library is released under ISC license, and currently this library is still under development. So... use at your own risk!!!
  • We're currently focusing on improving [email protected]. So there may be some inconsistent behaviors between v0.9 and v1.x. When you encounter something awkward, don't hesitate to give us feed back, we'll fix the problem right away!