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

crosstree

v0.0.3

Published

Compact, dependency-free, data synchronisation library for TypeScript and JavaScript.

Downloads

1

Readme

_:{ CrossTree

Compact, dependency-free, data synchronisation library for TypeScript and JavaScript.

What is it?

CrossTree provides a simple mechanism for conflict-free synchronisation of tree structures that is commutative, associative, and idempotent. It's transport independent, so you can synchronise changes over HTTP, WebSockets, peer-to-peer.

Data Structure

CrossTree data is made up of entry nodes. Every entry node can have sub-entries (a.k.a. children). There is a single root entry.

Every entry can have a:

  • value - v attribute
  • counter - c used to indicate the entry's version count or used as a numeric value.
  • timestamp - t when the entry was modified according to the local system clock (see exceptions below).
  • deletion flag - d means the entry was marked as deleted. Entries with no value often contain subentries, while entries with an explicit deletion flag can be ignored when traversing the tree.

Subentries must have an:

  • id - which is used to identify a subentry, but is not part of the entry's state.

Examples:

  • Use setValue on a tree path:
const treeA = new CrossTree();
treeA.subpath(["alice", "firstName"]).setValue("Alice");
treeA.subpath(["alice", "lastName"]).setValue("Bloggs");
let aliceData = treeA.toData();

aliceData ->

{
  _: {
    alice: {
      _: {
        firstName: { v: "Alice", t: 1572130604728 },
        lastName: { v: "Bloggs", t: 1572130604728 }
      }
    }
  }
}
  • Merge data with another replica:
const treeB = new CrossTree({ _: { bob: { _: { firstName: { v: "Bob", t: 1571241715839 } } } } });
treeB.mergeData(aliceData); // receive Alice's data
treeB.subpath(["alice", "firstName"]).getValue(); // -> Alice
let bobData = treeB.toData();

bobData ->

{
  _: {
    bob: {
      _: {
        firstName: { v: "Bob", t: 1571241715839 }
      }
    },
    alice: {
      _: {
        firstName: { v: "Alice", t: 1572130604728 },
        lastName: { v: "Bloggs", t: 1572130604728 }
      }
    }
  }
}
  • Send changes by observing tree:

treeA.observeTree( (event) => {
    const { change, before, after } = event;
    sendToBob( change );
    // change.data - new entry data
    // change.path - path of changed entry
});

Use Cases

  • File Structure
{ v: "My Repo"
  _: {
   "README.md": { v: "blob:1234", t: 1571200000000  },
   "package.json": { v: "blob:9876", t: 1572300000000 },
   "src": {
      v:"dir", t: 1573400000000,
      _:{
          "index.ts": { v: "text:hello", t: 1574500000000 }
      }
   }
}}
  • Records
{ _:{
  "6fa459ea": {
    _:{
      "name": { v: "Alice", t: 1571200000000 },
      "type": { v: "admin", t: 1571200000000 },
    }
  },
  "ee8a3ca4": {
    _:{
      "name": { v: "Bob", t: 1574500000000 },
      "type": { v: "guest", t: 1574500000000 },
    }
  },
}}
  • Spreadsheet
{ v: "Sheet 1"
  _: {
   "A1": { v: "Column 1", t: 1571200000000 },
   "A2": { v: "613423", t: 1572300000000 },
   "A3": { v: "875243", t: 1572300000000 },
}}

Merge Rules

Every value change requires a new entry state, which is merged into the local replica and can be sent to other replicas. At each tree path, the entry states are ordered and the "greatest" entry overwrites any previous entry. Thus a local value change needs to generate an entry state "greater" than the previous state. Once the new entry is merged into the tree, the old entry is discarded.

States are ordered by:

  • timestamp (t)
  • counter (c)
  • value (v)

A new entry state (a) is considered greater than (b) when:

  • a.t > b.t - its timestamp is greater or when:
  • (a.t == b.t && a.c > b.c) - timestamps are equal, but its counter value is greater or when:
  • (a.t == b.t && a.c == b.c && a.v > b.v) - timestamps and counters are equal, but its value is greater

Union Properties

Sample data:

A={_:{"alice":{t:15000,v:"red"}}}

B={_:{"alice":{t:16500,v:"green"}}}

C={_:{"bob":{t:15000,v:"blue"}}}

  • Commutativity: The order of changes sent betwen CrossTree instances doesn't impact the resulting state.

A∪B=B∪A={_:{"alice":{t:16500,v:"green"}}} - B's timestamp is larger, thus it overwrites A

  • Idempotence: Changes or whole replicas can be re-merged multiple times without duplication.

A∪A=A={_:{"alice":{t:15000,v:"red"}}} - two identical records of "alice" merge into one.

  • Associativity: Changes can be applied opportunistically as the network allows or on demand. The timing or order will not impact the resulting state.

(A∪B)∪C=A∪(B∪C)={_:{"alice":{t:16500,v:"green"},"bob":{t:15000,v:"blue"}}}

Three-way Merge

When synchronising with a remote replica, it's still possible to perform a manual three-way merge, as long as you retain the last state sent to the server (common ancestor), compare it with the local state and the remote state. However, whenever possible, you should design conflict-free structures, by putting conflict prone values into separate entry paths and deriving the final information. For more examples see this article.

Limitations

  • Node ID needs to be a string compatible with JavaScript's Object keys, as entry data is stored internally using Object.
  • Data passed to the CrossTree constructor (new CrossTree(data)) is mutated, when CrossTree is performing writes. If you don't want CrossTree to mutate the passed data object, use the tree.mergeData(data) method on an empty tree or perform a deep-clone before passing it to the constructor.

Related

https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type https://medium.com/bpxl-craft/building-a-peer-to-peer-whiteboarding-app-for-ipad-2a4c7728863e