Files
certd/packages/ui/certd-server/src/modules/plugin/service/plugin-service.ts
T

277 lines
6.9 KiB
TypeScript
Raw Normal View History

2025-04-06 00:20:05 +08:00
import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
import { BaseService, PageReq } from "@certd/lib-server";
import { PluginEntity } from "../entity/plugin.js";
import { InjectEntityModel } from "@midwayjs/typeorm";
import { Repository } from "typeorm";
import { isComm } from "@certd/plus-core";
import { BuiltInPluginService } from "../../pipeline/service/builtin-plugin-service.js";
import { merge } from "lodash-es";
import { accessRegistry, pluginRegistry } from "@certd/pipeline";
import { dnsProviderRegistry } from "@certd/plugin-cert";
import { logger } from "@certd/basic";
2025-04-09 00:00:53 +08:00
import yaml from "js-yaml";
2025-04-10 00:22:05 +08:00
import { getDefaultAccessPlugin, getDefaultDeployPlugin, getDefaultDnsPlugin } from "./default-plugin.js";
2025-04-09 00:00:53 +08:00
2024-10-13 01:27:08 +08:00
@Provide()
@Scope(ScopeEnum.Request, { allowDowngrade: true })
2024-10-13 01:27:08 +08:00
export class PluginService extends BaseService<PluginEntity> {
@InjectEntityModel(PluginEntity)
repository: Repository<PluginEntity>;
@Inject()
builtInPluginService: BuiltInPluginService;
2024-10-13 01:27:08 +08:00
//@ts-ignore
getRepository() {
return this.repository;
}
2024-10-14 00:19:55 +08:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async page(pageReq: PageReq<PluginEntity>) {
2025-04-06 18:06:21 +08:00
2025-04-09 00:00:53 +08:00
if (pageReq.query.type && pageReq.query.type !== "builtIn") {
2025-04-06 18:06:21 +08:00
return await super.page(pageReq);
}
2024-10-14 00:19:55 +08:00
const builtInList = await this.getBuiltInEntityList();
return {
records: builtInList,
total: builtInList.length,
offset: 0,
2025-04-06 00:20:05 +08:00
limit: 99999
2024-10-14 00:19:55 +08:00
};
}
async getEnabledBuildInGroup() {
const groups = this.builtInPluginService.getGroups();
2024-12-03 00:35:34 +08:00
for (const key in groups) {
const group = groups[key];
group.plugins.forEach(item => {
delete item.input;
});
}
2024-10-14 00:19:55 +08:00
if (!isComm()) {
return groups;
}
const list = await this.list({
query: {
2025-04-06 00:20:05 +08:00
type: "builtIn",
disabled: true
}
2024-10-14 00:19:55 +08:00
});
const disabledNames = list.map(it => it.name);
for (const key in groups) {
const group = groups[key];
if (!group.plugins) {
continue;
}
group.plugins = group.plugins.filter(it => !disabledNames.includes(it.name));
}
return groups;
}
2025-04-06 00:20:05 +08:00
2024-10-14 00:19:55 +08:00
async getEnabledBuiltInList(): Promise<any> {
const builtInList = this.builtInPluginService.getList();
if (!isComm()) {
return builtInList;
}
const list = await this.list({
query: {
2025-04-06 00:20:05 +08:00
type: "builtIn",
disabled: true
}
2024-10-14 00:19:55 +08:00
});
const disabledNames = list.map(it => it.name);
return builtInList.filter(it =>{
return !disabledNames.includes(it.name)
});
2024-10-14 00:19:55 +08:00
}
async getBuiltInEntityList() {
const builtInList = this.builtInPluginService.getList();
const list = await this.list({
query: {
2025-04-06 00:20:05 +08:00
type: "builtIn"
}
2024-10-14 00:19:55 +08:00
});
const records: PluginEntity[] = [];
for (const item of builtInList) {
let record = list.find(it => it.name === item.name);
if (!record) {
record = new PluginEntity();
record.disabled = false;
}
merge(record, {
name: item.name,
title: item.title,
2025-04-06 00:20:05 +08:00
type: "builtIn",
2024-10-14 00:19:55 +08:00
icon: item.icon,
desc: item.desc,
2025-04-06 00:20:05 +08:00
group: item.group
2024-10-14 00:19:55 +08:00
});
records.push(record);
}
return records;
}
2024-10-14 10:27:11 +08:00
async setDisabled(opts: { id?: number; name?: string; type: string; disabled: boolean }) {
2024-10-14 00:19:55 +08:00
const { id, name, type, disabled } = opts;
if (!type) {
2025-04-06 00:20:05 +08:00
throw new Error("参数错误: type 不能为空");
2024-10-14 00:19:55 +08:00
}
if (id > 0) {
//update
await this.repository.update({ id }, { disabled });
return;
}
2025-04-06 00:20:05 +08:00
if (name && type === "builtIn") {
2024-10-14 00:19:55 +08:00
const pluginEntity = new PluginEntity();
pluginEntity.name = name;
pluginEntity.type = type;
pluginEntity.disabled = disabled;
await this.repository.save(pluginEntity);
return;
}
2025-04-06 00:20:05 +08:00
throw new Error("参数错误: id 和 name 必须有一个");
2024-10-13 01:27:08 +08:00
}
2024-12-03 00:55:37 +08:00
async getDefineByType(type: string) {
return this.builtInPluginService.getByType(type);
}
2025-04-06 00:20:05 +08:00
2025-04-10 00:22:05 +08:00
/**
* 新增
* @param param 数据
*/
async add(param: any) {
const old = await this.repository.findOne({
where: {
name: param.name,
author: param.author
}
})
if (old) {
throw new Error(`插件${param.author}/${param.name}已存在`);
}
let plugin:any = {}
if (param.pluginType === "access") {
plugin = getDefaultAccessPlugin()
delete param.group
2025-04-10 00:22:05 +08:00
}else if (param.pluginType === "deploy") {
plugin = getDefaultDeployPlugin()
}else if (param.pluginType === "dnsProvider") {
plugin = getDefaultDnsPlugin()
delete param.group
2025-04-10 00:22:05 +08:00
}else{
throw new Error(`插件类型${param.pluginType}不支持`);
}
return await super.add({
...param,
...plugin
});
}
2025-04-09 00:00:53 +08:00
async compile(code: string) {
const ts = await import("typescript")
return ts.transpileModule(code, {
compilerOptions: { module: ts.ModuleKind.ESNext }
}).outputText;
}
async getPluginTarget(pluginName: string) {
2025-04-06 00:20:05 +08:00
//获取插件类实例对象
let author = undefined;
2025-04-09 00:00:53 +08:00
let name = "";
if (pluginName.includes("/")) {
const arr = pluginName.split("/");
2025-04-06 00:20:05 +08:00
author = arr[0];
name = arr[1];
2025-04-09 00:00:53 +08:00
} else {
2025-04-06 00:20:05 +08:00
name = pluginName;
}
const info = await this.find({
where: {
name: name,
author: author
}
});
2025-04-09 00:00:53 +08:00
if (info && info.length > 0) {
2025-04-06 00:20:05 +08:00
const plugin = info[0];
2025-04-09 00:00:53 +08:00
try{
const AsyncFunction = Object.getPrototypeOf(async () => {
}).constructor;
// const script = await this.compile(plugin.content);
const script = plugin.content
const getPluginClass = new AsyncFunction(script);
return await getPluginClass({ logger: logger });
2025-04-09 00:00:53 +08:00
}catch (e) {
logger.error("编译插件失败:",e)
2025-04-09 00:00:53 +08:00
throw e
}
2025-04-06 00:20:05 +08:00
}
2025-04-08 22:56:38 +08:00
throw new Error(`插件${pluginName}不存在`);
2025-04-06 00:20:05 +08:00
}
2025-04-09 00:00:53 +08:00
2025-04-06 00:20:05 +08:00
/**
* 从数据库加载插件
*/
async registerFromDb() {
const res = await this.list({
buildQuery: ((bq) => {
2025-04-09 00:00:53 +08:00
bq.andWhere("type != :type", {
type: "builtIn"
});
2025-04-06 00:20:05 +08:00
})
});
for (const item of res) {
2025-04-09 00:00:53 +08:00
await this.registerPlugin(item);
2025-04-08 22:56:38 +08:00
}
}
2025-04-06 00:20:05 +08:00
2025-04-09 00:00:53 +08:00
async registerPlugin(plugin: PluginEntity) {
2025-04-08 22:56:38 +08:00
const metadata = yaml.load(plugin.metadata);
const item = {
...plugin,
...metadata
2025-04-09 00:00:53 +08:00
};
2025-04-08 22:56:38 +08:00
delete item.metadata;
delete item.content;
2025-04-09 00:00:53 +08:00
if (item.author) {
item.name = item.author + "/" + item.name;
2025-04-08 22:56:38 +08:00
}
2025-04-09 00:00:53 +08:00
let registry = null;
if (item.pluginType === "access") {
2025-04-08 22:56:38 +08:00
registry = accessRegistry;
} else if (item.pluginType === "deploy") {
2025-04-08 22:56:38 +08:00
registry = pluginRegistry;
2025-04-09 00:00:53 +08:00
} else if (item.pluginType === "dnsProvider") {
registry = dnsProviderRegistry;
} else {
logger.warn(`插件${item.name}类型错误:${item.pluginType}`);
return;
2025-04-08 22:56:38 +08:00
}
registry.register(item.name, {
2025-04-09 00:00:53 +08:00
define: item,
target: async () => {
return await this.getPluginTarget(item.name);
2025-04-08 22:56:38 +08:00
}
});
2025-04-06 00:20:05 +08:00
}
2025-04-08 22:56:38 +08:00
2024-10-13 01:27:08 +08:00
}