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

syncod

v0.0.1

Published

Encode JavaScript objects into flat strings dictionary. Synchronise complex nested structures over the network with minimal overhead.

Downloads

240

Readme

syncod

Encode JavaScript objects into flat strings dictionary (similar to the dictionary coder one). Since the dictionary extends only for new data, it can naturally be used to synchronise complex nested objects between two applications over the network. Works both for mutable and immutable cases and supports circular references.

Quick start

Install

npm i syncod

Encoder

import { Encoder, sha1 } from "syncod";
const encoder = new Encoder(sha1);

const users = [{ id: 1 }, { id: 2 }, { id: 3 }];
const articles = [
  { id: 1, user: users[2] },
  { id: 2, user: users[2] },
  { id: 3, user: users[0] }
];
const state = { users, articles };

const state_enc_1 = encoder.encode(state);
const dict_1 = encoder.commit();

// Send state_enc_1 and dict_1 to Decoder side

state.me = users[0];
users.splice(1, 1);

const state_enc_2 = encoder.encode(state);

// There is no need to make commit right after encoding
// Let's create and encode a new object first

const all_users = users.slice();
all_users.push({ id: 2 });
const all_users_ref_2 = encoder.encode(all_users);

const dict_2 = encoder.commit();
// Dictionary contains only records for the new (all_users) and changed (state, users) objects.
// The recreated object { id: 2 } is regarded as existing and is not put in the dictionary
console.log(dict_2);

// Send state_enc_2, all_users_ref_2 and dict_2 to Decoder side

Decoder

import { Decoder, sha1 } from "syncod";
const decoder = new Decoder();

// enc_1 and dict_1 are received from Encoder

decoder.assign(dict_1);
console.log(decoder.decode(state_enc_1));

// state_enc_2, all_users_ref_2 and dict_2 are received from Encoder

decoder.assign(dict_2);
console.log(decoder.decode(state_enc_2));
console.log(decoder.decode(all_users_ref_2));

syncod VS JSON

JSON is just fine if you plan to submit the object just once, but it consumes too much bandwidth if you sync data periodically (or even on each update). Moreover, JSON doesn't support circular references.

syncod VS flatted

flatted is like JSON but with circular references. The output array changes significally even on small changes of the object, hence there is no way to calculate nice diffs.

syncod VS deepdiff

There are a bunch of libraries for deep differences calculation (e.g. deep-diff), but you still have to serialize the result somehow before sending over the wire, solving issues with mutability/immutability and circular references.

Usage

Create your Encoder and Decoder instances (normally, they will be inside different applications).

There are two arguments for Encoder constructor.

First argument (required) is any hash function, which takes string and returns string. The only restriction is the result characters should be inside U+0000 - U+D7FF range . Package is served with simple sha1 hash function, which can be used by default.

Second argument (optional) is the minimal length for the string to be saved into the dictionary (by default all strings are put inline). Useful if you have big text values inside your data.

import { Encoder, Decoder, sha1 } from "syncod";

const encoder = new Encoder(sha1, 50);
const decoder = new Decoder();

Now encode the objects and make commit to get the new table entries and send them to the decoder. If the object was encoded, it (or objects inside it) should not be changed before the commit call

const enc_1 = encoder.encode(obj_1);
const enc_2 = encoder.encode(obj_2);
// obj_1.key = "value" here can break the data
const dict = encoder.commit();

assign the dictionary to the encoder and decode the data

decoder.assign(dict);
console.log(decoder.decode(enc_1));
console.log(decoder.decode(enc_2));

Requirements

Object.keys and the basic support of Map and Set are required (can be polyfilled).