Skip to content

Commit

Permalink
Merge pull request #481 from primus/msgpack
Browse files Browse the repository at this point in the history
Add msgpack parser
  • Loading branch information
3rd-Eden committed Jul 19, 2016
2 parents d3b57d5 + f7b95b2 commit ce32e11
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions parsers.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
},
"binary": {
"server": "binary-pack"
},
"msgpack": {
"server": "primus-msgpack"
}
}
55 changes: 55 additions & 0 deletions parsers/msgpack.js
Original file line number Diff line number Diff line change
@@ -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;
})();
`;
10 changes: 10 additions & 0 deletions test/primus.parsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ce32e11

Please sign in to comment.