Fix message.encode API

This commit is contained in:
Kagami Hiiragi 2015-01-26 19:34:29 +03:00
parent aa691b4d1f
commit 874acfac45
2 changed files with 12 additions and 11 deletions

View File

@ -74,20 +74,21 @@ var message = exports.message = {
/**
* Encode message structure.
* @param {{command: string, payload: Buffer}} opts - Encode options
* @param {string} command - Message command
* @param {Bufer} payload - Message payload
* @return {Buffer} Encoded message structure.
*/
encode: function(opts) {
assert(opts.command.length <= 12, "Command is too long");
assert(isAscii(opts.command), "Non-ASCII characters in command");
assert(opts.payload.length <= 262144, "Payload is too big");
var buf = new Buffer(24 + opts.payload.length);
encode: function(command, payload) {
assert(command.length <= 12, "Command is too long");
assert(isAscii(command), "Non-ASCII characters in command");
assert(payload.length <= 262144, "Payload is too big");
var buf = new Buffer(24 + payload.length);
buf.fill(0);
buf.writeUInt32BE(message.MAGIC, 0, true);
buf.write(opts.command, 4);
buf.writeUInt32BE(opts.payload.length, 16, true);
getmsgchecksum(opts.payload).copy(buf, 20);
opts.payload.copy(buf, 24);
buf.write(command, 4);
buf.writeUInt32BE(payload.length, 16, true);
getmsgchecksum(payload).copy(buf, 20);
payload.copy(buf, 24);
return buf;
},
};

View File

@ -130,7 +130,7 @@ describe("Common structures", function() {
});
it("should encode", function() {
expect(message.encode({command: "test", payload: Buffer("payload")}).toString("hex")).to.equal("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164");
expect(message.encode("test", Buffer("payload")).toString("hex")).to.equal("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164");
});
});