openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
78 lines (77 loc) • 2.75 kB
JavaScript
import { n as describeRootFileOpenFailure } from "./boundary-file-read-BfaFLDqt.mjs";
import { t as hasErrnoCode } from "./errno-DFcoqIT9.mjs";
import { c as getPluginCacheRoot, l as getPluginCacheSource } from "./plugin-cache-B_eRuqTM.mjs";
import { r as openPluginRootFileSync } from "./path-safety-CTChYC4Z.mjs";
import { r as isJavaScriptModulePath, t as PLUGIN_SOURCE_MODULE_EXTENSIONS } from "./native-module-require-CqM0GFP3.mjs";
import { t as getCachedPluginModuleLoader } from "./plugin-module-loader-cache-DgNX_BTY.mjs";
import { createRequire } from "node:module";
import fs from "node:fs";
import path from "node:path";
//#region src/channels/plugins/module-loader.ts
const nodeRequire = createRequire(import.meta.url);
function loadModule(e) {
const t = path.extname(e).toLowerCase();
if (!isJavaScriptModulePath(e) && !PLUGIN_SOURCE_MODULE_EXTENSIONS.includes(t)) throw new Error(`channel plugin module must be built JavaScript: ${e}`);
return getCachedPluginModuleLoader({
modulePath: e,
importerUrl: import.meta.url,
loaderFilename: import.meta.url,
cacheScopeKey: "channel-plugin-module-loader"
})(e);
}
function resolveExistingPluginModulePath(e, t) {
const n = getPluginCacheRoot(e).artifacts;
const r = `channel-specifier:${t}`;
const i = n.get(r);
if (i) return i.modulePath;
const a = resolvePluginModulePath(e, t);
n.set(r, {
modulePath: a,
boundaryRoot: e
});
return a;
}
function resolvePluginModulePath(e, t) {
const n = path.resolve(e, t.replace(/\\/g, "/"));
try {
return nodeRequire.resolve(n);
} catch (e) {
if (!hasErrnoCode(e, "MODULE_NOT_FOUND")) throw e;
}
if (!path.extname(n)) for (const e of PLUGIN_SOURCE_MODULE_EXTENSIONS) {
const t = `${n}${e}`;
if (fs.existsSync(t)) return t;
}
return n;
}
/**
* Loads a channel plugin module after enforcing plugin-root file boundaries.
*
* `rootDir` is always the plugin's own directory, so the containment failure is
* reported against that one root; no caller boundary override exists.
*/
function loadChannelPluginModule(e) {
const t = getPluginCacheSource(e.modulePath);
const n = `channel-plugin-module:${path.resolve(e.rootDir)}`;
const r = t.variants.get(n)?.exports;
if (r) return r.value;
const i = "plugin root";
const a = openPluginRootFileSync({
filePath: e.modulePath,
rootPath: e.rootDir,
rejectHardlinks: false
});
if (!a.ok) throw new Error(describeRootFileOpenFailure({
failure: a,
subject: "plugin module path",
boundaryLabel: i,
filePath: e.modulePath
}), { cause: a.error });
const o = a.path;
fs.closeSync(a.fd);
const s = loadModule(o);
t.variants.set(n, { exports: { value: s } });
return s;
}
//#endregion
export { resolveExistingPluginModulePath as n, loadChannelPluginModule as t };