Skip to content

feat(converter): x2t WASM 走 gzip 客户端解压,解除 CF Pages 迁移硬阻塞#100

Merged
chaxus merged 2 commits into
mainfrom
feat/wasm-gzip-cf
Jul 5, 2026
Merged

feat(converter): x2t WASM 走 gzip 客户端解压,解除 CF Pages 迁移硬阻塞#100
chaxus merged 2 commits into
mainfrom
feat/wasm-gzip-cf

Conversation

@chaxus

@chaxus chaxus commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

背景

迁 Cloudflare Pages 的唯一硬阻塞:CF Pages 单文件上限 25 MiB,而 public/wasm/x2t/x2t.wasm = 55 M,部署时直接被拒。(dist 文件数 336,远低于 2 万上限,无其它阻塞。)

方案

x2t.js(Emscripten)一旦发现 Module.wasmBinary 有值就跳过自己对 x2t.wasm 的 fetch(源码第 284 行)。于是:

  • 只部署 x2t.wasm.gz(11M) + x2t.js,删掉 x2t.wasm(55M)与 x2t.wasm.br(8M)
  • @ranuts/converter 在注入 x2t.js 前新增 prepareWasmBinary():fetch(.gz) → 原生 DecompressionStream('gzip') 解压 → 塞进 window.Module.wasmBinary(无新依赖)

关键坑:服务器对 .gz 处理不一致

  • vite dev/preview.gzContent-Encoding: gzip → 浏览器已透明解压(体是裸 wasm \0asm)
  • CF/GitHub Pages 通常发原始 gzip 字节(1f 8b)

→ 按 magic bytes 探测:是 gzip 才手动解压,已是裸 wasm 就直接用。两种托管都正确。

验证(端到端)

环节 结果
isGzip=false 分支(preview 自动解压) 54.7M · WebAssembly.compile OK(28 exports)
isGzip=true 分支(静态托管原始 gzip) 54.7M · compile OK
Playwright 集成 x2t 运行时 onRuntimeInitialized OK;FS/ccall 可用;对 x2t.wasm 请求 0 次(只请求 .gz)
构建产物 find dist -size +25M(最大文件 11M)
回归 lint:ts 通过 · 240 tests 全过

影响

  • CF Pages 迁移唯一硬阻塞解除;对现有 GitHub Pages 部署也安全(magic 探测)
  • 改动集中在 converter 包 + 删两个大文件,主 app 零改动
  • converter dist/ 已 gitignore,CI pnpm installprepare 钩子用新 src 重建

遗留(非阻塞)

build:single(单 HTML)靠内联裸 wasm,删裸文件后需改为内联 .gz + 同款解压。非部署链,单列跟进。

详见 docs/explorations/2026-07-05-x2t-wasm-gzip-cf-pages.md

🤖 Generated with Claude Code

chaxus and others added 2 commits July 5, 2026 18:34
…gration

CF Pages rejects files >25 MiB at deploy; the raw x2t.wasm is 55 MB. Ship
only the ~11 MB x2t.wasm.gz and decompress it in the browser, handing the
bytes to Emscripten via Module.wasmBinary (which then skips its own fetch of
x2t.wasm entirely).

- prepareWasmBinary(): fetch .gz -> detect gzip magic (1f 8b) -> decompress
  with native DecompressionStream('gzip') only when still compressed. Some
  servers (Vite dev/preview) send Content-Encoding: gzip and the browser has
  already decompressed it (bytes start with \0asm); static hosts (CF/GitHub
  Pages) send raw gzip. The magic-byte check handles both.
- Drop public/wasm/x2t/x2t.wasm (55M) and x2t.wasm.br (8M); keep .gz + x2t.js.

Verified: both decompression branches compile a valid 54.7M wasm; Playwright
integration confirms x2t runtime initializes and zero requests hit the raw
x2t.wasm; dist has no file >25 MiB; lint + 240 tests pass.

Known follow-up (non-blocking): build:single inlines the raw wasm and needs
updating to inline the .gz instead.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Includes two docs carried in from #98 (privacy-analytics, sw-stale-build)
that were never formatted.
@chaxus chaxus merged commit 4be0e65 into main Jul 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant