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

swarm-syncable

v1.2.1

Published

Swarm: replicated data types (syncables) ========================================

Downloads

88

Readme

Swarm: replicated data types (syncables)

This package contains Swarm replicated data types (RDTs) defined over a partially ordered log of operations (POLO, also hyperlog). Practically, this is Swarm client core (see test/ for APi use examples).

  • [x] OpStream - a stream of operations, our most fundamental abstraction
  • [x] Syncable (+RDT) - the abstract base class for all syncables
  • [x] Host - a container for Syncables, handles all the server/peer sync
  • Basic syncable types
    • [x] LWWObject - a last-write-wins object
      • merge is per-field, the bigger timestamp wins
      • can only have primitive fields (strings, numbers, references to other syncables)
      • stream-friendly (field name is mentioned in the specifier)
      • the metadata object (type /Swarm) is actually an LWWObject
    • [ ] LWWArray - last-write-wins array/matrix
      • can't add/remove rows/columns
      • primitive fields
    • [ ] LWWJSON - a deep JSON object
      • deep merge
    • [ ] Counter - (CRDT) an integer that is modified by increments/decrements
    • [ ] ORSet - (CRDT) a set of JSON objects
      • arbitrary on-demand client-side sorting

In Swarm, everything is an OpStream:

  • a database is a partially ordered stream of ops (p.o. log, hyperlog)
  • a Peer is an instance the database, it only has its local linear arrival order,
  • a Host is a subset of the full log, as a client only subscribes to some objects,
  • an object is a partially ordered stream of ops too (a database has many objects), and finally
  • a Syncable is an instance of an object, having its own local linear arrival order.

Hence, Peer, Host and Syncable implement the OpStream interface. As OpStream is asynchronous, any network transport or storage implements that interface too.

A Syncable object is split into two parts:

  • RDT, the inner state machine that implements all the math,
  • a Syncable: the outer JavaScript API, including all the write, query and listen methods.

General RDT state-machine-like rules are:

  • every RDT starts at the zero (default) state of its respective type;
  • every mutation to an RDT is serialized as an immutable atomic operation (op);
  • every such op is eventually delivered to every replica of the object;
  • the order of delivery may vary, but it never violates causality;
  • once all the ops reach all the replicas, their states converge.

Each Syncable is synchronously connected to its Swarm Host. A changed Syncable submits an op requests to its host, so the host creates an immutable op and feeds it back to the Syncable and its RDT. A hostless Syncable is read-only, although you can feeds it ops manually.

Syncable's id is typically a Lamport timestamp of the object creation event. Global objects may have transcendent (zero origin) Base64x64 ids.

Syncable.Ref is a small class used as a wrapper for a reference (i.e. one syncable referring to another).

INTEGRATE, MAKE EXAMPLES

The RDT object change sequence is:

  1. API user calls an API method (like Model.set) that
  2. prepares and submits an op request to the Host that
  3. creates a timestamped immutable op and feeds it back to the RDT object that
  4. adjusts its state based on those changes.
  5. (the Host also relays the op to other replicas, that apply it too, etc etc)

Host acts as a registry of all the sessions' syncables and a keeper of the clock. In the Lamport's model terms, it is a "process" (see swarm-host).

A Swarm sync client. Use dependency injection for any particular storage or transport method (LevelUP and stream-url based, respectively).

API use example:

var SwarmClient = require('swarm-client');
require('stream-url-ws');

var swarm = new SwarmClient({
    user_id: 'joe',
    db_id:   'db',
    connect: 'ws://server.com:1234/swarm',
    callback: onConnected
});

function onConnected (err) {
    // rehydrate, subscribe, rerender, etc etc
    var obj = swarm.get('/Type#id', onObjectLoad);
}

function onObjectLoad () {
    // the data is live
}

for working examples, see the swarm-examples package. for a server-side daemon, see swarm-server.