provider-registry-runtime.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {
  2. ProviderRegistryService,
  3. MutableAccountProviderConfigSource,
  4. createFailClosedAccountProviderConfigSnapshot,
  5. type AccountProviderConfigSnapshot,
  6. type ProviderSource,
  7. } from "@zcode/provider";
  8. import {
  9. NodeProviderConfigRuntime,
  10. type NodeProviderConfigRuntimeOptions,
  11. } from "./provider-config-runtime.js";
  12. export interface NodeProviderRegistryRuntimeOptions extends NodeProviderConfigRuntimeOptions {
  13. readonly accountSource?: ProviderSource<AccountProviderConfigSnapshot>;
  14. readonly createAccountSource?: (
  15. configService: NodeProviderConfigRuntime["configService"],
  16. ) => ProviderSource<AccountProviderConfigSnapshot>;
  17. }
  18. /** 一个 Node.js 进程内共享的 Config + Registry 生命周期。 */
  19. export class NodeProviderRegistryRuntime {
  20. readonly configService: NodeProviderConfigRuntime["configService"];
  21. readonly registryService: ProviderRegistryService;
  22. readonly #configRuntime: NodeProviderConfigRuntime;
  23. readonly #accountSource: ProviderSource<AccountProviderConfigSnapshot>;
  24. #disposed = false;
  25. constructor(options: NodeProviderRegistryRuntimeOptions) {
  26. this.#configRuntime = new NodeProviderConfigRuntime(options);
  27. this.configService = this.#configRuntime.configService;
  28. this.#accountSource =
  29. options.createAccountSource?.(this.configService) ??
  30. options.accountSource ??
  31. new MutableAccountProviderConfigSource();
  32. this.registryService = new ProviderRegistryService({
  33. configSource: this.configService,
  34. accountSource: this.#accountSource,
  35. });
  36. }
  37. async start(): Promise<void> {
  38. this.#assertNotDisposed();
  39. await this.#configRuntime.start();
  40. if (this.#accountSource instanceof MutableAccountProviderConfigSource) {
  41. const account = await this.#accountSource.read();
  42. if (account.basedOnZCodeBuiltinRevision === "uninitialized") {
  43. this.#accountSource.replace(
  44. createFailClosedAccountProviderConfigSnapshot(await this.configService.read()),
  45. "initial-fail-closed",
  46. );
  47. }
  48. }
  49. return this.registryService.start();
  50. }
  51. get personalRepository(): NodeProviderConfigRuntime["personalRepository"] {
  52. return this.#configRuntime.personalRepository;
  53. }
  54. onDidCheckZCodeBuiltin(listener: () => Promise<void>): () => void {
  55. return this.#configRuntime.onDidCheckZCodeBuiltin(listener);
  56. }
  57. dispose(): void {
  58. if (this.#disposed) return;
  59. this.#disposed = true;
  60. this.registryService.dispose();
  61. this.#configRuntime.dispose();
  62. }
  63. #assertNotDisposed(): void {
  64. if (this.#disposed) throw new Error("NodeProviderRegistryRuntime 已 dispose");
  65. }
  66. }
  67. export function createNodeProviderRegistryRuntime(
  68. options: NodeProviderRegistryRuntimeOptions,
  69. ): NodeProviderRegistryRuntime {
  70. return new NodeProviderRegistryRuntime(options);
  71. }