UNPKG

phecda-server

Version:

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

102 lines (92 loc) 3.84 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/nats/bind.ts var _nats = require('nats'); var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var debug = _debug2.default.call(void 0, "phecda-server/nats"); async function bind(nc, { moduleMap, meta }, opts = {}) { const { globalGuards, globalFilter, globalPipe, globalAddons = [], defaultQueue } = opts; const sc = _nats.StringCodec.call(void 0, ); const subscriptionMap = {}; 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', _2 => _2.queue]) !== void 0) { debug(`register method "${method}" in module "${tag}"`); return true; } }); _chunkHKN3AAB2js.detectAopDep.call(void 0, meta, { addons: globalAddons, guards: globalGuards }, "rpc"); _chunkHKN3AAB2js.Context.applyAddons(globalAddons, nc, "nats"); 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); subscriptionMap[queue] = nc.subscribe(queue, { queue, callback: handleRequest }); } } } } _chunk4LLLQOMFjs.__name.call(void 0, subscribeQueues, "subscribeQueues"); async function handleRequest(_, msg) { const data = JSON.parse(sc.decode(msg.data)); const { tag, method, id, _ps, args } = data; if (_ps !== 1) return; debug(`invoke method "${method}" in module "${tag}"`); const meta2 = metaMap.get(tag)[method]; const { data: { rpc: { isEvent } = {} } } = meta2; if (isEvent) msg.respond("{}"); const aop = _chunkHKN3AAB2js.Context.getAop(meta2, { globalFilter, globalGuards, globalPipe }); const context = new (0, _chunkHKN3AAB2js.Context)({ type: "nats", category: "rpc", moduleMap, meta: meta2, tag, method, args, id, msg, isEvent, // @ts-expect-error nats ts problem queue: msg._msg.subject.toString() }); await context.run(aop, (returnData) => { if (!isEvent) msg.respond(sc.encode(JSON.stringify({ data: returnData, id }))); }, (err) => { if (!isEvent) msg.respond(sc.encode(JSON.stringify({ data: err, error: true, id }))); }); } _chunk4LLLQOMFjs.__name.call(void 0, handleRequest, "handleRequest"); subscribeQueues(); _chunk4LLLQOMFjs.HMR.call(void 0, async () => { for (const i in subscriptionMap) subscriptionMap[i].unsubscribe(); await subscribeQueues(); }); } _chunk4LLLQOMFjs.__name.call(void 0, bind, "bind"); exports.bind = bind;