UNPKG

phecda-server

Version:

server framework that provide IOC/type-reuse/http&rpc-adaptor

119 lines (110 loc) 4.15 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _chunkHKN3AAB2js = require('../../chunk-HKN3AAB2.js'); var _chunk4LLLQOMFjs = require('../../chunk-4LLLQOMF.js'); // src/rpc/kafka/bind.ts var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var debug = _debug2.default.call(void 0, "phecda-server/kafka"); async function bind({ consumer, producer }, { moduleMap, meta }, opts = {}) { const { globalGuards, globalFilter, globalPipe, globalAddons = [], defaultQueue } = opts; const existQueue = /* @__PURE__ */ new Set(); const metaMap = _chunkHKN3AAB2js.createControllerMetaMap.call(void 0, meta, (meta2) => { const { controller, rpc, method, tag } = meta2.data; if (controller === "rpc" && _optionalChain([rpc, 'optionalAccess', _ => _.queue]) !== void 0) { debug(`register method "${method}" in module "${tag}"`); return true; } }); _chunkHKN3AAB2js.detectAopDep.call(void 0, meta, { guards: globalGuards, addons: globalAddons }, "rpc"); _chunkHKN3AAB2js.Context.applyAddons(globalAddons, { consumer, producer }, "kafka"); async function subscribeQueues() { existQueue.clear(); for (const [tag, record] of metaMap) { for (const method in record) { const meta2 = metaMap.get(tag)[method]; const { data: { rpc } } = meta2; if (rpc) { const queue = rpc.queue || defaultQueue || tag; if (existQueue.has(queue)) continue; existQueue.add(queue); await consumer.subscribe({ topic: queue, fromBeginning: true }); } } } } _chunk4LLLQOMFjs.__name.call(void 0, subscribeQueues, "subscribeQueues"); await subscribeQueues(); await consumer.run({ eachMessage: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, async ({ message, partition, topic, heartbeat, pause }) => { if (!existQueue.has(topic)) return; const data = JSON.parse(message.value.toString()); const { tag, method, id, queue: clientQueue, _ps, args } = data; if (_ps !== 1) return; debug(`invoke method "${method}" in module "${tag}"`); const meta2 = metaMap.get(tag)[method]; const { data: { rpc } } = meta2; const isEvent = rpc.isEvent; const aop = _chunkHKN3AAB2js.Context.getAop(meta2, { globalFilter, globalGuards, globalPipe }); const context = new (0, _chunkHKN3AAB2js.Context)({ type: "kafka", category: "rpc", moduleMap, meta: meta2, args, id, tag, method, partition, topic, heartbeat, pause, isEvent, queue: topic }); await context.run(aop, (returnData) => { if (!isEvent) { producer.send({ topic: clientQueue, messages: [ { value: JSON.stringify({ data: returnData, id }) } ] }); } }, (err) => { if (!isEvent) { producer.send({ topic: clientQueue, messages: [ { value: JSON.stringify({ data: err, error: true, id }) } ] }); } }); }, "eachMessage") }); } _chunk4LLLQOMFjs.__name.call(void 0, bind, "bind"); exports.bind = bind;