mirror of https://github.com/certd/certd
chore:
parent
4bb0918e27
commit
7545194f97
|
@ -9,7 +9,9 @@ import { merge } from "lodash-es";
|
||||||
import { accessRegistry, pluginRegistry } from "@certd/pipeline";
|
import { accessRegistry, pluginRegistry } from "@certd/pipeline";
|
||||||
import { dnsProviderRegistry } from "@certd/plugin-cert";
|
import { dnsProviderRegistry } from "@certd/plugin-cert";
|
||||||
import { logger } from "@certd/basic";
|
import { logger } from "@certd/basic";
|
||||||
import yaml from 'js-yaml'
|
import yaml from "js-yaml";
|
||||||
|
|
||||||
|
|
||||||
@Provide()
|
@Provide()
|
||||||
@Scope(ScopeEnum.Request, { allowDowngrade: true })
|
@Scope(ScopeEnum.Request, { allowDowngrade: true })
|
||||||
export class PluginService extends BaseService<PluginEntity> {
|
export class PluginService extends BaseService<PluginEntity> {
|
||||||
|
@ -27,7 +29,7 @@ export class PluginService extends BaseService<PluginEntity> {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
async page(pageReq: PageReq<PluginEntity>) {
|
async page(pageReq: PageReq<PluginEntity>) {
|
||||||
|
|
||||||
if(pageReq.query.type && pageReq.query.type !=='builtIn'){
|
if (pageReq.query.type && pageReq.query.type !== "builtIn") {
|
||||||
return await super.page(pageReq);
|
return await super.page(pageReq);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,15 +143,22 @@ export class PluginService extends BaseService<PluginEntity> {
|
||||||
return this.builtInPluginService.getByType(type);
|
return this.builtInPluginService.getByType(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPluginTarget(pluginName: string){
|
async compile(code: string) {
|
||||||
|
const ts = await import("typescript")
|
||||||
|
return ts.transpileModule(code, {
|
||||||
|
compilerOptions: { module: ts.ModuleKind.ESNext }
|
||||||
|
}).outputText;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getPluginTarget(pluginName: string) {
|
||||||
//获取插件类实例对象
|
//获取插件类实例对象
|
||||||
let author = undefined;
|
let author = undefined;
|
||||||
let name = '';
|
let name = "";
|
||||||
if(pluginName.includes('/')){
|
if (pluginName.includes("/")) {
|
||||||
const arr = pluginName.split('/');
|
const arr = pluginName.split("/");
|
||||||
author = arr[0];
|
author = arr[0];
|
||||||
name = arr[1];
|
name = arr[1];
|
||||||
}else {
|
} else {
|
||||||
name = pluginName;
|
name = pluginName;
|
||||||
}
|
}
|
||||||
const info = await this.find({
|
const info = await this.find({
|
||||||
|
@ -158,58 +167,69 @@ export class PluginService extends BaseService<PluginEntity> {
|
||||||
author: author
|
author: author
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (info&&info.length > 0) {
|
if (info && info.length > 0) {
|
||||||
const plugin = info[0];
|
const plugin = info[0];
|
||||||
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor;
|
|
||||||
const getPluginClass = new AsyncFunction(plugin.content);
|
try{
|
||||||
const pluginClass = await getPluginClass({logger: logger});
|
const AsyncFunction = Object.getPrototypeOf(async () => {
|
||||||
return new pluginClass()
|
}).constructor;
|
||||||
|
// const script = await this.compile(plugin.content);
|
||||||
|
const script = plugin.content
|
||||||
|
const getPluginClass = new AsyncFunction(script);
|
||||||
|
const pluginClass = await getPluginClass({ logger: logger });
|
||||||
|
return new pluginClass();
|
||||||
|
}catch (e) {
|
||||||
|
logger.error("实例化插件失败:",e)
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
throw new Error(`插件${pluginName}不存在`);
|
throw new Error(`插件${pluginName}不存在`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从数据库加载插件
|
* 从数据库加载插件
|
||||||
*/
|
*/
|
||||||
async registerFromDb() {
|
async registerFromDb() {
|
||||||
const res = await this.list({
|
const res = await this.list({
|
||||||
buildQuery: ((bq) => {
|
buildQuery: ((bq) => {
|
||||||
bq.andWhere( "type != :type", {
|
bq.andWhere("type != :type", {
|
||||||
type: 'builtIn'
|
type: "builtIn"
|
||||||
})
|
});
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const item of res) {
|
for (const item of res) {
|
||||||
await this.registerPlugin(item);
|
await this.registerPlugin(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async registerPlugin(plugin:PluginEntity){
|
async registerPlugin(plugin: PluginEntity) {
|
||||||
const metadata = yaml.load(plugin.metadata);
|
const metadata = yaml.load(plugin.metadata);
|
||||||
const item = {
|
const item = {
|
||||||
...plugin,
|
...plugin,
|
||||||
...metadata
|
...metadata
|
||||||
}
|
};
|
||||||
delete item.metadata;
|
delete item.metadata;
|
||||||
delete item.content;
|
delete item.content;
|
||||||
if(item.author){
|
if (item.author) {
|
||||||
item.name = item.author +"/"+ item.name;
|
item.name = item.author + "/" + item.name;
|
||||||
}
|
}
|
||||||
let registry = null
|
let registry = null;
|
||||||
if(item.pluginType === 'access'){
|
if (item.pluginType === "access") {
|
||||||
registry = accessRegistry;
|
registry = accessRegistry;
|
||||||
}else if (item.pluginType === 'plugin'){
|
} else if (item.pluginType === "plugin") {
|
||||||
registry = pluginRegistry;
|
registry = pluginRegistry;
|
||||||
}else if (item.pluginType === 'dnsProvider'){
|
} else if (item.pluginType === "dnsProvider") {
|
||||||
registry = dnsProviderRegistry
|
registry = dnsProviderRegistry;
|
||||||
}else {
|
} else {
|
||||||
logger.warn(`插件${item.name}类型错误:${item.pluginType}`)
|
logger.warn(`插件${item.name}类型错误:${item.pluginType}`);
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
registry.register(item.name, {
|
registry.register(item.name, {
|
||||||
define:item,
|
define: item,
|
||||||
target: ()=>{
|
target: () => {
|
||||||
return this.getPluginTarget(item.name);
|
return this.getPluginTarget(item.name);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue