Skip to content

Commit e74f97e

Browse files
Merge pull request #473 from splitio/ioredis-v5-remove-proxy
Update `RedisAdapter` to avoid an additional wrapper layer via a Proxy
2 parents f86d2f4 + de8e406 commit e74f97e

File tree

3 files changed

+7
-20
lines changed

3 files changed

+7
-20
lines changed

src/storages/inRedis/RedisAdapter.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { setToArray } from '../../utils/lang/sets';
1818
const LOG_PREFIX = 'storage:redis-adapter: ';
1919

2020
// If we ever decide to fully wrap every method, there's a Commander.getBuiltinCommands from ioredis.
21-
const METHODS_TO_PROMISE_WRAP = ['set', 'exec', 'del', 'get', 'keys', 'sadd', 'srem', 'sismember', 'smembers', 'incr', 'rpush', 'expire', 'mget', 'lrange', 'ltrim', 'hset', 'hincrby', 'popNRaw'];
21+
const METHODS_TO_PROMISE_WRAP = ['set', 'exec', 'del', 'get', 'keys', 'sadd', 'srem', 'sismember', 'smembers', 'incr', 'decr', 'rpush', 'expire', 'mget', 'lrange', 'ltrim', 'hset', 'hincrby', 'popNRaw', 'hgetall', 'llen', 'hget'];
2222
const METHODS_TO_PROMISE_WRAP_EXEC = ['pipeline'];
2323

2424
// Not part of the settings since it'll vary on each storage. We should be removing storage specific logic from elsewhere.
@@ -45,7 +45,7 @@ interface IRedisCommand {
4545

4646
/**
4747
* Redis adapter on top of the library of choice (written with ioredis) for some extra control.
48-
* Refactored to use Composition and Proxy instead of Inheritance to support both v4 and v5.
48+
* Refactored to use Composition instead of Inheritance to support both v4 and v5.
4949
*/
5050
export class RedisAdapter {
5151
// eslint-disable-next-line no-undef -- Index signature to allow proxying dynamic ioredis methods without TS errors
@@ -73,23 +73,10 @@ export class RedisAdapter {
7373
this._listenToEvents();
7474
this._setTimeoutWrappers();
7575
this._setDisconnectWrapper();
76+
}
7677

77-
// Return a Proxy. This allows the adapter to act exactly like an extended class.
78-
// If a method/property is accessed that we didn't explicitly wrap, it forwards it to `this.client`.
79-
return new Proxy(this, {
80-
get(target: RedisAdapter, prop: string | symbol) {
81-
// If the property exists on our wrapper (like wrapped 'get', 'set', or internal methods)
82-
if (prop in target) {
83-
return target[prop as keyof RedisAdapter];
84-
}
85-
// If it doesn't exist on our wrapper but exists on the real client (like 'on', 'quit')
86-
if (target.client && prop in target.client) {
87-
const val = target.client[prop];
88-
return typeof val === 'function' ? val.bind(target.client) : val;
89-
}
90-
return undefined;
91-
}
92-
});
78+
on(event: string, listener: (...args: any[]) => void) {
79+
return this.client.on(event, listener);
9380
}
9481

9582
_listenToEvents() {

src/storages/inRedis/__tests__/ImpressionCountsCacheInRedis.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('IMPRESSION COUNTS CACHE IN REDIS', () => {
8383
test('POST IMPRESSION COUNTS IN REDIS FUNCTION', async () => {
8484
const connection = new RedisAdapter(loggerMock);
8585
// @TODO next line is not required with ioredis
86-
await new Promise(res => connection.once('ready', res));
86+
await new Promise(res => connection.on('ready', res));
8787

8888
const counter = new ImpressionCountsCacheInRedis(loggerMock, key, connection);
8989
// Clean up in case there are still keys there.

src/storages/inRedis/__tests__/UniqueKeysCacheInRedis.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe('UNIQUE KEYS CACHE IN REDIS', () => {
147147
test('Should call "onFullQueueCb" when the queue is full. "popNRaw" should pop items.', async () => {
148148
const connection = new RedisAdapter(loggerMock);
149149
// @TODO next line is not required with ioredis
150-
await new Promise(res => connection.once('ready', res));
150+
await new Promise(res => connection.on('ready', res));
151151

152152
const cache = new UniqueKeysCacheInRedis(loggerMock, key, connection, 3);
153153

0 commit comments

Comments
 (0)