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

hypelight-protocol

v4.3.4

Published

Stream that implements the hypercore protocol

Downloads

8

Readme

hypercore-protocol

Stream that implements the hypercore protocol

npm install hypercore-protocol

build status

Usage

var protocol = require('hypercore-protocol')

var p = protocol()

// open a channel specified by a 32 byte key
var channel = p.open(Buffer('deadbeefdeadbeefdeadbeefdeadbeef'))

channel.request({block: 42})
channel.on('data', function (message) {
  console.log(message) // contains message.block and message.value
})

stream.pipe(anotherStream).pipe(stream)

API

var p = protocol([options], [onopen])

Create a new protocol instance. The returned object is a duplex stream that you should pipe to another protocol instance over a stream based transport

If the remote peer joins a channel you haven't opened, hypercore will call an optional onopen method if you specify it with the discovery key for that channel.

var p = protocol(function (discoveryKey) {
  // remote peer joined discoveryKey but you haven't
  // you can open the channel now if you want to join the channel

  // open with corresponding key to join
  var channel = p.open(Buffer('deadbeefdeadbeefdeadbeefdeadbeef'))
})

See below for more information about channels, keys, and discovery keys. Other options include:

{
  id: optionalPeerId, // you can use this to detect if you connect to yourself
  encrypt: true // set to false to disable encryption for debugging purposes
}

If you don't specify a peer id a random 32 byte will be used. You can access the peer id using p.id and the remote peer id using p.remoteId.

var channel = p.open(key, [options])

Open a stream channel. A channel uses the sodium module to encrypt all messages using the key you specify. The discovery key for the channel is send unencrypted together with a random 24 byte nonce. If you do not specify a discovery key in the options map, an HMAC of the string hypercore using the key as the password will be used.

p.on('handshake')

Emitted when a protocol handshake has been received. Afterwards you can check .remoteId to get the remote peer id.

p.setTimeout(ms, [ontimeout])

Will call the timeout function if the remote peer hasn't send any messages within ms. Will also send a heartbeat message to the other peer if you've been inactive for ms / 2

Channel API

channel.end()

Ends a channel

channel.on('end')

Emitted when a channel is ended, either by you or the remote peer. No other events will be emitted after this.

channel.request(message)

Send a request message. See the protobuf schema or more information

channel.on('request', message)

Emitted when a request message is received

channel.data(message)

Send a data message. See the protobuf schema or more information

channel.on('data', message)

Emitted when a data message is received

channel.cancel(message)

Send a cancel message. See the protobuf schema or more information

channel.on('cancel', message)

Emitted when a cancel message is received

channel.have(message)

Send a have message. See the protobuf schema or more information

channel.on('have', message)

Emitted when a have message is received

channel.want(message)

Send a want message. See the protobuf schema or more information

channel.on('want', message)

Emitted when a want message is received

channel.resume()

Send a resume signal

channel.on('resume')

Emitted when a resume signal is received

channel.pause()

Send a pause signal

channel.on('pause')

Emitted when a pause signal is received

You can always check the paused state by accessing .remotePaused and .amPaused to see wheather or not the remote is pausing us or we are pausing the remote.

Extension API

protocol = protocol.use(extensionName)

Use an extension specified by the string name you pass in. Returns a new prototype

Will create a new method on all your channel objects that has the same name as the extension and emit an event with the same name when an extension message is received

protocol = protocol.use('ping')

var p = protocol()
var channel = p.open(someKey)

channel.on('handshake', function () {
  channel.on('ping', function (message) {
    console.log('received ping message', message)
  })

  channel.ping(Buffer('this is a ping message!'))
})

Per default all messages are buffers. If you want to encode/decode your messages you can specify an abstract-encoding compliant encoder as well

protocol = protocol.use({
  ping: someEncoder
})

var bool = p.remoteSupports(extensionName)

After the protocol instance emits handshake you can call this method to check if the remote peer also supports one of your extensions.

License

MIT