项目作者: t-mullen

项目描述 :
Easy, fast, streaming wire protocols.
高级语言: JavaScript
项目地址: git://github.com/t-mullen/wire-protocol.git
创建时间: 2018-02-15T02:12:18Z
项目社区:https://github.com/t-mullen/wire-protocol

开源协议:MIT License

下载


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

  1. npm install --save wire-protocol

or, without Browserify:

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

Usage

  1. // A protocol is just a set of messages we can send
  2. var protocol = [{
  3. name: 'firstMessage', // name of message
  4. type: 'object',
  5. first: true,
  6. length: 13,
  7. done: function (data, next) {
  8. next('secondMessage', 11)
  9. }
  10. }, {
  11. name: 'secondMessage',
  12. type: 'string'
  13. }]
  14. // This module turns your protocol spec into a fully-fledged wire protocol
  15. var wire = new WireProtocol(protocol)
  16. // send your messages!
  17. wire.send('firstMessage', {a: 'hello'})
  18. wire.send('secondMessage', 'hello world')
  19. // The WireProtocol object is a Duplex stream
  20. wire.pipe(net.Socket()).pipe(wire)
  21. wire.on('firstMessage', function (data) { // listen for messages as events
  22. console.log(data) // {a: 'hello'}
  23. })

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:

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

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.

Diagram of a Length Prefix

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.