openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
160 lines (159 loc) • 4.8 kB
JavaScript
import { i as asOptionalObjectRecord } from "./record-coerce-CVPuDqcc.mjs";
import { c as formatErrorStackWithMessage } from "./error-coercion-hDrHOSGy.mjs";
import { t as safeParseJson } from "./json-coercion-CaJLaav-.mjs";
import { m as normalizeTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs";
import { c as readFileDescriptorBoundedSync, o as openRootFileSync } from "./boundary-file-read-BfaFLDqt.mjs";
import { m as isPathInsideWithRealpath } from "./includes-wjVi5Ymt.mjs";
import { t as MANIFEST_KEY } from "./legacy-names-Cq8ruaO5.mjs";
import { r as parseFrontmatterBlockResult } from "./frontmatter-CLR6jwh6.mjs";
import { n as resolveHookManifestMetadata } from "./frontmatter-CTZaq8xF.mjs";
import fs from "node:fs";
import path from "node:path";
//#region src/hooks/discovery.ts
const HOOK_METADATA_MAX_BYTES = 1048576;
function readHookPackagePaths(e, t) {
const n = readRootFileUtf8({
absolutePath: path.join(e, "package.json"),
rootPath: e,
boundaryLabel: "hook package directory",
maxBytes: HOOK_METADATA_MAX_BYTES
}, t);
const r = n === null ? void 0 : asOptionalObjectRecord(safeParseJson(n));
return normalizeTrimmedStringList(asOptionalObjectRecord(r?.[MANIFEST_KEY])?.hooks);
}
function resolveContainedDir(e, t) {
const n = path.resolve(e);
const r = path.resolve(e, t);
if (!isPathInsideWithRealpath(n, r, { requireRealpath: true })) return null;
return r;
}
function loadHookFromDir(e, t) {
const n = path.join(e.hookDir, "HOOK.md");
const r = readRootFileUtf8({
absolutePath: n,
rootPath: e.hookDir,
boundaryLabel: "hook directory",
maxBytes: HOOK_METADATA_MAX_BYTES
}, t);
if (r === null) return null;
try {
const { frontmatter: i, issues: a } = parseFrontmatterBlockResult(r);
const o = i.name || path.basename(e.hookDir);
const s = i.description || "";
const c = [
"handler.ts",
"handler.js",
"index.ts",
"index.js"
];
let l;
for (const t of c) {
const n = withOpenedRootFileSync({
absolutePath: path.join(e.hookDir, t),
rootPath: e.hookDir,
boundaryLabel: "hook directory"
}, (e) => e.path);
if (n) {
l = n;
break;
}
}
if (!l) t?.(`Hook "${o}" has HOOK.md but no readable handler in ${e.hookDir}`);
let u = e.hookDir;
try {
u = fs.realpathSync.native(e.hookDir);
} catch {}
return {
hook: {
name: o,
description: s,
source: e.source,
pluginId: e.pluginId,
filePath: n,
baseDir: u,
handlerPath: l
},
frontmatter: i,
invalidMetadata: a.length > 0,
metadata: resolveHookManifestMetadata(i)
};
} catch (n) {
const r = n instanceof Error ? formatErrorStackWithMessage(n) : String(n);
t?.(`Failed to load hook from ${e.hookDir}: ${r}`);
return null;
}
}
function loadHooksFromCandidate(e, t) {
const { hookDir: n, source: r, pluginId: i } = e;
const a = readHookPackagePaths(n, t);
if (a.length === 0) {
if (!fs.existsSync(path.join(n, "HOOK.md"))) return null;
const r = loadHookFromDir(e, t);
return r ? [r] : [];
}
const o = [];
for (const e of a) {
const a = resolveContainedDir(n, e);
if (!a) {
t?.(`Ignoring out-of-package hook path "${e}" in ${n} (must be within package directory)`);
continue;
}
const s = loadHookFromDir({
hookDir: a,
source: r,
pluginId: i
}, t);
if (s) o.push(s);
}
return o;
}
function loadHookEntriesFromDir(e, t) {
const { dir: n, source: r, pluginId: i } = e;
if (e.rootDir && !isPathInsideWithRealpath(e.rootDir, n, { requireRealpath: true })) {
t?.(`Plugin hook path is missing or escapes plugin root (${i}): ${n}`);
return [];
}
if (!fs.existsSync(n) || !fs.statSync(n).isDirectory()) return [];
return (e.includeRoot ? loadHooksFromCandidate({
hookDir: n,
source: r,
pluginId: i
}, t) : null) ?? fs.readdirSync(n, { withFileTypes: true }).flatMap((e) => {
if (!e.isDirectory()) return [];
return loadHooksFromCandidate({
hookDir: path.join(n, e.name),
source: r,
pluginId: i
}, t) ?? [];
});
}
function readRootFileUtf8(e, t) {
return withOpenedRootFileSync(e, (n) => {
try {
return readFileDescriptorBoundedSync(n.fd, e.maxBytes).toString("utf-8");
} catch (n) {
if (n instanceof RangeError) t?.(`Ignoring oversized hook metadata ${e.absolutePath}: file exceeds the ${e.maxBytes}-byte limit`);
else t?.(`Failed to read hook metadata ${e.absolutePath}; check file permissions and disk health.`);
return null;
}
});
}
function withOpenedRootFileSync(e, t) {
const n = openRootFileSync({
absolutePath: e.absolutePath,
rootPath: e.rootPath,
boundaryLabel: e.boundaryLabel,
rejectSymlinks: false
});
if (!n.ok) return null;
try {
return t({
fd: n.fd,
path: n.path
});
} finally {
fs.closeSync(n.fd);
}
}
//#endregion
export { loadHookEntriesFromDir as t };