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 🙏

© 2025 – Pkg Stats / Ryan Hefner

hypersource

v0.1.6

Published

Build WebSocket APIs that leverage the HyperCore Protocol

Downloads

11

Readme

hypersource

Build WebSocket APIs that leverage the HyperCore Protocol

Installation

$ npm install hypersource

Example

Below creates a web socket server that echos the first value as a response.

// can use hyperdrive, hypertrie, or hyperdb too
const hypercore = require('hypercore')
const ram = require('random-access-memory')
const hs = require('hypersource')

const server = hs.createServer((req, res) => {
  console.log('request from:', req.url)

  // 'req.key' points to the public key for this feed
  const reader = hypercore(ram, req)

  // 'res.key' and 'res.secretKey' contain the ephemeral key pair
  // used for writing a response. The public key is stored in the
  // 'userData' of the handshake
  const writer = hypercore(ram, res)

  // replicate from request
  reader.replicate(req)
  // replicate to response
  writer.replicate(res)

  // echo first value to writer
  reader.get(0, (err, buf) => writer.append(buf))

  // close response when writer uploads
  writer.once('upload', () => res.destroy())
})

server.listen(3000, () => {
  const { protocol, address, port } = server.address()
  console.log('Listening on %s//%s:%s', protocol, address, port)
})

Using simple-websocket we can connect to this host and initiate a request.

const WebSocket = require('simple-websocket')
const hypercore = require('hypercore')
const pump = require('pump')
const ram = require('random-access-memory')

const request = hypercore(ram)
request.ready(() => {
  const key = request.key.toString('hex')
  const stream = request.replicate({ live: true })
  const socket = new WebSocket(`ws://localhost:3000/${key}`)

  pump(stream, socket, stream).once('handshake', () => {
    const remotePublicKey = stream.remoteUserData
    const response = hypercore(ram, remotePublicKey)

    request.append('hello world')
    response.replicate({ stream, live: true })
    response.get(0, (err, res) => {
      console.log('response', res.toString()); // 'hello world'
    })
  })
})

API

server = hs.createServer([opts[, onrequest]])

Create a new hypersource web socket server where onrequest is called when the 'request' event is emitted and opts is an optional object that is passed directly to simple-websocket Server.

server.listen(port[, host[, callback]])

Listen on port on an optional host calling callback when the 'listening' event is fired.

addrinfo = server.address()

Returns the address info for the server.

const addrinfo = server.address() // { protocol: 'ws:', address: '::', family: 'IPv6', port: 3000 }

server.close()

Close the server.

server.on('error', error)

Emitted when an error occurs.

server.on('connection', socket, httpRequest)

Emitted when a connection has been established where socket is a Duplex stream that wraps the underlying web socket and httpRequest is a http.IncomingMessage containing HTTP request information.

server.on('request', request, response, discoveryKey)

Emitted when a request has been established where request and response both wrap hypercore-protocol streams and discoveryKey is the discovery key for the request.

The request and response object contains useful properties for creating hypercore instances and replicating their feeds.

request.url

The URL associated with the request.

request.method

The HTTP method associated with the request.

request.headers

The HTTP headers associated with the request.

request.key

The public key associated with the request.

request.publicKey

An alias to request.key.

request.discoveryKey

The discovery key associated with the request.

request.stream

The hypercore-protocol stream that is associated with the request.

response.key

The public key associated with the response.

response.publicKey

An alias to response.key.

response.secretKey

The secret key associated with the response.

response.discoveryKey

The discovery key associated with the response.

request.stream

The hypercore-protocol stream that is associated with the request.

License

MIT