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

chore: mark private variables as such and remove obsolete ones

This commit is contained in:
Ruben Bridgewater
2017-05-28 07:15:20 +02:00
parent ba7a39c443
commit b2e18344d9
21 changed files with 179 additions and 165 deletions

View File

@@ -16,7 +16,7 @@ var reconnect = function (client, why, err) {
}
function onStreamError (client, err) {
if (client.closing) {
if (client._closing) {
return
}
@@ -26,7 +26,7 @@ function onStreamError (client, err) {
client.ready = false
// Only emit the error if the retryStrategy option is not set
if (client.retryStrategyProvided === false) {
if (client._retryStrategyProvided === false) {
client.emit('error', err)
}
// 'error' events get turned into exceptions if they aren't listened for. If the user handled this error
@@ -61,7 +61,7 @@ function createParser (client) {
setImmediate(() => client.emit('error', err))
},
returnBuffers: client.buffers || client.messageBuffers,
stringNumbers: client.options.stringNumbers || false
stringNumbers: client._options.stringNumbers || false
})
}
@@ -78,12 +78,12 @@ function connect (client) {
const parser = createParser(client)
client._replyParser = parser
if (client.options.stream) {
if (client._options.stream) {
// Only add the listeners once in case of a reconnect try (that won't work)
if (client._stream) {
return
}
client._stream = client.options.stream
client._stream = client._options.stream
} else {
// On a reconnect destroy the former stream and retry
if (client._stream) {
@@ -92,25 +92,25 @@ function connect (client) {
}
/* istanbul ignore if: travis does not work with stunnel atm. Therefore the tls tests are skipped on travis */
if (client.options.tls) {
client._stream = tls.connect(client.connectionOptions)
if (client._options.tls) {
client._stream = tls.connect(client._connectionOptions)
} else {
client._stream = net.createConnection(client.connectionOptions)
client._stream = net.createConnection(client._connectionOptions)
}
}
const stream = client._stream
if (client.options.connectTimeout) {
if (client._options.connectTimeout) {
// TODO: Investigate why this is not properly triggered
stream.setTimeout(client.connectTimeout, () => {
stream.setTimeout(client._options.connectTimeout, () => {
// Note: This is only tested if a internet connection is established
reconnect(client, 'timeout')
})
}
/* istanbul ignore next: travis does not work with stunnel atm. Therefore the tls tests are skipped on travis */
const connectEvent = client.options.tls ? 'secureConnect' : 'connect'
const connectEvent = client._options.tls ? 'secureConnect' : 'connect'
stream.once(connectEvent, () => {
stream.removeAllListeners('timeout')
client.timesConnected++
@@ -142,11 +142,13 @@ function connect (client) {
stream.setNoDelay()
// Fire the command before redis is connected to be sure it's the first fired command
if (client.authPass !== undefined) {
// Fire the command before redis is connected to be sure it's the first fired command.
// TODO: Consider calling the ready check before Redis is connected as well.
// That could improve the ready performance. Measure the rough time difference!
if (client._options.password !== undefined) {
client.ready = true
client.auth(client.authPass).catch((err) => {
client.closing = true
client.auth(client._options.password).catch((err) => {
client._closing = true
process.nextTick(() => {
client.emit('error', err)
client.end(true)

View File

@@ -46,7 +46,7 @@ RedisClient.prototype.monitor = function monitor () {
const callOnWrite = () => {
// Activating monitor mode has to happen before Redis returned the callback. The monitor result is returned first.
// Therefore we expect the command to be properly processed. If this is not the case, it's not an issue either.
this.monitoring = true
this._monitoring = true
}
return this.internalSendCommand(new Command('monitor', [], callOnWrite))
}
@@ -54,16 +54,16 @@ RedisClient.prototype.monitor = function monitor () {
// Only works with batch, not in a transaction
Multi.prototype.monitor = function monitor () {
// Use a individual command, as this is a special case that does not has to be checked for any other command
if (this.exec !== this.execTransaction) {
if (this._type !== 'multi') {
const callOnWrite = () => {
this._client.monitoring = true
this._client._monitoring = true
}
this._queue.push(new Command('monitor', [], callOnWrite))
return this
}
// Set multi monitoring to indicate the exec that it should abort
// Remove this "hack" as soon as Redis might fix this
this.monitoring = true
this._monitoring = true
return this
}
@@ -91,7 +91,7 @@ RedisClient.prototype.quit = function quit () {
// this.ready = this.offlineQueue.length === 0;
const backpressureIndicator = this.internalSendCommand(new Command('quit', [], null, quitCallback(this)))
// Calling quit should always end the connection, no matter if there's a connection or not
this.closing = true
this._closing = true
this.ready = false
return backpressureIndicator
}
@@ -100,7 +100,7 @@ RedisClient.prototype.quit = function quit () {
Multi.prototype.quit = function quit () {
const callOnWrite = () => {
// If called in a multi context, we expect redis is available
this._client.closing = true
this._client._closing = true
this._client.ready = false
}
this._queue.push(new Command('quit', [], null, quitCallback(this._client), callOnWrite))
@@ -198,7 +198,7 @@ RedisClient.prototype.auth = function auth (pass) {
debug('Sending auth to %s id %s', this.address, this.connectionId)
// Stash auth for connect and reconnect.
this.authPass = pass
this._options.password = pass
const ready = this.ready
this.ready = ready || this.offlineQueue.length === 0
const tmp = this.internalSendCommand(new Command('auth', [pass], null, authCallback(this, pass)))
@@ -211,7 +211,7 @@ Multi.prototype.auth = function auth (pass) {
debug('Sending auth to %s id %s', this.address, this.connectionId)
// Stash auth for connect and reconnect.
this.authPass = pass
this._client._options.password = pass
this._queue.push(new Command('auth', [pass], null, authCallback(this._client)))
return this
}
@@ -229,7 +229,7 @@ RedisClient.prototype.client = function client () {
const replyOnOff = arr[1].toString().toUpperCase()
if (replyOnOff === 'ON' || replyOnOff === 'OFF' || replyOnOff === 'SKIP') {
callOnWrite = () => {
this.reply = replyOnOff
this._reply = replyOnOff
}
}
}
@@ -249,7 +249,7 @@ Multi.prototype.client = function client () {
const replyOnOff = arr[1].toString().toUpperCase()
if (replyOnOff === 'ON' || replyOnOff === 'OFF' || replyOnOff === 'SKIP') {
callOnWrite = () => {
this._client.reply = replyOnOff
this._client._reply = replyOnOff
}
}
}
@@ -264,7 +264,7 @@ RedisClient.prototype.subscribe = function subscribe () {
arr[i] = arguments[i]
}
const callOnWrite = () => {
this.pubSubMode = this.pubSubMode || this.commandQueue.length + 1
this._pubSubMode = this._pubSubMode || this.commandQueue.length + 1
}
return this.internalSendCommand(new Command('subscribe', arr, callOnWrite))
}
@@ -276,7 +276,7 @@ Multi.prototype.subscribe = function subscribe () {
arr[i] = arguments[i]
}
const callOnWrite = () => {
this._client.pubSubMode = this._client.pubSubMode || this._client.commandQueue.length + 1
this._client._pubSubMode = this._client._pubSubMode || this._client.commandQueue.length + 1
}
this._queue.push(new Command('subscribe', arr, callOnWrite))
return this
@@ -290,7 +290,7 @@ RedisClient.prototype.unsubscribe = function unsubscribe () {
}
const callOnWrite = () => {
// Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
this.pubSubMode = this.pubSubMode || this.commandQueue.length + 1
this._pubSubMode = this._pubSubMode || this.commandQueue.length + 1
}
return this.internalSendCommand(new Command('unsubscribe', arr, callOnWrite))
}
@@ -303,7 +303,7 @@ Multi.prototype.unsubscribe = function unsubscribe () {
}
const callOnWrite = () => {
// Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
this._client.pubSubMode = this._client.pubSubMode || this._client.commandQueue.length + 1
this._client._pubSubMode = this._client._pubSubMode || this._client.commandQueue.length + 1
}
this._queue.push(new Command('unsubscribe', arr, callOnWrite))
return this
@@ -316,7 +316,7 @@ RedisClient.prototype.psubscribe = function psubscribe () {
arr[i] = arguments[i]
}
const callOnWrite = () => {
this.pubSubMode = this.pubSubMode || this.commandQueue.length + 1
this._pubSubMode = this._pubSubMode || this.commandQueue.length + 1
}
return this.internalSendCommand(new Command('psubscribe', arr, callOnWrite))
}
@@ -328,7 +328,7 @@ Multi.prototype.psubscribe = function psubscribe () {
arr[i] = arguments[i]
}
const callOnWrite = () => {
this._client.pubSubMode = this._client.pubSubMode || this._client.commandQueue.length + 1
this._client._pubSubMode = this._client._pubSubMode || this._client.commandQueue.length + 1
}
this._queue.push(new Command('psubscribe', arr, callOnWrite))
return this
@@ -342,7 +342,7 @@ RedisClient.prototype.punsubscribe = function punsubscribe () {
}
const callOnWrite = () => {
// Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
this.pubSubMode = this.pubSubMode || this.commandQueue.length + 1
this._pubSubMode = this._pubSubMode || this.commandQueue.length + 1
}
return this.internalSendCommand(new Command('punsubscribe', arr, callOnWrite))
}
@@ -355,7 +355,7 @@ Multi.prototype.punsubscribe = function punsubscribe () {
}
const callOnWrite = () => {
// Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
this._client.pubSubMode = this._client.pubSubMode || this._client.commandQueue.length + 1
this._client._pubSubMode = this._client._pubSubMode || this._client.commandQueue.length + 1
}
this._queue.push(new Command('punsubscribe', arr, callOnWrite))
return this

View File

@@ -22,7 +22,7 @@ function pipelineTransactionCommand (multi, command, index) {
if (err) {
tmp(err)
err.position = index
multi.errors.push(err)
multi._errors.push(err)
return
}
tmp(null, reply)
@@ -73,7 +73,7 @@ function multiCallback (multi, replies) {
function execTransaction (multi) {
const client = multi._client
const queue = multi._queue
if (multi.monitoring || client.monitoring) {
if (multi._monitoring || client._monitoring) {
const err = new RangeError(
'Using transaction with a client that is in monitor mode does not work due to faulty return values of Redis.'
)
@@ -87,9 +87,8 @@ function execTransaction (multi) {
})
}
const len = queue.length
multi.errors = []
multi._errors = []
client._multi = true
multi.wantsBuffers = new Array(len)
// Silently ignore this error. We'll receive the error for the exec as well
const promises = [client.internalSendCommand(new Command('multi', [])).catch(() => {})]
// Drain queue, callback will catch 'QUEUED' or error
@@ -100,7 +99,7 @@ function execTransaction (multi) {
const main = client.internalSendCommand(new Command('exec', []))
return Promise.all(promises).then(() => main.then((replies) => multiCallback(multi, replies)).catch((err) => {
err.errors = multi.errors
err.errors = multi._errors
return Promise.reject(err)
}))
}

View File

@@ -6,8 +6,8 @@ const utils = require('./utils')
function offlineCommand (client, command) {
const commandName = command.command.toUpperCase()
if (client.closing || !client.enableOfflineQueue) {
const msg = client.closing === true
if (client._closing || client._options.enableOfflineQueue === false) {
const msg = client._closing === true
? 'The connection is already closed.'
: client._stream.writable === true
? 'The connection is not yet established and the offline queue is deactivated.'

View File

@@ -12,7 +12,7 @@ function subscribeUnsubscribe (client, reply, type) {
// Subscribe commands take an optional callback and also emit an event, but only the Last_ response is included in the callback
// The pub sub commands return each argument in a separate return value and have to be handled that way
const commandObj = client.commandQueue.get(0)
const buffer = client.options.returnBuffers || client.options.detectBuffers && commandObj.bufferArgs
const buffer = client._options.returnBuffers || client._options.detectBuffers && commandObj.bufferArgs
const channel = (buffer || reply[1] === null) ? reply[1] : reply[1].toString()
const count = +reply[2] // Return the channel counter as number no matter if `stringNumbers` is activated or not
debug(type, channel)
@@ -20,38 +20,38 @@ function subscribeUnsubscribe (client, reply, type) {
// Emit first, then return the callback
if (channel !== null) { // Do not emit or "unsubscribe" something if there was no channel to unsubscribe from
if (type === 'subscribe' || type === 'psubscribe') {
client.subscriptionSet[`${type}_${channel}`] = channel
client._subscriptionSet[`${type}_${channel}`] = channel
} else {
const innerType = type === 'unsubscribe' ? 'subscribe' : 'psubscribe' // Make types consistent
delete client.subscriptionSet[`${innerType}_${channel}`]
delete client._subscriptionSet[`${innerType}_${channel}`]
}
client.emit(type, channel, count)
client.subscribeChannels.push(channel)
client._subscribeChannels.push(channel)
}
if (commandObj.argsLength === 1 || client.subCommandsLeft === 1 || commandObj.argsLength === 0 && (count === 0 || channel === null)) {
if (commandObj.argsLength === 1 || client._subCommandsLeft === 1 || commandObj.argsLength === 0 && (count === 0 || channel === null)) {
if (count === 0) { // Unsubscribed from all channels
var runningCommand
var i = 1
client.pubSubMode = 0 // Deactivating pub sub mode
client._pubSubMode = 0 // Deactivating pub sub mode
// This should be a rare case and therefore handling it this way should be good performance wise for the general case
for (runningCommand = client.commandQueue.get(i); runningCommand !== undefined; runningCommand = client.commandQueue.get(i)) {
if (SUBSCRIBE_COMMANDS[runningCommand.command]) {
client.pubSubMode = i // Entering pub sub mode again
client._pubSubMode = i // Entering pub sub mode again
break
}
i++
}
}
client.commandQueue.shift()
commandObj.callback(null, [count, client.subscribeChannels])
client.subscribeChannels = []
client.subCommandsLeft = 0
commandObj.callback(null, [count, client._subscribeChannels])
client._subscribeChannels = []
client._subCommandsLeft = 0
} else {
if (client.subCommandsLeft !== 0) {
client.subCommandsLeft--
if (client._subCommandsLeft !== 0) {
client._subCommandsLeft--
} else {
client.subCommandsLeft = commandObj.argsLength ? commandObj.argsLength - 1 : count
client._subCommandsLeft = commandObj.argsLength ? commandObj.argsLength - 1 : count
}
}
}
@@ -59,14 +59,14 @@ function subscribeUnsubscribe (client, reply, type) {
function returnPubSub (client, reply) {
const type = reply[0].toString()
if (type === 'message') { // Channel, message
if (!client.options.returnBuffers || client.messageBuffers) { // Backwards compatible. Refactor this in v.3 to always return a string on the normal emitter
if (!client._options.returnBuffers || client.messageBuffers) { // Backwards compatible. Refactor this in v.3 to always return a string on the normal emitter
client.emit('message', reply[1].toString(), reply[2].toString())
client.emit('messageBuffer', reply[1], reply[2])
} else {
client.emit('message', reply[1], reply[2])
}
} else if (type === 'pmessage') { // Pattern, channel, message
if (!client.options.returnBuffers || client.messageBuffers) { // Backwards compatible. Refactor this in v.3 to always return a string on the normal emitter
if (!client._options.returnBuffers || client.messageBuffers) { // Backwards compatible. Refactor this in v.3 to always return a string on the normal emitter
client.emit('pmessage', reply[1].toString(), reply[2].toString(), reply[3].toString())
client.emit('pmessageBuffer', reply[1], reply[2], reply[3])
} else {

View File

@@ -11,14 +11,14 @@ function onConnect (client) {
// fast properties. If that's not the case, make them fast properties
// again!
client.connected = true
client._stream.setKeepAlive(client.options.socketKeepalive)
client._stream.setKeepAlive(client._options.socketKeepalive)
client._stream.setTimeout(0)
// TODO: Deprecate the connect event.
client.emit('connect')
utils.setReconnectDefaults(client)
if (client.options.noReadyCheck) {
if (client._options.noReadyCheck) {
readyHandler(client)
} else {
readyCheck(client)
@@ -53,21 +53,21 @@ function readyHandler (client) {
if (client.selectedDb !== undefined) {
client.internalSendCommand(new Command('select', [client.selectedDb])).catch((err) => {
if (!client.closing) {
if (!client._closing) {
// TODO: These internal things should be wrapped in a
// special error that describes what is happening
process.nextTick(client.emit, 'error', err)
}
})
}
if (client.monitoring) { // Monitor has to be fired before pub sub commands
if (client._monitoring) { // Monitor has to be fired before pub sub commands
client.internalSendCommand(new Command('monitor', [])).catch((err) => {
if (!client.closing) {
if (!client._closing) {
process.nextTick(client.emit, 'error', err)
}
})
}
const callbackCount = Object.keys(client.subscriptionSet).length
const callbackCount = Object.keys(client._subscriptionSet).length
// TODO: Replace the disableResubscribing by a individual function that may be called
// Add HOOKS!!!
// Replace the disableResubscribing by:
@@ -77,14 +77,14 @@ function readyHandler (client) {
// subscriptions: true,
// // individual: function noop () {}
// }
if (!client.options.disableResubscribing && callbackCount) {
if (!client._options.disableResubscribing && callbackCount) {
debug('Sending pub/sub commands')
for (const key in client.subscriptionSet) {
if (client.subscriptionSet.hasOwnProperty(key)) {
for (const key in client._subscriptionSet) {
if (client._subscriptionSet.hasOwnProperty(key)) {
const command = key.slice(0, key.indexOf('_'))
const args = client.subscriptionSet[key]
const args = client._subscriptionSet[key]
client[command]([args]).catch((err) => {
if (!client.closing) {
if (!client._closing) {
process.nextTick(client.emit, 'error', err)
}
})
@@ -133,7 +133,7 @@ function readyCheck (client) {
debug('Redis server still loading, trying again in %s', retryTime)
setTimeout((client) => readyCheck(client), retryTime, client)
}).catch((err) => {
if (client.closing) {
if (client._closing) {
return
}

View File

@@ -47,7 +47,7 @@ function reconnect (client, why, error) {
debug('Redis connection is gone from %s event.', why)
client.connected = false
client.ready = false
client.pubSubMode = 0
client._pubSubMode = 0
client.emit('end')
@@ -63,7 +63,7 @@ function reconnect (client, why, error) {
}
// If client is a requested shutdown, then don't retry
if (client.closing) {
if (client._closing) {
debug('Connection ended by quit / end command, not retrying.')
flushAndError(client, 'Stream connection ended and command aborted.', 'NR_CLOSED', {
error
@@ -94,7 +94,7 @@ function reconnect (client, why, error) {
}
// Retry commands after a reconnect instead of throwing an error. Use this with caution
if (client.options.retryUnfulfilledCommands) {
if (client._options.retryUnfulfilledCommands) {
client.offlineQueue.unshift.apply(client.offlineQueue, client.commandQueue.toArray())
client.commandQueue.clear()
// TODO: If only the pipelineQueue contains the error we could improve the situation.

View File

@@ -15,8 +15,8 @@ function onError (client, err) {
}
// Count down pub sub mode if in entering modus
if (client.pubSubMode > 1) {
client.pubSubMode--
if (client._pubSubMode > 1) {
client._pubSubMode--
}
const match = err.message.match(utils.errCode)
@@ -40,7 +40,7 @@ function onResult (client, reply) {
// If in monitor mode, all normal commands are still working and we only want to emit the streamlined commands
// As this is not the average use case and monitor is expensive anyway, let's change the code here, to improve
// the average performance of all other commands in case of no monitor mode
if (client.monitoring) {
if (client._monitoring) {
var replyStr
if (client.buffers && Buffer.isBuffer(reply)) {
replyStr = reply.toString()
@@ -58,10 +58,10 @@ function onResult (client, reply) {
return
}
}
if (client.pubSubMode === 0) {
if (client._pubSubMode === 0) {
normalReply(client, reply)
} else if (client.pubSubMode !== 1) {
client.pubSubMode--
} else if (client._pubSubMode !== 1) {
client._pubSubMode--
normalReply(client, reply)
} else if (!(reply instanceof Array) || reply.length <= 2) {
// Only PING and QUIT are allowed in this context besides the pub sub commands

View File

@@ -139,7 +139,7 @@ function warn (client, msg) {
* @returns {string|number|null|Buffer|any[]|object}
*/
function handleReply (client, reply, command) {
if (client.options.detectBuffers === true && command.bufferArgs === false) { // client.messageBuffers
if (client._options.detectBuffers === true && command.bufferArgs === false) { // client.messageBuffers
reply = replyToStrings(reply)
}

View File

@@ -128,8 +128,8 @@ function returnErr (client, command) {
function normalizeAndWrite (client, command) {
const args = command.args
const origName = command.command
const renameCommands = client.renameCommands
const name = renameCommands[origName] !== undefined
const renameCommands = client._options.renameCommands
const name = renameCommands !== undefined && renameCommands[origName] !== undefined
? renameCommands[origName]
: origName
@@ -142,12 +142,12 @@ function normalizeAndWrite (client, command) {
return returnErr(client, command)
}
if (typeof client.options.prefix === 'string') {
if (typeof client._options.prefix === 'string') {
const prefixKeys = Commands.getKeyIndexes(origName, copy)
prefixKeys.forEach((i) => {
// Attention it would be to expensive to detect if the input is non utf8 Buffer
// In that case the prefix *might* destroys user information
copy[i] = client.options.prefix + copy[i]
copy[i] = client._options.prefix + copy[i]
})
}