UNPKG

@golevelup/nestjs-rabbitmq

Version:
49 lines 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.assertRabbitMqUri = exports.matchesRoutingKey = void 0; function matchesRoutingKey(routingKey, pattern) { // An empty string is a valid pattern therefore // we should only exclude null values and empty array if (pattern === undefined || (Array.isArray(pattern) && pattern.length === 0)) return false; const patterns = Array.isArray(pattern) ? pattern : [pattern]; for (const p of patterns) { if (routingKey === p) return true; const splitKey = routingKey.split('.'); const splitPattern = p.split('.'); let starFailed = false; for (let i = 0; i < splitPattern.length; i++) { if (splitPattern[i] === '#') return true; if (splitPattern[i] !== '*' && splitPattern[i] !== splitKey[i]) { starFailed = true; break; } } if (!starFailed && splitKey.length === splitPattern.length) return true; } return false; } exports.matchesRoutingKey = matchesRoutingKey; /** * Validates a rabbitmq uri * @see https://www.rabbitmq.com/docs/uri-spec#the-amqps-uri-scheme * @param uri * @returns */ const assertRabbitMqUri = (uri) => { if (Array.isArray(uri)) { for (const u of uri) { (0, exports.assertRabbitMqUri)(u); } return; } const rmqUri = new URL(uri); if (!rmqUri.protocol.startsWith('amqp')) { throw new Error('RabbitMQ URI protocol must start with amqp or amqps'); } }; exports.assertRabbitMqUri = assertRabbitMqUri; //# sourceMappingURL=utils.js.map