Release EPUB Tools 0.1.0
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
import { Blob as NodeBlob, File as NodeFile } from "node:buffer";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { closeBook, openEpub } from "../../src/epub/archive";
|
||||
import { patchPackageMetadata, rebuildEpub } from "../../src/epub/export";
|
||||
import { resolvePackageHref, validateEntryPath } from "../../src/epub/paths";
|
||||
import { renderChapter, revokeRenderedChapter } from "../../src/epub/reader";
|
||||
import type { EpubBook } from "../../src/epub/types";
|
||||
import { createEpubFixture } from "./fixture";
|
||||
|
||||
let opened: EpubBook[] = [];
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal("Blob", NodeBlob);
|
||||
vi.stubGlobal("File", NodeFile);
|
||||
});
|
||||
afterEach(async () => {
|
||||
for (const book of opened) await closeBook(book);
|
||||
opened = [];
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("EPUB path policy", () => {
|
||||
it("resolves package-relative references and rejects traversal", () => {
|
||||
expect(
|
||||
resolvePackageHref("EPUB/text/chapter.xhtml", "../images/cover.png#art"),
|
||||
).toBe("EPUB/images/cover.png");
|
||||
expect(() => resolvePackageHref("chapter.xhtml", "../outside")).toThrow(
|
||||
/escapes/u,
|
||||
);
|
||||
expect(() => validateEntryPath("../escape")).toThrow(/traversal/u);
|
||||
});
|
||||
});
|
||||
|
||||
describe("EPUB package inspection", () => {
|
||||
it("opens metadata, navigation, spine, and bounded inventory", async () => {
|
||||
const book = await openEpub(await createEpubFixture());
|
||||
opened.push(book);
|
||||
expect(book.package.metadata).toMatchObject({
|
||||
title: "Fixture Book",
|
||||
language: "en",
|
||||
identifier: "urn:test:book",
|
||||
});
|
||||
expect(book.package.navigation[0]).toMatchObject({
|
||||
label: "Opening chapter",
|
||||
path: "EPUB/chapter.xhtml",
|
||||
});
|
||||
expect(book.package.spine[0]?.item?.path).toBe("EPUB/chapter.xhtml");
|
||||
expect(book.summary).toMatchObject({
|
||||
mimetypeFirst: true,
|
||||
mimetypeStored: true,
|
||||
encryptedResources: false,
|
||||
});
|
||||
expect(book.issues.filter((item) => item.severity === "error")).toEqual([]);
|
||||
});
|
||||
|
||||
it("reports active content and broken resources", async () => {
|
||||
const book = await openEpub(
|
||||
await createEpubFixture({ activeContent: true, brokenLink: true }),
|
||||
);
|
||||
opened.push(book);
|
||||
expect(book.issues.map((item) => item.code)).toEqual(
|
||||
expect.arrayContaining(["active-content", "broken-link"]),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("safe rendering and editing", () => {
|
||||
it("removes active content, disables links, and resolves local images", async () => {
|
||||
vi.stubGlobal("URL", {
|
||||
...URL,
|
||||
createObjectURL: vi.fn(() => "blob:fixture-cover"),
|
||||
revokeObjectURL: vi.fn(),
|
||||
});
|
||||
const book = await openEpub(
|
||||
await createEpubFixture({ activeContent: true }),
|
||||
);
|
||||
opened.push(book);
|
||||
const chapter = await renderChapter(book, "EPUB/chapter.xhtml");
|
||||
expect(chapter.html).not.toMatch(/<script|<form|<input/iu);
|
||||
expect(chapter.html).toContain('src="blob:fixture-cover"');
|
||||
expect(chapter.html).toContain('data-epub-href="nav.xhtml"');
|
||||
expect(chapter.html).toContain("default-src 'none'");
|
||||
revokeRenderedChapter(chapter);
|
||||
});
|
||||
|
||||
it("patches metadata and rebuilds a readable normalized EPUB", async () => {
|
||||
const book = await openEpub(await createEpubFixture());
|
||||
opened.push(book);
|
||||
const metadata = {
|
||||
...book.package.metadata,
|
||||
title: "Updated title",
|
||||
creators: ["One", "Two"],
|
||||
subjects: ["Testing"],
|
||||
};
|
||||
const xml = patchPackageMetadata(book.packageXml, metadata);
|
||||
expect(xml).toContain("Updated title");
|
||||
expect(xml.match(/<dc:creator/gu)).toHaveLength(2);
|
||||
const rebuilt = await rebuildEpub(book, {
|
||||
metadata,
|
||||
normalizeTimestamps: true,
|
||||
});
|
||||
const reopened = await openEpub(
|
||||
new File([rebuilt.blob], "rebuilt.epub", {
|
||||
type: "application/epub+zip",
|
||||
}),
|
||||
);
|
||||
opened.push(reopened);
|
||||
expect(reopened.package.metadata.title).toBe("Updated title");
|
||||
expect(reopened.package.metadata.creators).toEqual(["One", "Two"]);
|
||||
expect(reopened.summary).toMatchObject({
|
||||
mimetypeFirst: true,
|
||||
mimetypeStored: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import {
|
||||
BlobWriter,
|
||||
TextReader,
|
||||
Uint8ArrayReader,
|
||||
ZipWriter,
|
||||
} from "@zip.js/zip.js";
|
||||
|
||||
export async function createEpubFixture(
|
||||
options: {
|
||||
brokenLink?: boolean;
|
||||
activeContent?: boolean;
|
||||
compressedMimetype?: boolean;
|
||||
} = {},
|
||||
): Promise<File> {
|
||||
const writer = new ZipWriter(new BlobWriter("application/epub+zip"));
|
||||
await writer.add("mimetype", new TextReader("application/epub+zip"), {
|
||||
level: options.compressedMimetype ? 6 : 0,
|
||||
});
|
||||
await writer.add(
|
||||
"META-INF/container.xml",
|
||||
new TextReader(
|
||||
`<?xml version="1.0"?><container xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><rootfiles><rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/></rootfiles></container>`,
|
||||
),
|
||||
);
|
||||
await writer.add(
|
||||
"EPUB/package.opf",
|
||||
new TextReader(
|
||||
`<?xml version="1.0" encoding="UTF-8"?><package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="book-id"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:identifier id="book-id">urn:test:book</dc:identifier><dc:title>Fixture Book</dc:title><dc:language>en</dc:language><dc:creator>Ada Example</dc:creator></metadata><manifest><item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/><item id="chapter" href="chapter.xhtml" media-type="application/xhtml+xml"/><item id="cover" href="cover.png" media-type="image/png" properties="cover-image"/></manifest><spine><itemref idref="chapter"/></spine></package>`,
|
||||
),
|
||||
);
|
||||
await writer.add(
|
||||
"EPUB/nav.xhtml",
|
||||
new TextReader(
|
||||
`<?xml version="1.0"?><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"><head><title>Contents</title></head><body><nav epub:type="toc"><ol><li><a href="chapter.xhtml">Opening chapter</a></li></ol></nav></body></html>`,
|
||||
),
|
||||
);
|
||||
await writer.add(
|
||||
"EPUB/chapter.xhtml",
|
||||
new TextReader(
|
||||
`<?xml version="1.0"?><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Opening chapter</title></head><body><h1>Hello</h1><p>Local reading.</p>${options.activeContent ? "<script>globalThis.pwned=true</script><form><input/></form>" : ""}<img src="cover.png"/><a href="${options.brokenLink ? "missing.xhtml" : "nav.xhtml"}">Next</a></body></html>`,
|
||||
),
|
||||
);
|
||||
await writer.add(
|
||||
"EPUB/cover.png",
|
||||
new Uint8ArrayReader(
|
||||
new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
||||
),
|
||||
);
|
||||
const blob = await writer.close();
|
||||
return new File([blob], "fixture.epub", { type: "application/epub+zip" });
|
||||
}
|
||||
Reference in New Issue
Block a user