@golevelup/nestjs-rabbitmq
Version:
Badass RabbitMQ addons for NestJS
68 lines • 2.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.converUriConfigObjectsToUris = exports.validateRabbitMqUris = 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 validateRabbitMqUris = (uri) => {
for (const u of uri) {
const rmqUri = new URL(u);
if (!rmqUri.protocol.startsWith('amqp')) {
throw new Error('RabbitMQ URI protocol must start with amqp or amqps');
}
}
};
exports.validateRabbitMqUris = validateRabbitMqUris;
const converUriConfigObjectsToUris = (uri) => {
const uris = [uri].flat();
return uris.map((u) => {
if (typeof u == 'string')
return u;
return amqplibUriConfigToUrl(u);
});
};
exports.converUriConfigObjectsToUris = converUriConfigObjectsToUris;
const amqplibUriConfigToUrl = ({ hostname, username, password, frameMax, heartbeat, vhost, protocol = 'amqp', port = 5672, }) => {
if (!hostname) {
throw new Error("Configuration object must contain a 'hostname' key.");
}
const auth = username && password
? `${encodeURIComponent(username)}:${encodeURIComponent(password)}@`
: '';
const params = new URLSearchParams();
if (frameMax)
params.set('frameMax', frameMax.toString());
if (heartbeat)
params.set('heartbeat', heartbeat.toString());
return `${protocol}://${auth}${hostname}:${port}${vhost !== null && vhost !== void 0 ? vhost : ''}${params.size == 0 ? '' : `?${params.toString()}`}`;
};
//# sourceMappingURL=utils.js.map