redis-smq-common
Version:
RedisSMQ Common Library provides many components that are mainly used by RedisSMQ and RedisSMQ Monitor.
289 lines • 9.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IoredisClient = void 0;
const ioredis_1 = require("ioredis");
const index_js_1 = require("../../../errors/index.js");
const index_js_2 = require("../../errors/index.js");
const redis_client_abstract_js_1 = require("../redis-client-abstract.js");
const ioredis_client_multi_js_1 = require("./ioredis-client-multi.js");
class IoredisClient extends redis_client_abstract_js_1.RedisClientAbstract {
constructor(config = {}) {
super();
this.client = new ioredis_1.Redis(config);
this.client.once('ready', () => {
this.connectionClosed = false;
this.init();
});
this.client.once('end', () => {
this.connectionClosed = true;
this.emit('end');
});
}
set(key, value, options, cb) {
if (options.exists && options.expire) {
this.client.set(key, value, options.expire.mode, options.expire.value, options.exists, cb);
}
else if (options.expire) {
this.client.set(key, value, options.expire.mode, options.expire.value, cb);
}
else if (options.exists) {
this.client.set(key, value, options.exists, cb);
}
else {
this.client.set(key, value, cb);
}
}
zadd(key, score, member, cb) {
this.client.zadd(key, score, member, cb);
}
multi() {
return new ioredis_client_multi_js_1.IoredisClientMulti(this.client);
}
watch(args, cb) {
this.client.watch(args, cb);
}
unwatch(cb) {
this.client.unwatch(cb);
}
sismember(key, member, cb) {
this.client.sismember(key, member, cb);
}
sscan(key, cursor, options, cb) {
const args = [key, cursor];
if (options.MATCH)
args.push('MATCH', options.MATCH);
if (options.COUNT)
args.push('COUNT', String(options.COUNT));
this.client.sscan(...args, (err, reply) => {
if (err)
return cb(err);
const [cursor, items] = reply !== null && reply !== void 0 ? reply : [];
if (!cursor || !items)
return cb(new index_js_2.RedisClientError());
cb(null, { cursor, items });
});
}
zcard(key, cb) {
this.client.zcard(key, cb);
}
zrange(key, min, max, cb) {
this.client.zrange(key, min, max, cb);
}
zscan(key, cursor, options, cb) {
const args = [key, cursor];
if (options.MATCH)
args.push('MATCH', options.MATCH);
if (options.COUNT)
args.push('COUNT', String(options.COUNT));
this.client.zscan(...args, (err, reply) => {
if (err)
cb(err);
else if (!reply)
cb(new index_js_1.CallbackEmptyReplyError());
else {
const result = new Set();
const [cursor, items] = reply;
while (items.length) {
const item = String(items.shift());
const score = String(items.shift());
result.add(item);
}
cb(null, { cursor, items: [...result] });
}
});
}
zrevrange(key, min, max, cb) {
this.client.zrevrange(key, min, max, cb);
}
zrem(source, id, cb) {
this.client.zrem(source, id, cb);
}
psubscribe(pattern) {
this.client.psubscribe(pattern);
}
punsubscribe(channel) {
if (channel)
this.client.punsubscribe(channel);
else
this.client.punsubscribe();
}
subscribe(channel) {
this.client.subscribe(channel);
}
unsubscribe(channel) {
if (channel)
this.client.unsubscribe(channel);
else
this.client.unsubscribe();
}
zrangebyscore(key, min, max, offset, count, cb) {
this.client.zrangebyscore(key, min, max, 'LIMIT', offset, count, cb);
}
smembers(key, cb) {
this.client.smembers(key, cb);
}
sadd(key, member, cb) {
this.client.sadd(key, member, cb);
}
srem(key, member, cb) {
this.client.srem(key, member, cb);
}
scard(key, cb) {
this.client.scard(key, cb);
}
hgetall(key, cb) {
this.client.hgetall(key, cb);
}
hscan(key, cursor, options, cb) {
const args = [key, cursor];
if (options.MATCH)
args.push('MATCH', options.MATCH);
if (options.COUNT)
args.push('COUNT', String(options.COUNT));
this.client.hscan(...args, (err, reply) => {
if (err)
return cb(err);
const [cursor, items] = reply !== null && reply !== void 0 ? reply : [];
if (!cursor || !items)
return cb(new index_js_2.RedisClientError());
const result = {};
while (items.length) {
const key = String(items.shift());
result[key] = String(items.shift());
}
cb(null, { cursor, result });
});
}
hget(key, field, cb) {
this.client.hget(key, field, cb);
}
hset(key, field, value, cb) {
this.client.hset(key, field, value, cb);
}
hdel(key, fields, cb) {
const f = Array.isArray(fields) ? fields : [fields];
this.client.hdel(key, ...f, cb);
}
lrange(key, start, stop, cb) {
this.client.lrange(key, start, stop, cb);
}
hkeys(key, cb) {
this.client.hkeys(key, cb);
}
hlen(key, cb) {
this.client.hlen(key, cb);
}
brpoplpush(source, destination, timeout, cb) {
this.client.brpoplpush(source, destination, timeout, cb);
}
rpoplpush(source, destination, cb) {
this.client.rpoplpush(source, destination, cb);
}
zrangebyscorewithscores(source, min, max, cb) {
this.client.zrangebyscore(source, min, max, 'WITHSCORES', (err, reply) => {
if (err)
cb(err);
else {
const replyRange = reply !== null && reply !== void 0 ? reply : [];
const range = {};
for (let slice = replyRange.splice(0, 2); slice.length > 0; slice = replyRange.splice(0, 2)) {
const [member, score] = slice;
range[score] = member;
}
cb(null, range);
}
});
}
rpop(key, cb) {
this.client.rpop(key, cb);
}
lrem(key, count, element, cb) {
this.client.lrem(key, count, element, cb);
}
publish(channel, message, cb) {
this.client.publish(channel, message, cb);
}
flushall(cb) {
this.client.flushall(cb);
}
loadScript(script, cb) {
this.client.script('LOAD', script, (err, data) => {
if (err)
return cb(err);
if (!data)
return cb(new index_js_1.CallbackEmptyReplyError());
if (typeof data !== 'string')
return cb(new index_js_2.RedisClientError());
cb(null, data);
});
}
evalsha(hash, args, cb) {
const arrArgs = Array.isArray(args)
? args.map((i) => String(i))
: [String(args)];
const [numKeys, ...keysAndArgs] = arrArgs;
this.client.evalsha(hash, numKeys, keysAndArgs, cb);
}
get(key, cb) {
this.client.get(key, cb);
}
del(key, cb) {
const keys = Array.isArray(key) ? key : [key];
this.client.del(...keys, cb);
}
llen(key, cb) {
this.client.llen(key, cb);
}
lmove(source, destination, from, to, cb) {
if (!this.validateRedisVersion(6, 2)) {
cb(new index_js_2.RedisClientError('Command not supported by your Redis server. Minimal required Redis server version is 6.2.0.'));
}
else {
if (from === 'LEFT' && to === 'RIGHT') {
this.client.lmove(source, destination, from, to, cb);
}
else if (from === 'RIGHT' && to === 'LEFT') {
this.client.lmove(source, destination, from, to, cb);
}
else {
cb(new index_js_2.RedisClientError('Invalid move direction. Use LEFT -> RIGHT or RIGHT -> LEFT.'));
}
}
}
zremrangebyscore(source, min, max, cb) {
this.client.zremrangebyscore(source, min, max, cb);
}
hmget(source, keys, cb) {
this.client.hmget(source, ...keys, cb);
}
halt(cb) {
if (!this.connectionClosed) {
this.client.once('end', cb);
this.end();
}
else
cb();
}
end() {
if (!this.connectionClosed) {
this.client.disconnect(false);
}
}
shutdown(cb = () => void 0) {
if (!this.connectionClosed) {
this.client.once('end', cb);
this.client.quit();
}
else
cb();
}
getInfo(cb) {
this.client.info(cb);
}
on(event, listener) {
this.client.on(event, listener);
return this;
}
}
exports.IoredisClient = IoredisClient;
//# sourceMappingURL=ioredis-client.js.map