UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

100 lines (99 loc) • 3.25 kB
import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs"; import { j as writeExternalFileWithinRoot } from "./fs-safe-BbWvmdVn.mjs"; import path from "node:path"; import fs from "node:fs/promises"; //#region src/logging/diagnostic-support-bundle.ts /** Creates a JSON support-bundle file with a safe relative path. */ function jsonSupportBundleFile(e, t) { return { path: assertSafeBundleRelativePath(e), mediaType: "application/json", content: `${JSON.stringify(t, null, 2)}\n` }; } /** Creates an NDJSON support-bundle file with a safe relative path. */ function jsonlSupportBundleFile(e, t) { return { path: assertSafeBundleRelativePath(e), mediaType: "application/x-ndjson", content: `${t.join("\n")}\n` }; } /** Creates a UTF-8 text support-bundle file with a safe relative path. */ function textSupportBundleFile(e, t) { return { path: assertSafeBundleRelativePath(e), mediaType: "text/plain; charset=utf-8", content: t.endsWith("\n") ? t : `${t}\n` }; } /** Summarizes support-bundle files for the bundle manifest. */ function supportBundleContents(e) { return e.map((e) => ({ path: e.path, mediaType: e.mediaType, bytes: Buffer.byteLength(e.content, "utf8") })); } function assertSafeBundleRelativePath(e) { const t = e.replaceAll("\\", "/"); if (!t || t.startsWith("/") || t.split("/").some((e) => e === "" || e === "." || e === "..")) throw new Error(`Invalid bundle file path: ${e}`); return t; } function resolveSupportBundleFilePath(e, t) { const n = assertSafeBundleRelativePath(t); const r = path.resolve(e); const i = path.resolve(r, n); if (i === r || !isPathInside(r, i)) throw new Error(`Bundle file path escaped output directory: ${t}`); return i; } /** Writes support-bundle files to a new private directory. */ async function writeSupportBundleDirectory(e) { await fs.mkdir(path.dirname(e.outputDir), { recursive: true, mode: 448 }); await fs.mkdir(e.outputDir, { mode: 448 }); for (const t of e.files) { const n = resolveSupportBundleFilePath(e.outputDir, t.path); await fs.mkdir(path.dirname(n), { recursive: true, mode: 448 }); await fs.writeFile(n, t.content, { encoding: "utf8", flag: "wx", mode: 384 }); } return supportBundleContents(e.files); } /** Writes support-bundle files to a private zip archive and returns the published path and byte size. */ async function writeSupportBundleZip(e) { const { default: t } = await import("jszip"); const n = new t(); for (const t of e.files) n.file(assertSafeBundleRelativePath(t.path), t.content); const r = await n.generateAsync({ type: "nodebuffer", compression: "DEFLATE", compressionOptions: { level: 6 } }); const i = path.resolve(e.outputPath); await fs.mkdir(path.dirname(i), { recursive: true, mode: 448 }); return { path: (await writeExternalFileWithinRoot({ rootDir: path.dirname(i), path: path.basename(i), fallbackFileName: "openclaw-support.zip", write: async (e) => { await fs.writeFile(e, r, { mode: 384 }); } })).path, bytes: r.length }; } //#endregion export { writeSupportBundleDirectory as a, textSupportBundleFile as i, jsonlSupportBundleFile as n, writeSupportBundleZip as o, supportBundleContents as r, jsonSupportBundleFile as t };