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

spb

v0.0.1

Published

Size-prefixed blobs for encoding messages in byte streams

Downloads

6

Readme

Size-prefixed blob streams

It is often useful to treat a continuous byte stream as a series of discrete messages. This library gives you a simple but effective way to do this, by prefacing each message with the size of the message. In other words, it saves you writing a bit of fiddly parsing code if you just want to deal with whole things (e.g., JSON objects) at a time.

You can wrap streams (e.g., net.Socket) or things that look like net.Server. If you wrap a stream, then it treats incoming data as size-prefix encoded and hands you each message (without the size prefix) as a 'data' event; if you write a string or buffer, it treats it as a message and encode it with a size prefix. If you wrap a server, it provides wrapped streams in the 'connection' event.

Trivial example

> var netserver = require('net').createServer();
> var msgserver = require('spb').server(netserver);
> msgserver.on('connection', function(connection) {
... connection.on('data', function(message) {
...... // do something with the whole message
...... });
... });
> netserver.listen(5975);

Reference

spb.stream(underlying)

Wraps a Stream.

The returned stream shall parse bytes from the underlying stream and emit 'data' events consisting of whole messages (without the length prefix). Buffers or strings written to the returned stream shall be encoded with a size-prefix and written to the underlying stream.

The wrapped stream relays events ('drain', 'error', 'end', 'pipe') from the underlying stream. Aside from 'data' it is equivalent to give a callback to either the underlying stream or the wrapped stream.

Likewise, the methods pause, resume, destroy, and destroySoon for the wrapped stream simply invoke the same on the underlying stream. It is not in general safe to invoke either write or setEncoding on the underlying stream, as these interfere with the size prefixing. setEncoding on the wrapped stream does the 'right thing', in other words, it results in the messages emitted being strings.

spb.server(underlying)

Wraps a server, providing connections that are wrapped streams.

The underlying server must look like a net.Server. Specifically, it must emit the 'listening', 'close' and 'connection' events, and have a close method. It is safe to invoke methods and to supply callbacks for the above events on the underlying server; only 'connection' is not equivalent.

A note on pipes

Piping a spb stream to a byte stream will simply strip out size prefixes, effectively concatenating the messages into undelimited bytes. Piping a byte stream to a spb stream will encode as discrete messages the (probably arbitrarily delimited) packets read on the byte stream.