From 56083e47517db0d13ed76eb2f0ec252af7cb9cfc Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Sat, 28 Feb 2015 22:25:28 +0300 Subject: [PATCH] Allow to pass services as buffer to net_addr --- lib/structs.js | 9 +++++++-- test.js | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/structs.js b/lib/structs.js index 4ff3a7c..898947b 100644 --- a/lib/structs.js +++ b/lib/structs.js @@ -761,7 +761,7 @@ exports.net_addr = { * included in short mode (current time by default) * @param {number=} opts.stream - Stream number of the node, not * included in short mode (1 by default) - * @param {Object=} opts.services - + * @param {(Object|Buffer)=} opts.services - * [Services]{@link module:bitmessage/structs.ServicesBitfield} * provided by the node (`NODE_NETWORK` by default) * @param {string} opts.host - IPv4/IPv6 address of the node @@ -788,7 +788,12 @@ exports.net_addr = { } var services = opts.services || ServicesBitfield().set(ServicesBitfield.NODE_NETWORK); - services.buffer.copy(buf, shift); + if (Buffer.isBuffer(services)) { + assert(services.length === 8, "Bad services buffer length"); + } else { + services = services.buffer; + } + services.copy(buf, shift); inet_pton(opts.host).copy(buf, shift + 8); buf.writeUInt16BE(opts.port, shift + 24); return buf; diff --git a/test.js b/test.js index 73ac5bf..a855d67 100644 --- a/test.js +++ b/test.js @@ -414,6 +414,16 @@ describe("Common structures", function() { var opts = {host: " 127.0.0.1", port: 1234}; expect(net_addr.encode.bind(null, opts)).to.throw(/bad octet/i); }); + + it("should allow to pass services as Buffer", function() { + var services = Buffer(8); + var res = net_addr.decode(net_addr.encode({ + host: "1.2.3.4", + port: 1234, + services: services, + })); + expect(bufferEqual(res.services.buffer, services)).to.be.true; + }); }); describe("inv_vect", function() {