Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add ability to specify own request id generator for a client, togethe…

…r with corresponding tests and README update
  • Loading branch information...
commit 36a7f0a9aa2bf30479f6ee7d4bbfb1e9241e950d 1 parent 9396de1
@tedeh authored
View
3  README.md
@@ -207,6 +207,7 @@ Every client supports these options:
* `reviver` -> Function to use as a JSON reviver
* `replacer` -> Function to use as a JSON replacer
+* `idGenerator` -> Function to generate request ids with. If omitted, Jayson will just generate a "random" number like this: `Math.round(Math.random() * Math.pow(2, 24))`
##### Client.http
@@ -320,7 +321,7 @@ server.http().listen(3001, '127.0.0.1', function() {
Every request to `add` on the public server will now relay the request to the private server. See the client example in `examples/relay/client.js`.
-#### Events
+#### Server events
In addition to events that are specific to a certain interface, all servers will emit the following events:
View
7 lib/client.js
@@ -20,7 +20,8 @@ var Client = function(server, options) {
var defaults = {
reviver: null,
- replacer: null
+ replacer: null,
+ idGenerator: utils.generateId
};
this.options = utils.merge(defaults, options || {});
@@ -73,7 +74,9 @@ Client.prototype.request = function(method, params, id, callback) {
var hasCallback = typeof(callback) === 'function';
try {
- var request = utils.request(method, params, id);
+ var request = utils.request(method, params, id, {
+ generator: this.options.idGenerator
+ });
} catch(err) {
if(hasCallback) return callback(err);
throw err;
View
12 lib/utils.js
@@ -8,11 +8,12 @@ var Utils = module.exports;
* @param {String} method Name of method to call
* @param {Array|Object} params Array of parameters passed to the method as specified, or an object of parameter names and corresponding value
* @param {String|Number|null} [id] Request ID can be a string, number, null for explicit notification or left out for automatic generation
+ * @param {Object} [options] Optional name => value pairs of settings
* @throws {TypeError} If any of the parameters are invalid
* @return {Object} A JSON-RPC 2.0 request
* @api public
*/
-Utils.request = function(method, params, id) {
+Utils.request = function(method, params, id, options) {
if(typeof(method) !== 'string') {
throw new TypeError(method + ' must be a string');
}
@@ -21,6 +22,8 @@ Utils.request = function(method, params, id) {
throw new TypeError(params + ' must be an object or an array');
}
+ options = options || {};
+
var request = {
jsonrpc: "2.0",
params: params,
@@ -29,7 +32,8 @@ Utils.request = function(method, params, id) {
// if id was left out, generate one (null means explicit notification)
if(typeof(id) === 'undefined') {
- request.id = Utils.generateId();
+ var generator = typeof(options.generator) === 'function' ? options.generator : Utils.generateId;
+ request.id = generator(request);
} else {
request.id = id;
}
@@ -46,7 +50,7 @@ Utils.request = function(method, params, id) {
* @api public
*/
Utils.response = function(error, result, id) {
- id = id || null;
+ id = typeof(id) === 'undefined' || id === null ? null : id;
var response = { jsonrpc: "2.0", id: id };
// one or the other with precedence for errors
if(error) response.error = error;
@@ -109,7 +113,7 @@ Utils.parseBody = function(req, reviver, callback) {
/**
* Wraps a server instance around a HTTP request listener (used by the HTTP and HTTPS server modules)
* @param {JaysonServer} server Instance of JaysonServer (typically jayson.Server.http or jayson.Server.https)
- * @param {Object} [options] Optional name => value pair of settings
+ * @param {Object} [options] Optional name => value pairs of settings
* @return {Function}
* @api private
*/
View
16 test/client.test.js
@@ -40,7 +40,7 @@ describe('jayson client instance', function() {
it('should support reviving and replacing', support.clientReviveReplace(client));
- it('should return the response as received if given a two-param callback', function(done) {
+ it('should return the response as received if given a callback with arity 2', function(done) {
var a = 11, b = 9;
client.request('add', [a, b], function(err, response) {
arguments.length.should.equal(2);
@@ -52,6 +52,20 @@ describe('jayson client instance', function() {
});
});
+ it('should support specifying a request id generator', function(done) {
+ var ordinal = 0, a = 9, b = 2;
+ client.options.idGenerator = function(request) { return ordinal++; };
+ client.request('add', [a, b], function(err, response) {
+ should.not.exist(err);
+ should.exist(response);
+ response.should.have.property('result', a + b);
+ response.should.have.property('id', 0);
+ ordinal.should.equal(1);
+ delete client.options.idGenerator;
+ done();
+ });
+ });
+
it('should be able to execute a notification request', function(done) {
var a = 3, b = 4;
client.request('add', [a, b], null, function(err, response) {
Please sign in to comment.
Something went wrong with that request. Please try again.