Skip to content

Commit

Permalink
refactor: get io methods names using reflection
Browse files Browse the repository at this point in the history
before, we kept a hard-coded list of method names. a lot was missing from it and it could be out of sync with the ioredis api
  • Loading branch information
Julien-R44 committed Jul 16, 2023
1 parent 5a4efcf commit 0b139c4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 163 deletions.
207 changes: 44 additions & 163 deletions src/io_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,167 +7,48 @@
* file that was distributed with this source code.
*/

export const ioMethods = [
'call',
import { Redis } from 'ioredis'

/**
* Returns all method names for a given class
*/
function getAllMethodNames(obj: any) {
let methods = new Set()
while ((obj = Reflect.getPrototypeOf(obj))) {
let keys = Reflect.ownKeys(obj)
keys.forEach((k) => methods.add(k))
}
return [...methods] as string[]
}

const ignoredMethods = [
'constructor',
'status',
'connect',
'send_command',
'bitcount',
'bitfield',
'get',
'getdel',
'getBuffer',
'set',
'setBuffer',
'callback',
'setnx',
'setex',
'psetex',
'append',
'strlen',
'del',
'exists',
'setbit',
'getbit',
'setrange',
'getrange',
'substr',
'incr',
'decr',
'mget',
'rpush',
'lpush',
'rpushx',
'lpushx',
'linsert',
'rpop',
'lpop',
'brpop',
'blpop',
'brpoplpush',
'llen',
'lindex',
'lset',
'lrange',
'ltrim',
'lrem',
'rpoplpush',
'sadd',
'srem',
'smove',
'sismember',
'scard',
'spop',
'srandmember',
'sinter',
'sinterstore',
'sunion',
'sunionstore',
'sdiff',
'sdiffstore',
'smembers',
'zadd',
'zincrby',
'zrem',
'zremrangebyscore',
'zremrangebyrank',
'zinterstore',
'zrange',
'zrevrange',
'zrangebyscore',
'zrevrangebyscore',
'zcount',
'zcard',
'zscore',
'zrank',
'zrevrank',
'hset',
'hsetBuffer',
'hsetnx',
'hget',
'hgetBuffer',
'hmget',
'hincrby',
'hincrbyfloat',
'hdel',
'hlen',
'hkeys',
'hvals',
'hgetall',
'hexists',
'incrby',
'incrbyfloat',
'decrby',
'getset',
'mset',
'msetnx',
'randomkey',
'select',
'move',
'rename',
'renamenx',
'expire',
'pexpire',
'expireat',
'pexpireat',
'keys',
'dbsize',
'auth',
'ping',
'echo',
'save',
'bgsave',
'bgrewriteaof',
'shutdown',
'lastsave',
'type',
'multi',
'exec',
'discard',
'sync',
'flushdb',
'flushall',
'sort',
'info',
'time',
'monitor',
'ttl',
'persist',
'slaveof',
'debug',
'config',
'watch',
'unwatch',
'cluster',
'restore',
'migrate',
'dump',
'object',
'client',
'eval',
'evalsha',
'script',
'scan',
'sscan',
'hscan',
'zscan',
'pfmerge',
'pfadd',
'pfcount',
'pipeline',
'scanStream',
'hscanStream',
'zscanStream',
'xack',
'xadd',
'xclaim',
'xdel',
'xgroup',
'xinfo',
'xlen',
'xpending',
'xrange',
'xread',
'xreadgroup',
'xrevrange',
'xtrim',
] as const
'disconnect',
'duplicate',
'subscribe',
'unsubscribe',
'psubscribe',
'punsubscribe',
'quit',
'publish',
'__defineGetter__',
'__defineSetter__',
'hasOwnProperty',
'__lookupGetter__',
'__lookupSetter__',
'isPrototypeOf',
'propertyIsEnumerable',
'toString',
'valueOf',
'__proto__',
'toLocaleString',
]

/**
* List of methods on Redis class
*/
export const ioMethods = getAllMethodNames(Redis.prototype).filter(
(method) => !ignoredMethods.includes(method)
) as string[]
27 changes: 27 additions & 0 deletions test/redis_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,31 @@ test.group('Redis Manager', () => {
expectTypeOf(redis.connection('cluster')).toEqualTypeOf<RedisClusterConnectionContract>()
expectTypeOf(redis.connection('primary')).toEqualTypeOf<RedisConnectionContract>()
})

test('should have every ioredis methods available', async ({ assert }) => {
const redis = new RedisManagerFactory({
connection: 'primary',
connections: {
primary: {
host: process.env.REDIS_HOST,
port: Number(process.env.REDIS_PORT),
},
cluster: {
clusters: clusterNodes,
},
},
}).create(new AppFactory().create(BASE_URL, () => {}))

/**
* Assert on some obscure methods to make sure
* they are available
*/
assert.isFunction(redis.geohash)
assert.isFunction(redis.spopBuffer)
assert.isFunction(redis.georadiusbymember)
assert.isFunction(redis.xack)
assert.isFunction(redis.xclaim)
assert.isFunction(redis.xgroup)
assert.isFunction(redis.decr)
})
})

0 comments on commit 0b139c4

Please sign in to comment.