mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 17:20:42 +00:00
perf: speed up security audit test imports
This commit is contained in:
@@ -36,6 +36,8 @@ type DefineBundledChannelEntryOptions<TPlugin = ChannelPlugin> = {
|
||||
secrets?: BundledEntryModuleRef;
|
||||
configSchema?: ChannelEntryConfigSchema<TPlugin> | (() => ChannelEntryConfigSchema<TPlugin>);
|
||||
runtime?: BundledEntryModuleRef;
|
||||
accountInspect?: BundledEntryModuleRef;
|
||||
features?: BundledChannelEntryFeatures;
|
||||
registerCliMetadata?: (api: OpenClawPluginApi) => void;
|
||||
registerFull?: (api: OpenClawPluginApi) => void;
|
||||
};
|
||||
@@ -53,15 +55,21 @@ export type BundledChannelSetupEntryFeatures = {
|
||||
legacySessionSurfaces?: boolean;
|
||||
};
|
||||
|
||||
export type BundledChannelEntryFeatures = {
|
||||
accountInspect?: boolean;
|
||||
};
|
||||
|
||||
export type BundledChannelEntryContract<TPlugin = ChannelPlugin> = {
|
||||
kind: "bundled-channel-entry";
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
configSchema: ChannelEntryConfigSchema<TPlugin>;
|
||||
features?: BundledChannelEntryFeatures;
|
||||
register: (api: OpenClawPluginApi) => void;
|
||||
loadChannelPlugin: () => TPlugin;
|
||||
loadChannelSecrets?: () => ChannelPlugin["secrets"] | undefined;
|
||||
loadChannelAccountInspector?: () => NonNullable<ChannelPlugin["config"]["inspectAccount"]>;
|
||||
setChannelRuntime?: (runtime: PluginRuntime) => void;
|
||||
};
|
||||
|
||||
@@ -332,6 +340,8 @@ export function defineBundledChannelEntry<TPlugin = ChannelPlugin>({
|
||||
secrets,
|
||||
configSchema,
|
||||
runtime,
|
||||
accountInspect,
|
||||
features,
|
||||
registerCliMetadata,
|
||||
registerFull,
|
||||
}: DefineBundledChannelEntryOptions<TPlugin>): BundledChannelEntryContract<TPlugin> {
|
||||
@@ -343,6 +353,13 @@ export function defineBundledChannelEntry<TPlugin = ChannelPlugin>({
|
||||
const loadChannelSecrets = secrets
|
||||
? () => loadBundledEntryExportSync<ChannelPlugin["secrets"] | undefined>(importMetaUrl, secrets)
|
||||
: undefined;
|
||||
const loadChannelAccountInspector = accountInspect
|
||||
? () =>
|
||||
loadBundledEntryExportSync<NonNullable<ChannelPlugin["config"]["inspectAccount"]>>(
|
||||
importMetaUrl,
|
||||
accountInspect,
|
||||
)
|
||||
: undefined;
|
||||
const setChannelRuntime = runtime
|
||||
? (pluginRuntime: PluginRuntime) => {
|
||||
const setter = loadBundledEntryExportSync<(runtime: PluginRuntime) => void>(
|
||||
@@ -359,6 +376,9 @@ export function defineBundledChannelEntry<TPlugin = ChannelPlugin>({
|
||||
name,
|
||||
description,
|
||||
configSchema: resolvedConfigSchema,
|
||||
...(features || accountInspect
|
||||
? { features: { ...features, ...(accountInspect ? { accountInspect: true } : {}) } }
|
||||
: {}),
|
||||
register(api: OpenClawPluginApi) {
|
||||
if (api.registrationMode === "cli-metadata") {
|
||||
registerCliMetadata?.(api);
|
||||
@@ -374,6 +394,7 @@ export function defineBundledChannelEntry<TPlugin = ChannelPlugin>({
|
||||
},
|
||||
loadChannelPlugin,
|
||||
...(loadChannelSecrets ? { loadChannelSecrets } : {}),
|
||||
...(loadChannelAccountInspector ? { loadChannelAccountInspector } : {}),
|
||||
...(setChannelRuntime ? { setChannelRuntime } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import fs from "node:fs";
|
||||
import { createRequire } from "node:module";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { openBoundaryFileSync } from "../infra/boundary-file-read.js";
|
||||
import { resolveBundledPluginsDir } from "../plugins/bundled-dir.js";
|
||||
import {
|
||||
@@ -275,6 +275,54 @@ export function loadBundledPluginPublicSurfaceModuleSync<T extends object>(param
|
||||
});
|
||||
}
|
||||
|
||||
export async function loadBundledPluginPublicSurfaceModule<T extends object>(params: {
|
||||
dirName: string;
|
||||
artifactBasename: string;
|
||||
trackedPluginId?: string | (() => string);
|
||||
env?: NodeJS.ProcessEnv;
|
||||
}): Promise<T> {
|
||||
const location = resolveFacadeModuleLocation(params);
|
||||
if (!location) {
|
||||
throw new Error(
|
||||
`Unable to resolve bundled plugin public surface ${params.dirName}/${params.artifactBasename}`,
|
||||
);
|
||||
}
|
||||
const cached = loadedFacadeModules.get(location.modulePath);
|
||||
if (cached) {
|
||||
return cached as T;
|
||||
}
|
||||
|
||||
const opened = openBoundaryFileSync({
|
||||
absolutePath: location.modulePath,
|
||||
rootPath: location.boundaryRoot,
|
||||
boundaryLabel:
|
||||
location.boundaryRoot === getOpenClawPackageRoot() ? "OpenClaw package root" : "plugin root",
|
||||
rejectHardlinks: false,
|
||||
});
|
||||
if (!opened.ok) {
|
||||
throw new Error(`Unable to open bundled plugin public surface ${location.modulePath}`, {
|
||||
cause: opened.error,
|
||||
});
|
||||
}
|
||||
fs.closeSync(opened.fd);
|
||||
|
||||
try {
|
||||
const loaded = (await import(pathToFileURL(location.modulePath).href)) as T;
|
||||
loadedFacadeModules.set(location.modulePath, loaded);
|
||||
loadedFacadePluginIds.add(
|
||||
typeof params.trackedPluginId === "function"
|
||||
? params.trackedPluginId()
|
||||
: (params.trackedPluginId ?? params.dirName),
|
||||
);
|
||||
return loaded;
|
||||
} catch {
|
||||
return loadFacadeModuleAtLocationSync({
|
||||
location,
|
||||
trackedPluginId: params.trackedPluginId ?? params.dirName,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function listImportedBundledPluginFacadeIds(): string[] {
|
||||
return [...loadedFacadePluginIds].toSorted((left, right) => left.localeCompare(right));
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ type FacadeModule = typeof import("@openclaw/telegram/contract-api.js");
|
||||
type SecurityAuditFacadeModule = typeof import("@openclaw/telegram/security-audit-contract-api.js");
|
||||
import {
|
||||
createLazyFacadeArrayValue,
|
||||
loadBundledPluginPublicSurfaceModule,
|
||||
loadBundledPluginPublicSurfaceModuleSync,
|
||||
} from "./facade-loader.js";
|
||||
|
||||
@@ -13,8 +14,8 @@ function loadFacadeModule(): FacadeModule {
|
||||
});
|
||||
}
|
||||
|
||||
function loadSecurityAuditFacadeModule(): SecurityAuditFacadeModule {
|
||||
return loadBundledPluginPublicSurfaceModuleSync<SecurityAuditFacadeModule>({
|
||||
async function loadSecurityAuditFacadeModule(): Promise<SecurityAuditFacadeModule> {
|
||||
return await loadBundledPluginPublicSurfaceModule<SecurityAuditFacadeModule>({
|
||||
dirName: "telegram",
|
||||
artifactBasename: "security-audit-contract-api.js",
|
||||
});
|
||||
@@ -31,8 +32,8 @@ export const singleAccountKeysToMove: FacadeModule["singleAccountKeysToMove"] =
|
||||
createLazyFacadeArrayValue(() => loadFacadeModule().singleAccountKeysToMove);
|
||||
|
||||
export const collectTelegramSecurityAuditFindings: FacadeModule["collectTelegramSecurityAuditFindings"] =
|
||||
((...args) =>
|
||||
loadSecurityAuditFacadeModule().collectTelegramSecurityAuditFindings(
|
||||
(async (...args) =>
|
||||
(await loadSecurityAuditFacadeModule()).collectTelegramSecurityAuditFindings(
|
||||
...args,
|
||||
)) as FacadeModule["collectTelegramSecurityAuditFindings"];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user