UNPKG

redis-smq-common

Version:

Provides essential components and utilities shared across RedisSMQ packages.

467 lines 12.9 kB
import { createClient } from '@redis/client'; import { RedisClientAbstract } from '../redis-client-abstract.js'; import { NodeRedisClientMulti } from './node-redis-client-multi.js'; import { CommandNotSupportedError } from '../../errors/index.js'; export class NodeRedisClient extends RedisClientAbstract { client; constructor(config = {}) { super(); this.client = createClient(config); this.client.once('ready', () => { this.connectionClosed = false; this.init(); }); this.client.once('end', () => { this.connectionClosed = true; this.emit('end'); }); this.client.connect(); } exists(key, cb) { this.client .exists(key) .then((reply) => cb(null, Boolean(reply))) .catch(cb); } set(key, value, options, cb) { this.client .set(key, value, { ...(options.expire ? { [options.expire.mode]: options.expire.value } : {}), ...(options.exists ? { [options.exists]: true } : {}), }) .then((reply) => cb(null, reply)) .catch(cb); } zadd(key, score, member, cb) { this.client .zAdd(key, { score, value: member }) .then((reply) => cb(null, reply)) .catch(cb); } multi() { return new NodeRedisClientMulti(this.client); } watch(args, cb) { this.client .watch(args) .then((reply) => cb(null, reply)) .catch(cb); } unwatch(cb) { this.client .unwatch() .then((reply) => cb(null, reply)) .catch(cb); } sismember(key, member, cb) { this.client .sIsMember(key, member) .then((reply) => cb(null, Number(reply))) .catch(cb); } zcard(key, cb) { this.client .zCard(key) .then((reply) => cb(null, reply)) .catch(cb); } zrange(key, min, max, cb) { this.client .zRange(key, min, max) .then((reply) => cb(null, reply)) .catch(cb); } zrevrange(key, min, max, cb) { this.client .zRange(key, min, max, { REV: true }) .then((reply) => cb(null, reply)) .catch(cb); } zrem(source, id, cb) { this.client .zRem(source, id) .then((reply) => cb(null, reply)) .catch(cb); } psubscribe(pattern) { this.client.pSubscribe(pattern, (message, channel) => { this.client.emit('pmessage', pattern, channel, message); }); } punsubscribe(channel) { this.client.pUnsubscribe(channel).catch(() => void 0); } subscribe(channel) { this.client.subscribe(channel, (message, channel) => { this.client.emit('message', channel, message); }); } unsubscribe(channel) { this.client.unsubscribe(channel).catch(() => void 0); } zrangebyscore(key, min, max, offset, count, cb) { this.client .zRangeByScore(key, min, max, { LIMIT: { offset, count, }, }) .then((reply) => cb(null, reply)) .catch(cb); } smembers(key, cb) { this.client .sMembers(key) .then((reply) => cb(null, reply)) .catch(cb); } sscan(key, cursor, options, cb) { this.client .sScan(key, cursor, options) .then(({ cursor, members }) => { cb(null, { cursor, items: members }); }) .catch(cb); } zscan(key, cursor, options, cb) { this.client .zScan(key, cursor, options) .then(({ cursor, members }) => { const result = new Set(); for (const i of members) { result.add(i.value); } cb(null, { cursor, items: [...result] }); }) .catch(cb); } sadd(key, member, cb) { this.client .sAdd(key, member) .then((reply) => cb(null, reply)) .catch(cb); } srem(key, member, cb) { this.client .sRem(key, member) .then((reply) => cb(null, reply)) .catch(cb); } hgetall(key, cb) { this.client .hGetAll(key) .then((reply) => cb(null, reply)) .catch(cb); } scard(key, cb) { this.client .sCard(key) .then((reply) => cb(null, reply)) .catch(cb); } hscan(key, cursor, options, cb) { this.client .hScan(key, cursor, options) .then(({ cursor, entries }) => { const result = {}; for (const item of entries) { result[item.field] = item.value; } cb(null, { cursor, result }); }) .catch(cb); } hget(key, field, cb) { this.client .hGet(key, field) .then((reply) => cb(null, reply ?? null)) .catch(cb); } hset(key, field, value, cb) { this.client .hSet(key, field, value) .then((reply) => cb(null, reply)) .catch(cb); } hdel(key, fields, cb) { this.client .hDel(key, fields) .then((reply) => cb(null, reply)) .catch(cb); } lrange(key, start, stop, cb) { this.client .lRange(key, start, stop) .then((reply) => cb(null, reply)) .catch(cb); } hkeys(key, cb) { this.client .hKeys(key) .then((reply) => cb(null, reply)) .catch(cb); } hlen(key, cb) { this.client .hLen(key) .then((reply) => cb(null, reply)) .catch(cb); } brpoplpush(source, destination, timeout, cb) { this.client .brPopLPush(source, destination, timeout) .then((reply) => cb(null, reply)) .catch(cb); } rpoplpush(source, destination, cb) { this.client .rPopLPush(source, destination) .then((reply) => cb(null, reply)) .catch(cb); } zrangebyscorewithscores(source, min, max, cb) { this.client .zRangeByScoreWithScores(source, min, max) .then((reply) => { const range = {}; for (const { score, value } of reply) { range[score] = value; } cb(null, range); }) .catch(cb); } rpop(key, cb) { this.client .rPop(key) .then((reply) => cb(null, reply)) .catch(cb); } lrem(key, count, element, cb) { this.client .lRem(key, count, element) .then((reply) => cb(null, reply)) .catch(cb); } lindex(key, index, cb) { this.client .lIndex(key, index) .then((reply) => cb(null, reply)) .catch(cb); } publish(channel, message, cb) { this.client .publish(channel, message) .then((reply) => cb(null, reply)) .catch(cb); } flushall(cb) { this.client .flushAll() .then((reply) => cb(null, reply)) .catch(cb); } loadScript(script, cb) { this.client .scriptLoad(script) .then((reply) => cb(null, reply)) .catch(cb); } evalsha(hash, args, cb) { const arrArgs = (Array.isArray(args) ? args : [args]).map(String); const numKeys = Number(arrArgs[0]); const keys = arrArgs.slice(1, numKeys + 1); const scriptArgs = arrArgs.slice(numKeys + 1); this.client .evalSha(hash, { keys, arguments: scriptArgs, }) .then((reply) => { if (Array.isArray(reply)) { cb(null, reply.map((i) => (i instanceof Buffer ? i.toString() : i))); } else if (reply instanceof Buffer) cb(null, reply.toString()); else cb(null, reply); }) .catch(cb); } get(key, cb) { this.client .get(key) .then((reply) => cb(null, reply)) .catch(cb); } del(key, cb) { this.client .del(key) .then((reply) => cb(null, reply)) .catch(cb); } llen(key, cb) { this.client .lLen(key) .then((reply) => cb(null, reply)) .catch(cb); } lmove(source, destination, from, to, cb) { if (!this.validateRedisVersion(6, 2)) { cb(new CommandNotSupportedError({ metadata: { command: 'lmove', }, })); } else { this.client .lMove(source, destination, from, to) .then((reply) => cb(null, reply)) .catch(cb); } } zremrangebyscore(source, min, max, cb) { this.client .zRemRangeByScore(source, min, max) .then((reply) => cb(null, reply)) .catch(cb); } hmget(source, keys, cb) { this.client .hmGet(source, keys) .then((reply) => cb(null, reply)) .catch(cb); } halt(cb) { if (!this.connectionClosed) { this.client.once('end', cb); this.end(); } else cb(); } end() { if (!this.connectionClosed) { this.client.disconnect().catch(() => void 0); } } shutdown(cb = () => void 0) { if (!this.connectionClosed) { this.client.once('end', cb); this.client.quit().catch(() => void 0); } else cb(); } getInfo(cb) { this.client .info() .then((reply) => cb(null, reply)) .catch(cb); } on(event, listener) { this.client.on(event, listener); return this; } ping(cb) { this.client .ping() .then((reply) => cb(null, reply)) .catch(cb); } mget(keys, cb) { this.client .mGet(keys) .then((reply) => cb(null, reply)) .catch(cb); } incr(key, cb) { this.client .incr(key) .then((reply) => cb(null, reply)) .catch(cb); } decr(key, cb) { this.client .decr(key) .then((reply) => cb(null, reply)) .catch(cb); } incrby(key, increment, cb) { this.client .incrBy(key, increment) .then((reply) => cb(null, reply)) .catch(cb); } decrby(key, decrement, cb) { this.client .decrBy(key, decrement) .then((reply) => cb(null, reply)) .catch(cb); } expire(key, seconds, cb) { this.client .expire(key, seconds) .then((reply) => cb(null, reply)) .catch(cb); } pexpire(key, milliseconds, cb) { this.client .pExpire(key, milliseconds) .then((reply) => cb(null, reply)) .catch(cb); } ttl(key, cb) { this.client .ttl(key) .then((reply) => cb(null, reply)) .catch(cb); } pttl(key, cb) { this.client .pTTL(key) .then((reply) => cb(null, reply)) .catch(cb); } lpush(key, elements, cb) { this.client .lPush(key, elements) .then((reply) => cb(null, reply)) .catch(cb); } rpush(key, elements, cb) { this.client .rPush(key, elements) .then((reply) => cb(null, reply)) .catch(cb); } lpop(key, cb) { this.client .lPop(key) .then((reply) => cb(null, reply)) .catch(cb); } ltrim(key, start, stop, cb) { this.client .lTrim(key, start, stop) .then((reply) => cb(null, reply)) .catch(cb); } zcount(key, min, max, cb) { this.client .zCount(key, min, max) .then((reply) => cb(null, reply)) .catch(cb); } zscore(key, member, cb) { this.client .zScore(key, member) .then((reply) => cb(null, reply !== null ? String(reply) : null)) .catch(cb); } } //# sourceMappingURL=node-redis-client.js.map