


















@@ -1,6 +1,8 @@
11import { createHash, randomUUID } from "node:crypto";
22import fs from "node:fs";
33import path from "node:path";
4+import JSZip from "jszip";
5+import * as tar from "tar";
46import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
57import { expectSingleNpmPackIgnoreScriptsCall } from "../test-utils/exec-assertions.js";
68import {
@@ -32,13 +34,23 @@ let tempDirIndex = 0;
3234const sharedArchivePathByName = new Map<string, string>();
33353436const fixturesDir = path.resolve(process.cwd(), "test", "fixtures", "hooks-install");
35-const zipHooksBuffer = fs.readFileSync(path.join(fixturesDir, "zip-hooks.zip"));
36-const zipTraversalBuffer = fs.readFileSync(path.join(fixturesDir, "zip-traversal.zip"));
37+const zipHooksBuffer = await createZipHookPackBuffer({
38+packageName: "@openclaw/zip-hooks",
39+hookName: "zip-hook",
40+hookDescription: "Zip hook",
41+heading: "Zip Hook",
42+});
43+const zipTraversalBuffer = await createZipBuffer([{ path: "../pwned.txt", contents: "pwned" }]);
3744const tarHooksBuffer = fs.readFileSync(path.join(fixturesDir, "tar-hooks.tar"));
3845const tarTraversalBuffer = fs.readFileSync(path.join(fixturesDir, "tar-traversal.tar"));
3946const tarEvilIdBuffer = fs.readFileSync(path.join(fixturesDir, "tar-evil-id.tar"));
4047const tarReservedIdBuffer = fs.readFileSync(path.join(fixturesDir, "tar-reserved-id.tar"));
41-const npmPackHooksBuffer = fs.readFileSync(path.join(fixturesDir, "npm-pack-hooks.tgz"));
48+const npmPackHooksBuffer = await createTarGzHookPackBuffer({
49+packageName: "@openclaw/test-hooks",
50+hookName: "one-hook",
51+hookDescription: "One hook",
52+heading: "One Hook",
53+});
42544355function makeTempDir() {
4456const dir = path.join(fixtureRoot, `case-${tempDirIndex++}`);
@@ -110,6 +122,95 @@ function writeHookPackManifest(params: {
110122);
111123}
112124125+async function createZipBuffer(entries: Array<{ path: string; contents: string }>) {
126+const zip = new JSZip();
127+for (const entry of entries) {
128+zip.file(entry.path, entry.contents);
129+}
130+return Buffer.from(await zip.generateAsync({ type: "nodebuffer", compression: "STORE" }));
131+}
132+133+function writeHookPackFiles(params: {
134+pkgDir: string;
135+packageName: string;
136+hookName: string;
137+hookDescription: string;
138+heading: string;
139+}) {
140+writeHookPackManifest({
141+pkgDir: params.pkgDir,
142+hooks: [`./hooks/${params.hookName}`],
143+});
144+const hookDir = path.join(params.pkgDir, "hooks", params.hookName);
145+fs.mkdirSync(hookDir, { recursive: true });
146+fs.writeFileSync(
147+path.join(hookDir, "HOOK.md"),
148+[
149+"---",
150+`name: ${params.hookName}`,
151+`description: ${params.hookDescription}`,
152+'metadata: {"openclaw":{"events":["command:new"]}}',
153+"---",
154+"",
155+`# ${params.heading}`,
156+].join("\n"),
157+"utf-8",
158+);
159+fs.writeFileSync(path.join(hookDir, "handler.ts"), "export default async () => {};\n", "utf-8");
160+161+const manifestPath = path.join(params.pkgDir, "package.json");
162+const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf-8")) as Record<string, unknown>;
163+manifest.name = params.packageName;
164+fs.writeFileSync(manifestPath, JSON.stringify(manifest), "utf-8");
165+}
166+167+async function createZipHookPackBuffer(params: {
168+packageName: string;
169+hookName: string;
170+hookDescription: string;
171+heading: string;
172+}) {
173+const packageJson = JSON.stringify({
174+name: params.packageName,
175+version: "0.0.1",
176+openclaw: { hooks: [`./hooks/${params.hookName}`] },
177+});
178+return createZipBuffer([
179+{ path: "package/package.json", contents: packageJson },
180+{
181+path: `package/hooks/${params.hookName}/HOOK.md`,
182+contents: [
183+"---",
184+`name: ${params.hookName}`,
185+`description: ${params.hookDescription}`,
186+'metadata: {"openclaw":{"events":["command:new"]}}',
187+"---",
188+"",
189+`# ${params.heading}`,
190+].join("\n"),
191+},
192+{
193+path: `package/hooks/${params.hookName}/handler.ts`,
194+contents: "export default async () => {};\n",
195+},
196+]);
197+}
198+199+async function createTarGzHookPackBuffer(params: {
200+packageName: string;
201+hookName: string;
202+hookDescription: string;
203+heading: string;
204+}) {
205+const workDir = path.join(fixtureRoot, "_generated", `pack-${randomUUID()}`);
206+const packageDir = path.join(workDir, "package");
207+fs.mkdirSync(packageDir, { recursive: true });
208+writeHookPackFiles({ pkgDir: packageDir, ...params });
209+const archivePath = path.join(workDir, "pack.tgz");
210+await tar.c({ cwd: workDir, file: archivePath, gzip: true }, ["package"]);
211+return fs.readFileSync(archivePath);
212+}
213+113214async function installArchiveFixture(params: { fileName: string; contents: Buffer }) {
114215const fixture = writeArchiveFixture(params);
115216const result = await installHooksFromArchive({
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。