From 874acfac45dcae03cd8bca24d28358bb56600645 Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Mon, 26 Jan 2015 19:34:29 +0300 Subject: [PATCH] Fix message.encode API --- lib/structs.js | 21 +++++++++++---------- test.js | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/structs.js b/lib/structs.js index c0991cc..3786139 100644 --- a/lib/structs.js +++ b/lib/structs.js @@ -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; }, }; diff --git a/test.js b/test.js index 36479db..2a119d7 100644 --- a/test.js +++ b/test.js @@ -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"); }); });