Fix message.encode API
This commit is contained in:
parent
aa691b4d1f
commit
874acfac45
|
@ -74,20 +74,21 @@ var message = exports.message = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode message structure.
|
* 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.
|
* @return {Buffer} Encoded message structure.
|
||||||
*/
|
*/
|
||||||
encode: function(opts) {
|
encode: function(command, payload) {
|
||||||
assert(opts.command.length <= 12, "Command is too long");
|
assert(command.length <= 12, "Command is too long");
|
||||||
assert(isAscii(opts.command), "Non-ASCII characters in command");
|
assert(isAscii(command), "Non-ASCII characters in command");
|
||||||
assert(opts.payload.length <= 262144, "Payload is too big");
|
assert(payload.length <= 262144, "Payload is too big");
|
||||||
var buf = new Buffer(24 + opts.payload.length);
|
var buf = new Buffer(24 + payload.length);
|
||||||
buf.fill(0);
|
buf.fill(0);
|
||||||
buf.writeUInt32BE(message.MAGIC, 0, true);
|
buf.writeUInt32BE(message.MAGIC, 0, true);
|
||||||
buf.write(opts.command, 4);
|
buf.write(command, 4);
|
||||||
buf.writeUInt32BE(opts.payload.length, 16, true);
|
buf.writeUInt32BE(payload.length, 16, true);
|
||||||
getmsgchecksum(opts.payload).copy(buf, 20);
|
getmsgchecksum(payload).copy(buf, 20);
|
||||||
opts.payload.copy(buf, 24);
|
payload.copy(buf, 24);
|
||||||
return buf;
|
return buf;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
2
test.js
2
test.js
|
@ -130,7 +130,7 @@ describe("Common structures", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should encode", 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");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user