fix(pipeline): 重构运行时依赖加载逻辑,修复火山引擎DNS解析报runtimeDepsService未初始化的bug

This commit is contained in:
xiaojunnuo
2026-07-11 23:40:13 +08:00
parent edda1b57f3
commit ec69b8f11b
18 changed files with 105 additions and 48 deletions
-3
View File
@@ -62,9 +62,6 @@ export abstract class BaseAccess implements IAccess {
if (!this.runtimeDepsService && this.ctx.serviceGetter) { if (!this.runtimeDepsService && this.ctx.serviceGetter) {
this.runtimeDepsService = await this.ctx.serviceGetter.get("runtimeDepsService"); this.runtimeDepsService = await this.ctx.serviceGetter.get("runtimeDepsService");
} }
if (this.runtimeDepsService && this.ctx.define?.name) {
await this.runtimeDepsService.ensureRuntimeDependencies({ pluginKeys: `access:${this.ctx.define.name}`, logger: this.ctx.logger });
}
} }
async onRequest(req: AccessRequestHandleReq) { async onRequest(req: AccessRequestHandleReq) {
@@ -47,7 +47,7 @@ export function AccessInput(input?: AccessInputDefine): PropertyDecorator {
}; };
} }
export async function newAccess(type: string, input: any, accessService: IAccessService, ctx?: AccessContext) { export async function newAccess(type: string, input: any, accessService: IAccessService, ctx: AccessContext) {
const register = accessRegistry.get(type); const register = accessRegistry.get(type);
if (register == null) { if (register == null) {
throw new Error(`access ${type} not found`); throw new Error(`access ${type} not found`);
@@ -112,9 +112,6 @@ export abstract class BaseNotification implements INotification {
if (!this.runtimeDepsService && this.ctx.serviceGetter) { if (!this.runtimeDepsService && this.ctx.serviceGetter) {
this.runtimeDepsService = await this.ctx.serviceGetter.get("runtimeDepsService"); this.runtimeDepsService = await this.ctx.serviceGetter.get("runtimeDepsService");
} }
if (this.runtimeDepsService && this.ctx.define?.name) {
await this.runtimeDepsService.ensureRuntimeDependencies({ pluginKeys: `notification:${this.ctx.define.name}`, logger: this.logger });
}
} }
setDefine = (define: NotificationDefine) => { setDefine = (define: NotificationDefine) => {
this.define = define; this.define = define;
-3
View File
@@ -181,9 +181,6 @@ export abstract class AbstractTaskPlugin implements ITaskPlugin {
if (!this.runtimeDepsService && this.ctx.serviceGetter) { if (!this.runtimeDepsService && this.ctx.serviceGetter) {
this.runtimeDepsService = await this.ctx.serviceGetter.get("runtimeDepsService"); this.runtimeDepsService = await this.ctx.serviceGetter.get("runtimeDepsService");
} }
if (this.runtimeDepsService && this.ctx.define?.name) {
await this.runtimeDepsService.ensureRuntimeDependencies({ pluginKeys: `plugin:${this.ctx.define.name}`, logger: this.logger });
}
// 将证书加入secret // 将证书加入secret
// @ts-ignore // @ts-ignore
if (this.cert && this.cert.crt && this.cert.key) { if (this.cert && this.cert.crt && this.cert.key) {
@@ -1,29 +1,20 @@
import { IAccessService, IRuntimeDepsService } from "@certd/pipeline"; import { IAccessService } from "@certd/pipeline";
export type AccessRuntimeDepsService = IRuntimeDepsService;
export class AccessGetter implements IAccessService { export class AccessGetter implements IAccessService {
userId: number; userId: number;
projectId?: number; projectId?: number;
runtimeDepsService?: AccessRuntimeDepsService; getter: <T>(id: any, userId?: number, projectId?: number, ignorePermission?: boolean) => Promise<T>;
getter: <T>(id: any, userId?: number, projectId?: number, ignorePermission?: boolean, runtimeDepsService?: AccessRuntimeDepsService) => Promise<T>; constructor(userId: number, projectId: number, getter: (id: any, userId: number, projectId?: number, ignorePermission?: boolean) => Promise<any>) {
constructor(
userId: number,
projectId: number,
getter: (id: any, userId: number, projectId?: number, ignorePermission?: boolean, runtimeDepsService?: AccessRuntimeDepsService) => Promise<any>,
runtimeDepsService?: AccessRuntimeDepsService
) {
this.userId = userId; this.userId = userId;
this.projectId = projectId; this.projectId = projectId;
this.getter = getter; this.getter = getter;
this.runtimeDepsService = runtimeDepsService;
} }
async getById<T = any>(id: any) { async getById<T = any>(id: any) {
return await this.getter<T>(id, this.userId, this.projectId, false, this.runtimeDepsService); return await this.getter<T>(id, this.userId, this.projectId, false);
} }
async getCommonById<T = any>(id: any) { async getCommonById<T = any>(id: any) {
return await this.getter<T>(id, 0, null, false, this.runtimeDepsService); return await this.getter<T>(id, 0, null, false);
} }
} }
@@ -1,8 +1,8 @@
import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core"; import { ApplicationContext, Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
import type { IMidwayContainer } from "@midwayjs/core";
import { InjectEntityModel } from "@midwayjs/typeorm"; import { InjectEntityModel } from "@midwayjs/typeorm";
import { In, Repository } from "typeorm"; import { In, Repository } from "typeorm";
import { AccessGetter, BaseService, PageReq, PermissionException, ValidateException } from "../../../index.js"; import { AccessGetter, BaseService, PageReq, PermissionException, ValidateException } from "../../../index.js";
import type { AccessRuntimeDepsService } from "./access-getter.js";
import { AccessEntity } from "../entity/access.js"; import { AccessEntity } from "../entity/access.js";
import { AccessDefine, accessRegistry, newAccess } from "@certd/pipeline"; import { AccessDefine, accessRegistry, newAccess } from "@certd/pipeline";
import { EncryptService } from "./encrypt-service.js"; import { EncryptService } from "./encrypt-service.js";
@@ -20,6 +20,9 @@ export class AccessService extends BaseService<AccessEntity> {
@Inject() @Inject()
encryptService: EncryptService; encryptService: EncryptService;
@ApplicationContext()
applicationContext: IMidwayContainer;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore //@ts-ignore
getRepository() { getRepository() {
@@ -161,7 +164,7 @@ export class AccessService extends BaseService<AccessEntity> {
}; };
} }
async getAccessById(id: any, checkUserId: boolean, userId?: number, projectId?: number, runtimeDepsService?: AccessRuntimeDepsService): Promise<any> { async getAccessById(id: any, checkUserId: boolean, userId?: number, projectId?: number): Promise<any> {
const entity = await this.info(id); const entity = await this.info(id);
if (entity == null) { if (entity == null) {
throw new Error(`该授权配置不存在,请确认是否已被删除:id=${id}`); throw new Error(`该授权配置不存在,请确认是否已被删除:id=${id}`);
@@ -184,20 +187,23 @@ export class AccessService extends BaseService<AccessEntity> {
id: entity.id, id: entity.id,
...setting, ...setting,
}; };
const taskServiceBuilder: any = await this.applicationContext.getAsync("taskServiceBuilder");
const serviceGetter = taskServiceBuilder.create({ userId: userId || 0, projectId });
const getAccessById = this.getById.bind(this); const getAccessById = this.getById.bind(this);
const accessGetter = new AccessGetter(userId, projectId, getAccessById, runtimeDepsService); const accessGetter = new AccessGetter(userId, projectId, getAccessById);
const accessContext = { const accessContext = {
logger, logger,
http, http,
utils, utils,
accessService: accessGetter, accessService: accessGetter,
serviceGetter,
} as any; } as any;
const access = await newAccess(entity.type, input, accessGetter, accessContext); const access = await newAccess(entity.type, input, accessGetter, accessContext);
return access; return access;
} }
async getById(id: any, userId: number, projectId?: number, _ignorePermission?: boolean, runtimeDepsService?: AccessRuntimeDepsService): Promise<any> { async getById(id: any, userId: number, projectId?: number, _ignorePermission?: boolean): Promise<any> {
return await this.getAccessById(id, true, userId, projectId, runtimeDepsService); return await this.getAccessById(id, true, userId, projectId);
} }
decryptAccessEntity(entity: AccessEntity): any { decryptAccessEntity(entity: AccessEntity): any {
@@ -121,9 +121,6 @@ export abstract class BaseAddon implements IAddon {
if (!this.runtimeDepsService && this.ctx.serviceGetter) { if (!this.runtimeDepsService && this.ctx.serviceGetter) {
this.runtimeDepsService = await this.ctx.serviceGetter.get("runtimeDepsService"); this.runtimeDepsService = await this.ctx.serviceGetter.get("runtimeDepsService");
} }
if (this.runtimeDepsService && this.define?.addonType && this.define?.name) {
await this.runtimeDepsService.ensureRuntimeDependencies({ pluginKeys: `addon:${this.define.addonType}:${this.define.name}`, logger: this.logger });
}
} }
setDefine = (define:AddonDefine) => { setDefine = (define:AddonDefine) => {
this.define = define; this.define = define;
@@ -45,9 +45,6 @@ export abstract class AbstractDnsProvider<T = any> implements IDnsProvider<T> {
if (!this.runtimeDepsService && this.ctx.serviceGetter) { if (!this.runtimeDepsService && this.ctx.serviceGetter) {
this.runtimeDepsService = await this.ctx.serviceGetter.get("runtimeDepsService"); this.runtimeDepsService = await this.ctx.serviceGetter.get("runtimeDepsService");
} }
if (this.runtimeDepsService && this.ctx.define?.name) {
await this.runtimeDepsService.ensureRuntimeDependencies({ pluginKeys: `dnsProvider:${this.ctx.define.name}`, logger: this.logger });
}
} }
async parseDomain(fullDomain: string) { async parseDomain(fullDomain: string) {
@@ -9,6 +9,7 @@ import { http, logger, utils } from "@certd/basic";
import { ApiTags } from "@midwayjs/swagger"; import { ApiTags } from "@midwayjs/swagger";
import { CodeService } from "../../../modules/basic/service/code-service.js"; import { CodeService } from "../../../modules/basic/service/code-service.js";
import { EmailService } from "../../../modules/basic/service/email-service.js"; import { EmailService } from "../../../modules/basic/service/email-service.js";
import { TaskServiceBuilder } from "../../../modules/pipeline/service/getter/task-service-getter.js";
/** /**
*/ */
@@ -40,6 +41,9 @@ export class MineController extends BaseController {
@Inject() @Inject()
emailService: EmailService; emailService: EmailService;
@Inject()
taskServiceBuilder: TaskServiceBuilder;
@Post("/info", { description: Constants.per.authOnly, summary: "查询用户信息" }) @Post("/info", { description: Constants.per.authOnly, summary: "查询用户信息" })
public async info() { public async info() {
const userId = this.getUserId(); const userId = this.getUserId();
@@ -176,11 +180,13 @@ export class MineController extends BaseController {
const getAccessById = this.accessService.getById.bind(this.accessService); const getAccessById = this.accessService.getById.bind(this.accessService);
const accessGetter = new AccessGetter(userId, undefined, getAccessById); const accessGetter = new AccessGetter(userId, undefined, getAccessById);
const serviceGetter = this.taskServiceBuilder.create({ userId });
const accessContext = { const accessContext = {
http, http,
logger, logger,
utils, utils,
accessService: accessGetter, accessService: accessGetter,
serviceGetter,
define: undefined, define: undefined,
} as any; } as any;
const access = await newAccess("acmeAccount", { caType: "letsencrypt", email: userEmail }, accessGetter, accessContext); const access = await newAccess("acmeAccount", { caType: "letsencrypt", email: userEmail }, accessGetter, accessContext);
@@ -8,7 +8,6 @@ import { TaskServiceBuilder } from "../../../modules/pipeline/service/getter/tas
import { cloneDeep } from "lodash-es"; import { cloneDeep } from "lodash-es";
import { ApiTags } from "@midwayjs/swagger"; import { ApiTags } from "@midwayjs/swagger";
import { AuthService } from "../../../modules/sys/authority/service/auth-service.js"; import { AuthService } from "../../../modules/sys/authority/service/auth-service.js";
import { RuntimeDepsService } from "../../../modules/runtime-deps/runtime-deps-service.js";
@Provide() @Provide()
@Controller("/api/pi/handle") @Controller("/api/pi/handle")
@@ -29,9 +28,6 @@ export class HandleController extends BaseController {
@Inject() @Inject()
notificationService: NotificationService; notificationService: NotificationService;
@Inject()
runtimeDepsService: RuntimeDepsService;
@Post("/access", { description: Constants.per.authOnly, summary: "处理授权请求" }) @Post("/access", { description: Constants.per.authOnly, summary: "处理授权请求" })
async accessRequest(@Body(ALL) body: AccessRequestHandleReq) { async accessRequest(@Body(ALL) body: AccessRequestHandleReq) {
let { projectId, userId } = await this.getProjectUserIdRead(); let { projectId, userId } = await this.getProjectUserIdRead();
@@ -64,12 +60,14 @@ export class HandleController extends BaseController {
} }
} }
const getAccessById = this.accessService.getById.bind(this.accessService); const getAccessById = this.accessService.getById.bind(this.accessService);
const accessGetter = new AccessGetter(userId, projectId, getAccessById, this.runtimeDepsService); const accessGetter = new AccessGetter(userId, projectId, getAccessById);
const serviceGetter = this.taskServiceBuilder.create({ userId, projectId });
const accessContext = { const accessContext = {
http, http,
logger, logger,
utils, utils,
accessService: accessGetter, accessService: accessGetter,
serviceGetter,
define: undefined, define: undefined,
} as any; } as any;
const access = await newAccess(body.typeName, inputAccess, accessGetter, accessContext); const access = await newAccess(body.typeName, inputAccess, accessGetter, accessContext);
@@ -2,6 +2,7 @@ import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
import { logger } from "@certd/basic"; import { logger } from "@certd/basic";
import { PluginService } from "../plugin/service/plugin-service.js"; import { PluginService } from "../plugin/service/plugin-service.js";
import { registerPaymentProviders } from "../suite/payments/index.js"; import { registerPaymentProviders } from "../suite/payments/index.js";
import { RuntimeDepsService } from "../runtime-deps/runtime-deps-service.js";
@Provide() @Provide()
@Scope(ScopeEnum.Request, { allowDowngrade: true }) @Scope(ScopeEnum.Request, { allowDowngrade: true })
@@ -9,6 +10,9 @@ export class AutoLoadPlugins {
@Inject() @Inject()
pluginService: PluginService; pluginService: PluginService;
@Inject()
runtimeDepsService: RuntimeDepsService;
async init() { async init() {
logger.info(`加载插件开始,加载模式:${process.env.certd_plugin_loadmode}`); logger.info(`加载插件开始,加载模式:${process.env.certd_plugin_loadmode}`);
if (process.env.certd_plugin_loadmode === "metadata") { if (process.env.certd_plugin_loadmode === "metadata") {
@@ -30,5 +34,8 @@ export class AutoLoadPlugins {
await registerPaymentProviders(); await registerPaymentProviders();
logger.info(`加载插件完成,加载模式:${process.env.certd_plugin_loadmode}`); logger.info(`加载插件完成,加载模式:${process.env.certd_plugin_loadmode}`);
// 收集插件 dependPackages 并安装
await this.runtimeDepsService.refreshPluginDeps();
} }
} }
@@ -66,9 +66,8 @@ export class TaskServiceGetter implements IServiceGetter {
async getAccessService(): Promise<AccessGetter> { async getAccessService(): Promise<AccessGetter> {
const accessService: AccessService = await this.appCtx.getAsync("accessService"); const accessService: AccessService = await this.appCtx.getAsync("accessService");
const runtimeDepsService = await this.getRuntimeDepsService();
const getAccessById = accessService.getById.bind(accessService); const getAccessById = accessService.getById.bind(accessService);
return new AccessGetter(this.userId, this.projectId, getAccessById, runtimeDepsService); return new AccessGetter(this.userId, this.projectId, getAccessById);
} }
async getCnameProxyService(): Promise<CnameProxyService> { async getCnameProxyService(): Promise<CnameProxyService> {
@@ -270,6 +270,7 @@ export class PluginService extends BaseService<PluginEntity> {
return; return;
} }
await this.registerPlugin(item); await this.registerPlugin(item);
await this.runtimeDepsService.refreshPluginDeps();
} }
async unRegisterById(id: any) { async unRegisterById(id: any) {
@@ -297,6 +298,7 @@ export class PluginService extends BaseService<PluginEntity> {
} else { } else {
logger.warn(`不支持的插件类型:${item.pluginType}`); logger.warn(`不支持的插件类型:${item.pluginType}`);
} }
await this.runtimeDepsService.refreshPluginDeps();
} }
async update(param: any) { async update(param: any) {
@@ -150,6 +150,11 @@ export class RuntimeDepsService {
@Config("runtimeDeps.lazyDependencies") @Config("runtimeDeps.lazyDependencies")
lazyDependencies: Record<string, string> = {}; lazyDependencies: Record<string, string> = {};
/**
* 从插件 registry 收集到的懒加载依赖,键为包名,值为版本范围
*/
pluginLazyDependencies: Record<string, string> = {};
@Inject() @Inject()
registryResolver!: NpmRegistryResolver; registryResolver!: NpmRegistryResolver;
@@ -339,7 +344,8 @@ export class RuntimeDepsService {
private async resolveMissingRuntimeSpecifier(specifier: string, runtimeError: any, logger?: ILogger) { private async resolveMissingRuntimeSpecifier(specifier: string, runtimeError: any, logger?: ILogger) {
const packageName = this.parsePackageName(specifier); const packageName = this.parsePackageName(specifier);
const lazyRange = this.lazyDependencies?.[packageName]; const mergedDeps = this.getMergedLazyDependencies();
const lazyRange = mergedDeps[packageName];
if (!lazyRange) { if (!lazyRange) {
try { try {
return this.resolveProjectSpecifier(specifier, runtimeError).resolved; return this.resolveProjectSpecifier(specifier, runtimeError).resolved;
@@ -565,6 +571,56 @@ export class RuntimeDepsService {
}); });
} }
/**
* 合并 package.json 的 lazyDependencies 和插件注册表收集到的 dependPackages
* 插件依赖优先(同名时使用插件声明的版本)
*/
getMergedLazyDependencies(): Record<string, string> {
return { ...this.lazyDependencies, ...this.pluginLazyDependencies };
}
/**
* 从所有插件注册表中收集 dependPackages,合并到 pluginLazyDependencies
*/
collectPluginDeps() {
const registries: Array<{ registry: Registry<any>; keyFormatter?: (name: string) => string }> = [
{ registry: pluginRegistry },
{ registry: accessRegistry },
{ registry: notificationRegistry },
{ registry: dnsProviderRegistry },
{ registry: addonRegistry },
];
const deps: Record<string, string> = {};
for (const { registry } of registries) {
const defineList = registry.getDefineList();
for (const define of defineList) {
const dependPackages = (define as any).dependPackages as Record<string, string> | undefined;
if (!dependPackages) {
continue;
}
for (const [pkgName, range] of Object.entries(dependPackages)) {
const existing = deps[pkgName];
if (existing && !areRangesCompatible(existing, range)) {
logger.warn(`懒加载依赖版本冲突: ${pkgName} => ${existing} vs ${range},保留已有版本`);
continue;
}
deps[pkgName] = range;
}
}
}
this.pluginLazyDependencies = deps;
logger.info(`从插件注册表收集到 ${Object.keys(deps).length} 个懒加载依赖`);
}
/**
* 刷新插件懒加载依赖:收集所有插件的 dependPackages 到内存列表,实际安装延迟到 importRuntime 时触发
*/
async refreshPluginDeps() {
this.collectPluginDeps();
}
private readInstallState(statePath: string): any { private readInstallState(statePath: string): any {
if (!fs.existsSync(statePath)) { if (!fs.existsSync(statePath)) {
return null; return null;
@@ -14,7 +14,8 @@ export class VolcengineCdnClient {
if (this.service) { if (this.service) {
return this.service; return this.service;
} }
const { cdn } = await this.opts.access.importRuntime("@volcengine/openapi"); const importRuntime = this.opts.importRuntime || this.opts.access.importRuntime.bind(this.opts.access);
const { cdn } = await importRuntime("@volcengine/openapi");
const service = new cdn.CdnService(); const service = new cdn.CdnService();
// 设置ak、sk // 设置ak、sk
service.setAccessKeyId(this.opts.access.accessKeyId); service.setAccessKeyId(this.opts.access.accessKeyId);
@@ -21,7 +21,8 @@ export class VolcengineDnsClient {
} }
async doRequest(req: VolcengineReq) { async doRequest(req: VolcengineReq) {
const { Signer } = await this.opts.access.importRuntime("@volcengine/openapi"); const importRuntime = this.opts.importRuntime || this.opts.access.importRuntime.bind(this.opts.access);
const { Signer } = await importRuntime("@volcengine/openapi");
// http request data // http request data
const openApiRequestData: any = { const openApiRequestData: any = {
@@ -1,10 +1,12 @@
import { VolcengineAccess } from "./access.js"; import { VolcengineAccess } from "./access.js";
import { HttpClient, ILogger } from "@certd/basic"; import { HttpClient, ILogger } from "@certd/basic";
import { ImportRuntime } from "@certd/pipeline";
export type VolcengineOpts = { export type VolcengineOpts = {
access: VolcengineAccess; access: VolcengineAccess;
logger: ILogger; logger: ILogger;
http: HttpClient; http: HttpClient;
importRuntime?: ImportRuntime;
}; };
export class VolcengineClient { export class VolcengineClient {
@@ -140,7 +142,8 @@ export class VolcengineClient {
} }
async getTOSService(opts: { region?: string }) { async getTOSService(opts: { region?: string }) {
const { TosClient } = await this.opts.access.importRuntime("@volcengine/tos-sdk"); const importRuntime = this.opts.importRuntime || this.opts.access.importRuntime.bind(this.opts.access);
const { TosClient } = await importRuntime("@volcengine/tos-sdk");
const client = new TosClient({ const client = new TosClient({
accessKeyId: this.opts.access.accessKeyId, accessKeyId: this.opts.access.accessKeyId,
@@ -169,7 +172,8 @@ export class VolcengineClient {
if (this.CommonService) { if (this.CommonService) {
return this.CommonService; return this.CommonService;
} }
const { Service } = await this.opts.access.importRuntime("@volcengine/openapi"); const importRuntime = this.opts.importRuntime || this.opts.access.importRuntime.bind(this.opts.access);
const { Service } = await importRuntime("@volcengine/openapi");
class CommonService extends Service { class CommonService extends Service {
Generic: any; Generic: any;
@@ -22,6 +22,7 @@ export class VolcengineDnsProvider extends AbstractDnsProvider {
access: this.access, access: this.access,
logger: this.logger, logger: this.logger,
http: this.http, http: this.http,
importRuntime: this.importRuntime.bind(this),
}); });
} }