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

mplex2

v1.0.0-alpha.8

Published

ES6 stream multiplexer

Downloads

2

Readme

mplex2

A stream multiplexer written in ES6.


MPlex is a duplex stream with the following additional events:

  • ready: Emitted when the stream fully initialized itself. Call API methods only after this event.
  • stream: The remote side requested a stream.
    • options: Data sent by the remote when requesting this stream
    • fn(reason): A function to be called with a string as a reason of declining the stream request, or without parameters. In the letter case the request is accepted, and the function returns an MStream instance ready to read and write
  • globalRequest: The remote side issued a global request.
    • data: The request as set by the remote
    • fn(success, details): A function to be called to send the response. success can be true or false, details is some JSON serializable data.
  • close: No more action on the stream. Will be emitted after an error.

API:

  • new MPlex([options])
    • options <Object>
      • highWaterMark: The same as with streams (16384)
      • wantMaxPacketSize: The maximum size of a packet the remote side is allowed to send (8192). The minimum is 1024. Handled automatically
      • wantContinueAfter: After sending this amount of data to an underlying stream, MPlex will not send more data to this stream unless an explicit continue instruction on the stream. Handled automatically, see continueLevel
      • continueLevel: The stream will send continue to the remote only when data in its' buffer is less than this amount
      • streamHighWaterMark: highWaterMark for underlying streams
      • maxID: The maximum number of open streams in MPlex
  • MPlex.newStream(options, callback):
    • options: Any JSON serializable data, will be sent to the remote
    • callback(error, stream): Called when the request is accepted or rejected by the remote.
      • error: an error instance indicating the reason of decline
      • stream: an MStream instance (see below) which is ready for reding and writing
  • MPlex.globalRequest(data, callback): x
    • data: JSON serializable data to send
    • callback(error, sucess, details): will be called when a response arrives (or if error occures during sending).
      • error: If not null, an error occured during sending the request
      • success: boolean value received from the remote, indicating the status of the request.
      • details: additional data from the remote.
  • MPlex.close(): Close MPlex.

MStream is a duplex stream returned by MPlex.newStream or the second parameter (a function, fn) of the stream event. Apart from the standard stream events, it also emits close, which indicates no more processing will happen on this stream.

API:

  • MStream.close(): Close this stream.

HeartBeatMPlex is an extension to MPlex. It accepts the following extra options:

  • wantHeartBeatMilli: Requests the remote side not to be idle for longer than this amount in milliseconds.
  • heartBeatThreshold: If the remote is idle for more than wantHeartBeatMilli + heartBeatThreshold milliseconds, an error will be emitted.

A basic example

const {MPlex} = require('mplex2')

const mplex1 = new MPlex()
const mplex2 = new MPlex()

mplex1.on('ready', () => {
  mplex1.newStream('simple stream', (err, stream) => {
    if (err) console.log('rejected')
    if (stream) {
      stream.end('message to remote')
      stream.once('finish', stream.close)
    }
  })
})

mplex2.on('stream', (options, fn) => {
  const stream = fn()  // accept
  stream.on('data', d => console.log(d.toString()))
  stream.on('close', () => console.log('remote closed'))
})

mplex1.pipe(mplex2).pipe(mplex1)