gitCheckpointStore.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { getAppConfigDir, getWorkspaceHash } from "../../paths.js";
  4. import type { GitCheckpointMeta } from "@zcode/shared";
  5. function isGitCheckpointMeta(value: unknown): value is GitCheckpointMeta {
  6. if (typeof value !== "object" || value === null) {
  7. return false;
  8. }
  9. const candidate = value as Partial<GitCheckpointMeta>;
  10. return (
  11. typeof candidate.checkpointId === "string" &&
  12. typeof candidate.workspacePath === "string" &&
  13. typeof candidate.repoRoot === "string" &&
  14. typeof candidate.workspaceInRepoPath === "string" &&
  15. typeof candidate.createdAt === "number" &&
  16. typeof candidate.refName === "string" &&
  17. typeof candidate.commitOid === "string" &&
  18. candidate.scope === "workspace"
  19. );
  20. }
  21. /** 一个 workspace 的检查点目录;项目数据删除按同一规则定位,不能另算 hash。 */
  22. export function getWorkspaceCheckpointDirectory(workspacePath: string, rootDir?: string): string {
  23. return join(rootDir ?? join(getAppConfigDir(), "checkpoints"), getWorkspaceHash(workspacePath));
  24. }
  25. export class GitCheckpointStore {
  26. constructor(
  27. private readonly options?: {
  28. rootDir?: string;
  29. },
  30. ) {}
  31. private readonly writeChains = new Map<string, Promise<void>>();
  32. private checkpointDir(workspacePath: string): string {
  33. return getWorkspaceCheckpointDirectory(workspacePath, this.options?.rootDir);
  34. }
  35. private checkpointFilePath(workspacePath: string, checkpointId: string): string {
  36. return join(this.checkpointDir(workspacePath), `${checkpointId}.json`);
  37. }
  38. private async waitForPendingWrite(filePath: string): Promise<void> {
  39. await this.writeChains.get(filePath)?.catch(() => {});
  40. }
  41. private enqueueWrite<T>(filePath: string, operation: () => Promise<T>): Promise<T> {
  42. const previous = this.writeChains.get(filePath) ?? Promise.resolve();
  43. const result = previous.catch(() => {}).then(operation);
  44. const completion = result.then(
  45. () => undefined,
  46. () => undefined,
  47. );
  48. this.writeChains.set(filePath, completion);
  49. void completion.finally(() => {
  50. if (this.writeChains.get(filePath) === completion) {
  51. this.writeChains.delete(filePath);
  52. }
  53. });
  54. return result;
  55. }
  56. private async writeManifestAtomic(filePath: string, meta: GitCheckpointMeta): Promise<void> {
  57. const tempPath = `${filePath}.${process.pid}.${Date.now().toString(36)}.${Math.random()
  58. .toString(36)
  59. .slice(2, 8)}.tmp`;
  60. await writeFile(tempPath, `${JSON.stringify(meta, null, 2)}\n`, "utf-8");
  61. await rename(tempPath, filePath);
  62. }
  63. async save(meta: GitCheckpointMeta): Promise<void> {
  64. const filePath = this.checkpointFilePath(meta.workspacePath, meta.checkpointId);
  65. await this.enqueueWrite(filePath, async () => {
  66. await mkdir(this.checkpointDir(meta.workspacePath), { recursive: true });
  67. await this.writeManifestAtomic(filePath, meta);
  68. });
  69. }
  70. async load(workspacePath: string, checkpointId: string): Promise<GitCheckpointMeta | null> {
  71. const filePath = this.checkpointFilePath(workspacePath, checkpointId);
  72. try {
  73. await this.waitForPendingWrite(filePath);
  74. const raw = await readFile(filePath, "utf-8");
  75. const parsed = JSON.parse(raw) as unknown;
  76. return isGitCheckpointMeta(parsed) ? parsed : null;
  77. } catch {
  78. return null;
  79. }
  80. }
  81. async delete(workspacePath: string, checkpointId: string): Promise<void> {
  82. const filePath = this.checkpointFilePath(workspacePath, checkpointId);
  83. await this.enqueueWrite(filePath, async () => {
  84. await rm(filePath, { force: true });
  85. });
  86. }
  87. }