Archived
fix(sessions): add safe legacy archive migration core
This commit is contained in:
@@ -31,7 +31,7 @@ export interface ArchivedSessionRecord {
|
||||
parentSessionPath?: string;
|
||||
}
|
||||
|
||||
interface ArchiveFile {
|
||||
export interface SessionArchiveFile {
|
||||
sessions: ArchivedSessionRecord[];
|
||||
}
|
||||
|
||||
@@ -153,17 +153,17 @@ export class SessionArchiveStore {
|
||||
}
|
||||
}
|
||||
|
||||
private async read(): Promise<ArchiveFile> {
|
||||
private async read(): Promise<SessionArchiveFile> {
|
||||
try {
|
||||
const value: unknown = JSON.parse(await readFile(this.filePath, "utf8"));
|
||||
return parseArchiveFile(value);
|
||||
return parseSessionArchiveFile(value);
|
||||
} catch (error: unknown) {
|
||||
if (isNodeErrorWithCode(error, "ENOENT")) return { sessions: [] };
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private async write(data: ArchiveFile): Promise<void> {
|
||||
private async write(data: SessionArchiveFile): Promise<void> {
|
||||
await mkdir(dirname(this.filePath), { recursive: true });
|
||||
const tempPath = join(dirname(this.filePath), `.${basename(this.filePath)}.${String(process.pid)}.${Date.now().toString()}.${randomUUID()}.tmp`);
|
||||
try {
|
||||
@@ -231,7 +231,7 @@ async function pathExists(path: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
function parseArchiveFile(value: unknown): ArchiveFile {
|
||||
export function parseSessionArchiveFile(value: unknown): SessionArchiveFile {
|
||||
if (!isRecord(value) || !Array.isArray(value["sessions"])) throw new Error("Invalid archive file");
|
||||
return { sessions: value["sessions"].map(parseArchivedSessionRecord) };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user