Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/cli/unpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ export async function unpackExtension({

for (const relativePath in decompressed) {
if (Object.prototype.hasOwnProperty.call(decompressed, relativePath)) {
// Skip explicit directory entries (keys ending in "/"). ZIP archives
// produced by most tooling (e.g. `zip -r`) include these with empty
// data; writing to a path with a trailing slash fails and would abort
// the whole unpack. Needed parent directories are created below.
if (relativePath.endsWith("/")) {
continue;
}

const data = decompressed[relativePath];
const fullPath = join(finalOutputDir, relativePath);

Expand Down
41 changes: 41 additions & 0 deletions test/unpack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import fs from "node:fs";
import { join } from "node:path";

import { zipSync } from "fflate";

import { unpackExtension } from "../src/cli/unpack";

describe("unpackExtension", () => {
const tmpRoot = join(__dirname, "temp-unpack-dir-entries");
const mcpbPath = join(tmpRoot, "demo.mcpb");
const outDir = join(tmpRoot, "out");

beforeEach(() => {
fs.rmSync(tmpRoot, { recursive: true, force: true });
fs.mkdirSync(tmpRoot, { recursive: true });
});

afterAll(() => {
fs.rmSync(tmpRoot, { recursive: true, force: true });
});

it("unpacks archives that contain explicit directory entries", () => {
// Standard ZIP tooling emits directory entries (keys ending in "/").
const archive = zipSync({
"manifest.json": new TextEncoder().encode("{}"),
"server/": new Uint8Array(0),
"server/index.js": new TextEncoder().encode("console.log('hi');"),
});
fs.writeFileSync(mcpbPath, archive);

const ok = unpackExtension({ mcpbPath, outputDir: outDir, silent: true });

return Promise.resolve(ok).then((result) => {
expect(result).toBe(true);
expect(fs.existsSync(join(outDir, "manifest.json"))).toBe(true);
expect(fs.existsSync(join(outDir, "server", "index.js"))).toBe(true);
// The directory entry must not be written as a file.
expect(fs.statSync(join(outDir, "server")).isDirectory()).toBe(true);
});
});
});