From 3247a069a59c8e6576b1f8ea4df97d81f9a86928 Mon Sep 17 00:00:00 2001 From: Takeshi Ikuma Date: Sun, 10 Nov 2019 21:44:06 -0600 Subject: [PATCH 1/2] Updated to be zmq6 compatible --- README.md | 6 +++++- lib/client.js | 6 ++++-- lib/server.js | 6 ++++-- lib/socket.js | 15 +++++++++++---- package.json | 10 +++++----- test/buffers.js | 8 +++++++- test/errors.js | 14 ++++++++++---- test/heartbeats.js | 16 +++++++++++----- test/invocation.js | 14 ++++++++++---- test/repro-10.js | 8 +++++++- test/streams.js | 14 ++++++++++---- test/timeouts.js | 14 ++++++++++---- 12 files changed, 94 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index e7c5877..360c0ad 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,8 @@ Events: Methods: * `bind(endpoint)` - Binds the server to the specified ZeroMQ endpoint. + Returns `Promise`, which is resolved when the + socket was successfully bound (new in v0.9.9.beta). * `connect(endpoint)` - Connects the server to the specified ZeroMQ endpoint. * `close()` - Closes the ZeroMQ socket. @@ -100,7 +102,9 @@ Events: Methods: -* `bind(endpoint)` - Binds the client to the specified ZeroMQ endpoint. +* `bind(endpoint)` - Binds the client to the specified ZeroMQ endpoint. Returns + `Promise`, which is resolved when the socket was + successfully bound (new in v0.9.9.beta). * `connect(endpoint)` - Connects the client to the specified ZeroMQ endpoint. * `close()` - Closes the ZeroMQ socket. * `invoke(method, arguments..., callback)` - Invokes a remote method. diff --git a/lib/client.js b/lib/client.js index 8f2ceae..d310679 100644 --- a/lib/client.js +++ b/lib/client.js @@ -51,11 +51,13 @@ function Client(options) { nodeUtil.inherits(Client, events.EventEmitter); -//Binds to a ZeroMQ endpoint +//Binds to a ZeroMQ endpoint (async) //endpoint : String // The ZeroMQ endpoint +//return : Promise +// Resolved when the socket was successfully bound. Client.prototype.bind = function(endpoint) { - this._socket.bind(endpoint); + return this._socket.bind(endpoint); }; //Connects to a ZeroMQ endpoint diff --git a/lib/server.js b/lib/server.js index 8910d59..f659ef6 100644 --- a/lib/server.js +++ b/lib/server.js @@ -192,11 +192,13 @@ Server.prototype._recv = function(event, context) { } } -//Binds to a ZeroMQ endpoint +//Binds to a ZeroMQ endpoint (async) //endpoint : String // The ZeroMQ endpoint +//return : Promise +// Resolved when the socket was successfully bound. Server.prototype.bind = function(endpoint) { - this._socket.bind(endpoint); + return this._socket.bind(endpoint); }; //Connects to a ZeroMQ endpoint diff --git a/lib/socket.js b/lib/socket.js index be95dcd..31199c0 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -22,7 +22,7 @@ // DEALINGS IN THE SOFTWARE. var nodeUtil = require("util"), - zmq = require("zeromq"), + zmq = require("zeromq/v5-compat"), nodeEvents = require("events"), events = require("./events"), util = require("./util"), @@ -72,12 +72,19 @@ Socket.prototype.send = function(event) { this._zmqSocket.send.call(this._zmqSocket, message); }; -//Binds to a ZeroMQ endpoint +//Binds to a ZeroMQ endpoint (async) //endpoint : String // The ZeroMQ endpoint +//return : Promise +// Resolved when the socket was successfully bound. Socket.prototype.bind = function(endpoint) { - this._zmqSocket.bindSync(endpoint); -} + return new Promise((resolve, reject) => { + this._zmqSocket.bind(endpoint, err => { + if (err) reject(err); + resolve(); + }); + }); +}; //Connects to a ZeroMQ endpoint //endpoint : String diff --git a/package.json b/package.json index bb4690c..5465fb9 100644 --- a/package.json +++ b/package.json @@ -29,13 +29,13 @@ ], "dependencies": { "msgpack-lite": "^0.1.26", - "underscore": "1.3.3", - "uuid": "^3.0.0", - "zeromq": "^4.6.0" + "underscore": "^1.9.1", + "uuid": "^3.3.3", + "zeromq": "^6.0.0-beta.3" }, "devDependencies": { - "nodeunit": "0.9.1", - "temp": "0.8.1" + "nodeunit": "^0.11.3", + "temp": "^0.9.1" }, "license": "MIT" } diff --git a/test/buffers.js b/test/buffers.js index 6b31df2..fc35e75 100644 --- a/test/buffers.js +++ b/test/buffers.js @@ -36,10 +36,16 @@ module.exports = { reply(); } }); - this.srv.bind(endpoint); + this.srv + .bind(endpoint) + .then(() => { this.cli = new zerorpc.Client({ timeout: 5 }); this.cli.connect(endpoint); cb(); + }) + .catch(err => { + console.error(err); + }); }, tearDown: function(cb) { this.cli.close(); diff --git a/test/errors.js b/test/errors.js index 4551d09..778225a 100644 --- a/test/errors.js +++ b/test/errors.js @@ -55,10 +55,16 @@ module.exports = { } }); - this.srv.bind(endpoint); - this.cli = new zerorpc.Client({ timeout: 5 }); - this.cli.connect(endpoint); - cb(); + this.srv + .bind(endpoint) + .then(() => { + this.cli = new zerorpc.Client({ timeout: 5 }); + this.cli.connect(endpoint); + cb(); + }) + .catch(err => { + console.error(err) + }); }, tearDown: function(cb) { this.cli.close(); diff --git a/test/heartbeats.js b/test/heartbeats.js index fc441ce..16a915e 100644 --- a/test/heartbeats.js +++ b/test/heartbeats.js @@ -45,14 +45,20 @@ module.exports = { }, 250); } }, heartbeat); - this.srv.bind(endpoint); this.srv.on('error', function(err) { //console.log('on error', err); }); - this.cli = new zerorpc.Client({ timeout: 11000, heartbeat: heartbeat }); - this.cli.connect(endpoint); - this.killed = false; - cb(); + this.srv + .bind(endpoint) + .then(() => { + this.cli = new zerorpc.Client({ timeout: 11000, heartbeat: heartbeat }); + this.cli.connect(endpoint); + this.killed = false; + cb(); + }) + .catch(err => { + console.error(err) + }); }, tearDown: function(cb) { if (!this.cli.closed()) { diff --git a/test/invocation.js b/test/invocation.js index fc9e438..dff36b7 100644 --- a/test/invocation.js +++ b/test/invocation.js @@ -37,10 +37,16 @@ module.exports = { reply(null, n + 42, false); } }); - this.srv.bind(endpoint); - this.cli = new zerorpc.Client({ timeout: 5 }); - this.cli.connect(endpoint); - cb(); + this.srv + .bind(endpoint) + .then(() => { + this.cli = new zerorpc.Client({ timeout: 5 }); + this.cli.connect(endpoint); + cb(); + }) + .catch(err => { + console.error(err) + }); }, tearDown: function(cb) { this.cli.close(); diff --git a/test/repro-10.js b/test/repro-10.js index 5cdc544..0885cad 100644 --- a/test/repro-10.js +++ b/test/repro-10.js @@ -35,7 +35,9 @@ module.exports = { } }); - srv.bind(endpoint); + srv + .bind(endpoint) + .then(() => { cli = new zerorpc.Client({ timeout: 5 }); setTimeout(function() { @@ -49,5 +51,9 @@ module.exports = { test.done(); }); }, 10000); + }) + .catch(err => { + console.error(err) + }); } }; diff --git a/test/streams.js b/test/streams.js index 62b1170..ce88a2a 100644 --- a/test/streams.js +++ b/test/streams.js @@ -60,10 +60,16 @@ module.exports = { }, 1000); } }); - this.srv.bind(endpoint); - this.cli = new zerorpc.Client({ timeout: 5 }); - this.cli.connect(endpoint); - cb(); + this.srv + .bind(endpoint) + .then(() => { + this.cli = new zerorpc.Client({ timeout: 5 }); + this.cli.connect(endpoint); + cb(); + }) + .catch(err => { + console.error(err) + }); }, tearDown: function(cb) { this.cli.close(); diff --git a/test/timeouts.js b/test/timeouts.js index 6a4a91d..22412dc 100644 --- a/test/timeouts.js +++ b/test/timeouts.js @@ -36,10 +36,16 @@ module.exports = { }, 6 * 1000); } }); - this.srv.bind(endpoint); - this.cli = new zerorpc.Client({ timeout: 5 }); - this.cli.connect(endpoint); - cb(); + this.srv + .bind(endpoint) + .then(() => { + this.cli = new zerorpc.Client({ timeout: 5 }); + this.cli.connect(endpoint); + cb(); + }) + .catch(err => { + console.error(err) + }); }, tearDown: function(cb) { this.cli.close(); From 3ffa2e47171c95fa4016cee9f61ec0b1dc23699e Mon Sep 17 00:00:00 2001 From: Takeshi Ikuma Date: Sun, 10 Nov 2019 21:45:57 -0600 Subject: [PATCH 2/2] Updated Buffer instantiation --- lib/events.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/events.js b/lib/events.js index bcc75d0..560a4e7 100644 --- a/lib/events.js +++ b/lib/events.js @@ -38,7 +38,7 @@ function serialize(event) { message = message.concat(event.envelope); } - message.push(new Buffer(0)); + message.push(new Buffer.alloc(0)); message.push(msgpack.encode(payload)); return message; }