diff --git a/benchmark/index.js b/benchmark/index.js index d56162451b..affe737045 100644 --- a/benchmark/index.js +++ b/benchmark/index.js @@ -1,5 +1,4 @@ const cronometro = require('cronometro'), - { once } = require('events'), newRedis = require('redis-new'), oldRedis = require('redis-old'); diff --git a/lib/client.ts b/lib/client.ts index 6bd3b82311..2b5ca3e6a3 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -1,7 +1,7 @@ import RedisSocket, { RedisSocketOptions } from './socket'; import RedisCommandsQueue, { PubSubListener, PubSubSubscribeCommands, PubSubUnsubscribeCommands, QueueCommandOptions } from './commands-queue'; import COMMANDS from './commands'; -import { RedisCommand, RedisCommands, RedisModules, RedisReply } from './commands'; +import { RedisCommand, RedisModules, RedisReply } from './commands'; import RedisMultiCommand, { MultiQueuedCommand, RedisMultiCommandType } from './multi-command'; import EventEmitter from 'events'; import { CommandOptions, commandOptions, isCommandOptions } from './command-options'; diff --git a/lib/cluster-slots.ts b/lib/cluster-slots.ts index e4f8a36a8a..153faedd3e 100644 --- a/lib/cluster-slots.ts +++ b/lib/cluster-slots.ts @@ -56,7 +56,7 @@ export default class RedisClusterSlots { const client = RedisClient.create({ socket: socketOptions }); @@ -98,6 +98,8 @@ export default class RedisClusterSlots, promises: Array>): RedisClientType { diff --git a/lib/cluster.ts b/lib/cluster.ts index 397e9b0532..3ee303c019 100644 --- a/lib/cluster.ts +++ b/lib/cluster.ts @@ -98,7 +98,7 @@ export default class RedisCluster, options?: ClientCommandOptions, - redirections: number = 0 + redirections = 0 ): Promise> { const client = this.#slots.getClient(firstKey, isReadonly); @@ -118,7 +118,7 @@ export default class RedisCluster, redisArgs: Array, options?: ClientCommandOptions, - redirections: number = 0 + redirections = 0 ): Promise> { const client = this.#slots.getClient( RedisCluster.#extractFirstKey(script, originalArgs, redisArgs), @@ -136,7 +136,7 @@ export default class RedisCluster, redirections: number = 0): Promise { + async #handleCommandError(err: Error, client: RedisClientType, redirections = 0): Promise { if (redirections < (this.#options.maxCommandRedirections ?? 16)) { throw err; } diff --git a/lib/commands-queue.ts b/lib/commands-queue.ts index d79b857c55..2a5e822336 100644 --- a/lib/commands-queue.ts +++ b/lib/commands-queue.ts @@ -1,4 +1,4 @@ -import LinkedList, { Node } from 'yallist'; +import LinkedList from 'yallist'; import RedisParser from 'redis-parser'; import { AbortError } from './errors'; import { RedisReply } from './commands'; @@ -208,7 +208,7 @@ export default class RedisCommandsQueue { return this.#pushPubSubCommand(command, channelsToSubscribe); } - unsubscribe(command: PubSubUnsubscribeCommands, channels?: string | Array, listener?: PubSubListener) { + unsubscribe(command: PubSubUnsubscribeCommands, channels?: string | Array, listener?: PubSubListener): Promise { const listeners = command === PubSubUnsubscribeCommands.UNSUBSCRIBE ? this.#pubSubListeners.channels : this.#pubSubListeners.patterns; if (!channels) { listeners.clear(); diff --git a/lib/commands/BITFIELD.ts b/lib/commands/BITFIELD.ts index 0da1fbc2e4..4ed701e1fe 100644 --- a/lib/commands/BITFIELD.ts +++ b/lib/commands/BITFIELD.ts @@ -24,7 +24,7 @@ interface BitFieldOptions { OVERFLOW?: 'WRAP' | 'SAT' | 'FAIL'; } -export function transformArguments(key: string, options?: BitFieldOptions) { +export function transformArguments(key: string, options?: BitFieldOptions): Array { const args = ['BITFIELD', key]; if (options?.GET) { diff --git a/lib/commands/BRPOP.ts b/lib/commands/BRPOP.ts index fa7165941e..a537ded413 100644 --- a/lib/commands/BRPOP.ts +++ b/lib/commands/BRPOP.ts @@ -1,5 +1,3 @@ -import { transformReplyStringArray } from './generic-transformers'; - export const FIRST_KEY_INDEX = 1; export function transformArguments(key: string | Array, timeout: number): Array { diff --git a/lib/commands/BRPOPLPUSH.ts b/lib/commands/BRPOPLPUSH.ts index 8bfcc69051..f6a3bc1b8a 100644 --- a/lib/commands/BRPOPLPUSH.ts +++ b/lib/commands/BRPOPLPUSH.ts @@ -1,4 +1,4 @@ -import { transformReplyNumber, transformReplyNumberNull } from './generic-transformers'; +import { transformReplyNumberNull } from './generic-transformers'; export const FIRST_KEY_INDEX = 1; diff --git a/lib/commands/CLUSTER_INFO.spec.ts b/lib/commands/CLUSTER_INFO.spec.ts index fef59690b2..a4def45cb7 100644 --- a/lib/commands/CLUSTER_INFO.spec.ts +++ b/lib/commands/CLUSTER_INFO.spec.ts @@ -23,7 +23,7 @@ describe('CLUSTER INFO', () => { 'cluster_my_epoch:2', 'cluster_stats_messages_sent:1483972', 'cluster_stats_messages_received:1483968' - ].join('\n')), + ].join('\r\n')), { state: 'ok', slots: { diff --git a/lib/commands/CLUSTER_INFO.ts b/lib/commands/CLUSTER_INFO.ts index 457bdcbb3a..634515f927 100644 --- a/lib/commands/CLUSTER_INFO.ts +++ b/lib/commands/CLUSTER_INFO.ts @@ -20,26 +20,28 @@ interface ClusterInfoReply { }; } -const regex = /.*:(?.*)(\n?)/g; - export function transformReply(reply: string): ClusterInfoReply { - const iterator = reply.matchAll(regex); + const lines = reply.split('\r\n'); return { - state: iterator.next().value[1], + state: extractLineValue(lines[0]), slots: { - assigned: Number(iterator.next().value[1]), - ok: Number(iterator.next().value[1]), - pfail: Number(iterator.next().value[1]), - fail: Number(iterator.next().value[1]) + assigned: Number(extractLineValue(lines[1])), + ok: Number(extractLineValue(lines[2])), + pfail: Number(extractLineValue(lines[3])), + fail: Number(extractLineValue(lines[4])) }, - knownNodes: Number(iterator.next().value[1]), - size: Number(iterator.next().value[1]), - currentEpoch: Number(iterator.next().value[1]), - myEpoch: Number(iterator.next().value[1]), + knownNodes: Number(extractLineValue(lines[5])), + size: Number(extractLineValue(lines[6])), + currentEpoch: Number(extractLineValue(lines[7])), + myEpoch: Number(extractLineValue(lines[8])), stats: { - messagesSent: Number(iterator.next().value[1]), - messagesReceived: Number(iterator.next().value[1]) + messagesSent: Number(extractLineValue(lines[9])), + messagesReceived: Number(extractLineValue(lines[10])) } }; } + +export function extractLineValue(line: string): string { + return line.substring(line.indexOf(':') + 1); +} diff --git a/lib/commands/DEL.spec.ts b/lib/commands/DEL.spec.ts index 30a2c286b0..ec780de67a 100644 --- a/lib/commands/DEL.spec.ts +++ b/lib/commands/DEL.spec.ts @@ -1,5 +1,4 @@ import { strict as assert } from 'assert'; -import RedisClient from '../client'; import { TestRedisServers, itWithClient } from '../test-utils'; import { transformArguments } from './DEL'; diff --git a/lib/commands/EXPIREAT.ts b/lib/commands/EXPIREAT.ts index b41f98d382..4e5cb244fe 100644 --- a/lib/commands/EXPIREAT.ts +++ b/lib/commands/EXPIREAT.ts @@ -1,4 +1,3 @@ -import { time } from 'console'; import { transformReplyBoolean } from './generic-transformers'; export function transformArguments(key: string, timestamp: number | Date): Array { diff --git a/lib/commands/HSETNX.ts b/lib/commands/HSETNX.ts index 83a7463da5..0eef875252 100644 --- a/lib/commands/HSETNX.ts +++ b/lib/commands/HSETNX.ts @@ -1,4 +1,4 @@ -import { transformReplyBoolean, transformReplyNumber } from './generic-transformers'; +import { transformReplyBoolean } from './generic-transformers'; export const FIRST_KEY_INDEX = 1; diff --git a/lib/commands/READONLY.spec.ts b/lib/commands/READONLY.spec.ts index bf5ecfc139..aa4db47f81 100644 --- a/lib/commands/READONLY.spec.ts +++ b/lib/commands/READONLY.spec.ts @@ -1,5 +1,4 @@ import { strict as assert } from 'assert'; -import { TestRedisServers, itWithClient } from '../test-utils'; import { transformArguments } from './READONLY'; describe('READONLY', () => { diff --git a/lib/commands/SISMEMBER.ts b/lib/commands/SISMEMBER.ts index 149fc30289..661410fce0 100644 --- a/lib/commands/SISMEMBER.ts +++ b/lib/commands/SISMEMBER.ts @@ -1,4 +1,4 @@ -import { transformReplyBoolean, transformReplyNumber } from './generic-transformers'; +import { transformReplyBoolean } from './generic-transformers'; export const FIRST_KEY_INDEX = 1; diff --git a/lib/commands/SMEMBERS.ts b/lib/commands/SMEMBERS.ts index f42e438668..d7e75daaa3 100644 --- a/lib/commands/SMEMBERS.ts +++ b/lib/commands/SMEMBERS.ts @@ -1,4 +1,4 @@ -import { transformReplyNumber, transformReplyStringArray } from './generic-transformers'; +import { transformReplyStringArray } from './generic-transformers'; export const FIRST_KEY_INDEX = 1; diff --git a/lib/commands/XGROUP_DESTROY.ts b/lib/commands/XGROUP_DESTROY.ts index cd41ecb121..1fd25550c3 100644 --- a/lib/commands/XGROUP_DESTROY.ts +++ b/lib/commands/XGROUP_DESTROY.ts @@ -1,4 +1,4 @@ -import { transformReplyBoolean, transformReplyNumber } from './generic-transformers'; +import { transformReplyBoolean } from './generic-transformers'; export const FIRST_KEY_INDEX = 2; diff --git a/lib/commands/ZLEXCOUNT.ts b/lib/commands/ZLEXCOUNT.ts index 7b25486313..2ba50dda73 100644 --- a/lib/commands/ZLEXCOUNT.ts +++ b/lib/commands/ZLEXCOUNT.ts @@ -1,4 +1,4 @@ -import { transformArgumentNumberInfinity, transformReplyNumber } from './generic-transformers'; +import { transformReplyNumber } from './generic-transformers'; export const FIRST_KEY_INDEX = 1; diff --git a/lib/commands/ZPOPMAX_COUNT.ts b/lib/commands/ZPOPMAX_COUNT.ts index 61ab3213ae..abfa8494ec 100644 --- a/lib/commands/ZPOPMAX_COUNT.ts +++ b/lib/commands/ZPOPMAX_COUNT.ts @@ -1,4 +1,3 @@ -import { deepEqual } from 'assert/strict'; import { transformReplySortedSetWithScores } from './generic-transformers'; import { transformArguments as transformZPopMaxArguments } from './ZPOPMAX'; diff --git a/lib/multi-command.ts b/lib/multi-command.ts index 86e1fc294c..69a0146cca 100644 --- a/lib/multi-command.ts +++ b/lib/multi-command.ts @@ -169,7 +169,7 @@ export default class RedisMultiCommand> { + async exec(execAsPipeline = false): Promise> { if (execAsPipeline) { return this.execAsPipeline(); } else if (!this.#queue.length) { diff --git a/lib/socket.ts b/lib/socket.ts index 586d02242a..fddeb2561d 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -59,15 +59,15 @@ export default class RedisSocket extends EventEmitter { } static #isUrlSocket(options: RedisSocketOptions): options is RedisUrlSocketOptions { - return options.hasOwnProperty('url'); + return Object.prototype.hasOwnProperty.call(options, 'url'); } static #isUnixSocket(options: RedisSocketOptions): options is RedisUnixSocketOptions { - return options.hasOwnProperty('path'); + return Object.prototype.hasOwnProperty.call(options, 'path'); } static #isTlsSocket(options: RedisSocketOptions): options is RedisTlsSocketOptions { - return options.hasOwnProperty('tls'); + return Object.prototype.hasOwnProperty.call(options, 'tls'); } readonly #initiator?: RedisSocketInitiator; diff --git a/lib/test-utils.ts b/lib/test-utils.ts index 1d3ae12c56..b41c53ba46 100644 --- a/lib/test-utils.ts +++ b/lib/test-utils.ts @@ -1,5 +1,5 @@ import assert from 'assert/strict'; -import RedisClient, { RedisClientOptions, RedisClientType } from './client'; +import RedisClient, { RedisClientType } from './client'; import { RedisModules } from './commands'; import { RedisLuaScripts } from './lua-script'; import { spawn } from 'child_process';