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

@geut/saga

v1.0.0

Published

A helper module to share operations between peers built on top of hyperdb.

Downloads

5

Readme

@geut/saga

A helper module to share operations between peers built on top of hyperdb.


Install

npm install @geut/saga

Usage

First, instantiate saga:

const saga = Saga(ram, publicKey, username);

Then, we will need a swarm of peers:

const sw = swarm({
  id: username,
  stream: () => {
    return saga.replicate();
  }
});

sw.join(signalhub(discoveryKey, signalUrls), webrtcOpts);

sw.on('connection', async peer => {
  try {
    await saga.connect(peer);
  } catch (err) {
    console.log(err);
  }
});

Hint: You can use @geut/discovery-swarm-webrtc.

Hint2: You will need a signal server, like signalhubws.

After that, you are ready to use saga. This include, sending and receiving operations that you can apply on each peer.

API

initialize

Returns a promise.

Used to trigger hyperdb creation and setup the watch for operations.

connect

Peer | Required. The peer must have a remoteUserData property.

This will authorize the peer to be a writer. Emits a join event along with peer data. Finally, if the peer leaves, it will also emit a leave event, indicating the peer.

replicate

It will replicate the stream. Uses hyperdb replicates method under the hood with some fixed values:

{
  live: true,
  userData: JSON.stringify({
    key: this.db.local.key,
    username: this.username,
    timestamp: this.timestamp
  })
}

writeOperation

Object | Optional

Use this method to send a new operation to every peer. It will also add the username of the sender and a timestamp.

Events

saga inherits from node.js EventEmitter. It will emit the following events:

join

peerData: Object

After authorizing a new peer, saga will emit a join event with some peer data. This is triggered by the connect method (see above).

leave

peerData: Object

After a peer leaves, the leave event will be emitted with some peer data.

operation

Object

The operation event will be emitted after reading feed history changes (see hyperdb) caused by new operations arrival. You can listen to this event to retrieve the latest operations that you can apply to regenerate the distributed state between peers. The event will contain an object with the username of the sender, a timestamp and the operation.

Motivation

The idea came up after playing with olaf, a P2P Dat powered chat application. That is when saga first appears. Later we were playing with the idea of CRDT based editor, also Dat powered. I wanted to re-use some parts and saga was my first option. I only need to make some subtle changes, like mostly renaming messages to operations in order to be more generic.

When creating P2P apps, the absence of a centralized server, empowers peers (former clients). Since now they can share data between each other, it is useful to have a way to re-create locally changes that have happened in another peer, this is were you can use saga. Also keep in mind that libraries like Automerge are a great match!


Brought to you by GEUT LABS ʘ