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

@organicdesign/crdts

v0.3.4

Published

A group of useful CRDTs implementing interfaces from @organicdesign/crdt-interfaces.

Downloads

10

Readme

CRDTs

A group of useful CRDTs implementing interfaces from @organicdesign/crdt-interfaces.

Table of Contents

Install

npm i @organicdesign/crdts

Usage

Each CRDT exposes a class and an instantiation method. Most of these CRDTs use a basic protocol encoded in CBOR by default and have room for improvement in regards to efficiency and specification.

// 'createCRDT' is a placeholder for a specific CRDT creation method.
const crdt = createCRDT({ id: new Uint8Array([1]) });

await crdt.start();

console.log(crdt.toValue());

If you are using these CRDTs with Libp2p you should use the peer ID as the CRDT id:

createCRDT({ id: libp2p.peerId.toBytes() });

You may also pass custom synchronizers, serializers and broadcasters via the options:

createCRDT({
	synchronizers: [createSynchronizer()],
	broadcasters: [createBroadcaster()],
	serializers: [createSerializer()]
});

These CRDTs provide methods for synchronization but of course will not automatically synchronize themselves, so you will need to create a system for synchronizing them or if you use Libp2p you can use @organicdesign/libp2p-crdt-synchronizer.

import { createCRDTSynchronizer } from "@organicdesign/libp2p-crdt-synchronizer";

const synchronizer = createCRDTSynchronizer()(libp2p);
const crdt = createCRDT({ id: libp2p.peerId.toBytes() });

await Promise.all([synchronizer.start(), crdt.start()]);

synchronizer.set("my-crdt", crdt);

G-Counter

import { createGCounter } from "@organicdesign/crdts";

const counter = createGCounter({ id: new Uint8Array([123]) });

counter.increment(1);

console.log(counter.toValue()); // 1

A grow only counter.

PN-Counter

import { createPNCounter } from "@organicdesign/crdts";

const counter = createPNCounter({ id: new Uint8Array([123]) });

counter.increment(5);
counter.decrement(2);

console.log(counter.toValue()); // 3

A positive/negative counter (one that can grow and shrink).

G-Set

import { createGSet } from "@organicdesign/crdts";

const set = createGSet({ id: new Uint8Array([123]) });

// Do anything you can do with a native Set.
set.add("value-1");

console.log(set.toValue()); // Set [ "value-1" ]

A grow only set.

MV-Register

import { createMVRegister } from "@organicdesign/crdts";

const register = createMVRegister({ id: new Uint8Array([123]) });

register.set("value-1");

console.log(register.toValue()); // [ "value-1" ]

A multi-value register. This register relies on logical time only and will return an array with all values that are set at the same time.

LWW-Register

import { createLWWRegister } from "@organicdesign/crdts";

const register = createLWWRegister({ id: new Uint8Array([123]) });

register.set("value-1");

console.log(register.toValue()); // "value-1"

A last write wins register. Uses hybrid time and IDs to ensure consistency.

LWW-Map

import { createLWWMap } from "@organicdesign/crdts";

const map = createLWWMap({ id: new Uint8Array([123]) });

// Do anything you can do with a Map
map.set("key-1", "value-1");

console.log(map.toValue()); // Map { "key-1" → "value-1" }

A last write wins map. Uses hybrid time and IDs to ensure consistency.

CRDT-Map

import { createCRDTMap, createGCounter } from "@organicdesign/crdts";

const map = createCRDTMap({ id: new Uint8Array([123]) });
const counter = createGCounter({ id: new Uint8Array([123]) });

map.set("key-1", counter);
counter.increment(1);

console.log(map.toValue()); // Map { "key-1" → 1 }

A map containing only CRDTs as it's values. You will need to ensure each instance of this type has the same structure of values.

Building

To build the project files run:

npm run build

Testing

To run the tests:

npm run test

To lint files:

npm run lint