You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-07 13:22:56 +03:00
wip
This commit is contained in:
@@ -49,7 +49,7 @@ interface DecoderOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Decoder {
|
export class Decoder {
|
||||||
private _config;
|
private readonly _config;
|
||||||
|
|
||||||
private _cursor = 0;
|
private _cursor = 0;
|
||||||
|
|
||||||
|
@@ -6,7 +6,7 @@ import { ChannelListeners, PubSub, PubSubCommand, PubSubListener, PubSubType, Pu
|
|||||||
import { AbortError, ErrorReply } from '../errors';
|
import { AbortError, ErrorReply } from '../errors';
|
||||||
import { EventEmitter } from 'stream';
|
import { EventEmitter } from 'stream';
|
||||||
|
|
||||||
export interface QueueCommandOptions {
|
export interface CommandOptions {
|
||||||
chainId?: symbol;
|
chainId?: symbol;
|
||||||
asap?: boolean;
|
asap?: boolean;
|
||||||
abortSignal?: AbortSignal;
|
abortSignal?: AbortSignal;
|
||||||
@@ -149,7 +149,7 @@ export default class RedisCommandsQueue {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
addCommand<T>(args: CommandArguments, options?: QueueCommandOptions): Promise<T> {
|
addCommand<T>(args: CommandArguments, options?: CommandOptions): Promise<T> {
|
||||||
if (this._maxLength && this._waitingToBeSent.length + this._waitingForReply.length >= this._maxLength) {
|
if (this._maxLength && this._waitingToBeSent.length + this._waitingForReply.length >= this._maxLength) {
|
||||||
return Promise.reject(new Error('The queue is full'));
|
return Promise.reject(new Error('The queue is full'));
|
||||||
} else if (options?.abortSignal?.aborted) {
|
} else if (options?.abortSignal?.aborted) {
|
||||||
@@ -256,30 +256,32 @@ export default class RedisCommandsQueue {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getCommandToSend(): CommandArguments | undefined {
|
*waitingToBeSent() {
|
||||||
const toSend = this._waitingToBeSent.shift();
|
let toSend = this._waitingToBeSent.shift();
|
||||||
if (!toSend) return;
|
while (toSend) {
|
||||||
|
let encoded: CommandArguments;
|
||||||
|
try {
|
||||||
|
encoded = encodeCommand(toSend.args);
|
||||||
|
} catch (err) {
|
||||||
|
toSend.reject(err);
|
||||||
|
toSend = this._waitingToBeSent.shift();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let encoded: CommandArguments;
|
if (toSend.abort) {
|
||||||
try {
|
RedisCommandsQueue._removeAbortListener(toSend);
|
||||||
encoded = encodeCommand(toSend.args);
|
toSend.abort = undefined;
|
||||||
} catch (err) {
|
}
|
||||||
toSend.reject(err);
|
|
||||||
return;
|
// TODO reuse `toSend` or create new object?
|
||||||
|
(toSend as any).args = undefined;
|
||||||
|
(toSend as any).chainId = undefined;
|
||||||
|
|
||||||
|
this._waitingForReply.push(toSend);
|
||||||
|
this._chainInExecution = toSend.chainId;
|
||||||
|
yield encoded;
|
||||||
|
toSend = this._waitingToBeSent.shift();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (toSend.abort) {
|
|
||||||
RedisCommandsQueue._removeAbortListener(toSend);
|
|
||||||
toSend.abort = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO reuse `toSend` or create new object?
|
|
||||||
(toSend as any).args = undefined;
|
|
||||||
(toSend as any).chainId = undefined;
|
|
||||||
|
|
||||||
this._waitingForReply.push(toSend);
|
|
||||||
this._chainInExecution = toSend.chainId;
|
|
||||||
return encoded;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _flushWaitingForReply(err: Error): void {
|
private _flushWaitingForReply(err: Error): void {
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import COMMANDS from '../commands';
|
import COMMANDS from '../commands';
|
||||||
import RedisSocket, { RedisSocketOptions, RedisTlsSocketOptions } from './socket';
|
import RedisSocket, { RedisSocketOptions, RedisTlsSocketOptions } from './socket';
|
||||||
import RedisCommandsQueue, { QueueCommandOptions } from './commands-queue';
|
import RedisCommandsQueue, { CommandOptions } from './commands-queue';
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import { attachConfig, functionArgumentsPrefix, getTransformReply, scriptArgumentsPrefix } from '../commander';
|
import { attachConfig, functionArgumentsPrefix, getTransformReply, scriptArgumentsPrefix } from '../commander';
|
||||||
import { ClientClosedError, ClientOfflineError, DisconnectsClientError, WatchError } from '../errors';
|
import { ClientClosedError, ClientOfflineError, DisconnectsClientError, WatchError } from '../errors';
|
||||||
@@ -12,7 +12,7 @@ import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-co
|
|||||||
import { RedisMultiQueuedCommand } from '../multi-command';
|
import { RedisMultiQueuedCommand } from '../multi-command';
|
||||||
import HELLO, { HelloOptions } from '../commands/HELLO';
|
import HELLO, { HelloOptions } from '../commands/HELLO';
|
||||||
import { ReplyWithTypeMapping, CommandReply } from '../RESP/types';
|
import { ReplyWithTypeMapping, CommandReply } from '../RESP/types';
|
||||||
import SCAN, { ScanOptions, ScanCommonOptions } from '../commands/SCAN';
|
import { ScanOptions, ScanCommonOptions } from '../commands/SCAN';
|
||||||
import { RedisLegacyClient, RedisLegacyClientType } from './legacy-mode';
|
import { RedisLegacyClient, RedisLegacyClientType } from './legacy-mode';
|
||||||
// import { RedisClientPool } from './pool';
|
// import { RedisClientPool } from './pool';
|
||||||
|
|
||||||
@@ -20,8 +20,9 @@ export interface RedisClientOptions<
|
|||||||
M extends RedisModules = RedisModules,
|
M extends RedisModules = RedisModules,
|
||||||
F extends RedisFunctions = RedisFunctions,
|
F extends RedisFunctions = RedisFunctions,
|
||||||
S extends RedisScripts = RedisScripts,
|
S extends RedisScripts = RedisScripts,
|
||||||
RESP extends RespVersions = RespVersions
|
RESP extends RespVersions = RespVersions,
|
||||||
> extends CommanderConfig<M, F, S, RESP> {
|
TYPE_MAPPING extends TypeMapping = TypeMapping
|
||||||
|
> extends CommanderConfig<M, F, S, RESP>, TypeMappingOption<TYPE_MAPPING> {
|
||||||
/**
|
/**
|
||||||
* `redis[s]://[[username][:password]@][host][:port][/db-number]`
|
* `redis[s]://[[username][:password]@][host][:port][/db-number]`
|
||||||
* See [`redis`](https://www.iana.org/assignments/uri-schemes/prov/redis) and [`rediss`](https://www.iana.org/assignments/uri-schemes/prov/rediss) IANA registration for more details
|
* See [`redis`](https://www.iana.org/assignments/uri-schemes/prov/redis) and [`rediss`](https://www.iana.org/assignments/uri-schemes/prov/rediss) IANA registration for more details
|
||||||
@@ -67,40 +68,60 @@ export interface RedisClientOptions<
|
|||||||
pingInterval?: number;
|
pingInterval?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface TypeMappingOption<TYPE_MAPPING extends TypeMapping> {
|
||||||
|
/**
|
||||||
|
* Maps bettwen RESP types to JavaScript types
|
||||||
|
*/
|
||||||
|
typeMapping?: TYPE_MAPPING;
|
||||||
|
}
|
||||||
|
|
||||||
type WithCommands<
|
type WithCommands<
|
||||||
RESP extends RespVersions,
|
RESP extends RespVersions,
|
||||||
TYPE_MAPPING extends TypeMapping
|
TYPE_MAPPING extends TypeMapping
|
||||||
> = {
|
> = {
|
||||||
[P in keyof typeof COMMANDS]: CommandSignature<(typeof COMMANDS)[P], RESP, TYPE_MAPPING>;
|
[P in keyof typeof COMMANDS]: CommandSignature<(typeof COMMANDS)[P], RESP, TYPE_MAPPING>;
|
||||||
};
|
};
|
||||||
|
|
||||||
type WithModules<
|
type WithModules<
|
||||||
M extends RedisModules,
|
M extends RedisModules,
|
||||||
RESP extends RespVersions,
|
RESP extends RespVersions,
|
||||||
TYPE_MAPPING extends TypeMapping
|
TYPE_MAPPING extends TypeMapping
|
||||||
> = {
|
> = {
|
||||||
[P in keyof M]: {
|
[P in keyof M]: {
|
||||||
[C in keyof M[P]]: CommandSignature<M[P][C], RESP, TYPE_MAPPING>;
|
[C in keyof M[P]]: CommandSignature<M[P][C], RESP, TYPE_MAPPING>;
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
type WithFunctions<
|
type WithFunctions<
|
||||||
F extends RedisFunctions,
|
F extends RedisFunctions,
|
||||||
RESP extends RespVersions,
|
RESP extends RespVersions,
|
||||||
TYPE_MAPPING extends TypeMapping
|
TYPE_MAPPING extends TypeMapping
|
||||||
> = {
|
> = {
|
||||||
[L in keyof F]: {
|
[L in keyof F]: {
|
||||||
[C in keyof F[L]]: CommandSignature<F[L][C], RESP, TYPE_MAPPING>;
|
[C in keyof F[L]]: CommandSignature<F[L][C], RESP, TYPE_MAPPING>;
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
type WithScripts<
|
type WithScripts<
|
||||||
S extends RedisScripts,
|
S extends RedisScripts,
|
||||||
RESP extends RespVersions,
|
RESP extends RespVersions,
|
||||||
TYPE_MAPPING extends TypeMapping
|
TYPE_MAPPING extends TypeMapping
|
||||||
> = {
|
> = {
|
||||||
[P in keyof S]: CommandSignature<S[P], RESP, TYPE_MAPPING>;
|
[P in keyof S]: CommandSignature<S[P], RESP, TYPE_MAPPING>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type RedisClientExtensions<
|
||||||
|
M extends RedisModules = {},
|
||||||
|
F extends RedisFunctions = {},
|
||||||
|
S extends RedisScripts = {},
|
||||||
|
RESP extends RespVersions = 2,
|
||||||
|
TYPE_MAPPING extends TypeMapping = {}
|
||||||
|
> = (
|
||||||
|
WithCommands<RESP, TYPE_MAPPING> &
|
||||||
|
WithModules<M, RESP, TYPE_MAPPING> &
|
||||||
|
WithFunctions<F, RESP, TYPE_MAPPING> &
|
||||||
|
WithScripts<S, RESP, TYPE_MAPPING>
|
||||||
|
);
|
||||||
|
|
||||||
export type RedisClientType<
|
export type RedisClientType<
|
||||||
M extends RedisModules = {},
|
M extends RedisModules = {},
|
||||||
@@ -109,18 +130,11 @@ export type RedisClientType<
|
|||||||
RESP extends RespVersions = 2,
|
RESP extends RespVersions = 2,
|
||||||
TYPE_MAPPING extends TypeMapping = {}
|
TYPE_MAPPING extends TypeMapping = {}
|
||||||
> = (
|
> = (
|
||||||
RedisClient<M, F, S, RESP, TYPE_MAPPING> &
|
RedisClient<M, F, S, RESP, TYPE_MAPPING> &
|
||||||
WithCommands<RESP, TYPE_MAPPING> &
|
RedisClientExtensions<M, F, S, RESP, TYPE_MAPPING>
|
||||||
WithModules<M, RESP, TYPE_MAPPING> &
|
);
|
||||||
WithFunctions<F, RESP, TYPE_MAPPING> &
|
|
||||||
WithScripts<S, RESP, TYPE_MAPPING>
|
|
||||||
);
|
|
||||||
|
|
||||||
export interface ClientCommandOptions extends QueueCommandOptions {
|
type ProxyClient = RedisClient<any, any, any, any, any> & { commandOptions?: CommandOptions };
|
||||||
// isolated?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
type ProxyClient = RedisClient<{}, {}, {}, RespVersions, TypeMapping> & { commandOptions?: ClientCommandOptions };
|
|
||||||
|
|
||||||
type NamespaceProxyClient = { self: ProxyClient };
|
type NamespaceProxyClient = { self: ProxyClient };
|
||||||
|
|
||||||
@@ -181,8 +195,8 @@ export default class RedisClient<
|
|||||||
reply = await this.sendCommand(redisArgs, this.commandOptions).catch((err: unknown) => {
|
reply = await this.sendCommand(redisArgs, this.commandOptions).catch((err: unknown) => {
|
||||||
if (!(err as Error)?.message?.startsWith?.('NOSCRIPT')) throw err;
|
if (!(err as Error)?.message?.startsWith?.('NOSCRIPT')) throw err;
|
||||||
|
|
||||||
args[0] = 'EVAL';
|
redisArgs[0] = 'EVAL';
|
||||||
args[1] = script.SCRIPT;
|
redisArgs[1] = script.SCRIPT;
|
||||||
return this.sendCommand(redisArgs, this.commandOptions);
|
return this.sendCommand(redisArgs, this.commandOptions);
|
||||||
});
|
});
|
||||||
return transformReply ?
|
return transformReply ?
|
||||||
@@ -195,8 +209,9 @@ export default class RedisClient<
|
|||||||
M extends RedisModules = {},
|
M extends RedisModules = {},
|
||||||
F extends RedisFunctions = {},
|
F extends RedisFunctions = {},
|
||||||
S extends RedisScripts = {},
|
S extends RedisScripts = {},
|
||||||
RESP extends RespVersions = 2
|
RESP extends RespVersions = 2,
|
||||||
>(config?: CommanderConfig<M, F, S, RESP>) {
|
TYPE_MAPPING extends TypeMapping = {}
|
||||||
|
>(config?: CommanderConfig<M, F, S, RESP> & TypeMappingOption<TYPE_MAPPING>) {
|
||||||
const Client = attachConfig({
|
const Client = attachConfig({
|
||||||
BaseClass: RedisClient,
|
BaseClass: RedisClient,
|
||||||
commands: COMMANDS,
|
commands: COMMANDS,
|
||||||
@@ -212,7 +227,7 @@ export default class RedisClient<
|
|||||||
return (options?: Omit<RedisClientOptions, keyof Exclude<typeof config, undefined>>) => {
|
return (options?: Omit<RedisClientOptions, keyof Exclude<typeof config, undefined>>) => {
|
||||||
// returning a proxy of the client to prevent the namespaces.self to leak between proxies
|
// returning a proxy of the client to prevent the namespaces.self to leak between proxies
|
||||||
// namespaces will be bootstraped on first access per proxy
|
// namespaces will be bootstraped on first access per proxy
|
||||||
return Object.create(new Client(options)) as RedisClientType<M, F, S, RESP>;
|
return Object.create(new Client(options)) as RedisClientType<M, F, S, RESP, TYPE_MAPPING>;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,8 +235,9 @@ export default class RedisClient<
|
|||||||
M extends RedisModules = {},
|
M extends RedisModules = {},
|
||||||
F extends RedisFunctions = {},
|
F extends RedisFunctions = {},
|
||||||
S extends RedisScripts = {},
|
S extends RedisScripts = {},
|
||||||
RESP extends RespVersions = 2
|
RESP extends RespVersions = 2,
|
||||||
>(this: void, options?: RedisClientOptions<M, F, S, RESP>) {
|
TYPE_MAPPING extends TypeMapping = {}
|
||||||
|
>(this: void, options?: RedisClientOptions<M, F, S, RESP, TYPE_MAPPING>) {
|
||||||
return RedisClient.factory(options)(options);
|
return RedisClient.factory(options)(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,6 +324,12 @@ export default class RedisClient<
|
|||||||
this._selectedDB = options.database;
|
this._selectedDB = options.database;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options?.typeMapping) {
|
||||||
|
(this as unknown as ProxyClient).commandOptions = {
|
||||||
|
typeMapping: options.typeMapping
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,7 +412,7 @@ export default class RedisClient<
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (promises.length) {
|
if (promises.length) {
|
||||||
this._tick(true);
|
this._write();
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -417,10 +439,10 @@ export default class RedisClient<
|
|||||||
.on('ready', () => {
|
.on('ready', () => {
|
||||||
this.emit('ready');
|
this.emit('ready');
|
||||||
this._setPingTimer();
|
this._setPingTimer();
|
||||||
this._tick();
|
this._scheduleWrite();
|
||||||
})
|
})
|
||||||
.on('reconnecting', () => this.emit('reconnecting'))
|
.on('reconnecting', () => this.emit('reconnecting'))
|
||||||
.on('drain', () => this._tick())
|
.on('drain', () => this._scheduleWrite())
|
||||||
.on('end', () => this.emit('end'));
|
.on('end', () => this.emit('end'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -440,7 +462,7 @@ export default class RedisClient<
|
|||||||
}, this._options.pingInterval);
|
}, this._options.pingInterval);
|
||||||
}
|
}
|
||||||
|
|
||||||
withCommandOptions<T extends ClientCommandOptions>(options: T) {
|
withCommandOptions<T extends CommandOptions>(options: T) {
|
||||||
const proxy = Object.create(this.self);
|
const proxy = Object.create(this.self);
|
||||||
proxy.commandOptions = options;
|
proxy.commandOptions = options;
|
||||||
return proxy as RedisClientType<
|
return proxy as RedisClientType<
|
||||||
@@ -453,8 +475,8 @@ export default class RedisClient<
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _commandOptionsProxy<
|
private _commandOptionsProxy<
|
||||||
K extends keyof ClientCommandOptions,
|
K extends keyof CommandOptions,
|
||||||
V extends ClientCommandOptions[K]
|
V extends CommandOptions[K]
|
||||||
>(
|
>(
|
||||||
key: K,
|
key: K,
|
||||||
value: V
|
value: V
|
||||||
@@ -523,7 +545,7 @@ export default class RedisClient<
|
|||||||
|
|
||||||
sendCommand<T = ReplyUnion>(
|
sendCommand<T = ReplyUnion>(
|
||||||
args: CommandArguments,
|
args: CommandArguments,
|
||||||
options?: ClientCommandOptions
|
options?: CommandOptions
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
if (!this._socket.isOpen) {
|
if (!this._socket.isOpen) {
|
||||||
return Promise.reject(new ClientClosedError());
|
return Promise.reject(new ClientClosedError());
|
||||||
@@ -532,7 +554,7 @@ export default class RedisClient<
|
|||||||
}
|
}
|
||||||
|
|
||||||
const promise = this._queue.addCommand<T>(args, options);
|
const promise = this._queue.addCommand<T>(args, options);
|
||||||
this._tick();
|
this._scheduleWrite();
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -546,7 +568,7 @@ export default class RedisClient<
|
|||||||
private _pubSubCommand(promise: Promise<void> | undefined) {
|
private _pubSubCommand(promise: Promise<void> | undefined) {
|
||||||
if (promise === undefined) return Promise.resolve();
|
if (promise === undefined) return Promise.resolve();
|
||||||
|
|
||||||
this._tick();
|
this._scheduleWrite();
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -672,32 +694,19 @@ export default class RedisClient<
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _tick(force = false): void {
|
private _write() {
|
||||||
if (this._socket.writableNeedDrain || (!force && !this._socket.isReady)) {
|
this._socket.write(this._queue.waitingToBeSent());
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._socket.cork();
|
|
||||||
|
|
||||||
do {
|
|
||||||
const args = this._queue.getCommandToSend();
|
|
||||||
if (args === undefined) break;
|
|
||||||
|
|
||||||
this._socket.writeCommand(args);
|
|
||||||
} while (!this._socket.writableNeedDrain);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _addMultiCommands(
|
private _scheduledWrite?: NodeJS.Immediate;
|
||||||
commands: Array<RedisMultiQueuedCommand>,
|
|
||||||
chainId?: symbol,
|
private _scheduleWrite() {
|
||||||
typeMapping?: TypeMapping
|
if (!this.isReady || this._scheduledWrite) return;
|
||||||
) {
|
|
||||||
return Promise.all(
|
this._scheduledWrite = setImmediate(() => {
|
||||||
commands.map(({ args }) => this._queue.addCommand(args, {
|
this._write();
|
||||||
chainId,
|
this._scheduledWrite = undefined;
|
||||||
typeMapping
|
});
|
||||||
}))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -713,7 +722,7 @@ export default class RedisClient<
|
|||||||
typeMapping: (this as ProxyClient).commandOptions?.typeMapping
|
typeMapping: (this as ProxyClient).commandOptions?.typeMapping
|
||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
this._tick();
|
this._scheduleWrite();
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -747,7 +756,7 @@ export default class RedisClient<
|
|||||||
this._queue.addCommand(['EXEC'], { chainId })
|
this._queue.addCommand(['EXEC'], { chainId })
|
||||||
);
|
);
|
||||||
|
|
||||||
this._tick();
|
this._scheduleWrite();
|
||||||
|
|
||||||
const results = await Promise.all(promises),
|
const results = await Promise.all(promises),
|
||||||
execResult = results[results.length - 1];
|
execResult = results[results.length - 1];
|
||||||
@@ -772,7 +781,7 @@ export default class RedisClient<
|
|||||||
async* scanIterator(
|
async* scanIterator(
|
||||||
this: RedisClientType<M, F, S, RESP, TYPE_MAPPING>,
|
this: RedisClientType<M, F, S, RESP, TYPE_MAPPING>,
|
||||||
options?: ScanOptions & ScanIteratorOptions
|
options?: ScanOptions & ScanIteratorOptions
|
||||||
): AsyncIterable<ReplyWithTypeMapping<CommandReply<typeof SCAN, RESP>['keys'], TYPE_MAPPING>> {
|
) {
|
||||||
let cursor = options?.cursor ?? 0;
|
let cursor = options?.cursor ?? 0;
|
||||||
do {
|
do {
|
||||||
const reply = await this.scan(cursor, options);
|
const reply = await this.scan(cursor, options);
|
||||||
@@ -827,7 +836,7 @@ export default class RedisClient<
|
|||||||
return this._socket.quit(async () => {
|
return this._socket.quit(async () => {
|
||||||
clearTimeout(this._pingTimer);
|
clearTimeout(this._pingTimer);
|
||||||
const quitPromise = this._queue.addCommand<string>(['QUIT']);
|
const quitPromise = this._queue.addCommand<string>(['QUIT']);
|
||||||
this._tick();
|
this._scheduleWrite();
|
||||||
return quitPromise;
|
return quitPromise;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -842,7 +851,7 @@ export default class RedisClient<
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close the client. Wait for pending replies.
|
* Close the client. Wait for pending commands.
|
||||||
*/
|
*/
|
||||||
close() {
|
close() {
|
||||||
return new Promise<void>(resolve => {
|
return new Promise<void>(resolve => {
|
||||||
|
@@ -88,7 +88,7 @@ export default class RedisClientMultiCommand<REPLIES = []> {
|
|||||||
private static _createCommand(command: Command, resp: RespVersions) {
|
private static _createCommand(command: Command, resp: RespVersions) {
|
||||||
const transformReply = getTransformReply(command, resp);
|
const transformReply = getTransformReply(command, resp);
|
||||||
return function (this: RedisClientMultiCommand, ...args: Array<unknown>) {
|
return function (this: RedisClientMultiCommand, ...args: Array<unknown>) {
|
||||||
return this._multi.addCommand(
|
return this.addCommand(
|
||||||
command.transformArguments(...args),
|
command.transformArguments(...args),
|
||||||
transformReply
|
transformReply
|
||||||
);
|
);
|
||||||
@@ -98,7 +98,7 @@ export default class RedisClientMultiCommand<REPLIES = []> {
|
|||||||
private static _createModuleCommand(command: Command, resp: RespVersions) {
|
private static _createModuleCommand(command: Command, resp: RespVersions) {
|
||||||
const transformReply = getTransformReply(command, resp);
|
const transformReply = getTransformReply(command, resp);
|
||||||
return function (this: { self: RedisClientMultiCommand }, ...args: Array<unknown>) {
|
return function (this: { self: RedisClientMultiCommand }, ...args: Array<unknown>) {
|
||||||
return this.self._multi.addCommand(
|
return this.self.addCommand(
|
||||||
command.transformArguments(...args),
|
command.transformArguments(...args),
|
||||||
transformReply
|
transformReply
|
||||||
);
|
);
|
||||||
@@ -112,7 +112,7 @@ export default class RedisClientMultiCommand<REPLIES = []> {
|
|||||||
const fnArgs = fn.transformArguments(...args),
|
const fnArgs = fn.transformArguments(...args),
|
||||||
redisArgs: CommandArguments = prefix.concat(fnArgs);
|
redisArgs: CommandArguments = prefix.concat(fnArgs);
|
||||||
redisArgs.preserve = fnArgs.preserve;
|
redisArgs.preserve = fnArgs.preserve;
|
||||||
return this.self._multi.addCommand(
|
return this.self.addCommand(
|
||||||
redisArgs,
|
redisArgs,
|
||||||
transformReply
|
transformReply
|
||||||
);
|
);
|
||||||
@@ -164,6 +164,11 @@ export default class RedisClientMultiCommand<REPLIES = []> {
|
|||||||
|
|
||||||
select = this.SELECT;
|
select = this.SELECT;
|
||||||
|
|
||||||
|
addCommand(args: CommandArguments, transformReply?: TransformReply) {
|
||||||
|
this._multi.addCommand(args, transformReply);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
async exec<T extends MultiReply = MULTI_REPLY['GENERIC']>(execAsPipeline = false): Promise<MultiReplyType<T, REPLIES>> {
|
async exec<T extends MultiReply = MULTI_REPLY['GENERIC']>(execAsPipeline = false): Promise<MultiReplyType<T, REPLIES>> {
|
||||||
if (execAsPipeline) return this.execAsPipeline<T>();
|
if (execAsPipeline) return this.execAsPipeline<T>();
|
||||||
|
|
||||||
|
@@ -82,14 +82,6 @@ export default class RedisSocket extends EventEmitter {
|
|||||||
return this._isReady;
|
return this._isReady;
|
||||||
}
|
}
|
||||||
|
|
||||||
// `writable.writableNeedDrain` was added in v15.2.0 and therefore can't be used
|
|
||||||
// https://nodejs.org/api/stream.html#stream_writable_writableneeddrain
|
|
||||||
private _writableNeedDrain = false;
|
|
||||||
|
|
||||||
get writableNeedDrain(): boolean {
|
|
||||||
return this._writableNeedDrain;
|
|
||||||
}
|
|
||||||
|
|
||||||
private _isSocketUnrefed = false;
|
private _isSocketUnrefed = false;
|
||||||
|
|
||||||
constructor(initiator: RedisSocketInitiator, options?: RedisSocketOptions) {
|
constructor(initiator: RedisSocketInitiator, options?: RedisSocketOptions) {
|
||||||
@@ -149,7 +141,6 @@ export default class RedisSocket extends EventEmitter {
|
|||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
this._socket = await this._createSocket();
|
this._socket = await this._createSocket();
|
||||||
this._writableNeedDrain = false;
|
|
||||||
this.emit('connect');
|
this.emit('connect');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -203,10 +194,7 @@ export default class RedisSocket extends EventEmitter {
|
|||||||
this._onSocketError(new SocketClosedUnexpectedlyError());
|
this._onSocketError(new SocketClosedUnexpectedlyError());
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on('drain', () => {
|
.on('drain', () => this.emit('drain'))
|
||||||
this._writableNeedDrain = false;
|
|
||||||
this.emit('drain');
|
|
||||||
})
|
|
||||||
.on('data', data => this.emit('data', data));
|
.on('data', data => this.emit('data', data));
|
||||||
|
|
||||||
resolve(socket);
|
resolve(socket);
|
||||||
@@ -240,14 +228,20 @@ export default class RedisSocket extends EventEmitter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
writeCommand(args: Array<RedisArgument>): void {
|
write(iterator: IterableIterator<Array<RedisArgument>>): void {
|
||||||
if (!this._socket) {
|
if (!this._socket) {
|
||||||
throw new ClientClosedError();
|
throw new ClientClosedError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._socket.cork();
|
||||||
|
for (const args of iterator) {
|
||||||
|
for (const toWrite of args) {
|
||||||
|
this._socket.write(toWrite);
|
||||||
|
}
|
||||||
|
|
||||||
for (const toWrite of args) {
|
if (this._socket.writableNeedDrain) break;
|
||||||
this._writableNeedDrain = !this._socket.write(toWrite);
|
|
||||||
}
|
}
|
||||||
|
this._socket.uncork();
|
||||||
}
|
}
|
||||||
|
|
||||||
async quit<T>(fn: () => Promise<T>): Promise<T> {
|
async quit<T>(fn: () => Promise<T>): Promise<T> {
|
||||||
@@ -289,20 +283,12 @@ export default class RedisSocket extends EventEmitter {
|
|||||||
this.emit('end');
|
this.emit('end');
|
||||||
}
|
}
|
||||||
|
|
||||||
private _isCorked = false;
|
|
||||||
|
|
||||||
cork(): void {
|
cork(): void {
|
||||||
if (!this._socket || this._isCorked) {
|
this._socket?.cork();
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
this._socket.cork();
|
uncork(): void {
|
||||||
this._isCorked = true;
|
this._socket?.uncork();
|
||||||
|
|
||||||
setImmediate(() => {
|
|
||||||
this._socket?.uncork();
|
|
||||||
this._isCorked = false;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ref(): void {
|
ref(): void {
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
import { ClientCommandOptions, RedisClientOptions } from '../client';
|
import { RedisClientOptions } from '../client';
|
||||||
|
import { CommandOptions } from '../client/commands-queue';
|
||||||
import { Command, CommandArguments, CommanderConfig, CommandPolicies, CommandWithPoliciesSignature, TypeMapping, RedisArgument, RedisFunction, RedisFunctions, RedisModules, RedisScript, RedisScripts, ReplyUnion, RespVersions } from '../RESP/types';
|
import { Command, CommandArguments, CommanderConfig, CommandPolicies, CommandWithPoliciesSignature, TypeMapping, RedisArgument, RedisFunction, RedisFunctions, RedisModules, RedisScript, RedisScripts, ReplyUnion, RespVersions } from '../RESP/types';
|
||||||
import COMMANDS from '../commands';
|
import COMMANDS from '../commands';
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
@@ -69,7 +70,7 @@ export type RedisClusterType<
|
|||||||
> = RedisCluster<M, F, S, RESP, TYPE_MAPPING, POLICIES> & WithCommands<RESP, TYPE_MAPPING, POLICIES>;
|
> = RedisCluster<M, F, S, RESP, TYPE_MAPPING, POLICIES> & WithCommands<RESP, TYPE_MAPPING, POLICIES>;
|
||||||
// & WithModules<M> & WithFunctions<F> & WithScripts<S>
|
// & WithModules<M> & WithFunctions<F> & WithScripts<S>
|
||||||
|
|
||||||
export interface ClusterCommandOptions extends ClientCommandOptions {
|
export interface ClusterCommandOptions extends CommandOptions {
|
||||||
policies?: CommandPolicies;
|
policies?: CommandPolicies;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user