UNPKG

phecda-server

Version:

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

103 lines (93 loc) 3.71 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 _chunk76UDKZOJjs = require('../../chunk-76UDKZOJ.js'); var _chunkLLF55NZPjs = require('../../chunk-LLF55NZP.js'); // src/rpc/redis/bind.ts var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var debug = _debug2.default.call(void 0, "phecda-server/redis"); function bind({ sub, pub }, { moduleMap, meta }, opts = {}) { const { globalGuards, globalFilter, globalPipe, globalAddons = [], defaultQueue } = opts; const metaMap = _chunk76UDKZOJjs.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; } }); _chunk76UDKZOJjs.detectAopDep.call(void 0, meta, { guards: globalGuards, addons: globalAddons }, "rpc"); const existQueue = /* @__PURE__ */ new Set(); _chunk76UDKZOJjs.Context.applyAddons(globalAddons, { pub, sub }, "redis"); 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 sub.subscribe(queue); } } } } _chunkLLF55NZPjs.__name.call(void 0, subscribeQueues, "subscribeQueues"); sub.on("message", async (channel, msg) => { if (!existQueue.has(channel)) return; if (msg) { const data = JSON.parse(msg); const { method, id, tag, queue: clientQueue, _ps, args } = data; debug(`invoke method "${method}" in module "${tag}"`); if (_ps !== 1) return; const meta2 = metaMap.get(tag)[method]; const { data: { rpc: { isEvent } = {} } } = meta2; const aop = _chunk76UDKZOJjs.Context.getAop(meta2, { globalFilter, globalGuards, globalPipe }); const context = new (0, _chunk76UDKZOJjs.Context)({ type: "redis", category: "rpc", moduleMap, redis: sub, meta: meta2, msg, channel, tag, method, args, id, isEvent, queue: channel }); await context.run(aop, (returnData) => { if (!isEvent) pub.publish(clientQueue, JSON.stringify({ data: returnData, id })); }, (err) => { if (!isEvent) { pub.publish(clientQueue, JSON.stringify({ data: err, error: true, id })); } }); } }); subscribeQueues(); _chunkLLF55NZPjs.HMR.call(void 0, async () => { for (const queue of existQueue) await sub.unsubscribe(queue); subscribeQueues(); }); } _chunkLLF55NZPjs.__name.call(void 0, bind, "bind"); exports.bind = bind;