You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
chore: refactor client to es6 class and sort requires
This commit is contained in:
139
index.js
139
index.js
@@ -4,27 +4,37 @@
|
||||
// We have to replace the error codes and make them coherent.
|
||||
// We also have to use InterruptError s instead of AbortError s.
|
||||
// The Error messages might be improved as well.
|
||||
// TODO: Rewrite this to classes
|
||||
const net = require('net')
|
||||
const util = require('util')
|
||||
const utils = require('./lib/utils')
|
||||
const Queue = require('denque')
|
||||
const EventEmitter = require('events')
|
||||
const Errors = require('redis-errors')
|
||||
const connect = require('./lib/connect')
|
||||
const net = require('net')
|
||||
const Commands = require('redis-commands')
|
||||
const Errors = require('redis-errors')
|
||||
const Command = require('./lib/command')
|
||||
const addCommand = require('./lib/commands')
|
||||
const connect = require('./lib/connect')
|
||||
const unifyOptions = require('./lib/createClient')
|
||||
const debug = require('./lib/debug')
|
||||
const flushAndError = require('./lib/flushAndError')
|
||||
const Multi = require('./lib/multi')
|
||||
const normalizeAndWriteCommand = require('./lib/writeCommands')
|
||||
const offlineCommand = require('./lib/offlineCommand')
|
||||
const utils = require('./lib/utils')
|
||||
const normalizeAndWriteCommand = require('./lib/writeCommands')
|
||||
const noop = function () {}
|
||||
|
||||
// Attention: The second parameter might be removed at will and is not officially supported.
|
||||
// Do not rely on this
|
||||
function RedisClient (options, stream) {
|
||||
class RedisClient extends EventEmitter {
|
||||
/**
|
||||
* Creates an instance of RedisClient.
|
||||
* @param {object} options
|
||||
* @param {any} [stream]
|
||||
*
|
||||
* @memberof RedisClient
|
||||
*/
|
||||
constructor (options, stream) {
|
||||
super()
|
||||
// Copy the options so they are not mutated
|
||||
options = utils.clone(options)
|
||||
EventEmitter.call(this)
|
||||
const cnxOptions = {}
|
||||
/* istanbul ignore next: travis does not work with stunnel atm. Therefore the tls tests are skipped on travis */
|
||||
for (const tlsOption in options.tls) {
|
||||
@@ -117,13 +127,10 @@ function RedisClient (options, stream) {
|
||||
}
|
||||
})
|
||||
}
|
||||
util.inherits(RedisClient, EventEmitter)
|
||||
|
||||
RedisClient.connectionId = 0
|
||||
|
||||
// Do not call internalSendCommand directly, if you are not absolutely certain it handles everything properly
|
||||
// e.g. monitor / info does not work with internalSendCommand only
|
||||
RedisClient.prototype.internalSendCommand = function (commandObj) {
|
||||
internalSendCommand (commandObj) {
|
||||
if (this.ready === false || this._stream.writable === false) {
|
||||
// Handle offline commands right away
|
||||
offlineCommand(this, commandObj)
|
||||
@@ -153,6 +160,111 @@ RedisClient.prototype.internalSendCommand = function (commandObj) {
|
||||
return commandObj.promise
|
||||
}
|
||||
|
||||
// Redirect calls to the appropriate function and use to send arbitrary / not supported commands
|
||||
sendCommand (command, args) {
|
||||
// Throw to fail early instead of relying in order in this case
|
||||
if (typeof command !== 'string') {
|
||||
throw new TypeError(`Wrong input type "${command !== null && command !== undefined ? command.constructor.name : command}" for command name`)
|
||||
}
|
||||
if (!Array.isArray(args)) {
|
||||
if (args === undefined || args === null) {
|
||||
args = []
|
||||
} else {
|
||||
throw new TypeError(`Wrong input type "${args.constructor.name}" for args`)
|
||||
}
|
||||
}
|
||||
|
||||
// Using the raw multi command is only possible with this function
|
||||
// If the command is not yet added to the client, the internal function should be called right away
|
||||
// Otherwise we need to redirect the calls to make sure the internal functions don't get skipped
|
||||
// The internal functions could actually be used for any non hooked function
|
||||
// but this might change from time to time and at the moment there's no good way to distinguish them
|
||||
// from each other, so let's just do it do it this way for the time being
|
||||
if (command === 'multi' || typeof this[command] !== 'function') {
|
||||
return this.internalSendCommand(new Command(command, args))
|
||||
}
|
||||
return this[command].apply(this, args)
|
||||
}
|
||||
|
||||
end (flush) {
|
||||
if (typeof flush !== 'boolean') {
|
||||
throw new TypeError('You must call "end" with the flush argument.')
|
||||
}
|
||||
|
||||
// Flush queue if wanted
|
||||
if (flush) {
|
||||
flushAndError(this, 'Connection forcefully ended and command aborted.', 'NR_CLOSED')
|
||||
}
|
||||
// Clear retryTimer
|
||||
if (this.retryTimer) {
|
||||
clearTimeout(this.retryTimer)
|
||||
this.retryTimer = null
|
||||
}
|
||||
this._stream.removeAllListeners()
|
||||
this._stream.on('error', noop)
|
||||
this.connected = false
|
||||
this.ready = false
|
||||
this.closing = true
|
||||
return this._stream.destroySoon()
|
||||
}
|
||||
|
||||
unref () {
|
||||
if (this.connected) {
|
||||
debug('Unref\'ing the socket connection')
|
||||
this._stream.unref()
|
||||
} else {
|
||||
debug('Not connected yet, will unref later')
|
||||
this.once('connect', function () {
|
||||
this.unref()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: promisify this
|
||||
duplicate (options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = null
|
||||
}
|
||||
const existingOptions = utils.clone(this.options)
|
||||
options = utils.clone(options)
|
||||
for (const elem in options) {
|
||||
if (options.hasOwnProperty(elem)) {
|
||||
existingOptions[elem] = options[elem]
|
||||
}
|
||||
}
|
||||
const client = new RedisClient(existingOptions)
|
||||
client.selectedDb = this.selectedDb
|
||||
if (typeof callback === 'function') {
|
||||
const errorListener = function (err) {
|
||||
callback(err)
|
||||
client.end(true)
|
||||
}
|
||||
const readyListener = function () {
|
||||
callback(null, client)
|
||||
client.removeAllListeners(errorListener)
|
||||
}
|
||||
client.once('ready', readyListener)
|
||||
client.once('error', errorListener)
|
||||
return client
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
// Note: this overrides a native function!
|
||||
multi (args) {
|
||||
return new Multi(this, 'multi', args)
|
||||
}
|
||||
|
||||
// Note: This is not a native function but is still handled as a individual command as it behaves just the same as multi
|
||||
batch (args) {
|
||||
return new Multi(this, 'batch', args)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
RedisClient.connectionId = 0
|
||||
|
||||
Commands.list.forEach((name) => addCommand(RedisClient.prototype, Multi.prototype, name))
|
||||
|
||||
module.exports = {
|
||||
@@ -172,4 +284,3 @@ module.exports = {
|
||||
// Add all redis commands / nodeRedis api to the client
|
||||
// TODO: Change the way this is included...
|
||||
require('./lib/individualCommands')
|
||||
require('./lib/extendedApi')
|
||||
|
@@ -1,12 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
const tls = require('tls')
|
||||
const Parser = require('redis-parser')
|
||||
const net = require('net')
|
||||
const onConnect = require('./readyHandler')
|
||||
const Parser = require('redis-parser')
|
||||
const tls = require('tls')
|
||||
const debug = require('./debug')
|
||||
const replyHandler = require('./replyHandler')
|
||||
const flushAndError = require('./flushAndError')
|
||||
const onConnect = require('./readyHandler')
|
||||
const replyHandler = require('./replyHandler')
|
||||
const onResult = replyHandler.onResult
|
||||
const onError = replyHandler.onError
|
||||
|
||||
|
@@ -1,116 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const utils = require('./utils')
|
||||
const debug = require('./debug')
|
||||
const RedisClient = require('../').RedisClient
|
||||
const Command = require('./command')
|
||||
const Multi = require('./multi')
|
||||
const flushAndError = require('./flushAndError')
|
||||
const noop = function () {}
|
||||
|
||||
/**********************************************
|
||||
All documented and exposed API belongs in here
|
||||
**********************************************/
|
||||
|
||||
// Redirect calls to the appropriate function and use to send arbitrary / not supported commands
|
||||
// TODO: REMOVE sendCommand and replace it by a function to add new commands
|
||||
// TODO: Add a library to add the sendCommand back in place for legacy reasons
|
||||
RedisClient.prototype.sendCommand = function (command, args) {
|
||||
// Throw to fail early instead of relying in order in this case
|
||||
if (typeof command !== 'string') {
|
||||
throw new TypeError(`Wrong input type "${command !== null && command !== undefined ? command.constructor.name : command}" for command name`)
|
||||
}
|
||||
if (!Array.isArray(args)) {
|
||||
if (args === undefined || args === null) {
|
||||
args = []
|
||||
} else {
|
||||
throw new TypeError(`Wrong input type "${args.constructor.name}" for args`)
|
||||
}
|
||||
}
|
||||
|
||||
// Using the raw multi command is only possible with this function
|
||||
// If the command is not yet added to the client, the internal function should be called right away
|
||||
// Otherwise we need to redirect the calls to make sure the internal functions don't get skipped
|
||||
// The internal functions could actually be used for any non hooked function
|
||||
// but this might change from time to time and at the moment there's no good way to distinguish them
|
||||
// from each other, so let's just do it do it this way for the time being
|
||||
if (command === 'multi' || typeof this[command] !== 'function') {
|
||||
return this.internalSendCommand(new Command(command, args))
|
||||
}
|
||||
return this[command].apply(this, args)
|
||||
}
|
||||
|
||||
RedisClient.prototype.end = function (flush) {
|
||||
if (typeof flush !== 'boolean') {
|
||||
throw new TypeError('You must call "end" with the flush argument.')
|
||||
}
|
||||
|
||||
// Flush queue if wanted
|
||||
if (flush) {
|
||||
flushAndError(this, 'Connection forcefully ended and command aborted.', 'NR_CLOSED')
|
||||
}
|
||||
// Clear retryTimer
|
||||
if (this.retryTimer) {
|
||||
clearTimeout(this.retryTimer)
|
||||
this.retryTimer = null
|
||||
}
|
||||
this._stream.removeAllListeners()
|
||||
this._stream.on('error', noop)
|
||||
this.connected = false
|
||||
this.ready = false
|
||||
this.closing = true
|
||||
return this._stream.destroySoon()
|
||||
}
|
||||
|
||||
RedisClient.prototype.unref = function () {
|
||||
if (this.connected) {
|
||||
debug('Unref\'ing the socket connection')
|
||||
this._stream.unref()
|
||||
} else {
|
||||
debug('Not connected yet, will unref later')
|
||||
this.once('connect', function () {
|
||||
this.unref()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: promisify this
|
||||
RedisClient.prototype.duplicate = function (options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = null
|
||||
}
|
||||
const existingOptions = utils.clone(this.options)
|
||||
options = utils.clone(options)
|
||||
for (const elem in options) {
|
||||
if (options.hasOwnProperty(elem)) {
|
||||
existingOptions[elem] = options[elem]
|
||||
}
|
||||
}
|
||||
const client = new RedisClient(existingOptions)
|
||||
client.selectedDb = this.selectedDb
|
||||
if (typeof callback === 'function') {
|
||||
const errorListener = function (err) {
|
||||
callback(err)
|
||||
client.end(true)
|
||||
}
|
||||
const readyListener = function () {
|
||||
callback(null, client)
|
||||
client.removeAllListeners(errorListener)
|
||||
}
|
||||
client.once('ready', readyListener)
|
||||
client.once('error', errorListener)
|
||||
return client
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
// Note: this overrides a native function!
|
||||
RedisClient.prototype.multi = function multi (args) {
|
||||
return new Multi(this, 'multi', args)
|
||||
}
|
||||
|
||||
// Note: This is not a native function but is still handled as a individual command as it behaves just the same as multi
|
||||
RedisClient.prototype.batch = function batch (args) {
|
||||
return new Multi(this, 'batch', args)
|
||||
}
|
@@ -1,8 +1,8 @@
|
||||
'use strict'
|
||||
|
||||
const Command = require('./command')
|
||||
const debug = require('./debug')
|
||||
const Multi = require('./multi')
|
||||
const Command = require('./command')
|
||||
const utils = require('./utils')
|
||||
const noPasswordIsSet = /no password is set/
|
||||
const RedisClient = require('../').RedisClient
|
||||
|
@@ -1,9 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
const Errors = require('redis-errors')
|
||||
const Queue = require('denque')
|
||||
const utils = require('./utils')
|
||||
const Errors = require('redis-errors')
|
||||
const Command = require('./command')
|
||||
const utils = require('./utils')
|
||||
const handleReply = utils.handleReply
|
||||
|
||||
/**
|
||||
|
@@ -1,8 +1,8 @@
|
||||
'use strict'
|
||||
|
||||
const Errors = require('redis-errors')
|
||||
const utils = require('./utils')
|
||||
const debug = require('./debug')
|
||||
const utils = require('./utils')
|
||||
|
||||
function offlineCommand (client, command) {
|
||||
const commandName = command.command.toUpperCase()
|
||||
|
@@ -1,7 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('./debug')
|
||||
const Command = require('./command')
|
||||
const debug = require('./debug')
|
||||
const utils = require('./utils')
|
||||
|
||||
function onConnect (client) {
|
||||
|
@@ -1,9 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
const Errors = require('redis-errors')
|
||||
const connect = require('./connect')
|
||||
const debug = require('./debug')
|
||||
const flushAndError = require('./flushAndError')
|
||||
const connect = require('./connect')
|
||||
|
||||
/**
|
||||
* @description Try connecting to a server again
|
||||
|
@@ -1,8 +1,8 @@
|
||||
'use strict'
|
||||
|
||||
const Buffer = require('buffer').Buffer
|
||||
const utils = require('./utils')
|
||||
const pubsub = require('./pubsub')
|
||||
const utils = require('./utils')
|
||||
|
||||
function onError (client, err) {
|
||||
const commandObj = client.commandQueue.shift()
|
||||
|
@@ -149,6 +149,11 @@ function handleReply (client, reply, command) {
|
||||
return reply
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Set default reconnect variables
|
||||
*
|
||||
* @param {RedisClient} client
|
||||
*/
|
||||
function setReconnectDefaults (client) {
|
||||
client.retryTimer = null
|
||||
client.retryTotaltime = 0
|
||||
|
@@ -1,8 +1,8 @@
|
||||
'use strict'
|
||||
|
||||
const Commands = require('redis-commands')
|
||||
const utils = require('./utils')
|
||||
const debug = require('./debug')
|
||||
const utils = require('./utils')
|
||||
// const isUint8Array = (() => {
|
||||
// try {
|
||||
// return process.binding('util').isUint8Array
|
||||
|
@@ -23,7 +23,7 @@
|
||||
"test": "nyc --cache mocha ./test/*.js ./test/commands/*.js --timeout=8000",
|
||||
"posttest": "npm run coverage",
|
||||
"compare": "node benchmarks/diff_multi_bench_output.js beforeBench.txt afterBench.txt",
|
||||
"lint": "eslint . --fix"
|
||||
"lint": "standard . --fix"
|
||||
},
|
||||
"dependencies": {
|
||||
"denque": "^1.1.1",
|
||||
|
Reference in New Issue
Block a user