multiple-redis
Advanced tools
Comparing version 0.0.12 to 0.0.13
@@ -16,3 +16,3 @@ <a name="MultiRedisClient"></a> | ||
* _static_ | ||
* [.createClient(clients)](#MultiRedisClient.createClient) ⇒ <code>[MultiRedisClient](#MultiRedisClient)</code> | ||
* [.createClient(clients, [options])](#MultiRedisClient.createClient) ⇒ <code>[MultiRedisClient](#MultiRedisClient)</code> | ||
* [.createClient(connectionInfo, [options])](#MultiRedisClient.createClient) ⇒ <code>[MultiRedisClient](#MultiRedisClient)</code> | ||
@@ -33,2 +33,3 @@ | ||
| [params.options] | <code>Array</code> | | Used when this client creates the redis clients (see redis module for more details) | | ||
| [params.options.childCommandTimeout] | <code>number</code> | <code>10000</code> | The per client command timeout | | ||
| [params.options.mergeDuplicateEndpoints] | <code>boolean</code> | <code>true</code> | True to merge duplicate endpoint configurations and prevent needless redis client connections | | ||
@@ -70,3 +71,3 @@ | ||
<a name="MultiRedisClient.createClient"></a> | ||
### MultiRedisClient.createClient(clients) ⇒ <code>[MultiRedisClient](#MultiRedisClient)</code> | ||
### MultiRedisClient.createClient(clients, [options]) ⇒ <code>[MultiRedisClient](#MultiRedisClient)</code> | ||
Creates and returns a new MultiRedisClient instance. | ||
@@ -78,5 +79,7 @@ | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| clients | <code>Array</code> | <code>redis</code> | The redis client/s | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| clients | <code>Array</code> | <code>redis</code> | | The redis client/s | | ||
| [options] | <code>Array</code> | | Various options | | ||
| [options.childCommandTimeout] | <code>number</code> | <code>10000</code> | The per client command timeout | | ||
@@ -109,2 +112,3 @@ **Example** | ||
| [options] | <code>Array</code> | | Used when this client creates the redis clients (see redis module for more details) | | ||
| [options.childCommandTimeout] | <code>number</code> | <code>10000</code> | The per client command timeout | | ||
| [options.mergeDuplicateEndpoints] | <code>boolean</code> | <code>true</code> | True to merge duplicate endpoint configurations and prevent needless redis client connections | | ||
@@ -111,0 +115,0 @@ |
@@ -23,2 +23,3 @@ 'use strict'; | ||
* @param {Array} [params.options] - Used when this client creates the redis clients (see redis module for more details) | ||
* @param {number} [params.options.childCommandTimeout=10000] - The per client command timeout | ||
* @param {boolean} [params.options.mergeDuplicateEndpoints=true] - True to merge duplicate endpoint configurations and prevent needless redis client connections | ||
@@ -32,2 +33,16 @@ */ | ||
var options = params.options || {}; | ||
self.childCommandTimeout = Math.max(options.childCommandTimeout || 10000, 1); | ||
/*jshint camelcase: false*/ | ||
//jscs:disable requireCamelCaseOrUpperCaseIdentifiers | ||
/*eslint-disable camelcase*/ | ||
//disable offline queue to prevent old data in redis clients from being sent | ||
if (options.enable_offline_queue === undefined) { | ||
options.enable_offline_queue = false; | ||
} | ||
/*eslint-enable camelcase*/ | ||
//jscs:enable requireCamelCaseOrUpperCaseIdentifiers | ||
/*jshint camelcase: true*/ | ||
if (params.clients) { | ||
@@ -40,3 +55,2 @@ if (Array.isArray(params.clients)) { | ||
} else { | ||
var options = params.options || {}; | ||
if (options.mergeDuplicateEndpoints === undefined) { | ||
@@ -63,3 +77,3 @@ options.mergeDuplicateEndpoints = true; | ||
['ready', 'connect', 'error', 'end', 'drain', 'idle', 'message', 'pmessage', 'subscribe', 'psubscribe', 'unsubscribe', 'punsubscribe'].forEach(function createProxy(event) { | ||
self.clients.forEach(function (client) { | ||
self.clients.forEach(function onClientEvent(client) { | ||
client.on(event, function onEvent() { | ||
@@ -215,2 +229,26 @@ var argumentsArray = Array.prototype.slice.call(arguments, 0); | ||
var originalAsyncCallback = asyncCallback; | ||
var ignore = false; | ||
var timeoutID; | ||
asyncCallback = function onWrappedCallback(error, output) { | ||
if (timeoutID) { | ||
clearTimeout(timeoutID); | ||
timeoutID = null; | ||
} | ||
if (!ignore) { | ||
ignore = true; | ||
originalAsyncCallback(error, output); | ||
originalAsyncCallback = null; | ||
} | ||
}; | ||
timeoutID = setTimeout(function timeoutCommand() { | ||
var error = new Error('Timeout on running command.'); | ||
if (!done) { | ||
redisError = error; | ||
} | ||
asyncCallback(error); | ||
}, self.childCommandTimeout); | ||
try { | ||
@@ -221,12 +259,14 @@ /*jshint camelcase: false*/ | ||
client.send_command(command, args, function onCommandEnd(error, output) { | ||
if (error) { | ||
redisError = error; | ||
} else if (output) { | ||
if (getCommand) { | ||
done = true; | ||
if (!ignore) { | ||
if (error) { | ||
redisError = error; | ||
} else if (output) { | ||
if (getCommand) { | ||
done = true; | ||
} | ||
redisOutput = output; | ||
} | ||
redisOutput = output; | ||
asyncCallback(null, redisOutput); | ||
} | ||
asyncCallback(null, redisOutput); | ||
}); | ||
@@ -237,4 +277,6 @@ /*eslint-enable camelcase*/ | ||
} catch (unhandledError) { | ||
redisError = unhandledError; | ||
asyncCallback(); | ||
if (!ignore) { | ||
redisError = unhandledError; | ||
asyncCallback(); | ||
} | ||
} | ||
@@ -258,2 +300,3 @@ }); | ||
module.exports = { | ||
/*eslint-disable valid-jsdoc*/ | ||
/** | ||
@@ -266,2 +309,4 @@ * Creates and returns a new MultiRedisClient instance. | ||
* @param {Array|redis} clients - The redis client/s | ||
* @param {Array} [options] - Various options | ||
* @param {number} [options.childCommandTimeout=10000] - The per client command timeout | ||
* @returns {MultiRedisClient} The multiple redis client instance | ||
@@ -292,2 +337,3 @@ * @example | ||
* @param {Array} [options] - Used when this client creates the redis clients (see redis module for more details) | ||
* @param {number} [options.childCommandTimeout=10000] - The per client command timeout | ||
* @param {boolean} [options.mergeDuplicateEndpoints=true] - True to merge duplicate endpoint configurations and prevent needless redis client connections | ||
@@ -316,6 +362,11 @@ * @returns {MultiRedisClient} The multiple redis client instance | ||
if (args.length === 2) { | ||
params.connectionInfo = args[0]; | ||
params.options = args[1]; | ||
} else if (args.length === 1) { | ||
if (args.length > 2) { | ||
throw new Error('Wrong arguments count provided.'); | ||
} | ||
if (args.length > 0) { | ||
if (args.length === 2) { | ||
params.options = args[1]; | ||
} | ||
var arg = args[0]; | ||
@@ -337,2 +388,3 @@ if (Array.isArray(arg)) { | ||
} | ||
/*eslint-enable valid-jsdoc*/ | ||
}; |
{ | ||
"name": "multiple-redis", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"description": "Run redis commands against multiple redis instances.", | ||
@@ -25,3 +25,3 @@ "main": "index.js", | ||
"async": "^1.4.2", | ||
"redis": "^2.2.3" | ||
"redis": "^2.2.5" | ||
}, | ||
@@ -28,0 +28,0 @@ "devDependencies": { |
@@ -135,2 +135,3 @@ # multiple-redis | ||
| ----------- | ------- | ----------- | | ||
| 2015-10-22 | v0.0.13 | Timeout child commands (see childCommandTimeout option) | | ||
| 2015-10-16 | v0.0.12 | Maintenance | | ||
@@ -137,0 +138,0 @@ | 2015-09-23 | v0.0.7 | Upgrade to redis 2.0 | |
@@ -13,3 +13,3 @@ 'use strict'; | ||
var noop = function () { | ||
var noop = function noop() { | ||
return undefined; | ||
@@ -20,5 +20,6 @@ }; | ||
var baseCreate = redis.createClient; | ||
function mockRedis() { | ||
var mockRedis = function mockRedis() { | ||
return (process.env.MULTIPLE_REDIS_TEST_USE_REDIS !== 'true'); | ||
} | ||
}; | ||
redis.createClient = function (port, host, options) { | ||
@@ -61,2 +62,14 @@ var redisClient; | ||
it('too many arguments', function () { | ||
try { | ||
MultipleRedis.createClient([{ | ||
host: 'localhost1', | ||
port: 1234 | ||
}], {}, {}); | ||
assert.fail(); | ||
} catch (error) { | ||
assert.isDefined(error); | ||
} | ||
}); | ||
it('redis clients', function () { | ||
@@ -145,2 +158,3 @@ var client = MultipleRedis.createClient([{ | ||
assert.deepEqual(options, { | ||
enable_offline_queue: false, | ||
someoption: 123, | ||
@@ -180,2 +194,3 @@ mergeDuplicateEndpoints: true | ||
assert.deepEqual(options, { | ||
enable_offline_queue: false, | ||
someoption: 'abc', | ||
@@ -473,2 +488,64 @@ mergeDuplicateEndpoints: true | ||
}); | ||
it('set partial timeout', function (done) { | ||
var count = 0; | ||
var createClient = function (valid) { | ||
return { | ||
on: noop, | ||
send_command: function (name, args, callback) { | ||
count++; | ||
assert.equal(name, 'set'); | ||
assert.deepEqual(args, ['my key', 'my value']); | ||
assert.isFunction(callback); | ||
if (valid) { | ||
callback(null, 'OK'); | ||
} | ||
} | ||
}; | ||
}; | ||
var client1 = createClient(true); | ||
var client2 = createClient(false); | ||
var client = MultipleRedis.createClient([client1, client2], { | ||
childCommandTimeout: 10 | ||
}); | ||
client.set('my key', 'my value', function (error, response) { | ||
assert.isNull(error); | ||
assert.equal(response, 'OK'); | ||
assert.equal(count, 2); | ||
done(); | ||
}); | ||
}); | ||
it('set full timeout', function (done) { | ||
var count = 0; | ||
var createClient = function () { | ||
return { | ||
on: noop, | ||
send_command: function (name, args, callback) { | ||
count++; | ||
assert.equal(name, 'set'); | ||
assert.deepEqual(args, ['my key', 'my value']); | ||
assert.isFunction(callback); | ||
} | ||
}; | ||
}; | ||
var client1 = createClient(); | ||
var client2 = createClient(); | ||
var client = MultipleRedis.createClient([client1, client2], { | ||
childCommandTimeout: 10 | ||
}); | ||
client.set('my key', 'my value', function (error, response) { | ||
assert.isDefined(error); | ||
assert.isUndefined(response); | ||
assert.equal(count, 2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -475,0 +552,0 @@ |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 5 instances in 1 package
83519
18
1392
145
7
Updatedredis@^2.2.5