102 lines
6.2 KiB
TypeScript
102 lines
6.2 KiB
TypeScript
import {
|
|
BlobWriter,
|
|
TextReader,
|
|
Uint8ArrayReader,
|
|
ZipWriter,
|
|
} from "@zip.js/zip.js";
|
|
|
|
export async function createEpubFixture(
|
|
options: {
|
|
brokenLink?: boolean;
|
|
activeContent?: boolean;
|
|
compressedMimetype?: boolean;
|
|
svgTitlePage?: boolean;
|
|
svgImageHref?: string;
|
|
repeatedImages?: number;
|
|
packagedStyles?: boolean;
|
|
obfuscatedFont?: boolean;
|
|
secondChapter?: boolean;
|
|
direction?: "ltr" | "rtl";
|
|
fixedLayout?: boolean;
|
|
} = {},
|
|
): Promise<File> {
|
|
const packagedStyles = options.packagedStyles || options.obfuscatedFont;
|
|
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>`,
|
|
),
|
|
);
|
|
if (options.obfuscatedFont)
|
|
await writer.add(
|
|
"META-INF/encryption.xml",
|
|
new TextReader(
|
|
`<?xml version="1.0" encoding="UTF-8"?><encryption xmlns="urn:oasis:names:tc:opendocument:xmlns:container" xmlns:enc="http://www.w3.org/2001/04/xmlenc#"><enc:EncryptedData><enc:EncryptionMethod Algorithm="http://www.idpf.org/2008/embedding"/><enc:CipherData><enc:CipherReference URI="EPUB/fonts/book.woff2"/></enc:CipherData></enc:EncryptedData></encryption>`,
|
|
),
|
|
);
|
|
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"${options.direction ? ` dir="${options.direction}"` : ""}><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>${options.fixedLayout ? '<meta property="rendition:layout">pre-paginated</meta><meta property="rendition:orientation">portrait</meta><meta property="rendition:spread">none</meta>' : ""}</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"/>${options.secondChapter ? '<item id="chapter-two" href="chapter-two.xhtml" media-type="application/xhtml+xml"/>' : ""}<item id="cover" href="cover.png" media-type="image/png" properties="cover-image"/>${packagedStyles ? '<item id="style" href="styles/book.css" media-type="text/css"/><item id="font" href="fonts/book.woff2" media-type="font/woff2"/>' : ""}${options.svgTitlePage ? '<item id="title-page" href="title.svg" media-type="image/svg+xml"/>' : ""}</manifest><spine${options.direction ? ` page-progression-direction="${options.direction}"` : ""}>${options.svgTitlePage ? '<itemref idref="title-page"/>' : ""}<itemref idref="chapter"/>${options.secondChapter ? '<itemref idref="chapter-two"/>' : ""}</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>${options.secondChapter ? '<ol><li><a href="chapter-two.xhtml#answer">Second chapter</a></li></ol>' : ""}</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"${options.direction ? ` dir="${options.direction}"` : ""}><head><title>Opening chapter</title>${packagedStyles ? '<link rel="stylesheet" href="styles/book.css"/>' : ""}</head><body><h1 id="opening">Hello</h1><p>Local reading and searchable phrase.</p>${options.activeContent ? '<script>globalThis.pwned=true</script><form><input/></form><meta http-equiv="refresh" content="0;url=https://example.invalid/export-leak"/><template><iframe src="https://example.invalid/template-leak"></iframe></template>' : ""}${Array.from({ length: options.repeatedImages ?? 1 }, () => '<img src="cover.png"/>').join("")}<a href="${options.brokenLink ? "missing.xhtml" : options.secondChapter ? "chapter-two.xhtml#answer" : "nav.xhtml"}">Next</a></body></html>`,
|
|
),
|
|
);
|
|
if (options.secondChapter)
|
|
await writer.add(
|
|
"EPUB/chapter-two.xhtml",
|
|
new TextReader(
|
|
`<?xml version="1.0"?><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Second chapter</title></head><body><h1 id="answer">Answer</h1><p>The searchable phrase appears twice: searchable phrase.</p></body></html>`,
|
|
),
|
|
);
|
|
if (packagedStyles) {
|
|
await writer.add(
|
|
"EPUB/styles/book.css",
|
|
new TextReader(
|
|
`@font-face{font-family:Fixture;src:url("../fonts/book.woff2")}body{font-family:Fixture;background-image:url("https://example.invalid/tracker.png")}h1{color:rebeccapurple}`,
|
|
),
|
|
);
|
|
const fontBytes = new Uint8Array([0x77, 0x4f, 0x46, 0x32, 0, 0, 0, 0]);
|
|
if (options.obfuscatedFont) {
|
|
const key = new Uint8Array(
|
|
await crypto.subtle.digest(
|
|
"SHA-1",
|
|
new TextEncoder().encode("urn:test:book"),
|
|
),
|
|
);
|
|
for (let index = 0; index < fontBytes.length; index += 1)
|
|
fontBytes[index] = fontBytes[index]! ^ key[index % key.length]!;
|
|
}
|
|
await writer.add("EPUB/fonts/book.woff2", new Uint8ArrayReader(fontBytes));
|
|
}
|
|
if (options.svgTitlePage)
|
|
await writer.add(
|
|
"EPUB/title.svg",
|
|
new TextReader(
|
|
`<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="100%" height="100%" viewBox="0 0 1283 1920" preserveAspectRatio="none"><a xlink:href="https://example.invalid/tracker"><image width="1283" height="1920" xlink:href="${options.svgImageHref ?? "cover.png"}"/></a></svg>`,
|
|
),
|
|
);
|
|
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" });
|
|
}
|