Skip to content

Commit

Permalink
Restored original WAMP ID random range and implemented registration o…
Browse files Browse the repository at this point in the history
…f extension types
  • Loading branch information
agronholm committed Mar 11, 2016
1 parent b9ab5d8 commit 4677f28
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
40 changes: 37 additions & 3 deletions package/lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,53 @@ exports.JSONSerializer = JSONSerializer;

try {
var msgpack = require('msgpack-lite');
var write_type = require("msgpack-lite/lib/write-type").type;
var write_token = require("msgpack-lite/lib/write-token").token;

// Save the original number encoder
var original_number = write_type.number;

// monkey patch the number encoder to send all generated
// WAMP IDs as integers
write_type.number = function (encoder, value) {
if (Number.isInteger(value) && value > 2147483648 && value <= 9007199254740991) {
return write_token[0xcf](encoder, value);
}
return original_number(encoder, value);
};

function MsgpackSerializer() {
// https://github.com/mcollina/msgpack5
this.SERIALIZER_ID = 'msgpack';
this.BINARY = true;
this.codec = msgpack.createCodec();
}

MsgpackSerializer.prototype.serialize = function (obj) {
return msgpack.encode(obj);
return msgpack.encode(obj, {codec: this.codec});
};

MsgpackSerializer.prototype.unserialize = function (payload) {
return msgpack.decode(payload);
return msgpack.decode(payload, {codec: this.codec});
};

/**
* Register a packer and/or unpacker functions for a given type.
*
* The msgpack specification allows applications to register up to 128 extension
* types.
*
* @param code numeric extension code (between 0-127)
* @param type constructor for the given type (only required when packer is defined)
* @param packer a function that takes an object and returns a Buffer
* @param unpacker a function that takes a Buffer and returns an instance of the given type
*/
MsgpackSerializer.prototype.registerExtType = function (code, type, packer, unpacker) {
if (packer && type) {
this.codec.addExtPacker(code, type, packer);
}
if (unpacker) {
this.codec.addExtUnpacker(code, unpacker);
}
};

exports.MsgpackSerializer = MsgpackSerializer;
Expand Down
2 changes: 1 addition & 1 deletion package/lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var WAMP_FEATURES = {
// generate a WAMP ID
//
function newid () {
return Math.floor(Math.random() * 2147483648);
return Math.floor(Math.random() * 9007199254740992);
}


Expand Down
2 changes: 1 addition & 1 deletion package/test/test_msgpack_serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ exports.testMsgpackSerialization = function (testcase) {

var pl2 = [];

var vals1 = [1.7, "hello", [1, 2, -3], {a: 5, b: "hello2"}, null];
var vals1 = [1.7, "hello", [1, 2, -3], {a: 5, b: "hello2"}, [-9007199254740991, 9007199254740991], null];

for (var i = 0; i < vals1.length; ++i) {

Expand Down
5 changes: 3 additions & 2 deletions package/test/test_msgpack_serialization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
4 "Result:" "hello"
5 "Result:" [1,2,-3]
6 "Result:" {"a":5,"b":"hello2"}
7 "Result:" null
8 "All finished."
7 "Result:" [-9007199254740991,9007199254740991]
8 "Result:" null
9 "All finished."

0 comments on commit 4677f28

Please sign in to comment.