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

@hyperswarm/secret-stream

v6.4.0

Published

Secret stream backed by Noise and libsodium's secretstream

Downloads

7,388

Readme

@hyperswarm/secret-stream

See the full API docs at docs.holepunch.to

Secret stream backed by Noise and libsodium's secretstream

npm install @hyperswarm/secret-stream

Usage

You can either make a secret stream from an existing transport stream.

const SecretStream = require('@hyperswarm/secret-stream')

const a = new SecretStream(true, tcpClientStream)
const b = new SecretStream(false, tcpServerStream)

// pipe the underlying rawstreams together

a.write(Buffer.from('hello encrypted!'))

b.on('data', function (data) {
  console.log(data) // <Buffer hello encrypted!>
})

Or by making your own pipeline

const a = new SecretStream(true)
const b = new SecretStream(false)

// pipe the underlying rawstreams together
a.rawStream.pipe(b.rawStream).pipe(a.rawStream)

a.write(Buffer.from('hello encrypted!'))

b.on('data', function (data) {
  console.log(data) // <Buffer hello encrypted!>
})

API

const s = new SecretStream(isInitiator, [rawStream], [options])

Make a new stream. isInitiator is a boolean indication whether you are the client or the server. rawStream can be set to an underlying transport stream you want to run the noise stream over.

Options include:

{
  pattern: 'XX', // which noise pattern to use
  remotePublicKey, // set if your handshake requires it
  keyPair: { publicKey, secretKey },
  handshake: { // if you want to use an handshake performed elsewhere pass it here
    tx,
    rx,
    hash,
    publicKey,
    remotePublicKey
  }
}

The SecretStream returned is a Duplex stream that you use as as normal stream, to write/read data from, except it's payloads are encrypted using the libsodium secretstream.

Note that this uses ed25519 for the handshakes per default.

If need to load the key pair asynchronously, then secret-stream also supports passing in a promise instead of the keypair that later resolves to { publicKey, secretKey }. The stream lifecycle will wait for the resolution and auto destroy the stream if the promise errors.

s.start(rawStream, [options])

Start a SecretStream from a rawStream asynchrously.

const s = new SecretStream({
  autoStart: false // call start manually
})

// ... do async stuff or destroy the stream

s.start(rawStream, {
  ... options from above
})

s.setTimeout(ms)

Set the stream timeout. If no data is received within a ms window, the stream is auto destroyed.

s.setKeepAlive(ms)

Send a heartbeat (empty message) every time the socket is idle for ms milliseconds. Note: If one side calls s.setKeepAlive() and the other does not, then the empty messages will be passed through to the piped stream.

s.publicKey

Get the local public key.

s.remotePublicKey

Get the remote's public key. Populated after open is emitted.

s.handshakeHash

Get the unique hash of this handshake. Populated after open is emitted.

s.keepAlive

Get the interval (in milliseconds) at which keep-alive messages are sent (0 means none are sent).

s.on('connect', onconnect)

Emitted when the handshake is fully done. It is safe to write to the stream immediately though, as data is buffered internally before the handshake has been completed.

keyPair = SecretStream.keyPair([seed])

Generate a ed25519 key pair.

License

Apache-2.0