├── .gitignore ├── .travis.yml ├── test.js ├── index.js ├── README.md ├── package.json └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '6' 4 | - '5' 5 | - '4' 6 | - '0.12' 7 | - '0.10' 8 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var assert = require('assert') 4 | var net = require('net') 5 | var msgpack = require('./') 6 | 7 | var server = net.createServer(function (socket) { 8 | var dup = msgpack(socket) 9 | 10 | dup.on('data', function (obj) { 11 | assert.deepEqual(obj, {hello: 'world'}) 12 | socket.destroy() 13 | server.close() 14 | }) 15 | }) 16 | 17 | server.listen(function () { 18 | var port = server.address().port 19 | var dup = msgpack(net.connect(port)) 20 | dup.write({hello: 'world'}) 21 | }) 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var pump = require('pump') 4 | var duplexify = require('duplexify') 5 | var through = require('through2') 6 | var lpstream = require('length-prefixed-stream') 7 | var msgpack = require('msgpack5')() 8 | 9 | module.exports = function Msgpack5Stream (stream) { 10 | var msgEncode = through.obj(function (data, enc, cb) { 11 | cb(null, msgpack.encode(data)) 12 | }) 13 | 14 | var msgDecode = through.obj(function (data, enc, cb) { 15 | cb(null, msgpack.decode(data)) 16 | }) 17 | 18 | var dup = duplexify.obj() 19 | 20 | var lpEncode = lpstream.encode() 21 | var lpDecode = lpstream.decode() 22 | 23 | pump(msgEncode, lpEncode, stream) 24 | pump(stream, lpDecode, msgDecode) 25 | 26 | dup.setWritable(msgEncode) 27 | dup.setReadable(msgDecode) 28 | 29 | return dup 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # msgpack5-stream 2 | 3 | Given a regular duplex stream, this module will return a duplex stream 4 | optimised for sending and receiving 5 | [msgpack5](https://github.com/mcollina/msgpack5) messages. 6 | 7 | [![Build status](https://travis-ci.org/watson/msgpack5-stream.svg?branch=master)](https://travis-ci.org/watson/msgpack5-stream) 8 | [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) 9 | 10 | ## Usage 11 | 12 | ```js 13 | var net = require('net') 14 | var msgpack = require('msgpack5-stream') 15 | 16 | var server = net.createServer(function (socket) { 17 | var dup = msgpack(socket) 18 | 19 | dup.on('data', function (obj) { 20 | console.log(obj) // { hello: 'world' } 21 | }) 22 | }) 23 | 24 | server.listen(3000, function () { 25 | var dup = msgpack(net.connect(3000)) 26 | dup.write({hello: 'world'}) 27 | }) 28 | ``` 29 | 30 | ## License 31 | 32 | MIT 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "msgpack5-stream", 3 | "version": "1.0.0", 4 | "description": "A duplex stream wrapper for msgpack5 ", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "standard && node test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/watson/msgpack5-stream.git" 12 | }, 13 | "dependencies": { 14 | "duplexify": "^3.4.5", 15 | "length-prefixed-stream": "^1.5.0", 16 | "msgpack5": "^3.4.1", 17 | "pump": "^1.0.1", 18 | "through2": "^2.0.1" 19 | }, 20 | "devDependencies": { 21 | "standard": "^8.4.0" 22 | }, 23 | "keywords": [ 24 | "msgpack5", 25 | "stream", 26 | "duplex", 27 | "duplexify" 28 | ], 29 | "author": "Thomas Watson Steen (https://twitter.com/wa7son)", 30 | "license": "MIT", 31 | "bugs": { 32 | "url": "https://github.com/watson/msgpack5-stream/issues" 33 | }, 34 | "homepage": "https://github.com/watson/msgpack5-stream#readme", 35 | "coordinates": [ 36 | 52.6481661, 37 | -7.197170099999999 38 | ] 39 | } 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Thomas Watson Steen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------