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

terminal-stream

v3.0.1

Published

A message oriented wrapper for stream oriented transports

Downloads

9

Readme

terminal-stream

A message oriented wrapper for stream oriented transports.

Why

Implementing something like RPC over a streaming transport (e.g. TCP) requires an intermediate mechanism to ensure only complete messages are delivered to the application, even though messages may be combined or fragmented in unpredictable ways.

How

Messages sent from a terminal stream are prefixed with a 32bit unsigned integer indicating the number of bytes they contain (accordingly the maximum message size is 2^32 bits or 4GB). As pieces of the message reach the next terminal stream in the pipeline, they are buffered into memory until the entire content is available, at which point a message event is dispatched with its data property set to the content. Note that a terminal stream will only work with an ordered underlying transport.

Example

import TerminalStream from 'terminal-stream'
import utf8 from 'utf8-transcoder'

var a = new TerminalStream()
a.addEventListener('message', evt => {
  var message = JSON.parse(utf8.decode(evt.data))
  console.log('a got:', message.name)
})

var b = new TerminalStream()
b.addEventListener('message', evt => {
  var message = JSON.parse(utf8.decode(evt.data))
  console.log('b got:', message.name)

  if (message.name === 'hello') {
    b.send(utf8.encode(JSON.stringify({ name: 'world' })))
  }
})

a._send = message => {
  for (var i = 0; i < message.length; i++) {
    b.receive(message.subarray(i, i + 1))
  }
}

b._send = message => {
  for (var i = 0; i < message.length; i++) {
    a.receive(message.subarray(i, i + 1))
  }
}

a.send(utf8.encode(JSON.stringify({ name: 'hello' })))
// b got: hello
// a got: world
$ npm run example
$ npm run example-browser

Constructor

var t = new TerminalStream()

Methods

t.send(message)

t._send(data)

Users must implement this with their transport of choice.

t.receive(data)

Users must pass data to this method from their transport of choice.

Events

t.dispatchEvent(new Event('message'))

Find message data on the data property of the event.

Test

$ npm test
$ npm test-browser

Prior art

License

MIT