phecda-server
Version:
server framework that provide IOC/type-reuse/http&rpc-adaptor
119 lines (117 loc) • 3.17 kB
JavaScript
import {
Context,
createControllerMetaMap,
detectAopDep
} from "../../chunk-WST6E6MQ.mjs";
import {
__name
} from "../../chunk-URKOYTBU.mjs";
// src/rpc/kafka/bind.ts
import Debug from "debug";
var debug = Debug("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 = createControllerMetaMap(meta, (meta2) => {
const { controller, rpc, method, tag } = meta2.data;
if (controller === "rpc" && rpc?.queue !== void 0) {
debug(`register method "${method}" in module "${tag}"`);
return true;
}
});
detectAopDep(meta, {
guards: globalGuards,
addons: globalAddons
}, "rpc");
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
});
}
}
}
}
__name(subscribeQueues, "subscribeQueues");
await subscribeQueues();
await consumer.run({
eachMessage: /* @__PURE__ */ __name(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 = Context.getAop(meta2, {
globalFilter,
globalGuards,
globalPipe
});
const context = new 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")
});
}
__name(bind, "bind");
export {
bind
};