phecda-server
Version:
server framework that provide IOC/type-reuse/http&rpc-adaptor
73 lines (71 loc) • 1.78 kB
JavaScript
import {
Context,
createControllerMetaMap,
detectAopDep
} from "../../chunk-WST6E6MQ.mjs";
import {
__name
} from "../../chunk-URKOYTBU.mjs";
// src/rpc/electron/bind.ts
import Debug from "debug";
var debug = Debug("phecda-server/electron");
function bind(IPC, { moduleMap, meta }, opts = {}) {
const { globalGuards, globalFilter, globalPipe, globalAddons = [] } = opts;
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");
IPC.handle("phecda-server:invoke", callback);
IPC.on("phecda-server:send", callback);
async function callback(event, data) {
const { method, id, tag, _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 = Context.getAop(meta2, {
globalFilter,
globalGuards,
globalPipe
});
const context = new Context({
type: "electron",
category: "rpc",
moduleMap,
meta: meta2,
tag,
method,
args,
id,
isEvent,
queue: tag,
event
});
return await context.run(aop, (returnData) => {
if (!isEvent) return {
data: returnData,
id
};
}, (err) => {
if (!isEvent) {
return {
data: err,
error: true,
id
};
}
});
}
__name(callback, "callback");
}
__name(bind, "bind");
export {
bind
};