Files
apes-sidebar/src/editor-limits.ts
T
Menghuan1918 d4ca9eb6fb feat(transfer): 流式传输与原生打包,JSONL 预览/编辑与大文件分页预览
宿主半:
- /sidebar/file 与 /sidebar/html 改为 createReadStream + pipeline(新增
  file-stream.ts),删除 mediaLimit;流开始后失败由 writeJson 的
  headersSent 守卫降级为 destroy,不再抛 ERR_HTTP_HEADERS_SENT
- 上传本就是流式的,只删掉客户端 MAX_UPLOAD_BYTES 预检与宿主 limit 逐块检查
- 删除 readLimit / mediaLimit / uploadLimit 三个配置键,新增共享常量模块
  editor-limits.ts(MAX_EDITABLE_BYTES = 5 MiB、PAGE_BYTES、写入体护栏)
- fs.read 重塑为 text | binary | too-large 三态,不再有「截断文本」这个
  既不可信又不可保存的中间态;新增 fs.readPage 字节窗口分页(按行对齐、
  按 path+size+mtime 缓存首次探测到的编码)
- 删除 JSON 版 fs.write,改为 /sidebar/write raw 路由(文档即请求体),
  拆掉 MAX_BODY_BYTES=1MiB 这道保存墙;编码与行尾还原逻辑原样搬到
  fs-operations.writeWorkspaceText
- 打包改原生命令:新增 archive-tools.ts(zip → tar 探测、公共祖先定位、
  argv 规划、stdio 重定向),archive-route 改为后台写临时文件 + 流式下发,
  删除手写 ZIP writer src/zip.ts;新增 unavailable 错误码

客户端半:
- 新增 text-pages.ts(字节偏移分页游标,显式翻页)、jsonl.ts(逐行记录
  解析与摘要)、lang-jsonl.ts(每行独立的 JSON tokenizer,替代会为每条记录
  产生错误节点的 json() 语法)、LargeFileView.tsx(记录卡片 / 日志行两种
  只读分页视图)
- 新增 jsonl viewer 描述符(预览 = 记录列表,编辑 = 可保存的 CodeMirror)
- FileViewerProps 用 size/largeFile 取代 truncated;超限文件改为只读分页
  预览并可下载,不再提供编辑器
- tags.invalid 由纯白改为红色波浪线(浅色主题下原本白底白字不可见)
- zip 下载改回普通 <a download> 直链,不再把整包放进浏览器内存
2026-10-11 22:25:25 +08:00

77 lines
3.1 KiB
TypeScript

/**
* The one size constant both halves need, kept dependency-free on purpose.
*
* This replaces the retired `readLimit` deployment knob (`config.ts`). The old
* cap answered "how many bytes may the host hand the client" by truncating the
* document — which produced an editor that could neither be trusted nor saved
* (issue #732: saving a partial document destroys the tail). Transfer size is
* no longer a thing this plugin limits at all: upload, download, the HTML
* preview route and the archive route all stream, so a 10 GB file is simply a
* 10 GB file.
*
* What is still bounded is what an EDITOR can hold. CodeMirror keeps the whole
* document in the browser, so a file above this size gets a read-only paged
* preview (see `paged-text.tsx` / `jsonl-preview.tsx`) instead of an edit
* buffer. VS Code draws the same line at a different number — its
* `getLargeFileConfirmationLimit()` is 50 MB on the web build — and the shape
* of the answer (threshold → degraded read-only view) is deliberately the same.
*
* Imported by BOTH halves, so this module must stay free of node builtins and
* of anything that reaches them.
*/
/**
* Text files at or below this size load whole and are editable; anything above
* gets the read-only paged preview.
*/
export const MAX_EDITABLE_BYTES = 5 * 1024 * 1024
/**
* Bytes one `fs.readPage` request returns. Pages are trimmed back to the last
* newline so a page is normally a whole number of lines (and therefore, for
* JSONL, a whole number of records); a single line longer than this is
* returned untrimmed and stitched back together by the client.
*
* Sized so that even a file of very short lines lands in the low thousands of
* rendered rows per page — the preview renders a page's rows as plain elements,
* and the page size is the only thing bounding that.
*/
export const PAGE_BYTES = 128 * 1024
/**
* Hard ceiling on one editor save request body.
*
* This is a SERVER-SIDE SANITY GUARD, not a product limit: the editor cannot
* produce more than {@link MAX_EDITABLE_BYTES} of text, so the UI can never
* reach it. It exists because the save route receives a raw body and the
* encoding transform (`restoreEol` + `encodeText`) needs the whole document as
* a string.
*/
export const WRITE_BODY_GUARD_BYTES = 8 * 1024 * 1024
/**
* Format a byte count for the "this file is too large to edit" notice. Kept
* here beside the constant so the host and the client round identically.
*
* @param bytes - the file's size.
* @returns e.g. `12.4 MB`, `512 KB`, `900 B`.
*/
export function formatByteSize(bytes: number): string {
if (!Number.isFinite(bytes) || bytes < 0) return '0 B'
if (bytes < 1024) return `${Math.round(bytes)} B`
const units = ['KB', 'MB', 'GB', 'TB']
let value = bytes / 1024
let unit = 0
while (value >= 1024 && unit < units.length - 1) {
value /= 1024
unit += 1
}
return `${value >= 10 ? Math.round(value) : value.toFixed(1)} ${units[unit]}`
}
/**
* The human-facing form of {@link MAX_EDITABLE_BYTES}, for copy that names the
* editing threshold.
*/
export const MAX_EDITABLE_LABEL = '5 MB'