Comparing version 0.9.6 to 0.9.7
@@ -41,7 +41,7 @@ // Open Source Initiative OSI - The MIT License (MIT):Licensing | ||
// considered timed out (default 30s) | ||
// * heartbeatInterval (number): The heartbeat interval in ms. | ||
// * heartbeatInterval (number): The heartbeat interval in ms. | ||
// (default 5000ms) | ||
function Client(options) { | ||
options = options || {}; | ||
var heartbeat = options.heartbeatInterval || DEFAULT_HEARTBEAT | ||
var heartbeat = options.heartbeatInterval || DEFAULT_HEARTBEAT; | ||
this._timeout = options.timeout || DEFAULT_TIMEOUT; | ||
@@ -79,2 +79,4 @@ this._socket = socket.client(heartbeat); | ||
//Calls a remote method | ||
//options: Object | ||
// Optional options object to override the constructor options (currently only timeout) | ||
//method : String | ||
@@ -86,15 +88,29 @@ // The method name | ||
// The callback to call on an update | ||
Client.prototype.invoke = function(method /*, args..., callback*/) { | ||
var self = this, | ||
hasCallback = typeof arguments[arguments.length - 1] == 'function', | ||
callback = hasCallback ? arguments[arguments.length - 1] : function() {}, | ||
args = Array.prototype.slice.call(arguments, 1, | ||
Client.prototype.invoke = function(/*options, */method /*, args..., callback*/) { | ||
var self = this; | ||
var hasCallback = typeof arguments[arguments.length - 1] == 'function'; | ||
var callback = hasCallback ? arguments[arguments.length - 1] : function() {}; | ||
var options = typeof method === 'object' ? | ||
method : | ||
undefined; | ||
var offset = 1; | ||
if (options === method) { | ||
method = arguments[1]; | ||
offset = 2; | ||
} | ||
var args = Array.prototype.slice.call(arguments, offset, | ||
hasCallback ? arguments.length - 1 : arguments.length); | ||
var alreadyCalled = false; | ||
var callbackErrorWrapper = function(error) { | ||
callback(error, undefined, false); | ||
if(alreadyCalled === false) { | ||
callback(error, undefined, false); | ||
alreadyCalled = true; | ||
} | ||
}; | ||
var ch = self._socket.openChannel(); | ||
middleware.addTimeout(self._timeout * 1000, ch, callbackErrorWrapper); | ||
var timeout = parseInt(options && options.timeout, 10) || self._timeout; | ||
middleware.addTimeout(timeout * 1000, ch, callbackErrorWrapper); | ||
@@ -127,3 +143,3 @@ //Associated callbacks to execute for various events | ||
}; | ||
ch.register(function(event) { | ||
@@ -130,0 +146,0 @@ var handler = handlers[event.name]; |
@@ -25,3 +25,3 @@ // Open Source Initiative OSI - The MIT License (MIT):Licensing | ||
var msgpack = require("msgpack"), | ||
uuid = require("node-uuid"); | ||
uuid = require("uuid"); | ||
@@ -88,4 +88,4 @@ //Serializes an event into an array of buffers that can be transmitted | ||
var uuidBase = uuid.v4().substring(0, 24), | ||
uuidCounter = 0; | ||
var uuidBase = uuid.v4().substring(0, 24); | ||
var uuidCounter = 0; | ||
@@ -92,0 +92,0 @@ function fastUUID() { |
@@ -40,5 +40,5 @@ // Open Source Initiative OSI - The MIT License (MIT):Licensing | ||
function getArguments(fun) { | ||
var m1 = /^[\s\(]*function[^(]*\(([^)]*)\)/, | ||
m2 = /\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g, | ||
m3 = /\s+/g; | ||
var m1 = /^[\s\(]*function[^(]*\(([^)]*)\)/; | ||
var m2 = /\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g; | ||
var m3 = /\s+/g; | ||
@@ -77,4 +77,4 @@ var names = fun.toString().match(m1)[1].replace(m2, '').replace(m3, '').split(','); | ||
function Server(context, heartbeat) { | ||
var self = this | ||
, heartbeat = heartbeat || DEFAULT_HEARTBEAT; | ||
var self = this; | ||
var heartbeat = heartbeat || DEFAULT_HEARTBEAT; | ||
@@ -81,0 +81,0 @@ |
{ | ||
"name": "zerorpc", | ||
"version": "0.9.6", | ||
"main": "./index.js", | ||
"author": "François-Xavier Bourlet <bombela+zerorpc@gmail.com>", | ||
"description": "A port of ZeroRPC to node.js", | ||
"contributors": [{ | ||
"name": "Francois-Xavier Bourlet", | ||
"email": "bombela@gmail.com" | ||
},{ | ||
"name": "Yusuf Simonson", | ||
"email": "simonson@gmail.com" | ||
}], | ||
"scripts": { | ||
"test": "./node_modules/.bin/nodeunit test" | ||
"name": "zerorpc", | ||
"version": "0.9.7", | ||
"main": "./index.js", | ||
"author": "François-Xavier Bourlet <bombela+zerorpc@gmail.com>", | ||
"description": "A port of ZeroRPC to node.js", | ||
"contributors": [ | ||
{ | ||
"name": "Francois-Xavier Bourlet", | ||
"email": "bombela@gmail.com" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/0rpc/zerorpc-node" | ||
}, | ||
"keywords": [ | ||
"zerorpc", | ||
"rpc", | ||
"distributed", | ||
"communication" | ||
], | ||
"dependencies": { | ||
"underscore": "1.3.3", | ||
"msgpack": "1.0.2", | ||
"node-uuid": "1.3.3", | ||
"zmq": "2.x" | ||
}, | ||
"devDependencies": { | ||
"nodeunit": "0.9.1", | ||
"temp": "0.8.1" | ||
}, | ||
"license": "MIT" | ||
{ | ||
"name": "Yusuf Simonson", | ||
"email": "simonson@gmail.com" | ||
} | ||
], | ||
"scripts": { | ||
"test": "./node_modules/.bin/nodeunit test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/0rpc/zerorpc-node" | ||
}, | ||
"keywords": [ | ||
"zerorpc", | ||
"rpc", | ||
"distributed", | ||
"communication" | ||
], | ||
"dependencies": { | ||
"msgpack": "1.0.2", | ||
"underscore": "1.3.3", | ||
"uuid": "^3.0.0", | ||
"zmq": "2.x" | ||
}, | ||
"devDependencies": { | ||
"nodeunit": "0.9.1", | ||
"temp": "0.8.1" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -10,6 +10,12 @@ zerorpc-node | ||
npm install zerorpc | ||
Make sure you have [ZeroMQ](https://github.com/zeromq/libzmq) installed. | ||
If you get the error `Package libzmq was not found`, take a look at [the fix for zeromq.node](https://github.com/JustinTulloss/zeromq.node/issues/55). If you get the error `Unable to load shared library <<path to zeromq.node>>/binding.node`, [make sure you run ldconfig](https://github.com/JustinTulloss/zeromq.node/issues/85). If that still doesn't work, check out [this ticket](https://github.com/JustinTulloss/zeromq.node/issues/92). | ||
Then: | ||
```bash | ||
npm install zerorpc | ||
``` | ||
If you get the error `Package libzmq was not found` after making sure ZeroMQ is installed, take a look at [the fix for zeromq.node](https://github.com/JustinTulloss/zeromq.node/issues/55). If you get the error `Unable to load shared library <<path to zeromq.node>>/binding.node`, [make sure you run ldconfig](https://github.com/JustinTulloss/zeromq.node/issues/85). If that still doesn't work, check out [this ticket](https://github.com/JustinTulloss/zeromq.node/issues/92). | ||
Servers | ||
@@ -20,4 +26,6 @@ ------- | ||
var zerorpc = require("zerorpc"); | ||
var server = new zerorpc.Server(context [, heartbeat]); | ||
```js | ||
var zerorpc = require("zerorpc"); | ||
var server = new zerorpc.Server(context [, heartbeat]); | ||
``` | ||
@@ -48,28 +56,30 @@ The constructor takes in a context object with the functions to expose | ||
var zerorpc = require("zerorpc"); | ||
```js | ||
var zerorpc = require("zerorpc"); | ||
var server = new zerorpc.Server({ | ||
addMan: function(sentence, reply) { | ||
reply(null, sentence + ", man!"); | ||
}, | ||
var server = new zerorpc.Server({ | ||
addMan: function(sentence, reply) { | ||
reply(null, sentence + ", man!"); | ||
}, | ||
add42: function(n, reply) { | ||
reply(null, n + 42); | ||
}, | ||
add42: function(n, reply) { | ||
reply(null, n + 42); | ||
}, | ||
iter: function(from, to, step, reply) { | ||
for(i=from; i<to; i+=step) { | ||
reply(null, i, true); | ||
} | ||
reply(); | ||
iter: function(from, to, step, reply) { | ||
for(i=from; i<to; i+=step) { | ||
reply(null, i, true); | ||
} | ||
}); | ||
server.bind("tcp://0.0.0.0:4242"); | ||
reply(); | ||
} | ||
}); | ||
server.on("error", function(error) { | ||
console.error("RPC server error:", error); | ||
}); | ||
server.bind("tcp://0.0.0.0:4242"); | ||
server.on("error", function(error) { | ||
console.error("RPC server error:", error); | ||
}); | ||
``` | ||
Clients | ||
@@ -80,4 +90,6 @@ ------- | ||
var zerorpc = require("zerorpc"); | ||
var client = new zerorpc.Client(options); | ||
```js | ||
var zerorpc = require("zerorpc"); | ||
var client = new zerorpc.Client(options); | ||
``` | ||
@@ -100,2 +112,3 @@ The constructor optionally takes in an options object. Allowable options: | ||
* `method` is the method name. | ||
* `arguments` are a list of arguments passed to the method, if there are no arguments then this argument is ommitted | ||
* `callback` is a method to call when there is an update. This callback is called as `callback(error, response, more)`, where error is an error object, response is the new update, and more is a boolean specifying whether new updates will be available later (i.e. whether the response is streaming). | ||
@@ -105,23 +118,23 @@ | ||
var zerorpc = require("zerorpc"); | ||
```js | ||
var zerorpc = require("zerorpc"); | ||
var client = new zerorpc.Client(); | ||
client.connect("tcp://127.0.0.1:4242"); | ||
var client = new zerorpc.Client(); | ||
client.connect("tcp://127.0.0.1:4242"); | ||
client.on("error", function(error) { | ||
console.error("RPC client error:", error); | ||
}); | ||
client.on("error", function(error) { | ||
console.error("RPC client error:", error); | ||
}); | ||
client.invoke("iter", 10, 20, 2, function(error, res, more) { | ||
if(error) { | ||
console.error(error); | ||
} else { | ||
console.log("UPDATE:", res); | ||
} | ||
client.invoke("iter", 10, 20, 2, function(error, res, more) { | ||
if(error) { | ||
console.error(error); | ||
} else { | ||
console.log("UPDATE:", res); | ||
} | ||
if(!more) { | ||
console.log("Done."); | ||
} | ||
}); | ||
if(!more) { | ||
console.log("Done."); | ||
} | ||
}); | ||
``` |
@@ -58,3 +58,18 @@ // Open Source Initiative OSI - The MIT License (MIT):Licensing | ||
}); | ||
} | ||
}, | ||
testOverride: function(test) { | ||
var timeout = 1, | ||
start; | ||
test.expect(4); | ||
start = Date.now(); | ||
this.cli.invoke({ timeout: timeout }, "quiet", function(error, res, more) { | ||
var end = Date.now(); | ||
test.ok(end - start - 1000 < 30, "Timeout should be ~1 second"); | ||
test.equal(error.name, "TimeoutExpired"); | ||
test.equal(res, null); | ||
test.equal(more, false); | ||
test.done(); | ||
}); | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64810
1531
135
+ Addeduuid@^3.0.0
+ Addeduuid@3.4.0(transitive)
- Removednode-uuid@1.3.3
- Removednode-uuid@1.3.3(transitive)