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

packet-stream

v2.0.6

Published

The core-logic for rpc/multiplexing protocols

Downloads

1,754

Readme

packet-stream

a simpler approach to rpc/multiplexing

proir work

Over the course of streams and so on with node, there have been many approaches to rpc and to multiplexing. substack's dnode was first - which supported async callbacks, but not streams. I wrote rpc-stream that was simpler than dnode, but could be piped over any node stream - this soon came to dnode. Later, I wrote mux-demux which supported streams but not callbacks. This was pretty good, because you could now stream many different things through one connection. Unfortunately, mux-demux used json encoding, and so did not support binary very well.

When leveldb came around, juliangruber wrote multilevel which used mux-demux and rpc-stream to create remote access to a leveldb instance. This worked pretty well, although it felt like a messy glue job.

Later, maxogden wrote multiplex which had better support for binary, as was later wrapped to a more convienient api by substack's dataplex

But something still wasn't right. Thing is, if you look into how all these modules are implemented, rpc or multiplexer, you'll see one thing: framed messages are sent over a stream. Basically, a multiplexer is tcp implemented on top of tcp, but is this really the right approach?

Shouldn't tcp be implemented on top of packets?

Any useful node api needs streams, but also needs callbacks and maybe events too. So therefore packet-stream provides messages as the fundamental building block, and implements request/response (async+callback) and streams (a sequence of messages) on top of messages.

This is a low level module that implements the core logic necessary for an rpc and multiplexing module - it is intended to be wrapped in something closer to how the user thinks - like muxrpc

Example

var packets = require('packet-stream')

var A = packets({
  //handle an ordinary message
  message: function (msg) { console.log ('message', msg) },

  //handle a request
  request: function (value, cb) {
    console.log('request', value)
    cb(null, {okay: true})
  })

  //handle a stream

  stream: function (stream) {
    console.log('connection')
    //create an echo server by connecting the stream to itself.
    //NOTE these are not normal node streams.
    stream.read = stream.write
  }

  //c
  close: function (err) {
    console.log('closed', err)
  }

})

var B = packets({})

// same as A.pipe(B).pipe(A)
// but simpler to implement internally.
A.read = B.write; B.read = A.write

//send a message
B.message('HELLO THERE')

B.request({foo: 'bar'}, function (err, value) {
  if(err) throw err
  console.log('response', value)
})

var stream = B.stream()

stream.read = function (data) {
  console.log(data)
}

stream.write('open - write to stream')

weird streams

yes, I have weird streams. But they are easy to wrap with more normal streams and using simple message oriented streams means that the entire implementation could fit into 100 lines, correction 200 lines now that there is full error checking, and close

weird-stream have two methods - read and write. read is for data coming out of the stream, and write is for data going in.

The user is required to reassign the read method to call another function, for example, the write method of another stream.

// A.pipe(B)
A.read = B.write

// B.pipe(A)
B.read = A.write

write(data, end) and read(data, end) both take two arguments. data and end. If end is truthy, data must be ignored. back pressure is not currently supported.

License

MIT