UNPKG

redis-smq

Version:

A simple high-performance Redis message queue for Node.js.

58 lines 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TopicExchange = void 0; const exchange_1 = require("./exchange"); const types_1 = require("../../../types"); const redis_smq_common_1 = require("redis-smq-common"); const queue_1 = require("../queue-manager/queue"); const redis_keys_1 = require("../../common/redis-keys/redis-keys"); const invalid_exchange_data_error_1 = require("./errors/invalid-exchange-data.error"); class TopicExchange extends exchange_1.Exchange { constructor(topic) { super(topic, types_1.EExchangeType.TOPIC); } validateBindingParams(topicParams) { return typeof topicParams === 'string' ? redis_keys_1.redisKeys.validateRedisKey(topicParams) : { topic: redis_keys_1.redisKeys.validateRedisKey(topicParams.topic), ns: redis_keys_1.redisKeys.validateNamespace(topicParams.ns), }; } getTopicParams(config) { if (typeof this.bindingParams === 'string') { return { topic: this.bindingParams, ns: config.namespace, }; } return this.bindingParams; } matchQueues(config, queues, cb) { const topicParams = this.getTopicParams(config); const matched = []; const regExp = new RegExp(topicParams.topic); redis_smq_common_1.async.eachOf(queues, (queue, index, done) => { if (queue.ns === topicParams.ns && regExp.test(queue.name)) matched.push(queue); done(); }, (err) => cb(err, matched)); } getQueues(redisClient, config, cb) { queue_1.Queue.list(redisClient, (err, reply) => { if (err) cb(err); else this.matchQueues(config, reply !== null && reply !== void 0 ? reply : [], cb); }); } static fromJSON(json) { if (!json.bindingParams || json.type !== types_1.EExchangeType.TOPIC) throw new invalid_exchange_data_error_1.InvalidExchangeDataError(); const e = new TopicExchange(json.bindingParams); e.fromJSON(json); return e; } } exports.TopicExchange = TopicExchange; //# sourceMappingURL=topic-exchange.js.map