Skip to content

Commit

Permalink
Add ability to specify own request id generator for a client, togethe…
Browse files Browse the repository at this point in the history
…r with corresponding tests and README update
  • Loading branch information
tedeh committed Sep 21, 2012
1 parent 9396de1 commit 36a7f0a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -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

Expand Down Expand Up @@ -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:

Expand Down
7 changes: 5 additions & 2 deletions lib/client.js
Expand Up @@ -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 || {});
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions lib/utils.js
Expand Up @@ -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');
}
Expand All @@ -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,
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
16 changes: 15 additions & 1 deletion test/client.test.js
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit 36a7f0a

Please sign in to comment.