repo.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { readFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { atomicWritePrivateTextFile, withFileLock } from "@zcode/shared/node";
  4. import {
  5. botsConfigFileSchema,
  6. botsStateFileSchema,
  7. type BotsConfigFile,
  8. type BotsStateFile,
  9. } from "@zcode/shared";
  10. import { getAppConfigDir } from "../paths.js";
  11. import {
  12. BOTS_CONFIG_FILE,
  13. BOTS_LEGACY_CONFIG_FILE,
  14. BOTS_LEGACY_STATE_FILE,
  15. BOTS_V2_STATE_FILE,
  16. BOTS_STATE_FILE,
  17. createDefaultBotsConfig,
  18. } from "./config.js";
  19. import { importLegacyBotConfig, importLegacyBotState } from "./storageMigration.js";
  20. async function readOptionalJson(path: string): Promise<unknown | undefined> {
  21. try {
  22. return JSON.parse(await readFile(path, "utf8"));
  23. } catch (error) {
  24. if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
  25. throw error;
  26. }
  27. }
  28. async function writeJson(path: string, value: unknown): Promise<void> {
  29. await atomicWritePrivateTextFile(path, `${JSON.stringify(value, null, 2)}\n`);
  30. }
  31. export class BotsRepo {
  32. async readConfig(): Promise<BotsConfigFile> {
  33. const path = join(getAppConfigDir(), BOTS_CONFIG_FILE);
  34. return withFileLock(path, async () => {
  35. const current = await readOptionalJson(path);
  36. // 回滚兼容:v3 已存在就只认 v3;损坏时暴露错误,绝不能恢复旧 Bot 或覆盖用户新修改。
  37. if (current !== undefined) return botsConfigFileSchema.parse(current);
  38. const legacy = await readOptionalJson(join(getAppConfigDir(), BOTS_LEGACY_CONFIG_FILE));
  39. const config = botsConfigFileSchema.parse(
  40. legacy === undefined ? createDefaultBotsConfig() : importLegacyBotConfig(legacy),
  41. );
  42. await writeJson(path, config);
  43. return config;
  44. });
  45. }
  46. async writeConfig(config: BotsConfigFile): Promise<BotsConfigFile> {
  47. const parsed = botsConfigFileSchema.parse(config);
  48. const path = join(getAppConfigDir(), BOTS_CONFIG_FILE);
  49. await withFileLock(path, () => writeJson(path, parsed));
  50. return parsed;
  51. }
  52. async readState(): Promise<BotsStateFile> {
  53. const path = join(getAppConfigDir(), BOTS_STATE_FILE);
  54. return withFileLock(path, async () => {
  55. const current = await readOptionalJson(path);
  56. if (current !== undefined) return botsStateFileSchema.parse(current);
  57. const v2 = await readOptionalJson(join(getAppConfigDir(), BOTS_V2_STATE_FILE));
  58. const legacy =
  59. v2 === undefined
  60. ? await readOptionalJson(join(getAppConfigDir(), BOTS_LEGACY_STATE_FILE))
  61. : v2;
  62. const state = botsStateFileSchema.parse(
  63. legacy === undefined ? { version: 3, bots: {} } : importLegacyBotState(legacy),
  64. );
  65. // 在同一文件锁内固定迁移结果;后续登录/套餐变化不再重新解释旧身份。
  66. await writeJson(path, state);
  67. return state;
  68. });
  69. }
  70. async writeState(state: BotsStateFile): Promise<BotsStateFile> {
  71. const parsed = botsStateFileSchema.parse(state);
  72. const path = join(getAppConfigDir(), BOTS_STATE_FILE);
  73. await withFileLock(path, () => writeJson(path, parsed));
  74. return parsed;
  75. }
  76. }