UNPKG

fastify-rabbitmq

Version:

A Fastify RabbitMQ Plugin Developed in Pure TypeScript.

139 lines (138 loc) 5.7 kB
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); //#region \0rolldown/runtime.js var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); //#endregion let fastify_plugin = require("fastify-plugin"); fastify_plugin = __toESM(fastify_plugin); let rabbitmq_client = require("rabbitmq-client"); let _fastify_error = require("@fastify/error"); _fastify_error = __toESM(_fastify_error); //#region src/errors.ts const errors = { /** Error if there is an invalid option used during registration. */ FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS: (0, _fastify_error.default)("FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS", "Invalid options: %s"), /** Error if there is an setup error of the plugin itself. */ FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS: (0, _fastify_error.default)("FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS", "Setup error: %s"), /** If an invalid usage error was done, this error would pop up. */ FASTIFY_RABBIT_MQ_ERR_USAGE: (0, _fastify_error.default)("FASTIFY_RABBIT_MQ_ERR_USAGE", "Usage error: %s") }; //#endregion //#region src/validation.ts /** * Validate Options * * The plugin validates only the *shape* of `connection` -- that it is a * non-empty connection string or a `ConnectionOptions` object. Parsing the URL * and validating the broker options (hosts, TLS, reconnect, etc.) is delegated * to `rabbitmq-client`. The shape guard exists because `new Connection(...)` * accepts garbage (a number, an array, `null`, `{}`) without throwing and then * silently fails to connect at runtime; rejecting it here surfaces a clear * registration-time error instead. * @since 1.0.0 * @param options */ const validateOpts = async (options) => { const { connection } = options; if (connection === void 0) throw new errors.FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS("connection must be defined."); if (typeof connection === "string") { if (connection.length === 0) throw new errors.FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS("connection string must not be empty."); return; } if (!(typeof connection === "object" && connection !== null && !Array.isArray(connection))) throw new errors.FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS("connection must be a connection string or a ConnectionOptions object."); }; //#endregion //#region src/index.ts /** * How we talk with Fastify * @since 1.0.0 * @param fastify * @param options * @param connection */ const decorateFastifyInstance = (fastify, options, connection) => { const { namespace = "" } = options; if (namespace !== void 0 && namespace !== "") fastify.log.debug("[fastify-rabbitmq] Namespace Attempt: %s", namespace); if (namespace !== void 0 && namespace !== "") { if (fastify.rabbitmq === void 0) fastify.decorate("rabbitmq", Object.create(null)); if (fastify.rabbitmq[namespace] !== void 0) throw new errors.FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS(`Already registered with namespace: ${namespace}`); fastify.log.trace(`[fastify-rabbitmq] Decorate Fastify with Namespace: ${namespace}`); fastify.rabbitmq[namespace] = connection; } else if (fastify.rabbitmq !== void 0) throw new errors.FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS("Already registered."); if (fastify.rabbitmq === void 0) { fastify.log.trace("[fastify-rabbitmq] Decorate Fastify"); fastify.decorate("rabbitmq", connection); } }; /** * Main Function * @since 1.0.0 * @example * This is the basics on how to use this plugin: * ```js * app.register(fastifyRabbit, { * connection: 'amqp://guest:guest@localhost' * }) * ``` * This will allow you to read from your Fastify "object" and * use this plugin at the "rabbitmq" level. From there you can execute and maintain * the RabbitMQ Connection using the 'rabbitmq-client' package, which is wrapping around * this plugin to execute functions it provides. * * @see [https://cody-greene.github.io/node-rabbitmq-client/latest/index.html](https://cody-greene.github.io/node-rabbitmq-client/latest/index.html) * */ const fastifyRabbit = (0, fastify_plugin.default)(async (fastify, opts) => { await validateOpts(opts); const { connection } = opts; decorateFastifyInstance(fastify, opts, new rabbitmq_client.Connection(connection)); }); //#endregion Object.defineProperty(exports, "AMQPChannelError", { enumerable: true, get: function() { return rabbitmq_client.AMQPChannelError; } }); Object.defineProperty(exports, "AMQPConnectionError", { enumerable: true, get: function() { return rabbitmq_client.AMQPConnectionError; } }); Object.defineProperty(exports, "AMQPError", { enumerable: true, get: function() { return rabbitmq_client.AMQPError; } }); Object.defineProperty(exports, "ConsumerStatus", { enumerable: true, get: function() { return rabbitmq_client.ConsumerStatus; } }); exports.decorateFastifyInstance = decorateFastifyInstance; exports.default = fastifyRabbit; //# sourceMappingURL=index.cjs.map