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

@cheprasov/jsbt

v1.2.3

Published

JSBT is a library for serializing structured JavaScript data. The library is JavaScript oriented and tries to resolve JS needs for better data serialization.

Downloads

11

Readme

MIT license

@cheprasov/jsbt (v1.2.3)

JSBT is a library for serializing structured JavaScript data. The library is JavaScript oriented and tries to resolve JS needs for better data serialization.

Features:

  • Super easy to use (serialization works straight away without using any predefined schemas or structure descriptions).
  • Supports main JS types: booleans, numbers (as integers or float), bigints, arrays, typed arrays, objects, sets, maps, symbols, dates.
  • Allows encode and decode Object Wrappers for primitive values.
  • Created for communication between services written at JavaScript / TypeScript. (Node <-> Browser, Node <-> Node, Browser <-> Browser and so on).
  • Optimized for performance and compact size.
  • Size optimisation for repeated key or values.
  • Supports circular data dependencies.
  • Supports serialization for linked objects.
  • Supports Streams:
    • Supports stream data decoding.
    • Possible to parse message before all data is received.
    • Allows to send several data structures via one stream message.
  • It is much powerful than JSON and could replace JSON for communication between microservices.
  • Written on TypeScript and supports types.
  • Could be implemented by other languages.
  • The small library does not have dependencies.

0. Specification

See specification

1. How to install

> npm install @cheprasov/jsbt
import { JSBT } from '@cheprasov/jsbt';

2. Examples

2.1 Encoding and decoding simple data

import { JSBT } from '@cheprasov/jsbt';

const user = {
    name: 'Alex',
    age: 38,
    country: 'UK',
    birthday: new Date('1984-10-10')
};

// Encode
const encodedUser = JSBT.encode(user);

// Decode
const decodedUser = JSBT.decode(encodedUser);

// Yes, it supports Date serialisation
console.log(decodedUser.birthday instanceof Date); // true

2.2 Encoding and decoding data with link refs

import { JSBT } from '@cheprasov/jsbt';
const users = {
    Alex: {
        name: 'Alex',
        age: 38,
        country: 'UK',
        children: null
    },
    Irina: {
        name: 'Irina',
        age: 40,
        country: 'UK',
        children: null,
    },
    Matvey: {
        name: 'Matvey',
        age: 2,
        parents: null,
    },
};

users.Alex.children = users.Irina.children = [users.Matvey];
users.Matvey.parents = [users.Alex, users.Irina];

// Encode
const encodedUsers = JSBT.encode(users);
console.log(encodedUsers.length); // 112

// Decode
const decodedUsers = JSBT.decode(encodedUsers);

console.log(decodedUsers)
console.log(decodedUsers.Alex.children === decodedUsers.Irina.children); // true
console.log(decodedUsers.Matvey.parents[0] === decodedUsers.Alex); // true
console.log(decodedUsers.Matvey.parents[1] === decodedUsers.Irina); // true

2.3 Encoding and decoding instances of a Class

For getting props for Encoding Class Instances it will be used first found of the following methods:

  • toJSBT()
  • toJSON()
  • valueOf()

Instance will be encoded like a object simple with props. Also the decoded object would have configurable, not enumerable and not writable prop __jsbtConstructorName with construcor name of encoded instance.

import { JSBT } from '@cheprasov/jsbt';

export class User {

    protected _name: string;
    protected _email: string;

    constructor(name: string, email: string) {
        this._name = name;
        this._email = email;
    }

    toJSBT() { // or toJSON
        return {
            name: this._name,
            email: this._email,
        };
    }

}

const user = new User('Alex', '[email protected]');

// Encode
const encodedUser = JSBT.encode(user);

// Decode
const decodedUser = JSBT.decode(encodedUser);

console.log(decodedUser);
// { name: 'Alex', email: '[email protected]' }

console.log('Construnctor Name: ', decodedUser.__jsbtConstructorName);
// Construnctor Name: User

3. How to use

3.1 Encoding data

import { JSBT } from '@cheprasov/jsbt';

const userData = {
    name: 'Alex',
    age: 38, 
    country: 'UK'
};

// Encode user data
const jsbt = JSBT.encode(userData);

3.2 Decoding data

import { JSBT } from '@cheprasov/jsbt';

const userData = {
    name: 'Alex',
    age: 38, 
    country: 'UK'
};

// Encode user data
const jsbt = JSBT.encode(userData); 

// Decode user data
const decodedUser = JSBT.decode(jsbt); 

3.3 Decoding stream data

import { JSBT } from '@cheprasov/jsbt';

const stream = new ByteStream();

// on loading add portions of message via stream.addMessages()

const loader = new DataLoader(); // Pseudo loader

// Add loaded data portion to stream
loader.onLoadPortionData((data: string) => {
    stream.addMessages(data);
});

// Add last data portion to stream and mark as received all data
loader.onCompleteLoading((data: string) => {
    stream.completeStream(data);
});

// Decode data via stream
const data = JSBT.decodeStream(stream); // returns Promise and it will be resolved on receiving enough bytes for decoding data structure

Something does not work

Feel free to fork project, fix bugs, write tests and finally request for pull