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

hyperlog-seed

v1.0.2

Published

seed content hashes embedded in a hyperlog

Downloads

1

Readme

hyperlog-seed

seed content hashes embedded in a hyperlog

example

First we will generate some elliptic curve keys:

$ node -pe "JSON.stringify(require('ssb-keys').generate())" > keys.json

Now we can seed content from stdin to webtorrent and write the magnet link to a swarmlog:

var webtorrent = require('webtorrent')
var parseTorrent = require('parse-torrent')
var level = require('level')
var swarmlog = require('swarmlog')

var log = swarmlog({
  keys: require('./keys.json'),
  db: level('/tmp/webtorrent-publish.db'),
  sodium: require('chloride'),
  valueEncoding: 'json',
  hubs: [ 'https://signalhub.mafintosh.com' ]
})

var client = webtorrent()
client.seed([process.stdin], { name: 'test.txt' }, function (torrent) {
  log.append({ link: torrent.magnetURI })
  console.log(torrent.magnetURI)
})

We can now write a mirroring service to download and seed everything published to our swarmlog:

var webtorrent = require('webtorrent')
var parseTorrent = require('parse-torrent')
var level = require('level')
var fs = require('fs')
var path = require('path')
var mkdirp = require('mkdirp')
var sub = require('subleveldown')
var swarmlog = require('swarmlog')

var db = level('/tmp/webtorrent-mirror.db')
var log = swarmlog({
  id: process.argv[2],
  db: sub(db, 'log'),
  sodium: require('chloride'),
  valueEncoding: 'json',
  hubs: [ 'https://signalhub.mafintosh.com' ]
})

var hseed = require('hyperlog-seed')
var seeder = hseed({
  db: sub(db, 'seed'),
  log: log,
  map: function (row) {
    if (row.link) return { type: 'put', link: row.link }
    if (row.unlink) return { type: 'del', link: row.unlink }
  }
})
var dir = '/tmp/webtorrent'

var client = webtorrent()

seeder.on('seed', function (link) {
  console.log('SEED', link)
  var t = parseTorrent(link)
  var tdir = path.join(dir, t.infoHash)
  fs.readdir(tdir, function (err, files) {
    if (!files) client.add(link, onadd)
    else {
      var tfiles = files.map(function (f) { return path.join(tdir, f) })
      client.seed(tfiles, onseed)
    }
  })
  function onadd (s) {
    var tdir = path.join(dir, t.infoHash)
    mkdirp(tdir, function () {
      t.files.forEach(function (file) {
        file.createReadStream()
          .pipe(fs.createWriteStream(path.join(tdir, t.infoHash)))
      })
    })
  }
  function onseed (s) {
    if (s.infoHash !== t.infoHash) {
      client.remove(s)
      client.add(link, onadd)
    }
  }
})
seeder.on('unseed', function (link) {
  console.log('UNSEED', link)
  var t = parseTorrent(link)
  for (var i = 0; i < client.torrents.length; i++) {
    var c = client.torrents[i]
    if (c.infoHash === t.infoHash) c.destroy()
  }
})

Given the public key from our publisher, we can mirror the webtorrent magnet links it posts to its swarmlog:

$ json public < keys.json
VtSrpZkaY9570yX4TQZEPDwFWkYApGt2otPsLN3Bw/0=.ed25519
$ electron-spawn mirror.js VtSrpZkaY9570yX4TQZEPDwFWkYApGt2otPsLN3Bw/0=.ed25519

Finally, we can publish torrents to the swarmlog:

$ echo OH HI HELLO | electron-spawn publish.js
magnet:?xt=urn:btih:5302fe31bf355f91a47f47cea74e57652b7fa3e3&dn=test.txt&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.webtorrent.io

and download them with webtorrent elsewhere, even when the publisher is offline:

$ electron-spawn `which webtorrent` 'magnet:?xt=urn:btih:5302fe31bf355f91a47f47cea74e57652b7fa3e3&dn=test.txt&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.webtorrent.io'

If you restart the mirror process, it will resume seeding torrents from the swarmlog feed. If you publish more torrents to the feed, these new torrents will also be mirrored.

api

var hseed = require('hyperlog-seed')

var seeder = hseed(opts)

Create a new seeder from:

  • opts.db - a leveldb instance
  • opts.log - a hyperlog instance
  • opts.map(row) - a function mapping rows to link and unlink operations

To add a magnet link for seeding, opts.map should return:

{ type: 'put', link: 'magnet:...' }

To remove a magnet link from seeding, opts.map should return:

{ type: 'put', unlink: 'magnet:...' }

seeder.on('seed', function (link) {})

This event fires when the seeder should start seeding a magnet link. This happens when a new link is posted to the log and when the seeder resumes after a restart.

seeder.on('unseed', function (link) {})

This event fires when the seeder should stop seeding a magnet link. This happens when the log removes link.

license

BSD