phecda-server
Version:
server framework that provide IOC/type-reuse/http&rpc-adaptor
74 lines (72 loc) • 1.86 kB
JavaScript
import {
Context,
createControllerMetaMap,
detectAopDep
} from "../../chunk-WST6E6MQ.mjs";
import {
__name
} from "../../chunk-URKOYTBU.mjs";
// src/rpc/ws/bind.ts
import Debug from "debug";
var debug = Debug("phecda-server/ws");
function bind(wss, data, opts = {}) {
const { globalGuards, globalAddons, globalFilter, globalPipe } = opts;
const { moduleMap, meta } = data;
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");
wss.on("connection", (ws) => {
ws.on("message", async (raw) => {
try {
const data2 = JSON.parse(raw.toString());
const { method, id, tag, _ps, args, queue } = data2;
if (_ps !== 1) return;
const meta2 = metaMap.get(tag)[method];
const { data: { rpc: { isEvent } = {} } } = meta2;
const aop = Context.getAop(meta2, {
globalFilter,
globalGuards,
globalPipe
});
const context = new Context({
type: "ws",
category: "rpc",
meta: meta2,
moduleMap,
tag,
method,
args,
id,
queue,
wss,
ws
});
return await context.run(aop, (returnData) => {
if (!isEvent) ws.send(JSON.stringify({
id,
data: returnData
}));
}, (err) => {
if (!isEvent) ws.send(JSON.stringify({
id,
data: err,
error: true
}));
});
} catch (e) {
}
});
});
}
__name(bind, "bind");
export {
bind
};