import { expect, test } from "bun:test";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { crc32, deflateRawSync } from "node:zlib";
import "./chat/epub-reader.js";
function zip(files) {
const local = [], central = [];
let offset = 0;
for (const [path, contents, method = 8, declaredSize] of files) {
const name = Buffer.from(path), body = Buffer.from(contents);
const data = method === 8 ? deflateRawSync(body) : body;
const l = Buffer.alloc(30), c = Buffer.alloc(46);
l.writeUInt32LE(0x04034b50); c.writeUInt32LE(0x02014b50);
for (const [header, shift] of [[l, 0], [c, 2]]) {
header.writeUInt16LE(20, 4 + shift);
header.writeUInt16LE(method, 8 + shift);
header.writeUInt32LE(crc32(body), 14 + shift);
header.writeUInt32LE(data.length, 18 + shift);
header.writeUInt32LE(declaredSize ?? body.length, 22 + shift);
header.writeUInt16LE(name.length, 26 + shift);
}
c.writeUInt32LE(offset, 42);
local.push(l, name, data); central.push(c, name);
offset += l.length + name.length + data.length;
}
const index = Buffer.concat(central), end = Buffer.alloc(22);
end.writeUInt32LE(0x06054b50);
end.writeUInt16LE(files.length, 8); end.writeUInt16LE(files.length, 10);
end.writeUInt32LE(index.length, 12); end.writeUInt32LE(offset, 16);
return "data:application/epub+zip;base64," + Buffer.concat([...local, index, end]).toString("base64");
}
function book(chapters, extras = []) {
const manifest = chapters.map(([href, , type = "application/xhtml+xml"], i) =>
`
红楼梦𠀀。
次章。
有 空格
下一行', "text/html"]
], [["META-INF/encryption.xml", '
同文
")], ["../text/./第%20一.xhtml#end", null], ["../text/second.xhtml", xhtml("同文
")]]), hybrid: book([ ["../cover.xhtml", xhtml('')], [first, xhtml("可读正文
")], ["../text/scanned.html", '")] ]), legacy: book([[first, '
古 籍—𠀀 完
']]), long: book([[first, xhtml("" + "文".repeat(100000) + "末
")]]), missing: book([[first, xhtml("第一页
")], [second, null]]), locked: book([[first, xhtml("加密章节
")]], [["META-INF/encryption.xml", '坏了"]]), unterminated: book([[first, "
" + "