返回源码地图

packages/session/session-format/src/filename.ts

main snapshot · da00f7f5358f · 正文引用章节 16;完整原文可核对,不声称全文件人工逐行审计

完整原文供逐行核对;页面收录不代表每行都经过人工语义审核。MIT 许可见 许可证。

1/** Canonical raw log basename shared by every generation-addressed Session artifact. */
2
3import { sessionFormatVersion } from './json.ts'
4
5const CANONICAL_LOG_FILENAME = /^session(?:\.v([1-9][0-9]*))?\.jsonl$/u
6
7/**
8 * Name the raw JSONL log of one immutable Session format generation. Version
9 * zero keeps the original `session.jsonl`; every later generation carries a
10 * lowercase numeric `.vN` component before the `.jsonl` suffix.
11 * @param version - non-negative safe integer Session format version.
12 * @returns the canonical basename, without any compression suffix.
13 */
14export function sessionFormatLogFilename(version: number): string {
15 const generation = sessionFormatVersion(version, 'Session log generation version')
16 return generation === 0 ? 'session.jsonl' : `session.v${generation}.jsonl`
17}
18
19/**
20 * Read the generation named by one raw JSONL log basename. Temporary,
21 * uppercase, leading-zero, `.v0`, and compression-suffixed names are not
22 * canonical.
23 * @param filename - one basename from a Session directory or archive.
24 * @returns its Session format version, or `undefined` when the name is not canonical.
25 */
26export function parseSessionFormatLogFilename(filename: string): number | undefined {
27 const match = CANONICAL_LOG_FILENAME.exec(filename)
28 if (match === null) return undefined
29 if (match[1] === undefined) return 0
30 const version = Number(match[1])
31 return Number.isSafeInteger(version) ? version : undefined
32}