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

wire-protocol

v2.1.0

Published

Easy, fast streaming wire protocols.

Downloads

14

Readme

wire-protocol

JavaScript Style Guide Travis

Easy and fast streaming wire protocols.

Lets you send messages over a binary stream without worrying about where one message begins and another ends. Uses implicit length-prefix framing.

This module abstracts away the underlying parser, letting you forget about how your messages are buffered. Just serialize, send and forget!

Install

npm install --save wire-protocol

or, without Browserify:

<script src="dist/wire-protocol.js"></script>

Usage


// A protocol is just a set of messages we can send
var protocol = [{
  name: 'firstMessage', // name of message
  type: 'object',
  first: true,
  length: 13,
  done: function (data, next) {
    next('secondMessage', 11)
  }
}, {
  name: 'secondMessage',
  type: 'string'
}]

// This module turns your protocol spec into a fully-fledged wire protocol
var wire = new WireProtocol(protocol)

// send your messages!
wire.send('firstMessage', {a: 'hello'})
wire.send('secondMessage', 'hello world')

// The WireProtocol object is a Duplex stream
wire.pipe(net.Socket()).pipe(wire)

wire.on('firstMessage', function (data) { // listen for messages as events
  console.log(data) // {a: 'hello'}
})

API

var wire = new WireProtocol(protocol)

Creates a new endpoint to your wire protocol. This object is a Duplex stream.

wire.send(name, body)

Sends a message with the specified name and body.

  • name is the name of the message.
  • body is the unserialized data to send.

wire.on(name)

Listen for a message by name.

  • name is the name of the message.

Message Definitions

The constructor of WireProtocol expects an array of these definition objects.

They have the following form:

{
  name: String,                           // Name of the message.
  type: 'object' || 'string' || 'buffer', // Optional: Name of one of the default (de)serializers. (See Default Serializers below)
  first: Boolean,                         // Optional: true if this is the first message expected.
  length: Integer,                        // Optional*: The fixed length of this message (*required for the first message)
  done: function (data, next) {           // Function that is called when this message is done parsing.
    // data is the data of the message
    next(String, Integer) // next should be called with the name of the next expected message, and it's expected length
    // You may omit the length argument if the message has a specified fixed length
  },
  serializer: function (data) { // Optional: Provide your own serializer (See Custom Serializers below)
    return mySerializer(data) // MUST return a Buffer
  },
  deserializer: function (buffer) { // Optional: Provide your own deserializer
    return myDeserializer(buffer)
  }
}

Default Serializers

WireProtocol provides you with a few simple serializers. You can use them by specifying type in your message definition.

'buffer'

Does nothing. Expects message bodies to be Buffers and outputs Buffers. The default if no type is specified.

'string'

Expects message bodies to be strings and outputs strings.

'object'

Expects message bodies to be Javascript objects that can be serialized by JSON.stringify and outputs Javascript objects.

NOTE: To allow zero-length messages to be sent, undefined will be serialized to null.

Custom Serializers

Custom serializers are easy to implement. See src/serialize.js for examples.

Specifying a serializer option will override the default serializer. You may override only one of the options.

FAQ

What is implicit length-prefix framing?

When you send multiple messages over a binary stream, you can't immediately know when each messages starts and ends.

One solution is to first send the length of the message, then the message itself.

wire-protocol allows implicit length-prefixing, since it doesn't explicity write the length of each message. Message length can often be derived from the previous message, or is fixed, and no prefix needs to be sent at all.

Why not use protobuf?

Google's Protocol Buffers can also implement length-prefixed wire protocols. It's great if your application is cross-language or you need a complex serialization algorithm.

If you just want a Javascript wire protocol, are fine with using common serializers, don't want to spend time compiling and learning protobuf, and/or want to use a convenient stream API, use this module.

It's also perfectly reasonable to use protobuf for fast serialization and message definition, while using this module for "event-style" message handling.