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

@garbados/pouchdb-hypercore

v2.0.1

Published

Synchronize PouchDB or CouchDB with P2P Hypercores!

Downloads

3

Readme

pouchdb-hypercore

Build Status Coverage Status Stability NPM Version JS Standard Style

A PouchDB plugin that maps records in hypercores, a P2P append-only datastructure, to a PouchDB or CouchDB instance. The plugin allows you to follow changes in hypercores locally and remotely. In this way, you can build up a database from multiple P2P sources.

As an example, here's how to set up a PouchDB instance to follow a hypercore:

const Hypercore = require('hypercore')
const PouchDB = require('pouchdb')
PouchDB.plugin(require('pouchdb-hypercore'))

async function main () {
  // setup
  const hypercore = Hypercore('.example_hypercore')
  const pouch = new PouchDB('.example_pouchdb')
  await pouch.fromHypercore(hypercore)
  const key = hypercore.key.toString('hex')
  // hypercore -> pouchdb
  const seq = await new Promise((resolve, reject) => {
    this.hyper.append(JSON.stringify({ hello: 'world' }), (err, seq) => {
      if (err) { reject(err) } else { resolve(seq) }
    })
  })
  await new Promise((resolve) => { setTimeout(resolve, 100) })
  const doc = await this.pouch.get(`${key}@${seq}`)
  console.log(doc)
  >>> {_id: '{key}@{seq}', _rev: '1-...', hello: 'world' }
}

Why?

PouchDB and CouchDB are, at this point, enduring and reliable projects with powerful capabilities. Besides their distinctive sync capabilities and efficient map-reduce indexing, CouchDB and PouchDB have vibrant ecosystems of tools, documentation, plugins, and other goodies that reflect the ecosystem's maturity.

The Hypercore protocol has been used to build fascinating P2P projects like Beaker and Cabal. In essence, it gives you mutable torrents by way of a distributed append-only log. In fact it uses the same tracker infrastructure as torrents.

By mirroring hypercores in this way, it becomes easy to produce hypercore-to-http gateways, or to utilize CouchDB's advanced indexing and querying systems.

Why not map changes to writable hypercores?

This feature might return in the future, but for now it is unclear how to do this. Documents produced from hypercore log entries use {key}@{seq} as their ID, but it is impossible to determine that sequence number {seq} before appending the entry to the hypercore. There is also the matter of a hypercore and a PouchDB instance falling out of sync, which further complicates the task.

If you'd like to propose a solution, please chime in with an issue.

Install

Use NPM:

$ npm i @garbados/pouchdb-hypercore

Docs

The plugin adds or modifies the following methods of PouchDB instances:

async pouch.fromHypercore(hypercore)

Streams changes from a hypercore into the database. Does not write changes to the hypercore.

const pouch = new PouchDB(...)
pouch.fromHypercore(hypercore)
  .then(() => { /* ready */ })

async pouch.fromMultifeed(hypercore)

Streams changes from all the hypercores in a multifeed into the database. Does not write changes to the hypercore.

const pouch = new PouchDB(...)
pouch.fromMultifeed(multifeed)
  .then(() => { /* ready */ })

Test

npm test

License

Apache-2.0