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

hyperpc

v2.1.0

Published

Bidirectional RPC over any stream with callbacks, streams and promises

Downloads

4

Readme

hyperpc

Asynchronous bidirectional RPC in Javascript that works over any binary stream. Supports passing both callbacks and arbitrary streams (both in object and binary mode) to the remote end. An optional promise mode allows to return promises and use the remote API with async/await. Also supports rpcifying objects and constructors.

Uses multiplex under the hood to float many streams through a single binary stream.

In the spirit of dnode, rpc-stream, muxrpc and rpc-multistream.

Installation

npm install hyperpc

Usage

  var hyperpc = require('hyperpc')

  var values = ['hello', 'world!']
  var api = {
    upper: (str, cb) => cb(null, str.toUpperCase()),
    readStream: (str, cb) => {
      var rs = new stream.Readable({
        objectMode: true,
        read () { this.push(values.length ? values.shift() : null)}
      })
      cb(null, rs)
    }
  }

  var server = hyperpc(api)
  var client = hyperpc()

  server.pipe(client).pipe(server)
  // usually, you'd do something like:
  // server.pipe(serverSideTransportStream).pipe(server)
  // clientTransport.pipe(client).pipe(clientTransport)

  client.on('remote', (remote) => {
    remote.upper('foo', (err, res) => {
      console.log(res) // FOO
    })

    remote.readStream('bar', (err, rs) => {
      rs.on('data', (data) => {
        console.log(data)
      })
      rs.on('end', () => console.log('read stream ended'))

      // prints:
      // hello
      // world!
      // read stream ended
    })
  })
})

More examples are in test.js and examples/.

API

var stream = hyperpc([api], [opts])

api is an object of functions. The functions can be called from the remote site. The implementing side may call any callbacks that are passed. For both the call and the callbacks you may pass streams, callbacks or errors as args. They all work transparently over the remote connection. Supported streams are readable streams, writable streams, duplex streams in both object and binary modes. If a transform stream is passed, it is assumed to be a readable stream if it does not have pipes assigned (i.e. is piped to but not piped from).

opts and their defaults are:

  • log: false: Enable debug mode. Log all messages to console.log
  • name: null: Set a name for this end of the connection. Only used in log mode.
  • promise: false: Support returning promises (experimental)

RPCifying objects

hyperpc supports passing classes (functions with constructors) or instances (objects with bound function properties) through a helper, hyperpc.rpcify, to make transparent calls to the instance on the backend. See test/object.js for examples.

Support for promises and async/await

Return values are ignored, unless { promise: true } is set in opts AND the return value is a promise. In that case, on the remote end a promise is returned as well and the resolve/reject callbacks are streamed transparently.

This allows to use hyperpc with async/await:

  var api = {
    promtest: async function (str) {
      if (!str) throw new Error('no arg')
      return str.toUpperCase()
    }
  }

  var server = hyperpc(api, {promise: true})
  var client = hyperpc(null, {promise: true})

  pump(server, client, server)

  client.on('remote', async (api) => {
    var val = 'hello'
    try {
      var bar = await api.promtest(val)
      console.log(bar)
    } catch (err) {
      console.log(err.message)
    }
    // prints "HELLO", and would print "no arg" if val were false.
  })

Motivation

There's many RPC-over-streams modules already. Why another one? First, I wanted to learn streams in-depth. Second, hyperpc uses multiplex under the hood, and supports setting up arbitrary binary streams from both ends, so it should be fast to not only exchange RPC messages, but only binary data streams. No benchmarks though, yet.

Some differences to other great modules in this space:

  • dnode: The oldest kid on the block. Does not support streams as arguments natively though.
  • muxrpc: The preferred streaming RPC in Scuttlebut land. Uses pull-streams, which I didn't want to include. Needs a manifest, which hyperpc does not.
  • rpc-multistream: Similar feature set to hyperpc, also uses multiplex. hyperpc can be considered a rewrite, with additional suppport for Promises.