宿主半: - /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> 直链,不再把整包放进浏览器内存
96 lines
3.5 KiB
TypeScript
96 lines
3.5 KiB
TypeScript
/**
|
|
* JSONL (newline-delimited JSON) parsing for the preview.
|
|
*
|
|
* The editor's CodeMirror surface keeps using `@codemirror/lang-json` for
|
|
* `.json`; JSONL gets its own per-line language (see `lang-jsonl.ts`) because
|
|
* the stock JSON grammar treats a second top-level value as an error. For the
|
|
* PREVIEW the grammar is irrelevant: one line IS one record, so the preview
|
|
* parses each line on its own and never has to reason about the document as a
|
|
* single JSON value.
|
|
*
|
|
* Kept dependency-free and pure so the record handling is unit-testable
|
|
* without a DOM.
|
|
*/
|
|
import { extOf } from './paths.ts'
|
|
|
|
/** Whether a path is newline-delimited JSON (one record per line). */
|
|
export function isJsonlPath(path: string): boolean {
|
|
const ext = extOf(path)
|
|
return ext === 'jsonl' || ext === 'ndjson'
|
|
}
|
|
|
|
/** One parsed line. */
|
|
export type JsonlRecord =
|
|
| { ok: true; value: unknown }
|
|
| { ok: false; error: string }
|
|
|
|
/** A record that is an object or array can render as a `JsonTree`. */
|
|
export function isTreeRecord(value: unknown): value is object | unknown[] {
|
|
return typeof value === 'object' && value !== null
|
|
}
|
|
|
|
/**
|
|
* Parse one JSONL line.
|
|
*
|
|
* A blank line is NOT a record — JSONL writers commonly end the file with one
|
|
* newline, and the host's pages are line-aligned, so a trailing blank must not
|
|
* render as a broken record. It is reported as `{ok: false}` with an empty
|
|
* error so the caller can skip it instead.
|
|
*
|
|
* @param line - one line of the file, without its newline.
|
|
* @returns the parsed value, or the failure reason.
|
|
*/
|
|
export function parseRecord(line: string): JsonlRecord {
|
|
const trimmed = line.trim()
|
|
if (trimmed === '') return { ok: false, error: '' }
|
|
try {
|
|
return { ok: true, value: JSON.parse(trimmed) as unknown }
|
|
} catch (error: unknown) {
|
|
return { ok: false, error: error instanceof Error ? error.message : String(error) }
|
|
}
|
|
}
|
|
|
|
/** How many characters of a collapsed record's summary are kept. */
|
|
const SUMMARY_MAX = 200
|
|
|
|
/**
|
|
* One-line summary of a record for its collapsed row.
|
|
*
|
|
* Objects and arrays become `{a: 1, b: "x", rest: …}` rather than raw JSON:
|
|
* a collapsed row has to be scannable, and a raw `JSON.stringify` of a session
|
|
* event is a wall of punctuation. Nesting beyond the first level is elided —
|
|
* the expanded `JsonTree` is where detail lives.
|
|
*
|
|
* @param value - the parsed record.
|
|
* @returns a single-line summary, never longer than {@link SUMMARY_MAX}.
|
|
*/
|
|
export function summarizeRecord(value: unknown): string {
|
|
if (value === null) return 'null'
|
|
if (Array.isArray(value)) return `[${value.length} items]`
|
|
if (typeof value === 'object') {
|
|
const entries = Object.entries(value as Record<string, unknown>)
|
|
if (entries.length === 0) return '{}'
|
|
const parts: string[] = []
|
|
for (const [key, item] of entries) {
|
|
parts.push(`${key}: ${preview(item)}`)
|
|
if (parts.join(', ').length > SUMMARY_MAX) break
|
|
}
|
|
const joined = parts.join(', ')
|
|
return truncate(`{${joined}${parts.length < entries.length ? ', …' : ''}}`)
|
|
}
|
|
return truncate(JSON.stringify(value) ?? String(value))
|
|
}
|
|
|
|
/** The first-level preview of one property value. */
|
|
function preview(value: unknown): string {
|
|
if (value === null) return 'null'
|
|
if (Array.isArray(value)) return `[…${value.length}]`
|
|
if (typeof value === 'object') return '{…}'
|
|
return JSON.stringify(value) ?? String(value)
|
|
}
|
|
|
|
/** Clamp a summary to one scannable line. */
|
|
function truncate(text: string): string {
|
|
return text.length <= SUMMARY_MAX ? text : `${text.slice(0, SUMMARY_MAX - 1)}…`
|
|
}
|