Files
reader/web/test/helpers/fixtures.ts
cnb.dQC5J6ohAGA c18664f1e3 feat(reader): 按窗口读取、真机可编译的 Android 壳,以及分章契约的收口
两件事:把 PR #8 里那四项合并进来(A 选项),然后把 issue 里一直没落地的
「一次只取一章」从服务端契约一路做到客户端。

## 一、PR #8 的四项(Android 外壳最值钱)

- Android 原生整页图渲染:NativePageView + 桥接 v2(renderPage/hidePage)。
  只画整页图,EPUB 永远留在 WebView;画不了就返回 false 回退,不出现空白页。
- 离线队列:脏集合语义固定为契约,补上「请求在途时位置被改写」的三条用例。
- TXT 编码探测:GB18030 与 Big5 都解一遍再按损坏证据评分选优。
  原来「先 GB18030 失败再 Big5」永远走不到第二步——两者字节范围重叠,
  同一串字节通常两边都能解出合法字符,繁体书会整本乱码。
- 格式嗅探:magic number 优先于扩展名,并明确拒绝非书籍的媒体容器。

## 二、Android 壳第一次真的编译通过

上一轮说「没有 JDK/SDK,只写了契约」,这一轮补上了环境并真的编译。
编译立刻暴露三处——都是只有编译才能发现的:

- colors.xml 的注释里写了 --reader-paper。XML 禁止注释中出现双连字符,
  AAPT 直接拒绝整个文件。Android 壳此前从未构建过,所以从未发现。
- NativePageView.kt 用了 Uint8Array。那是 JavaScript 类型,Kotlin 里不存在;
  桥接的另一侧是 ByteArray。
- AndroidManifest.xml 引用了 @mipmap/ic_launcher,而资源里没有这个文件。

三条都让 :app:assembleDebug 失败,也就是说这个 APK 此前根本产不出来。
现在是 BUILD SUCCESSFUL,产物 3.6MB。

## 三、分窗读取:从契约做到客户端

服务端:
- /manifest 省略 group 现在等价于 group=0。原来省略意味着「发全部条目」,
  于是一本 1200 章的书在它最需要快的那个请求(打开书)里收到 1200 条记录,
  等于分窗对不传参的客户端完全失效。要完整结构用 ?group=all。
- /toc 在没有 OPF 标题映射时回落到章节自身的第一个标题。手作 EPUB 大量
  没有标题映射,原来整本目录会读成「第 1 章 … 第 300 章」,等于没有目录。
- 扫描时的 spine 读取不再解析 NCX 与 nav:扫一遍全库本该只读一次包文档。
- 尾部 Range(bytes=-N)在超过 8MB 时退化为整包响应并撤回 Accept-Ranges。
  前向流无法跳到文件末尾,服务这种请求意味着为三个字节读完整个 400MB 漫画包。

客户端:
- 新增 formats/remote-zip.ts:走 HTTP Range 的只读 ZIP,让浏览器端也能不下整本书。
- 新增 formats/windowed.ts:分窗文档。分窗是传输边界,目录必须完整,两者分开
  —— 合在一起就是原来目录面板显示「第 1 章 – 第 40 章」的原因。
- reader-screen 接上 /toc,并且点击目录项会先取对应窗口再跳。

## 验证

server:  133/133(typecheck + test + build)
web:     179 vitest + 29 node:test(typecheck + test + build)
android: BUILD SUCCESSFUL,app-debug.apk 3.6MB
shell:   契约双向校验 + 渲染模式词表校验通过
2026-09-16 19:51:38 +08:00

214 lines
7.9 KiB
TypeScript

import JSZip from 'jszip';
/**
* Fixture builders.
*
* Books are constructed in code rather than committed as binaries: a committed
* EPUB is an opaque blob nobody reviews, while these make the exact structure
* under test readable — which matters when the failing assertion is about spine
* order or a percent-encoded href.
*/
export async function makeEpub(options: {
chapters: Array<{ id: string; href: string; title: string; extra?: string }>;
opfPath?: string;
styles?: Array<{ path: string; css: string }>;
toc?: Array<{ href: string; label: string }>;
ncx?: Array<{ src: string; label: string }>;
direction?: 'ltr' | 'rtl';
includeIdentifier?: boolean;
cover?: { href: string; bytes: Uint8Array; mediaType: string };
/**
* Explicit spine idref order. Omitted means "same order as `chapters`".
*
* Exists so a test can assert that reading order comes from the spine rather
* than from the manifest or the archive, which is the mistake a naive loader
* makes on roughly every second real-world EPUB.
*/
spineOrder?: string[];
}): Promise<Uint8Array> {
const zip = new JSZip();
const opfPath = options.opfPath ?? 'OEBPS/content.opf';
const baseDir = opfPath.includes('/') ? opfPath.slice(0, opfPath.lastIndexOf('/') + 1) : '';
zip.file('mimetype', 'application/epub+zip');
zip.file(
'META-INF/container.xml',
`<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles><rootfile full-path="${opfPath}" media-type="application/oebps-package+xml"/></rootfiles>
</container>`,
);
const manifestItems: string[] = options.chapters.map(
(chapter) => `<item id="${chapter.id}" href="${chapter.href}" media-type="application/xhtml+xml"/>`,
);
const spineItems = (options.spineOrder ?? options.chapters.map((chapter) => chapter.id)).map(
(id) => `<itemref idref="${id}"/>`,
);
const properties: string[] = [];
if (options.toc) {
manifestItems.push('<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>');
properties.push('nav');
}
if (options.ncx) {
manifestItems.push('<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>');
}
if (options.styles) {
for (const [index, style] of options.styles.entries()) {
manifestItems.push(`<item id="css${index}" href="${style.path}" media-type="text/css"/>`);
}
}
if (options.cover) {
manifestItems.push(
`<item id="cover-img" href="${options.cover.href}" media-type="${options.cover.mediaType}" properties="cover-image"/>`,
);
manifestItems.push('<item id="cover-meta" href="cover.xhtml" media-type="application/xhtml+xml"/>');
}
zip.file(
opfPath,
`<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="bookid"${options.direction ? ` page-progression-direction="${options.direction}"` : ''}>
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>测试书</dc:title>
<dc:creator>某人</dc:creator>
${options.includeIdentifier === false ? '' : '<dc:identifier id="bookid">urn:uuid:test-1234</dc:identifier>'}
${options.cover ? '<meta name="cover" content="cover-img"/>' : ''}
</metadata>
<manifest>
${manifestItems.join('\n ')}
</manifest>
<spine${options.ncx ? ' toc="ncx"' : ''}>
${spineItems.join('\n ')}
</spine>
</package>`,
);
for (const chapter of options.chapters) {
zip.file(
`${baseDir}${chapter.href}`,
`<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>${chapter.title}</title></head>
<body><h1>${chapter.title}</h1><p>正文内容</p>${chapter.extra ?? ''}</body></html>`,
);
}
if (options.styles) {
for (const style of options.styles) zip.file(`${baseDir}${style.path}`, style.css);
}
if (options.toc) {
zip.file(
`${baseDir}nav.xhtml`,
`<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"><body>
<nav epub:type="toc"><ol>
${options.toc.map((entry) => `<li><a href="${entry.href}">${entry.label}</a></li>`).join('\n')}
</ol></nav></body></html>`,
);
}
if (options.ncx) {
zip.file(
`${baseDir}toc.ncx`,
`<?xml version="1.0" encoding="utf-8"?>
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1"><navMap>
${options.ncx
.map(
(entry) => `<navPoint id="np-${entry.src}"><navLabel><text>${entry.label}</text></navLabel><content src="${entry.src}"/></navPoint>`,
)
.join('\n')}
</navMap></ncx>`,
);
}
if (options.cover) {
zip.file(`${baseDir}${options.cover.href}`, options.cover.bytes);
zip.file(
`${baseDir}cover.xhtml`,
`<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"><body>
<div><img src="${options.cover.href}" alt="cover"/></div>
</body></html>`,
);
}
return zip.generateAsync({ type: 'uint8array' });
}
export async function makeCbz(pages: Array<{ name: string; bytes?: Uint8Array }>): Promise<Uint8Array> {
const zip = new JSZip();
for (const page of pages) {
zip.file(page.name, page.bytes ?? PNG_1X1);
}
return zip.generateAsync({ type: 'uint8array' });
}
/** A minimal valid 1x1 PNG, so image bytes are real rather than filler. */
export const PNG_1X1 = new Uint8Array([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4,
0x89, 0x00, 0x00, 0x00, 0x0a, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x00, 0x01, 0x00, 0x00,
0x05, 0x00, 0x01, 0x0d, 0x0a, 0x2d, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
0x42, 0x60, 0x82,
]);
/**
* GB18030 byte sequences for the detection tests.
*
* Hard-coded rather than produced with `Buffer.from(text, 'gb18030')` because
* Node's small-ICU build can *decode* GB18030 but not encode it. The bytes below
* are the canonical GBK/GB18030 encodings, which is what makes them a real test
* of the fallback path rather than of the test helper.
*/
export const GB18030_SAMPLES: Record<string, number[]> = {
// 第一章 起始
'第一章 起始': [0xb5, 0xda, 0xd2, 0xbb, 0xd5, 0xc2, 0x20, 0xc6, 0xf0, 0xca, 0xbc],
// 测试
测试: [0xb2, 0xe2, 0xca, 0xd4],
// 中文内容
中文内容: [0xd6, 0xd0, 0xce, 0xc4, 0xc4, 0xda, 0xc8, 0xdd],
};
export function gb18030Bytes(text: string): Uint8Array {
const bytes = GB18030_SAMPLES[text];
if (!bytes) throw new Error(`no GB18030 fixture for ${text}`);
return new Uint8Array(bytes);
}
/**
* Big5 byte sequences for the same reason, and for one more.
*
* Big5 is not a curiosity here: it is the encoding of a real share of
* Traditional-Chinese libraries, and — unlike GB18030 — its byte sequences
* usually *also* decode as valid GB18030. That overlap is exactly what the
* detection heuristic has to resolve, so these bytes are the test.
*/
export const BIG5_SAMPLES: Record<string, number[]> = {
// 第一章 起始
'第一章 起始': [0xb2, 0xc4, 0xa4, 0x40, 0xb3, 0xb9, 0x20, 0xb0, 0x5f, 0xa9, 0x6c],
// 測試
測試: [0xb4, 0xfa, 0xb8, 0xd5],
// 中文內容
中文內容: [0xa4, 0xa4, 0xa4, 0xe5, 0xa4, 0xba, 0xae, 0x65],
// 第一章 測試內容這是一段中文文本用來判斷亂碼
'第一章 測試內容這是一段中文文本用來判斷亂碼': [
0xb2, 0xc4, 0xa4, 0x40, 0xb3, 0xb9, 0x20, 0xb4, 0xfa, 0xb8, 0xd5, 0xa4, 0xba, 0xae, 0x65, 0xb3, 0x6f,
0xac, 0x4f, 0xa4, 0x40, 0xac, 0x71, 0xa4, 0xa4, 0xa4, 0xe5, 0xa4, 0xe5, 0xa5, 0xbb, 0xa5, 0xce, 0xa8,
0xd3, 0xa7, 0x50, 0xc2, 0x5f, 0xb6, 0xc3, 0xbd, 0x58,
],
};
export function big5Bytes(text: string): Uint8Array {
const bytes = BIG5_SAMPLES[text];
if (!bytes) throw new Error(`no Big5 fixture for ${text}`);
return new Uint8Array(bytes);
}
export function utf8(text: string): Uint8Array {
return new TextEncoder().encode(text);
}