perf: 自定义插件支持使用_ctx.import("/@/xxx.js")以绝对路径引用模块

This commit is contained in:
xiaojunnuo
2026-01-16 00:46:26 +08:00
parent 2fbb58eb2b
commit 9eace86aee
6 changed files with 69 additions and 152 deletions
@@ -14,7 +14,7 @@ export class BuiltInPluginService {
continue; continue;
} }
//@ts-ignore //@ts-ignore
if(Plugin.define?.type && Plugin.define?.type !== 'builtin'){ if(Plugin.define?.type && Plugin.define?.type.toLowerCase() !== 'builtin'){
continue; continue;
} }
list.push({ ...Plugin.define, key }); list.push({ ...Plugin.define, key });
@@ -1,16 +1,16 @@
import {Inject, Provide, Scope, ScopeEnum} from "@midwayjs/core"; import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
import {addonRegistry, BaseService, PageReq} from "@certd/lib-server"; import { addonRegistry, BaseService, PageReq } from "@certd/lib-server";
import {PluginEntity} from "../entity/plugin.js"; import { PluginEntity } from "../entity/plugin.js";
import {InjectEntityModel} from "@midwayjs/typeorm"; import { InjectEntityModel } from "@midwayjs/typeorm";
import {IsNull, Not, Repository} from "typeorm"; import { IsNull, Not, Repository } from "typeorm";
import {isComm} from "@certd/plus-core"; import { isComm } from "@certd/plus-core";
import {BuiltInPluginService} from "../../pipeline/service/builtin-plugin-service.js"; import { BuiltInPluginService } from "../../pipeline/service/builtin-plugin-service.js";
import {merge} from "lodash-es"; import { merge } from "lodash-es";
import {accessRegistry, notificationRegistry, pluginRegistry} from "@certd/pipeline"; import { accessRegistry, notificationRegistry, 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";
import {getDefaultAccessPlugin, getDefaultDeployPlugin, getDefaultDnsPlugin} from "./default-plugin.js"; import { getDefaultAccessPlugin, getDefaultDeployPlugin, getDefaultDnsPlugin } from "./default-plugin.js";
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
@@ -19,8 +19,21 @@ export type PluginImportReq = {
override?: boolean; override?: boolean;
}; };
async function importer(modulePath: string) {
if (!modulePath) {
throw new Error("modules path 不能为空")
}
if (!modulePath.startsWith("/@/")) {
return await import(modulePath)
}
modulePath = modulePath.replace("/@/", "")
//替换@为相对地址
modulePath = `../../../${modulePath}`
return await import(modulePath)
}
@Provide() @Provide()
@Scope(ScopeEnum.Request, {allowDowngrade: true}) @Scope(ScopeEnum.Request, { allowDowngrade: true })
export class PluginService extends BaseService<PluginEntity> { export class PluginService extends BaseService<PluginEntity> {
@InjectEntityModel(PluginEntity) @InjectEntityModel(PluginEntity)
repository: Repository<PluginEntity>; repository: Repository<PluginEntity>;
@@ -57,7 +70,7 @@ export class PluginService extends BaseService<PluginEntity> {
}; };
} }
async getEnabledBuildInGroup(opts?:{isSimple?:boolean,withSetting?:boolean}) { async getEnabledBuildInGroup(opts?: { isSimple?: boolean, withSetting?: boolean }) {
const groups = this.builtInPluginService.getGroups(); const groups = this.builtInPluginService.getGroups();
if (opts?.isSimple) { if (opts?.isSimple) {
for (const key in groups) { for (const key in groups) {
@@ -75,17 +88,17 @@ export class PluginService extends BaseService<PluginEntity> {
// 初始化设置 // 初始化设置
const settingPlugins = await this.repository.find({ const settingPlugins = await this.repository.find({
select:{ select: {
id:true, id: true,
name:true, name: true,
sysSetting:true sysSetting: true
}, },
where: { where: {
sysSetting : Not(IsNull()) sysSetting: Not(IsNull())
} }
}) })
//合并插件配置 //合并插件配置
const pluginSettingMap:any = {} const pluginSettingMap: any = {}
for (const item of settingPlugins) { for (const item of settingPlugins) {
if (!item.sysSetting) { if (!item.sysSetting) {
continue; continue;
@@ -99,7 +112,7 @@ export class PluginService extends BaseService<PluginEntity> {
} }
for (const item of group.plugins) { for (const item of group.plugins) {
const pluginSetting = pluginSettingMap[item.name]; const pluginSetting = pluginSettingMap[item.name];
if (pluginSetting){ if (pluginSetting) {
item.sysSetting = pluginSetting item.sysSetting = pluginSetting
} }
} }
@@ -172,13 +185,13 @@ export class PluginService extends BaseService<PluginEntity> {
} }
async setDisabled(opts: { id?: number; name?: string; type: string; disabled: boolean }) { async setDisabled(opts: { id?: number; name?: string; type: string; disabled: boolean }) {
const {id, name, type, disabled} = opts; const { id, name, type, disabled } = opts;
if (!type) { if (!type) {
throw new Error("参数错误: type 不能为空"); throw new Error("参数错误: type 不能为空");
} }
if (id > 0) { if (id > 0) {
//update //update
await this.repository.update({id}, {disabled}); await this.repository.update({ id }, { disabled });
return; return;
} }
@@ -214,7 +227,7 @@ export class PluginService extends BaseService<PluginEntity> {
throw new Error(`插件${param.author}/${param.name}已存在`); throw new Error(`插件${param.author}/${param.name}已存在`);
} }
if (param.type === "builtIn"){ if (param.type === "builtIn") {
return await super.add({ return await super.add({
...param, ...param,
}); });
@@ -233,7 +246,7 @@ export class PluginService extends BaseService<PluginEntity> {
throw new Error(`插件类型${param.pluginType}不支持`); throw new Error(`插件类型${param.pluginType}不支持`);
} }
const res= await super.add({ const res = await super.add({
...param, ...param,
...plugin ...plugin
}); });
@@ -242,18 +255,18 @@ export class PluginService extends BaseService<PluginEntity> {
return res return res
} }
async registerById(id: any) { async registerById(id: any) {
const item = await this.info(id); const item = await this.info(id);
if (!item) { if (!item) {
return; return;
} }
if(item.type === "builtIn"){ if (item.type === "builtIn") {
return; return;
} }
await this.registerPlugin(item); await this.registerPlugin(item);
} }
async unRegisterById(id: any){ async unRegisterById(id: any) {
const item = await this.info(id); const item = await this.info(id);
if (!item) { if (!item) {
return; return;
@@ -262,19 +275,19 @@ export class PluginService extends BaseService<PluginEntity> {
return; return;
} }
let name = item.name; let name = item.name;
if (item.author && !item.name.startsWith( `${item.author}/`)){ if (item.author && !item.name.startsWith(`${item.author}/`)) {
name = `${item.author}/${item.name}` name = `${item.author}/${item.name}`
} }
if (item.pluginType === "access"){ if (item.pluginType === "access") {
accessRegistry.unRegister(name) accessRegistry.unRegister(name)
}else if (item.pluginType === "deploy"){ } else if (item.pluginType === "deploy") {
pluginRegistry.unRegister(name) pluginRegistry.unRegister(name)
}else if (item.pluginType === "dnsProvider"){ } else if (item.pluginType === "dnsProvider") {
dnsProviderRegistry.unRegister(name) dnsProviderRegistry.unRegister(name)
}else if (item.pluginType === "notification"){ } else if (item.pluginType === "notification") {
notificationRegistry.unRegister(name) notificationRegistry.unRegister(name)
}else{ } else {
logger.warn(`不支持的插件类型:${item.pluginType}`) logger.warn(`不支持的插件类型:${item.pluginType}`)
} }
} }
@@ -291,7 +304,7 @@ export class PluginService extends BaseService<PluginEntity> {
} }
const res= await super.update(param); const res = await super.update(param);
await this.registerById(param.id); await this.registerById(param.id);
return res return res
@@ -300,14 +313,14 @@ export class PluginService extends BaseService<PluginEntity> {
async compile(code: string) { async compile(code: string) {
const ts = await import("typescript"); const ts = await import("typescript");
return ts.transpileModule(code, { return ts.transpileModule(code, {
compilerOptions: {module: ts.ModuleKind.ESNext} compilerOptions: { module: ts.ModuleKind.ESNext }
}).outputText; }).outputText;
} }
private async getPluginClassFromFile(item: any) { private async getPluginClassFromFile(item: any) {
const scriptFilePath = item.scriptFilePath; const scriptFilePath = item.scriptFilePath;
const res = await import((`../../..${scriptFilePath}`)) const res = await import((`../../..${scriptFilePath}`))
const classNames = Object.keys(res) const classNames = Object.keys(res)
return res[classNames[classNames.length - 1]] return res[classNames[classNames.length - 1]]
} }
@@ -336,8 +349,8 @@ export class PluginService extends BaseService<PluginEntity> {
}).constructor; }).constructor;
// const script = await this.compile(plugin.content); // const script = await this.compile(plugin.content);
const script = plugin.content; const script = plugin.content;
const getPluginClass = new AsyncFunction(script); const getPluginClass = new AsyncFunction("_ctx", script);
return await getPluginClass({logger: logger}); return await getPluginClass({ logger: logger, import: importer });
} catch (e) { } catch (e) {
logger.error("编译插件失败:", e); logger.error("编译插件失败:", e);
throw e; throw e;
@@ -379,7 +392,7 @@ export class PluginService extends BaseService<PluginEntity> {
} }
//排序 //排序
list = list.sort((a, b) => { list = list.sort((a, b) => {
return (a.order??10) - (b.order ??10); return (a.order ?? 10) - (b.order ?? 10);
}); });
for (const item of list) { for (const item of list) {
@@ -398,11 +411,11 @@ export class PluginService extends BaseService<PluginEntity> {
delete item.metadata; delete item.metadata;
delete item.content; delete item.content;
delete item.extra; delete item.extra;
if (item.author) { if (item.author) {
item.name = item.author + "/" + item.name; item.name = item.author + "/" + item.name;
} }
let name = item.name let name = item.name
if(item.addonType){ if (item.addonType) {
name = item.addonType + ":" + name; name = item.addonType + ":" + name;
} }
let registry = null; let registry = null;
@@ -414,9 +427,9 @@ export class PluginService extends BaseService<PluginEntity> {
registry = dnsProviderRegistry; registry = dnsProviderRegistry;
} else if (item.pluginType === "notification") { } else if (item.pluginType === "notification") {
registry = notificationRegistry; registry = notificationRegistry;
}else if (item.pluginType === "addon") { } else if (item.pluginType === "addon") {
registry = addonRegistry; registry = addonRegistry;
} else { } else {
logger.warn(`插件${name}类型错误:${item.pluginType}`); logger.warn(`插件${name}类型错误:${item.pluginType}`);
return; return;
} }
@@ -495,9 +508,9 @@ export class PluginService extends BaseService<PluginEntity> {
if (!old) { if (!old) {
//add //add
const {id} = await this.add(pluginEntity); const { id } = await this.add(pluginEntity);
pluginEntity.id = id; pluginEntity.id = id;
} else{ } else {
if (!req.override) { if (!req.override) {
throw new Error(`插件${loaded.author}/${loaded.name}已存在`); throw new Error(`插件${loaded.author}/${loaded.name}已存在`);
} }
@@ -511,8 +524,8 @@ export class PluginService extends BaseService<PluginEntity> {
} }
async deleteByIds(ids:any[]){ async deleteByIds(ids: any[]) {
await super.delete(ids); await super.delete(ids);
for (const id of ids) { for (const id of ids) {
await this.unRegisterById(id) await this.unRegisterById(id)
} }
@@ -1,98 +0,0 @@
import { HttpClient, HttpRequestConfig, ILogger } from "@certd/basic";
import { MaoyunAccess } from "./access.js";
export class MaoyunClient {
privateKeyPem = "";
http: HttpClient;
logger: ILogger;
access: MaoyunAccess;
token: string;
constructor(opts: { logger: ILogger; http: HttpClient; access: MaoyunAccess }) {
this.logger = opts.logger;
this.http = opts.http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uHZoR3HO4mie2adVf9NfAmkFsCluRAZKtQkNc\nt/VwlJEC6dChoZkU8Wzd0fSJKrdhjik2ayGXmzkCgYEAuie9s5UyzIXfTSwhCAkm\nT+TzE8Iu7Y0nxPnVM6+g2kNyoZvgqK23XUGDnuCRhzbiqGPGkQovN8Z0RUOiev1m\n3bgUHoAKWvECYrjURS1AxkAmuy8wPsYvyTLHOBpxOD5bLkjMGyVHe7AL59gTDktv\nh2oPEZibIamo6MJyhCxbYC0CgYAIZhnYL7MsO3phgRqR3oTyiDwJEq/RLIQWSFG4\nzNhk8BhPDxRvL7XIEQXQKndNwEyrpKJOri/euIDnlet9z7s1GRmX2/OxmS0LsFoN\nif/K7djUDn2L7RWwAQI0hsC1pNZTw7raoE5I/JB3FSifIFA4/3U5/GdqhvCOS+k9\ni7rUGQKBgQDPspapfGj2ozgWChJ2xMTGBhJhynM81w3j9w7MLvO/7/U43zYzKzyc\n7YJzApQOSwX/nLdquzi+UIbvuCB3npZVZl52S4f7BBcgLNQpdmcfWrAbDv5lySfn\n/KTN22Wxmhh20QgiNSxj+o+KIgdAgZCgWt7NrkZ5UX7Lo+ZfYU1xbg==\n-----END RSA PRIVATE KEY-----";
}
async sign(data: string) {
const { KJUR, KEYUTIL, hextob64 } = await import("jsrsasign");
const privateKey = KEYUTIL.getKey(this.privateKeyPem);
// 创建签名实例
const signature = new KJUR.crypto.Signature({
alg: "SHA256withRSA",
});
// 初始化私钥
signature.init(privateKey);
// 更新待签名数据(假设原文是字符串)
signature.updateString(data);
// 生成签名(默认返回十六进制字符串)
const hexSignature = signature.sign();
// 转换为 Base64(假设 Ix 是 Base64 编码)
return hextob64(hexSignature);
}
async doRequest(req: HttpRequestConfig) {
const timestamp = Date.now();
let data = "";
if (req.method.toLowerCase() === "get") {
// area_codes=&channel_type=0,1,2&domain_name=&https_status=&nonce=1747242446238&order=&page=1&page_size=10&status=&timestamp=1747242446238
let queryList = [];
for (const key in req.params) {
queryList.push(`${key}=${req.params[key]}`);
}
queryList.push(`nonce=${timestamp}`);
queryList.push(`timestamp=${timestamp}`);
//sort
queryList = queryList.sort();
data = queryList.join("&");
} else {
data = `body=${JSON.stringify(req.data || {})}&nonce=${timestamp}&timestamp=${timestamp}`;
}
const sign = await this.sign(data);
const headers: any = {
sign: sign,
timestamp: timestamp,
nonce: timestamp,
};
if (this.token) {
headers.Token = this.token;
}
const res = await this.http.request({
...req,
headers,
baseURL: "https://testaa.5678.jp",
});
if (!res.success && res.code !== 200) {
throw new Error(`请求失败:${res.msg}`);
}
return res.data;
}
async login() {
const req = {
email: this.access.username,
password: this.access.password,
accountType: 1,
};
const res = await this.doRequest({
url: "/api/vcloud/v1/userApi/noAuth/login",
method: "post",
data: req,
logRes: false,
logParams: false,
});
const { token } = res;
this.logger.info(`登录成功`);
this.token = token;
}
}
@@ -1,4 +1,3 @@
// 隐藏 通过下载插件形式分发 // 隐藏 通过下载插件形式分发
// export * from "./plugins/index.js"; // export * from "./plugins/index.js";
export * from "./access.js"; export * from "./access.js";
export * from "./client.js";
@@ -3,7 +3,7 @@ import { CertApplyPluginNames, CertInfo } from "@certd/plugin-cert";
import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from "@certd/plugin-lib"; import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from "@certd/plugin-lib";
import { AbstractPlusTaskPlugin } from "@certd/plugin-plus"; import { AbstractPlusTaskPlugin } from "@certd/plugin-plus";
import { MaoyunAccess } from "../access.js"; import { MaoyunAccess } from "../access.js";
import { MaoyunClient } from "../client.js"; import { MaoyunClient } from "@certd/plugin-plus";
@IsTaskPlugin({ @IsTaskPlugin({
//命名规范,插件类型+功能(就是目录plugin-demo中的demo),大写字母开头,驼峰命名 //命名规范,插件类型+功能(就是目录plugin-demo中的demo),大写字母开头,驼峰命名
+3
View File
@@ -885,6 +885,9 @@ importers:
form-data: form-data:
specifier: ^4.0.0 specifier: ^4.0.0
version: 4.0.2 version: 4.0.2
jsrsasign:
specifier: ^11.1.0
version: 11.1.0
querystring: querystring:
specifier: ^0.2.1 specifier: ^0.2.1
version: 0.2.1 version: 0.2.1