phecda-server
Version:
server framework that provide IOC/type-reuse/http&rpc-adaptor
72 lines (70 loc) • 1.77 kB
JavaScript
import {
Context,
createControllerMetaMap,
detectAopDep
} from "../../chunk-LNL3MVTR.mjs";
import {
__name
} from "../../chunk-ITDH2YDL.mjs";
// src/rpc/web-ext/bind.ts
import Debug from "debug";
var debug = Debug("phecda-server/electron");
function bind({ 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");
chrome.runtime.onMessage.addListener(callback);
async function callback(data, sender, sendResponse) {
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: "browser-extension",
category: "rpc",
moduleMap,
meta: meta2,
tag,
method,
args,
id,
isEvent,
queue: tag,
sender
});
return await context.run(aop, (returnData) => {
if (!isEvent) sendResponse({
data: returnData,
id
});
}, (err) => {
if (!isEvent) {
sendResponse({
data: err,
error: true,
id
});
}
});
}
__name(callback, "callback");
}
__name(bind, "bind");
export {
bind
};