Files
reader/web/test/detect-and-resolve.test.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

134 lines
5.5 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { detectFormat } from '../src/formats/detect.ts';
import { BookArchive, dirnameOf, normalise, resolveHref } from '../src/formats/zip.ts';
import { makeCbz } from './helpers/fixtures.ts';
/**
* The archive and resolution helpers.
*
* These are the lowest-level pieces of the format layer and the ones whose bugs
* are hardest to see: a mis-resolved href does not throw, it silently produces a
* broken image on one page of one book.
*/
describe('BookArchive', () => {
it('lists files but not directory entries', async () => {
const archive = await BookArchive.open(await makeCbz([{ name: 'a/001.png' }, { name: 'a/002.png' }]));
expect(archive.names().sort()).toEqual(['a/001.png', 'a/002.png']);
});
it('reads an entry as bytes and as text', async () => {
const archive = await BookArchive.open(await makeCbz([{ name: 'note.txt', bytes: new TextEncoder().encode('hello') }]));
expect(await archive.text('note.txt')).toBe('hello');
expect((await archive.bytes('note.txt'))?.byteLength).toBe(5);
});
it('finds an entry whose path differs only by case or leading slash', async () => {
// Archives produced on Windows and by some comic tools carry either.
const archive = await BookArchive.open(await makeCbz([{ name: 'Pages/001.png' }]));
expect(await archive.bytes('pages/001.png')).not.toBeNull();
expect(await archive.bytes('/Pages/001.png')).not.toBeNull();
});
it('returns null for a genuinely absent entry instead of throwing', async () => {
const archive = await BookArchive.open(await makeCbz([{ name: 'a.png' }]));
expect(await archive.bytes('missing.png')).toBeNull();
expect(await archive.text('missing.png')).toBeNull();
});
});
describe('normalise', () => {
it('lowercases, strips a leading slash and percent-decodes', () => {
expect(normalise('/Pages/%E4%B8%AD.png')).toBe('pages/中.png');
expect(normalise('./a/b.png')).toBe('a/b.png');
});
});
describe('dirnameOf', () => {
it('returns an empty string for a root-level file', () => {
expect(dirnameOf('a.png')).toBe('');
});
it('returns everything before the last slash', () => {
expect(dirnameOf('a/b/c.png')).toBe('a/b');
});
});
describe('resolveHref', () => {
it('does not let a traversal escape above the archive root', () => {
// '../../../etc/passwd' must resolve to a path inside the archive, never to
// an absolute one. The result is looked up in a map either way, but a
// resolver that can produce `../../` is the kind of thing that later becomes
// a real file read.
const resolved = resolveHref('a', '../../../etc/passwd');
expect(resolved.startsWith('/')).toBe(false);
expect(resolved.includes('..')).toBe(false);
});
it('treats a scheme-qualified href as absolute', () => {
expect(resolveHref('a', 'mailto:x@example.com')).toBe('mailto:x@example.com');
expect(resolveHref('a', 'https://x/y')).toBe('https://x/y');
});
it('drops a query string but keeps the fragment out of the path', () => {
expect(resolveHref('a', 'b.png?v=2#frag')).toBe('a/b.png');
});
it('handles a dot-segment in the middle of a path', () => {
expect(resolveHref('a/b', './c/./d.png')).toBe('a/b/c/d.png');
});
});
describe('detectFormat edge cases', () => {
it('prefers the extension when a zip could be either', () => {
const zipMagic = new Uint8Array([0x50, 0x4b, 0x03, 0x04]);
expect(detectFormat('x.cbz', zipMagic)).toBe('cbz');
expect(detectFormat('x.epub', zipMagic)).toBe('epub');
});
it('does not mistake a plain zip of pages for an EPUB', () => {
const zipMagic = new Uint8Array([0x50, 0x4b, 0x03, 0x04]);
expect(detectFormat('pages.zip', zipMagic)).toBe('cbz');
});
it('returns unknown for an empty file with an unknown extension', () => {
expect(detectFormat('x.dat', new Uint8Array())).toBe('unknown');
});
it('detects an image by extension when there are no bytes yet', () => {
expect(detectFormat('scan.JPG')).toBe('image');
});
it('believes the bytes over an extension that cannot be right', () => {
// The realistic case, not a hypothetical one: a batch conversion tool writes
// a JPEG and names it after the source page. Trusting the extension means the
// PNG loader runs on JPEG bytes and the reader sees a broken image.
expect(detectFormat('page-012.png', jpeg())).toBe('image');
expect(detectFormat('chapter.xhtml', png())).toBe('image');
});
it('refuses a non-book that happens to have a book extension', () => {
// An audio file named `.txt` used to be handed to the text decoder, which
// produced fifty thousand replacement characters rather than "this is not a
// book". Naming it unknown is the honest answer.
expect(detectFormat('track.txt', new Uint8Array([0x49, 0x44, 0x33, 0x03]))).toBe('unknown');
expect(detectFormat('video.epub', new Uint8Array([0x1a, 0x45, 0xdf, 0xa3]))).toBe('unknown');
});
it('still lets the extension break the tie between two plausible zip formats', () => {
// The ZIP signature admits EPUB and CBZ, so this is the one case where the
// name is still evidence rather than noise.
const zipMagic = new Uint8Array([0x50, 0x4b, 0x03, 0x04]);
expect(detectFormat('some-comic.cbz', zipMagic)).toBe('cbz');
expect(detectFormat('some-book.epub', zipMagic)).toBe('epub');
});
});
function png(): Uint8Array {
return new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
}
function jpeg(): Uint8Array {
return new Uint8Array([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10]);
}