Permalink
Browse files

[feature] Add msgpack parser

  • Loading branch information...
lpinca committed Jul 9, 2016
1 parent d3b57d5 commit f7b95b2237e4a8a69a331cab947547f104e59c58
Showing with 69 additions and 0 deletions.
  1. +1 −0 package.json
  2. +3 −0 parsers.json
  3. +55 −0 parsers/msgpack.js
  4. +10 −0 test/primus.parsers.test.js
View
@@ -77,6 +77,7 @@
"faye-websocket": "0.11.x",
"mocha": "2.5.x",
"pre-commit": "1.1.x",
"primus-msgpack": "1.0.x",
"pumpify": "1.3.x",
"querystringify": "0.0.x",
"recovery": "0.2.x",
View
@@ -5,5 +5,8 @@
},
"binary": {
"server": "binary-pack"
},
"msgpack": {
"server": "primus-msgpack"
}
}
View
@@ -0,0 +1,55 @@
'use strict';
const msgpack = require('primus-msgpack');
/**
* Message encoder.
*
* @param {Mixed} data The data that needs to be transformed.
* @param {Function} fn Completion callback.
* @api public
*/
exports.encoder = function encoder(data, fn) {
var err;
try { data = msgpack.encode(data); }
catch (e) { err = e; }
fn(err, data);
};
/**
* Message decoder.
*
* @param {Mixed} data The data that needs to be transformed.
* @param {Function} fn Completion callback.
* @api public
*/
exports.decoder = function decoder(data, fn) {
var err;
try {
data = msgpack.decode(data instanceof ArrayBuffer ? new Uint8Array(data) : data);
} catch (e) {
err = e;
}
fn(err, data);
};
//
// Expose the library so it can be added in our Primus module.
//
exports.library = `var msgpack = (function () {
var exports, mp;
try { mp = Primus.requires('primus-msgpack'); }
catch (e) {}
if (mp) return mp;
exports = {};
${msgpack.BrowserSource}
return exports.msgpack;
})();
`;
@@ -60,4 +60,14 @@ describe('Parsers', function () {
sendsAndReceivesTest('ejson', done);
});
});
describe('msgpack', function () {
it('connects with the parser', function (done) {
connectsTest('msgpack', done);
});
it('sends and receives data using the parser', function (done) {
sendsAndReceivesTest('msgpack', done);
});
});
});

0 comments on commit f7b95b2

Please sign in to comment.