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

@telamon/multifeed

v2.0.8

Published

multi-writer hypercore

Downloads

3

Readme

multifeed

multi-writer hypercore

Small module that manages multiple hypercores: feeds you create locally are writeable, others' are readonly. Replicating with another multifeed peers exchanges the content of all of the hypercores.

Usage

var multifeed = require('multifeed')
var hypercore = require('hypercore')
var ram = require('random-access-memory')

var multi = multifeed(hypercore, './db', { valueEncoding: 'json' })

// a multifeed starts off empty
console.log(multi.feeds().length)             // => 0

// create as many writeable feeds as you want; returns hypercores
multi.writer('local', function (err, w) {
  console.log(w.key, w.writeable, w.readable)   // => Buffer <0x..> true true
  console.log(multi.feeds().length)             // => 1

  // write data to any writeable feed, just like with hypercore
  w.append('foo', function () {
    var m2 = multifeed(hypercore, ram, { valueEncoding: 'json' })
    m2.writer('local', function (err, w2) {
      w2.append('bar', function () {
        replicate(multi, m2, function () {
          console.log(m2.feeds().length)        // => 2
          m2.feeds()[1].get(0, function (_, data) {
            console.log(data)                   // => foo
          })
          multi.feeds()[1].get(0, function (_, data) {
            console.log(data)                   // => bar
          })
        })
      })
    })
  })
})

function replicate (a, b, cb) {
  var r = a.replicate()
  r.pipe(b.replicate()).pipe(r)
    .once('end', cb)
    .once('error', cb)
}

API

var multifeed = require('multifeed')

var multi = multifeed(hypercore, storage[, opts])

Pass in the a hypercore module (require('hypercore')), a random-access-storage backend, and options. Included opts are passed into new hypercores created, and are the same as hypercore's.

multi.writer([name, ]cb)

If no name is given, a new local writeable feed is created and returned via cb.

If name is given and was created in the past on this local machine, it is returned. Otherwise it is created. This is useful for managing multiple local feeds, e.g.

var main = multi.writer('main')        // created if doesn't exist
var content = multi.writer('content')  // created if doesn't exist

main === multi.writer('main')          // => true

var feeds = multi.feeds()

An array of all hypercores in the multifeed. Check a feed's key to find the one you want, or check its writable / readable properties.

Only populated once multi.ready(fn) is fired.

var feed = multi.feed(key)

Fetch a feed by its key key (a Buffer or hex string).

var stream = multi.replicate([opts])

Create a duplex stream for replication.

Works just like hypercore, except all local hypercores are exchanged between replication endpoints.

Note: this stream is not an encrypted channel.

multi.on('feed', function (feed, name) { ... })

Emitted whenever a new feed is added, whether locally or remotely.

Install

With npm installed, run

$ npm install multifeed

Replicaton Policies

You can control what gets replicated by providing a policy as a plugin:

var multi = multifeed(hypercore, './db', { valueEncoding: 'json' })
multi.use({
  init: function(multifeed) {
    // called on multifeed.ready
    // use it for initalization.
  },
  have: function(local, share) {
    // called on peer connection
    // select which feeds to share
    share(local.keys, {
      random: local.keys.map(function() { return Math.random() - 0.5 })
    })
  },
  want: function(remote, request) {
    // called when remote peer's
    // share list is available.
    // remote.keys is always available
    // remote contains also all custom props sent by remote.
    var keys = remote.keys.filter(function(k, i){
        return remote.random[i] > 0.5
    })
    request(keys)
  }
})

See Also

License

ISC