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

hypercore-query-extension

v1.3.0

Published

query peers about hypercore feed data

Downloads

5

Readme

hypercore-query-extension

ask peers over an extension for which sequences in which feeds are relevant to custom query logic

This approach is very useful for sparse data from a potentially large number of feeds. Instead of downloading everything for each feed, you can ask peers which feeds and sequences are relevant to particular queries. For example, if you have a chat application, you might ask peers about the latest 20 messages in a channel.

example

This example builds a feed and a clone of that feed in memory. The feed is populated with a number between 0 and 99, inclusive every 50 milliseconds. The clone feed is opened in sparse mode and the clone tells the main feed through the query extension that it is only interested in numbers between 50 and 70, inclusive. The clone then downloads only those sequences that were mentioned in the query results.

var Query = require('hypercore-query-extension')
var { Readable, Transform } = require('readable-stream')
var ram = require('random-access-memory')

var hypercore = require('hypercore')
var feed0 = hypercore(ram)

setInterval(function () {
  var n = Math.floor(Math.random()*100)
  feed0.append(String(n))
}, 50)

feed0.ready(function () {
  var feed1 = hypercore(ram, feed0.key)
  var r0 = feed0.replicate(false, { download: false, live: true })
  var r1 = feed1.replicate(true, { sparse: true, live: true })
  r0.pipe(r1).pipe(r0)
  var q0 = new Query({ api: api(feed0) })
  var q1 = new Query({ api: api(feed1) })
  r0.registerExtension('query-example', q0.extension())
  r1.registerExtension('query-example', q1.extension())
  var s = q1.query('subscribe', JSON.stringify({ start: 50, end: 70 }))
  s.pipe(new Transform({
    objectMode: true,
    transform: function (row, enc, next) {
      if (!row.key.equals(feed0.key)) return next()
      feed1.update(row.seq, function () {
        feed1.get(row.seq, function (err, buf) {
          if (err) return next(err)
          console.log('n=', Number(buf.toString()))
          next()
        })
      })
    }
  }))
})

function api (feed) {
  var subs = []
  feed.on('append', function () {
    var seq = feed.length
    feed.get(seq, function (err, buf) {
      var n = Number(buf.toString())
      subs.forEach(({ start, end, stream }) => {
        if (n >= start && n <= end) {
          stream.push({ key: feed.key, seq })
        }
      })
    })
  })
  return { subscribe }
  function subscribe (args) {
    var { start, end } = JSON.parse(args.toString())
    var stream = new Readable({
      objectMode: true,
      read: function () {}
    })
    subs.push({ start, end, stream })
    return stream
  }
}

api

var Query = require('hypercore-query-extension')

var q = Query(opts)

Create a new Query instance q from:

  • opts.api - object mapping query names to implementation functions.

Each api function receives a Buffer of optional argument payload and must return a readable or duplex readableObjectMode stream that pushes object with a feed key (as a Buffer) and a sequence number.

If the stream is duplex, you'll be able to receive messages on the channel from the other side of the query. You can use this to adjust query parameters on the fly, for example if you have a query for a map and the user pans the map, changing the bounding box. The updated bounding box can be sent back up the stream without having to open a new query.

q.extension()

Return a function that can be passed to feed.registerExtension() or proto.registerExtension(). You'll almost always want to do:

feed.registerExtension('your-extension-name', q.extension())
// or:
proto.registerExtension('your-extension-name', q.extension())

var stream = q.query(name, data)

Return a duplex objectMode stream with results from calling the api endpoint name with an optional data payload (as a Buffer).

Each row from the readable side of the stream contains:

  • row.key - feed key (Buffer or hex string)
  • row.seq - feed sequence number

If the query supports duplex mode, you can write messages to the remote api by calling stream.write() with Buffer payloads.

license

BSD