1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00

chore: use standard

This commit is contained in:
Ruben Bridgewater
2017-05-06 07:06:52 +02:00
parent 5d29f541e9
commit f1a7bcd735
106 changed files with 10706 additions and 10978 deletions

View File

@@ -1,38 +1,38 @@
'use strict';
'use strict'
// helpers for configuring a redis client in
// its various modes, ipV6, ipV4, socket.
var redis = require('../../index');
var bluebird = require('bluebird');
var redis = require('../../index')
var bluebird = require('bluebird')
// Promisify everything
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
bluebird.promisifyAll(redis.RedisClient.prototype)
bluebird.promisifyAll(redis.Multi.prototype)
var config = {
redis: redis,
PORT: 6379,
HOST: {
IPv4: '127.0.0.1',
IPv6: '::1'
},
configureClient: function (ip, opts) {
var args = [];
// Do not manipulate the opts => copy them each time
opts = opts ? JSON.parse(JSON.stringify(opts)) : {};
redis: redis,
PORT: 6379,
HOST: {
IPv4: '127.0.0.1',
IPv6: '::1'
},
configureClient: function (ip, opts) {
var args = []
// Do not manipulate the opts => copy them each time
opts = opts ? JSON.parse(JSON.stringify(opts)) : {}
if (ip.match(/\.sock/)) {
args.push(ip);
} else {
args.push(config.PORT);
args.push(config.HOST[ip]);
opts.family = ip;
}
args.push(opts);
return args;
if (ip.match(/\.sock/)) {
args.push(ip)
} else {
args.push(config.PORT)
args.push(config.HOST[ip])
opts.family = ip
}
};
module.exports = config;
args.push(opts)
return args
}
}
module.exports = config

View File

@@ -1,20 +1,20 @@
// Spawned by the goodStacks.spec.js tests
'use strict';
'use strict'
var assert = require('assert');
var redis = require('../../index');
var client = redis.createClient();
var assert = require('assert')
var redis = require('../../index')
var client = redis.createClient()
// Both error cases would normally return bad stack traces
client.set('foo', function (err, res) {
assert(/good-traces.js:9:8/.test(err.stack));
client.set('foo', 'bar', function (err, res) {
assert(/good-traces.js:11:12/.test(err.stack));
client.quit(function () {
process.exit(0);
});
});
process.nextTick(function () {
client.stream.destroy();
});
});
assert(/good-traces.js:9:8/.test(err.stack))
client.set('foo', 'bar', function (err, res) {
assert(/good-traces.js:11:10/.test(err.stack))
client.quit(function () {
process.exit(0)
})
})
process.nextTick(function () {
client.stream.destroy()
})
})

View File

@@ -1,88 +1,88 @@
'use strict';
'use strict'
// helper to start and stop the redis process.
var config = require('./config');
var fs = require('fs');
var path = require('path');
var spawn = require('win-spawn');
var tcpPortUsed = require('tcp-port-used');
var bluebird = require('bluebird');
var config = require('./config')
var fs = require('fs')
var path = require('path')
var spawn = require('win-spawn')
var tcpPortUsed = require('tcp-port-used')
var bluebird = require('bluebird')
// wait for redis to be listening in
// all three modes (ipv4, ipv6, socket).
function waitForRedis (available, cb, port) {
if (process.platform === 'win32') return cb();
if (process.platform === 'win32') return cb()
var time = Date.now();
var running = false;
var socket = '/tmp/redis.sock';
if (port) {
// We have to distinguishe the redis sockets if we have more than a single redis instance running
socket = '/tmp/redis' + port + '.sock';
}
port = port || config.PORT;
var id = setInterval(function () {
if (running) return;
running = true;
bluebird.join(
tcpPortUsed.check(port, '127.0.0.1'),
tcpPortUsed.check(port, '::1'),
function (ipV4, ipV6) {
if (ipV6 === available && ipV4 === available) {
if (fs.existsSync(socket) === available) {
clearInterval(id);
return cb();
}
// The same message applies for can't stop but we ignore that case
throw new Error('Port ' + port + ' is already in use. Tests can\'t start.\n');
}
if (Date.now() - time > 6000) {
throw new Error('Redis could not start on port ' + (port || config.PORT) + '\n');
}
running = false;
}).catch(function (err) {
console.error('\x1b[31m' + err.stack + '\x1b[0m\n');
process.exit(1);
});
}, 100);
var time = Date.now()
var running = false
var socket = '/tmp/redis.sock'
if (port) {
// We have to distinguish the redis sockets if we have more than a single redis instance running
socket = '/tmp/redis' + port + '.sock'
}
port = port || config.PORT
var id = setInterval(function () {
if (running) return
running = true
bluebird.join(
tcpPortUsed.check(port, '127.0.0.1'),
tcpPortUsed.check(port, '::1'),
function (ipV4, ipV6) {
if (ipV6 === available && ipV4 === available) {
if (fs.existsSync(socket) === available) {
clearInterval(id)
return cb()
}
// The same message applies for can't stop but we ignore that case
throw new Error('Port ' + port + ' is already in use. Tests can\'t start.\n')
}
if (Date.now() - time > 6000) {
throw new Error('Redis could not start on port ' + (port || config.PORT) + '\n')
}
running = false
}).catch(function (err) {
console.error('\x1b[31m' + err.stack + '\x1b[0m\n')
process.exit(1)
})
}, 100)
}
module.exports = {
start: function (done, conf, port) {
var spawnFailed = false;
// spawn redis with our testing configuration.
var confFile = conf || path.resolve(__dirname, '../conf/redis.conf');
var rp = spawn('redis-server', [confFile], {});
start: function (done, conf, port) {
var spawnFailed = false
// spawn redis with our testing configuration.
var confFile = conf || path.resolve(__dirname, '../conf/redis.conf')
var rp = spawn('redis-server', [confFile], {})
// capture a failure booting redis, and give
// the user running the test some directions.
rp.once('exit', function (code) {
if (code !== 0) spawnFailed = true;
});
// capture a failure booting redis, and give
// the user running the test some directions.
rp.once('exit', function (code) {
if (code !== 0) spawnFailed = true
})
// wait for redis to become available, by
// checking the port we bind on.
waitForRedis(true, function () {
// return an object that can be used in
// an after() block to shutdown redis.
return done(null, {
spawnFailed: function () {
return spawnFailed;
},
stop: function (done) {
if (spawnFailed) return done();
rp.once('exit', function (code) {
var error = null;
if (code !== null && code !== 0) {
error = new Error('Redis shutdown failed with code ' + code);
}
waitForRedis(false, function () {
return done(error);
}, port);
});
rp.kill('SIGTERM');
}
});
}, port);
}
};
// wait for redis to become available, by
// checking the port we bind on.
waitForRedis(true, function () {
// return an object that can be used in
// an after() block to shutdown redis.
return done(null, {
spawnFailed: function () {
return spawnFailed
},
stop: function (done) {
if (spawnFailed) return done()
rp.once('exit', function (code) {
var error = null
if (code !== null && code !== 0) {
error = new Error('Redis shutdown failed with code ' + code)
}
waitForRedis(false, function () {
return done(error)
}, port)
})
rp.kill('SIGTERM')
}
})
}, port)
}
}

View File

@@ -1,83 +1,83 @@
'use strict';
'use strict'
// helper to start and stop the stunnel process.
var spawn = require('child_process').spawn;
var EventEmitter = require('events');
var fs = require('fs');
var path = require('path');
var util = require('util');
var spawn = require('child_process').spawn
var EventEmitter = require('events')
var fs = require('fs')
var path = require('path')
var util = require('util')
function once (cb) {
var called = false;
return function () {
if (called) return;
called = true;
cb.apply(this, arguments);
};
var called = false
return function () {
if (called) return
called = true
cb.apply(this, arguments)
}
}
function StunnelProcess (confDir) {
EventEmitter.call(this);
EventEmitter.call(this)
// set up an stunnel to redis; edit the conf file to include required absolute paths
var confFile = path.resolve(confDir, 'stunnel.conf');
var confText = fs.readFileSync(confFile + '.template').toString().replace(/__dirname,/g, confDir);
// set up an stunnel to redis; edit the conf file to include required absolute paths
var confFile = path.resolve(confDir, 'stunnel.conf')
var confText = fs.readFileSync(confFile + '.template').toString().replace(/__dirname,/g, confDir)
fs.writeFileSync(confFile, confText);
var stunnel = this.stunnel = spawn('stunnel', [confFile]);
fs.writeFileSync(confFile, confText)
var stunnel = this.stunnel = spawn('stunnel', [confFile])
// handle child process events, and failure to set up tunnel
var self = this;
this.timer = setTimeout(function () {
self.emit('error', new Error('Timeout waiting for stunnel to start'));
}, 8000);
// handle child process events, and failure to set up tunnel
var self = this
this.timer = setTimeout(function () {
self.emit('error', new Error('Timeout waiting for stunnel to start'))
}, 8000)
stunnel.on('error', function (err) {
self.clear();
self.emit('error', err);
});
stunnel.on('error', function (err) {
self.clear()
self.emit('error', err)
})
stunnel.on('exit', function (code) {
self.clear();
if (code === 0) {
self.emit('stopped');
} else {
self.emit('error', new Error('Stunnel exited unexpectedly; code = ' + code));
}
});
stunnel.on('exit', function (code) {
self.clear()
if (code === 0) {
self.emit('stopped')
} else {
self.emit('error', new Error('Stunnel exited unexpectedly; code = ' + code))
}
})
// wait to stunnel to start
stunnel.stderr.on('data', function (data) {
if (data.toString().match(/Service.+redis.+bound/)) {
clearTimeout(this.timer);
self.emit('started');
}
});
// wait to stunnel to start
stunnel.stderr.on('data', function (data) {
if (data.toString().match(/Service.+redis.+bound/)) {
clearTimeout(this.timer)
self.emit('started')
}
})
}
util.inherits(StunnelProcess, EventEmitter);
util.inherits(StunnelProcess, EventEmitter)
StunnelProcess.prototype.clear = function () {
this.stunnel = null;
clearTimeout(this.timer);
};
this.stunnel = null
clearTimeout(this.timer)
}
StunnelProcess.prototype.stop = function (done) {
if (this.stunnel) {
this.stunnel.kill();
}
};
if (this.stunnel) {
this.stunnel.kill()
}
}
module.exports = {
start: function (done, confDir) {
done = once(done);
var stunnel = new StunnelProcess(confDir);
stunnel.once('error', done.bind(done));
stunnel.once('started', done.bind(done, null, stunnel));
},
stop: function (stunnel, done) {
stunnel.removeAllListeners();
stunnel.stop();
stunnel.once('error', done.bind(done));
stunnel.once('stopped', done.bind(done, null));
}
};
start: function (done, confDir) {
done = once(done)
var stunnel = new StunnelProcess(confDir)
stunnel.once('error', done.bind(done))
stunnel.once('started', done.bind(done, null, stunnel))
},
stop: function (stunnel, done) {
stunnel.removeAllListeners()
stunnel.stop()
stunnel.once('error', done.bind(done))
stunnel.once('stopped', done.bind(done, null))
}
}

View File

@@ -1,17 +1,17 @@
// spawned by the unref tests in nodeRedis.spec.js.
// when configured, unref causes the client to exit
// as soon as there are no outstanding commands.
'use strict';
'use strict'
var redis = require('../../index');
var HOST = process.argv[2] || '127.0.0.1';
var PORT = process.argv[3];
var args = PORT ? [PORT, HOST] : [HOST];
var redis = require('../../index')
var HOST = process.argv[2] || '127.0.0.1'
var PORT = process.argv[3]
var args = PORT ? [PORT, HOST] : [HOST]
var c = redis.createClient.apply(redis, args);
var c = redis.createClient.apply(redis, args)
c.info(function (err, reply) {
if (err) process.exit(-1);
if (!reply.length) process.exit(-1);
process.stdout.write(reply.length.toString());
});
c.unref();
if (err) process.exit(-1)
if (!reply.length) process.exit(-1)
process.stdout.write(reply.length.toString())
})
c.unref()