Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* eslint func-names: ["error", "as-needed"] */
const redis = require('redis');
const commands = require('redis-commands').list;
const objectDecorator = require('./object-decorator');
const AsyncRedis = function (args) {
const client = Array.isArray(args) ? redis.createClient(...args) : redis.createClient(args);
return AsyncRedis.decorate(client);
};
AsyncRedis.createClient = (...args) => new AsyncRedis(args);
// this is the set of commands to NOT promisify
const commandsToSkipSet = new Set(['multi']);
// this is the set of commands to promisify
const commandSet = new Set(commands.filter(c => !commandsToSkipSet.has(c)));
AsyncRedis.decorate = redisClient => objectDecorator(redisClient, (name, method) => {
if (commandSet.has(name)) {
this.queue.push(new Command(command, arr, callback));
return this;
};
// Alias special function names (e.g. NR.RUN becomes NR_RUN and nr_run)
if (commandName !== command) {
Multi.prototype[commandName.toUpperCase()] = Multi.prototype[commandName] = Multi.prototype[command];
}
if (changeFunctionName) {
Object.defineProperty(Multi.prototype[command], 'name', {
value: commandName
});
}
}
};
commands.list.forEach(addCommand);
module.exports = addCommand;
it('should have decorated every command', async () => {
const commands = redisCommands.list;
commands.forEach(command => {
assert.isFunction(asyncRedisClient[command], `redis.${command} isn't decorated`);
});
});
});
function getCommands() {
var commands = {};
for (var i = 0; i < redisCommands.list.length; i++) {
commands[redisCommands.list[i].toUpperCase()] = client.send_command.bind(client);
}
commands[''] = noopCommand;
commands.HELP = helpCommand;
commands.NOOP = noopCommand;
commands.EXIT = quitCommand;
commands.QUIT = quitCommand;
commands.SAVE = saveCommand;
return commands;
}
function createRedisClient (p) {
const client = require('redis').createClient(p.s.redis.uri, p.s.redis.ca ? { tls: { ca: Buffer.from(p.s.redis.ca, 'base64').toString('binary') } } : {})
const noop = () => { }
let handler = noop
client.on('error', error => handler(error))
require('redis-commands').list.forEach(f => {
client[`${f}Async`] = function () {
let failed = false
return new Promise((resolve, reject) => {
handler = error => {
handler = noop
failed = true
reject(error)
}
client[f](...arguments, (error, result) => {
handler = noop
return error ? reject(error) : resolve(result)
})
}).catch(error => {
if (failed) client.end(true)
return Promise.reject(error)
})
function setupPrototype() {
var commands = redisCommands.list;
commands.forEach(function addCommandToPrototype(command) {
MultiRedisClient.prototype[command] = createCommandFunction(command);
MultiRedisClient.prototype[command.toUpperCase()] = MultiRedisClient.prototype[command];
});
}
'use strict';
var redis = require('redis'),
redisCmds = require('redis-commands').list;
function createCb(resolve, reject) {
return function (err, value) {
if (err !== null) {
reject(err);
} else {
resolve(value);
}
};
}
module.exports = function (promiseFactory) {
var mlproto = redis.Multi.prototype,
clproto = redis.RedisClient.prototype;
if (!promiseFactory) {
function createClient() {
return promisify(driver.createClient(url), without(redisCommands.list, 'multi'));
}