You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-04 15:02:09 +03:00
feat: accept Map and Set and flatten arguments
This commit is contained in:
95
index.js
95
index.js
@ -19,6 +19,7 @@ const Parser = require('redis-parser')
|
||||
const Errors = require('redis-errors')
|
||||
const debug = require('./lib/debug')
|
||||
const unifyOptions = require('./lib/createClient')
|
||||
const normalizeAndWriteCommand = require('./lib/writeCommands')
|
||||
const SUBSCRIBE_COMMANDS = {
|
||||
subscribe: true,
|
||||
unsubscribe: true,
|
||||
@ -115,6 +116,7 @@ function RedisClient (options, stream) {
|
||||
this.fireStrings = true // Determine if strings or buffers should be written to the stream
|
||||
this.pipeline = false
|
||||
this.subCommandsLeft = 0
|
||||
this.renameCommands = options.renameCommands || {}
|
||||
this.timesConnected = 0
|
||||
this.buffers = options.returnBuffers || options.detectBuffers
|
||||
this.options = options
|
||||
@ -651,7 +653,7 @@ function subscribeUnsubscribe (self, reply, type) {
|
||||
self.subscribeChannels.push(channel)
|
||||
}
|
||||
|
||||
if (commandObj.args.length === 1 || self.subCommandsLeft === 1 || commandObj.args.length === 0 && (count === 0 || channel === null)) {
|
||||
if (commandObj.argsLength === 1 || self.subCommandsLeft === 1 || commandObj.argsLength === 0 && (count === 0 || channel === null)) {
|
||||
if (count === 0) { // unsubscribed from all channels
|
||||
let runningCommand
|
||||
let i = 1
|
||||
@ -673,7 +675,7 @@ function subscribeUnsubscribe (self, reply, type) {
|
||||
if (self.subCommandsLeft !== 0) {
|
||||
self.subCommandsLeft--
|
||||
} else {
|
||||
self.subCommandsLeft = commandObj.args.length ? commandObj.args.length - 1 : count
|
||||
self.subCommandsLeft = commandObj.argsLength ? commandObj.argsLength - 1 : count
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -768,101 +770,14 @@ function handleOfflineCommand (self, commandObj) {
|
||||
// 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) {
|
||||
let arg, prefixKeys
|
||||
let i = 0
|
||||
let commandStr = ''
|
||||
const args = commandObj.args
|
||||
let command = commandObj.command
|
||||
const len = args.length
|
||||
let bigData = false
|
||||
const argsCopy = new Array(len)
|
||||
|
||||
if (this.ready === false || this.stream.writable === false) {
|
||||
// Handle offline commands right away
|
||||
handleOfflineCommand(this, commandObj)
|
||||
return commandObj.promise
|
||||
}
|
||||
|
||||
// TODO: Refactor this to also accept SET, MAP and ArrayBuffer
|
||||
// TODO: Add a utility function to create errors with all necessary params
|
||||
for (i = 0; i < len; i += 1) {
|
||||
if (typeof args[i] === 'string') {
|
||||
// 30000 seemed to be a good value to switch to buffers after testing and checking the pros and cons
|
||||
if (args[i].length > 30000) {
|
||||
bigData = true
|
||||
argsCopy[i] = Buffer.from(args[i])
|
||||
} else {
|
||||
argsCopy[i] = args[i]
|
||||
}
|
||||
} else if (typeof args[i] === 'object') { // Checking for object instead of Buffer.isBuffer helps us finding data types that we can't handle properly
|
||||
if (args[i] instanceof Date) { // Accept dates as valid input
|
||||
argsCopy[i] = args[i].toString()
|
||||
} else if (args[i] === null) {
|
||||
const err = new TypeError('The command contains a "null" argument but NodeRedis can only handle strings, numbers and buffers.')
|
||||
err.command = command.toUpperCase()
|
||||
err.args = args
|
||||
utils.replyInOrder(this, commandObj.callback, err, undefined, this.commandQueue)
|
||||
return commandObj.promise
|
||||
} else if (Buffer.isBuffer(args[i])) {
|
||||
argsCopy[i] = args[i]
|
||||
commandObj.bufferArgs = true
|
||||
bigData = true
|
||||
} else {
|
||||
const err = new TypeError('The command contains a argument of type "' + args[i].constructor.name + '" but NodeRedis can only handle strings, numbers, and buffers.')
|
||||
err.command = command.toUpperCase()
|
||||
err.args = args
|
||||
utils.replyInOrder(this, commandObj.callback, err, undefined, this.commandQueue)
|
||||
return commandObj.promise
|
||||
}
|
||||
} else if (typeof args[i] === 'undefined') {
|
||||
const err = new TypeError('The command contains a "undefined" argument but NodeRedis can only handle strings, numbers and buffers.')
|
||||
err.command = command.toUpperCase()
|
||||
err.args = args
|
||||
utils.replyInOrder(this, commandObj.callback, err, undefined, this.commandQueue)
|
||||
return commandObj.promise
|
||||
} else {
|
||||
// Seems like numbers are converted fast using string concatenation
|
||||
argsCopy[i] = `${args[i]}`
|
||||
}
|
||||
}
|
||||
normalizeAndWriteCommand(this, commandObj)
|
||||
|
||||
if (this.options.prefix) {
|
||||
prefixKeys = commands.getKeyIndexes(command, argsCopy)
|
||||
for (i = prefixKeys.pop(); i !== undefined; i = prefixKeys.pop()) {
|
||||
argsCopy[i] = this.options.prefix + argsCopy[i]
|
||||
}
|
||||
}
|
||||
if (this.options.renameCommands !== undefined && this.options.renameCommands[command]) {
|
||||
command = this.options.renameCommands[command]
|
||||
}
|
||||
// Always use 'Multi bulk commands', but if passed any Buffer args, then do multiple writes, one for each arg.
|
||||
// This means that using Buffers in commands is going to be slower, so use Strings if you don't already have a Buffer.
|
||||
commandStr = `*${len + 1}\r\n$${command.length}\r\n${command}\r\n`
|
||||
|
||||
if (bigData === false) { // Build up a string and send entire command in one write
|
||||
for (i = 0; i < len; i += 1) {
|
||||
arg = argsCopy[i]
|
||||
commandStr += `$${Buffer.byteLength(arg)}\r\n${arg}\r\n`
|
||||
}
|
||||
debug(`Send ${this.address} id ${this.connectionId}: ${commandStr}`)
|
||||
this.write(commandStr)
|
||||
} else {
|
||||
debug(`Send command (${commandStr}) has Buffer arguments`)
|
||||
this.fireStrings = false
|
||||
this.write(commandStr)
|
||||
|
||||
for (i = 0; i < len; i += 1) {
|
||||
arg = argsCopy[i]
|
||||
if (typeof arg === 'string') {
|
||||
this.write(`$${Buffer.byteLength(arg)}\r\n${arg}\r\n`)
|
||||
} else { // buffer
|
||||
this.write(`$${arg.length}\r\n`)
|
||||
this.write(arg)
|
||||
this.write('\r\n')
|
||||
}
|
||||
debug(`sendCommand: buffer send ${arg.length} bytes`)
|
||||
}
|
||||
}
|
||||
if (commandObj.callOnWrite) {
|
||||
commandObj.callOnWrite()
|
||||
}
|
||||
|
@ -4,9 +4,10 @@ const betterStackTraces = /development/i.test(process.env.NODE_ENV) || /\bredis\
|
||||
|
||||
// TODO: Change the arguments to an object
|
||||
// callOnWrite could be two things now
|
||||
function Command (command, args, callOnWrite, transformer) {
|
||||
this.command = command
|
||||
function Command (name, args, callOnWrite, transformer) {
|
||||
this.command = name
|
||||
this.args = args
|
||||
this.argsLength = 0
|
||||
this.bufferArgs = false
|
||||
var callback
|
||||
transformer = transformer || function (err, res) {
|
||||
|
@ -5,7 +5,8 @@ const Multi = require('./multi')
|
||||
const RedisClient = require('../').RedisClient
|
||||
const Command = require('./command')
|
||||
|
||||
const EMPTY_ARRAY = []
|
||||
const clientProto = RedisClient.prototype
|
||||
const multiProto = Multi.prototype
|
||||
|
||||
// TODO: Rewrite this including the individual commands into a Commands class
|
||||
// that provided a functionality to add new commands to the client
|
||||
@ -15,63 +16,35 @@ commands.list.forEach((command) => {
|
||||
const commandName = command.replace(/(?:^([0-9])|[^a-zA-Z0-9_$])/g, '_$1')
|
||||
|
||||
// Do not override existing functions
|
||||
if (!RedisClient.prototype[command]) {
|
||||
RedisClient.prototype[command] = function () {
|
||||
if (!clientProto[command]) {
|
||||
clientProto[command] = function () {
|
||||
const len = arguments.length
|
||||
var arr, i
|
||||
if (len === 0) {
|
||||
arr = EMPTY_ARRAY
|
||||
} else if (arguments[0].shift) {
|
||||
arr = arguments[0]
|
||||
} else if (len > 1 && arguments[1].shift) {
|
||||
const innerLen = arguments[1].length
|
||||
arr = new Array(innerLen + 1)
|
||||
arr[0] = arguments[0]
|
||||
for (i = 0; i < innerLen; i += 1) {
|
||||
arr[i + 1] = arguments[1][i]
|
||||
}
|
||||
} else {
|
||||
arr = new Array(len)
|
||||
for (i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
return this.internalSendCommand(new Command(command, arr))
|
||||
}
|
||||
if (RedisClient.prototype[command] !== commandName) {
|
||||
Object.defineProperty(RedisClient.prototype[command], 'name', {
|
||||
if (clientProto[command] !== commandName) {
|
||||
Object.defineProperty(clientProto[command], 'name', {
|
||||
value: commandName
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Do not override existing functions
|
||||
if (!Multi.prototype[command]) {
|
||||
Multi.prototype[command] = function () {
|
||||
if (!multiProto[command]) {
|
||||
multiProto[command] = function () {
|
||||
const len = arguments.length
|
||||
var arr, i
|
||||
if (len === 0) {
|
||||
arr = EMPTY_ARRAY
|
||||
} else if (arguments[0].shift) {
|
||||
arr = arguments[0]
|
||||
} else if (len > 1 && arguments[1].shift) {
|
||||
const innerLen = arguments[1].length
|
||||
arr = new Array(innerLen + 1)
|
||||
arr[0] = arguments[0]
|
||||
for (i = 0; i < innerLen; i += 1) {
|
||||
arr[i + 1] = arguments[1][i]
|
||||
}
|
||||
} else {
|
||||
arr = new Array(len)
|
||||
for (i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
this.queue.push(new Command(command, arr))
|
||||
return this
|
||||
}
|
||||
if (Multi.prototype[command] !== commandName) {
|
||||
Object.defineProperty(Multi.prototype[command], 'name', {
|
||||
if (multiProto[command] !== commandName) {
|
||||
Object.defineProperty(multiProto[command], 'name', {
|
||||
value: commandName
|
||||
})
|
||||
}
|
||||
|
@ -158,18 +158,12 @@ function infoCallback (self) {
|
||||
|
||||
// Store info in this.serverInfo after each call
|
||||
RedisClient.prototype.info = function info (section) {
|
||||
var args = []
|
||||
if (section !== undefined) {
|
||||
args = Array.isArray(section) ? section : [section]
|
||||
}
|
||||
const args = section ? [section] : []
|
||||
return this.internalSendCommand(new Command('info', args, null, infoCallback(this)))
|
||||
}
|
||||
|
||||
Multi.prototype.info = function info (section) {
|
||||
var args = []
|
||||
if (section !== undefined) {
|
||||
args = Array.isArray(section) ? section : [section]
|
||||
}
|
||||
const args = section ? [section] : []
|
||||
this.queue.push(new Command('info', args, null, infoCallback(this._client)))
|
||||
return this
|
||||
}
|
||||
@ -210,24 +204,10 @@ Multi.prototype.auth = function auth (pass) {
|
||||
}
|
||||
|
||||
RedisClient.prototype.client = function client () {
|
||||
var arr
|
||||
var len = arguments.length
|
||||
var i = 0
|
||||
if (Array.isArray(arguments[0])) {
|
||||
arr = arguments[0]
|
||||
} else if (Array.isArray(arguments[1])) {
|
||||
len = arguments[1].length
|
||||
arr = new Array(len + 1)
|
||||
arr[0] = arguments[0]
|
||||
for (; i < len; i += 1) {
|
||||
arr[i + 1] = arguments[1][i]
|
||||
}
|
||||
} else {
|
||||
len = arguments.length
|
||||
arr = new Array(len)
|
||||
for (; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const len = arguments.length
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const self = this
|
||||
var callOnWrite
|
||||
@ -280,75 +260,11 @@ Multi.prototype.client = function client () {
|
||||
return this
|
||||
}
|
||||
|
||||
RedisClient.prototype.hmset = function hmset () {
|
||||
var arr
|
||||
var len = arguments.length
|
||||
var i = 0
|
||||
if (Array.isArray(arguments[0])) {
|
||||
arr = arguments[0]
|
||||
} else if (Array.isArray(arguments[1])) {
|
||||
len = arguments[1].length
|
||||
arr = new Array(len + 1)
|
||||
arr[0] = arguments[0]
|
||||
for (; i < len; i += 1) {
|
||||
arr[i + 1] = arguments[1][i]
|
||||
}
|
||||
} else if (typeof arguments[1] === 'object' && (arguments.length === 2)) {
|
||||
arr = [arguments[0]]
|
||||
for (const field in arguments[1]) {
|
||||
arr.push(field, arguments[1][field])
|
||||
}
|
||||
} else {
|
||||
len = arguments.length
|
||||
arr = new Array(len)
|
||||
for (; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
}
|
||||
return this.internalSendCommand(new Command('hmset', arr))
|
||||
}
|
||||
|
||||
Multi.prototype.hmset = function hmset () {
|
||||
var arr
|
||||
var len = arguments.length
|
||||
var i = 0
|
||||
if (Array.isArray(arguments[0])) {
|
||||
arr = arguments[0]
|
||||
} else if (Array.isArray(arguments[1])) {
|
||||
len = arguments[1].length
|
||||
arr = new Array(len + 1)
|
||||
arr[0] = arguments[0]
|
||||
for (; i < len; i += 1) {
|
||||
arr[i + 1] = arguments[1][i]
|
||||
}
|
||||
} else if (typeof arguments[1] === 'object' && (arguments.length === 2)) {
|
||||
arr = [arguments[0]]
|
||||
for (const field in arguments[1]) {
|
||||
arr.push(field, arguments[1][field])
|
||||
}
|
||||
} else {
|
||||
len = arguments.length
|
||||
arr = new Array(len)
|
||||
for (; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
}
|
||||
this.queue.push(new Command('hmset', arr))
|
||||
return this
|
||||
}
|
||||
|
||||
RedisClient.prototype.subscribe = function subscribe () {
|
||||
var arr
|
||||
var len = arguments.length
|
||||
var i = 0
|
||||
if (Array.isArray(arguments[0])) {
|
||||
arr = arguments[0].slice(0)
|
||||
} else {
|
||||
len = arguments.length
|
||||
arr = new Array(len)
|
||||
for (; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const len = arguments.length
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const self = this
|
||||
const callOnWrite = function () {
|
||||
@ -358,17 +274,10 @@ RedisClient.prototype.subscribe = function subscribe () {
|
||||
}
|
||||
|
||||
Multi.prototype.subscribe = function subscribe () {
|
||||
var arr
|
||||
var len = arguments.length
|
||||
var i = 0
|
||||
if (Array.isArray(arguments[0])) {
|
||||
arr = arguments[0].slice(0)
|
||||
} else {
|
||||
len = arguments.length
|
||||
arr = new Array(len)
|
||||
for (; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const len = arguments.length
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const self = this._client
|
||||
const callOnWrite = function () {
|
||||
@ -379,17 +288,10 @@ Multi.prototype.subscribe = function subscribe () {
|
||||
}
|
||||
|
||||
RedisClient.prototype.unsubscribe = function unsubscribe () {
|
||||
var arr
|
||||
var len = arguments.length
|
||||
var i = 0
|
||||
if (Array.isArray(arguments[0])) {
|
||||
arr = arguments[0].slice(0)
|
||||
} else {
|
||||
len = arguments.length
|
||||
arr = new Array(len)
|
||||
for (; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const len = arguments.length
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const self = this
|
||||
const callOnWrite = function () {
|
||||
@ -400,17 +302,10 @@ RedisClient.prototype.unsubscribe = function unsubscribe () {
|
||||
}
|
||||
|
||||
Multi.prototype.unsubscribe = function unsubscribe () {
|
||||
var arr
|
||||
var len = arguments.length
|
||||
var i = 0
|
||||
if (Array.isArray(arguments[0])) {
|
||||
arr = arguments[0].slice(0)
|
||||
} else {
|
||||
len = arguments.length
|
||||
arr = new Array(len)
|
||||
for (; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const len = arguments.length
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const self = this._client
|
||||
const callOnWrite = function () {
|
||||
@ -422,17 +317,10 @@ Multi.prototype.unsubscribe = function unsubscribe () {
|
||||
}
|
||||
|
||||
RedisClient.prototype.psubscribe = function psubscribe () {
|
||||
var arr
|
||||
var len = arguments.length
|
||||
var i = 0
|
||||
if (Array.isArray(arguments[0])) {
|
||||
arr = arguments[0].slice(0)
|
||||
} else {
|
||||
len = arguments.length
|
||||
arr = new Array(len)
|
||||
for (; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const len = arguments.length
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const self = this
|
||||
const callOnWrite = function () {
|
||||
@ -442,17 +330,10 @@ RedisClient.prototype.psubscribe = function psubscribe () {
|
||||
}
|
||||
|
||||
Multi.prototype.psubscribe = function psubscribe () {
|
||||
var arr
|
||||
var len = arguments.length
|
||||
var i = 0
|
||||
if (Array.isArray(arguments[0])) {
|
||||
arr = arguments[0].slice(0)
|
||||
} else {
|
||||
len = arguments.length
|
||||
arr = new Array(len)
|
||||
for (; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const len = arguments.length
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const self = this._client
|
||||
const callOnWrite = function () {
|
||||
@ -463,17 +344,10 @@ Multi.prototype.psubscribe = function psubscribe () {
|
||||
}
|
||||
|
||||
RedisClient.prototype.punsubscribe = function punsubscribe () {
|
||||
var arr
|
||||
var len = arguments.length
|
||||
var i = 0
|
||||
if (Array.isArray(arguments[0])) {
|
||||
arr = arguments[0].slice(0)
|
||||
} else {
|
||||
len = arguments.length
|
||||
arr = new Array(len)
|
||||
for (; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const len = arguments.length
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const self = this
|
||||
const callOnWrite = function () {
|
||||
@ -484,17 +358,10 @@ RedisClient.prototype.punsubscribe = function punsubscribe () {
|
||||
}
|
||||
|
||||
Multi.prototype.punsubscribe = function punsubscribe () {
|
||||
var arr
|
||||
var len = arguments.length
|
||||
var i = 0
|
||||
if (Array.isArray(arguments[0])) {
|
||||
arr = arguments[0].slice(0)
|
||||
} else {
|
||||
len = arguments.length
|
||||
arr = new Array(len)
|
||||
for (; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const len = arguments.length
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
arr[i] = arguments[i]
|
||||
}
|
||||
const self = this._client
|
||||
const callOnWrite = function () {
|
||||
|
142
lib/writeCommands.js
Normal file
142
lib/writeCommands.js
Normal file
@ -0,0 +1,142 @@
|
||||
'use strict'
|
||||
|
||||
const Commands = require('redis-commands')
|
||||
const utils = require('./utils')
|
||||
const debug = require('./debug')
|
||||
// const isUint8Array = (() => {
|
||||
// try {
|
||||
// return process.binding('util').isUint8Array
|
||||
// } catch (e) {
|
||||
// // Fallback
|
||||
// return (val) => {
|
||||
// return Buffer.isBuffer(val) || ArrayBuffer.isView(val)
|
||||
// }
|
||||
// }
|
||||
// })()
|
||||
const copy = []
|
||||
|
||||
var bufferCount = 0
|
||||
var errors = null
|
||||
|
||||
function writeBuffers (client) {
|
||||
client.fireStrings = false
|
||||
|
||||
while (copy.length) {
|
||||
const arg = copy.shift()
|
||||
// TODO: Consider to convert the strings to buffers
|
||||
// This might actually improve the performance at
|
||||
// least in more modern Node versions
|
||||
if (typeof arg === 'string') {
|
||||
client.write(`$${Buffer.byteLength(arg)}\r\n${arg}\r\n`)
|
||||
} else { // buffer
|
||||
client.write(`$${arg.length}\r\n`)
|
||||
client.write(arg)
|
||||
client.write('\r\n')
|
||||
}
|
||||
debug('sendCommand: buffer send %s bytes', arg.length)
|
||||
}
|
||||
}
|
||||
|
||||
function toString (arg) {
|
||||
if (typeof arg === 'string') {
|
||||
copy.push(arg)
|
||||
} else if (typeof arg === 'number') {
|
||||
copy.push('' + arg)
|
||||
} else if (arg instanceof Array) {
|
||||
for (var i = 0; i < arg.length; i += 1) {
|
||||
toString(arg[i])
|
||||
}
|
||||
} else if (arg && arg.constructor.name === 'Buffer') {
|
||||
copy.push(arg)
|
||||
bufferCount++
|
||||
} else if (typeof arg === 'boolean') { // TODO: Remove this support and use hooks instead
|
||||
copy.push('' + arg)
|
||||
} else if (arg && arg.constructor.name === 'Object') { // Check if this is actually a good check or not
|
||||
// TODO: As soon as we add support for JSON
|
||||
// We could simple stringify this right here.
|
||||
// This might interfere with nested Objects though.
|
||||
// So we should only do this for the first level.
|
||||
const keys = Object.keys(arg)
|
||||
for (var j = 0; j < keys.length; j++) {
|
||||
copy.push(keys[j])
|
||||
toString(arg[keys[j]])
|
||||
}
|
||||
} else if (arg instanceof Map) {
|
||||
arg.forEach((val, key) => {
|
||||
toString(key)
|
||||
toString(val)
|
||||
})
|
||||
} else if (arg instanceof Set) {
|
||||
arg.forEach((val) => toString(val))
|
||||
} else if (arg && arg.constructor.name === 'Date') { // Check if this is actually a good check or not
|
||||
copy.push(arg.toString())
|
||||
} else {
|
||||
if (errors === null) {
|
||||
errors = []
|
||||
}
|
||||
errors.push(arg)
|
||||
}
|
||||
}
|
||||
|
||||
function returnErr (client, command) {
|
||||
const err = new TypeError('NodeRedis can not handle the provided arguments (see "error.issues" property).\n\nFurther information https://github.com/asd')
|
||||
err.command = command.name.toUpperCase()
|
||||
err.args = command.args
|
||||
err.issues = errors
|
||||
errors = null
|
||||
utils.replyInOrder(client, command.callback, err, undefined, client.commandQueue)
|
||||
}
|
||||
|
||||
// Always use 'Multi bulk commands', but if passed any Buffer args, then do multiple writes, one for each arg.
|
||||
// This means that using Buffers in commands is going to be slower, so use Strings if you don't already have a Buffer.
|
||||
|
||||
// TODO: It is faster to move this part somewhere else
|
||||
// We could move this to the function creation as well
|
||||
// if we use hooks for our individual commands!
|
||||
function normalizeAndWrite (client, command) {
|
||||
const args = command.args
|
||||
const origName = command.command
|
||||
const renameCommands = client.renameCommands
|
||||
const name = renameCommands[origName] !== undefined
|
||||
? renameCommands[origName]
|
||||
: origName
|
||||
|
||||
bufferCount = 0
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
toString(args[i])
|
||||
}
|
||||
|
||||
if (errors) {
|
||||
return returnErr(client, command)
|
||||
}
|
||||
|
||||
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]
|
||||
})
|
||||
}
|
||||
|
||||
const bufferArgs = bufferCount !== 0
|
||||
const len = copy.length
|
||||
var commandStr = `*${len + 1}\r\n$${name.length}\r\n${name}\r\n`
|
||||
|
||||
command.bufferArgs = bufferArgs
|
||||
command.argsLength = len
|
||||
|
||||
if (bufferArgs === false) {
|
||||
while (copy.length) {
|
||||
const arg = copy.shift()
|
||||
commandStr += `$${Buffer.byteLength(arg)}\r\n${arg}\r\n`
|
||||
}
|
||||
debug('Send %s id %s: %s', client.address, client.connectionId, commandStr)
|
||||
client.write(commandStr)
|
||||
} else {
|
||||
client.write(commandStr)
|
||||
writeBuffers(client)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = normalizeAndWrite
|
@ -209,15 +209,17 @@ if (process.platform !== 'win32') {
|
||||
client.set('foo', 'bar')
|
||||
client.subscribe('somechannel', 'another channel').then(() => {
|
||||
assert.strictEqual(client.pubSubMode, 1)
|
||||
client.get('foo').catch((err) => {
|
||||
assert(/ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message))
|
||||
done()
|
||||
client.once('ready', () => {
|
||||
client.get('foo').catch((err) => {
|
||||
assert(/ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message))
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
client.once('ready', () => {
|
||||
// Coherent behavior with all other offline commands fires commands before emitting but does not wait till they return
|
||||
// Coherent behavior with all other offline commands fires commands before emitting but does not wait till they return
|
||||
assert.strictEqual(client.pubSubMode, 2)
|
||||
client.ping(() => { // Make sure all commands were properly processed already
|
||||
client.ping().then(() => { // Make sure all commands were properly processed already
|
||||
client.stream.destroy()
|
||||
})
|
||||
})
|
||||
|
@ -152,12 +152,12 @@ describe('The \'batch\' method', () => {
|
||||
['hmset', arr3],
|
||||
['hmset', now, {123456789: 'abcdefghij', 'some manner of key': 'a type of value', 'otherTypes': 555}],
|
||||
['hmset', 'key2', {'0123456789': 'abcdefghij', 'some manner of key': 'a type of value', 'otherTypes': 999}],
|
||||
['hmset', 'batchhmset', ['batchbar', 'batchbaz']],
|
||||
['hmset', 'batchhmset', ['batchbar', 'batchbaz']]
|
||||
['hmset', new Set(['batchhmset', ['batchbar', 'batchbaz']])],
|
||||
['hmset', ['batchhmset'], new Map([['batchbar', 'batchbaz']])]
|
||||
])
|
||||
.hmget(now, 123456789, 'otherTypes')
|
||||
.hmget(now, 123456789, ['otherTypes'])
|
||||
.hmget('key2', arr2)
|
||||
.hmget(['batchhmset2', 'some manner of key', 'batchbar3'])
|
||||
.hmget(['batchhmset2', ['some manner of key', 'batchbar3']])
|
||||
.mget('batchfoo2', ['batchfoo3', 'batchfoo'])
|
||||
.exec().then((replies) => {
|
||||
assert.strictEqual(arr.length, 3)
|
||||
|
@ -16,15 +16,15 @@ describe('The nodeRedis client', () => {
|
||||
// Therefor individual commands always have to be handled in both cases
|
||||
fs.readFile(path.resolve(__dirname, '../lib/individualCommands.js'), 'utf8', (err, data) => {
|
||||
assert.strictEqual(err, null)
|
||||
const clientPrototype = data.match(/(\n| = )RedisClient\.prototype.[a-zA-Z_]+/g)
|
||||
const multiPrototype = data.match(/(\n| = )Multi\.prototype\.[a-zA-Z_]+/g)
|
||||
const clientPrototype = data.match(/(\n| = )RedisClient\.prototype.[a-z][a-zA-Z_]+/g)
|
||||
const multiPrototype = data.match(/(\n| = )Multi\.prototype\.[a-z][a-zA-Z_]+/g)
|
||||
// Check that every entry RedisClient entry has a correspondent Multi entry
|
||||
assert.strictEqual(clientPrototype.filter((entry) => {
|
||||
return multiPrototype.indexOf(entry.replace('RedisClient', 'Multi')) === -1
|
||||
}).length, 3) // multi and batch are included too
|
||||
assert.strictEqual(clientPrototype.length, multiPrototype.length + 3)
|
||||
return !multiPrototype.includes(entry.replace('RedisClient', 'Multi'))
|
||||
}).length, 2) // multi and batch are included too
|
||||
assert.strictEqual(clientPrototype.length, multiPrototype.length + 2)
|
||||
// Check that all entries exist only in lowercase variants
|
||||
assert.strictEqual(data.match(/(\n| = )RedisClient\.prototype.[a-zA-Z_]+/g).length, clientPrototype.length)
|
||||
assert.strictEqual(data.match(/(\n| = )RedisClient\.prototype.[a-z][a-zA-Z_]+/g).length, clientPrototype.length)
|
||||
done()
|
||||
})
|
||||
})
|
||||
@ -135,16 +135,8 @@ describe('The nodeRedis client', () => {
|
||||
|
||||
describe('big data', () => {
|
||||
// Check if the fast mode for big strings is working correct
|
||||
it('safe strings that are bigger than 30000 characters', () => {
|
||||
let str = 'foo ಠ_ಠ bar '
|
||||
while (str.length < 111111) {
|
||||
str += str
|
||||
}
|
||||
client.set('foo', str)
|
||||
return client.get('foo').then(helper.isString(str))
|
||||
})
|
||||
|
||||
it('safe strings that are bigger than 30000 characters with multi', () => {
|
||||
// TODO: Evaluate if this is still necessary after the refactoring
|
||||
it.skip('safe strings that are bigger than 30000 characters with multi', () => {
|
||||
let str = 'foo ಠ_ಠ bar '
|
||||
while (str.length < 111111) {
|
||||
str += str
|
||||
|
@ -506,7 +506,7 @@ describe('publish/subscribe', () => {
|
||||
})
|
||||
|
||||
it('executes when punsubscribe is called and there are no subscriptions', () => {
|
||||
return pub.batch().punsubscribe(helper.isDeepEqual([0, []])).exec()
|
||||
return pub.batch().punsubscribe().exec().then(helper.isDeepEqual([[0, []]]))
|
||||
})
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user