Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/client/lib/client/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,17 @@ describe('Client', () => {
assert.equal(await client.sendCommand(['PING']), 'PONG');
}, GLOBAL.SERVERS.OPEN);

testUtils.testWithClient('should respect type mapping proxy', async client => {
await client.set('key', 'value');

const reply = await client.withTypeMapping({
[RESP_TYPES.BLOB_STRING]: Buffer
}).sendCommand(['GET', 'key']);

assert.ok(reply instanceof Buffer);
assert.equal(reply.toString(), 'value');
}, GLOBAL.SERVERS.OPEN);

testUtils.testWithClient('Unactivated AbortController should not abort', async client => {
await client.sendCommand(['PING'], {
abortSignal: new AbortController().signal
Expand Down
34 changes: 21 additions & 13 deletions packages/client/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ export type RedisClientType<
RedisClientExtensions<M, F, S, RESP, TYPE_MAPPING>
);

type ProxyClient = RedisClient<any, any, any, any, any>;
type ProxyClient = RedisClient<RedisModules, RedisFunctions, RedisScripts, RespVersions, TypeMapping>;
type ProxyClientConstructor = new (
options?: RedisClientOptions<RedisModules, RedisFunctions, RedisScripts, RespVersions, TypeMapping>
) => ProxyClient;

type NamespaceProxyClient = { _self: ProxyClient };

Expand Down Expand Up @@ -320,7 +323,10 @@ export default class RedisClient<
}
}

static #SingleEntryCache = new SingleEntryCache<any, any>()
static #SingleEntryCache = new SingleEntryCache<
CommanderConfig<RedisModules, RedisFunctions, RedisScripts, RespVersions> | undefined,
ProxyClientConstructor
>()

static factory<
M extends RedisModules = {},
Expand All @@ -340,18 +346,20 @@ export default class RedisClient<
createFunctionCommand: RedisClient.#createFunctionCommand,
createScriptCommand: RedisClient.#createScriptCommand,
config
});
}) as ProxyClientConstructor;

Client.prototype.Multi = RedisClientMultiCommand.extend(config);

RedisClient.#SingleEntryCache.set(config, Client);
}

const ClientConstructor = Client;

return <TYPE_MAPPING extends TypeMapping = {}>(
options?: Omit<RedisClientOptions<M, F, S, RESP, TYPE_MAPPING>, keyof Exclude<typeof config, undefined>>
) => {
// returning a "proxy" to prevent the namespaces._self to leak between "proxies"
return Object.create(new Client(options)) as RedisClientType<M, F, S, RESP, TYPE_MAPPING>;
return Object.create(new ClientConstructor(options)) as RedisClientType<M, F, S, RESP, TYPE_MAPPING>;
};
}

Expand Down Expand Up @@ -598,11 +606,11 @@ export default class RedisClient<
const cscConfig = this.#options.clientSideCache;
this.#clientSideCache = new BasicClientSideCache(cscConfig);
}
this.#queue.addPushHandler((push: Array<any>): boolean => {
if (push[0].toString() !== 'invalidate') return false;
this.#queue.addPushHandler((push: Array<unknown>): boolean => {
if (String(push[0]) !== 'invalidate') return false;

if (push[1] !== null) {
for (const key of push[1]) {
for (const key of push[1] as Iterable<RedisArgument>) {
this.#clientSideCache?.invalidate(key)
}
} else {
Expand All @@ -612,11 +620,11 @@ export default class RedisClient<
return true
});
} else if (options?.emitInvalidate) {
this.#queue.addPushHandler((push: Array<any>): boolean => {
if (push[0].toString() !== 'invalidate') return false;
this.#queue.addPushHandler((push: Array<unknown>): boolean => {
if (String(push[0]) !== 'invalidate') return false;

if (push[1] !== null) {
for (const key of push[1]) {
for (const key of push[1] as Iterable<RedisArgument>) {
this.emit('invalidate', key);
}
} else {
Expand Down Expand Up @@ -1057,7 +1065,7 @@ export default class RedisClient<
*/
_ejectSocket(): RedisSocket {
const socket = this._self.#socket;
// @ts-ignore
// @ts-expect-error allow temporarily clearing the socket during internal socket replacement
this._self.#socket = null;
socket.removeAllListeners();
return socket;
Expand Down Expand Up @@ -1183,7 +1191,7 @@ export default class RedisClient<

// Merge global options with provided options
const opts = {
...this._self._commandOptions,
...this._commandOptions,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spread drops inherited base command options from proxy

Medium Severity

Spreading this._commandOptions only copies own enumerable properties, but _commandOptionsProxy builds the proxy's _commandOptions via Object.create(this._commandOptions ?? null), placing the base client's options on the prototype. When a client has constructor-level commandOptions (e.g. timeout or typeMapping) and a proxy overrides a different key (e.g. withAbortSignal), the base options sit on the prototype and are silently dropped by the spread. The previous code (this._self._commandOptions) spread the client's plain object directly, preserving all base options.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0b73a74. Configure here.

...options,
};

Expand Down Expand Up @@ -1524,7 +1532,7 @@ export default class RedisClient<

MULTI<isTyped extends MultiMode = MULTI_MODE['TYPED']>() {
type Multi = new (...args: ConstructorParameters<typeof RedisClientMultiCommand>) => RedisClientMultiCommandType<isTyped, [], M, F, S, RESP, TYPE_MAPPING>;
return new ((this as any).Multi as Multi)(
return new ((this as unknown as { Multi: Multi }).Multi)(
this._executeMulti.bind(this),
this._executePipeline.bind(this),
this._commandOptions?.typeMapping
Expand Down