endpoint-scoped-zcode-builtin-source.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import type { ProviderConfigLayerSnapshot, ProviderSource } from "@zcode/provider";
  2. import {
  3. normalizeZCodeBuiltinEndpointOrigin,
  4. resolveZCodeBuiltinCachePaths,
  5. } from "./zcode-builtin-cache-paths.js";
  6. import { NodeZCodeBuiltinProviderConfigSource } from "./zcode-builtin-provider-config-source.js";
  7. import {
  8. ZCodeBuiltinRemoteSynchronizer,
  9. type ZCodeBuiltinRefreshResult,
  10. type ZCodeBuiltinRemoteSynchronizerOptions,
  11. } from "./zcode-builtin-remote-synchronizer.js";
  12. export interface EndpointScopedZCodeBuiltinSourceOptions {
  13. readonly bundledFilePath: string;
  14. readonly environmentConfigRoot: string;
  15. readonly platform: string;
  16. readonly appVersion: string;
  17. readonly resolveEndpointOrigin: () => string | Promise<string>;
  18. readonly fetchRelease: ZCodeBuiltinRemoteSynchronizerOptions["fetchRelease"];
  19. readonly onRefreshResult?: ZCodeBuiltinRemoteSynchronizerOptions["onRefreshResult"];
  20. readonly watch?: boolean;
  21. }
  22. /**
  23. * 让 Environment 的 ZCode 控制面 Endpoint 同时决定 Active/LKG 与刷新控制路径。
  24. * Endpoint 切换只替换当前 Source,不读取上一 Endpoint 的缓存。
  25. */
  26. export class EndpointScopedZCodeBuiltinSource implements ProviderSource<ProviderConfigLayerSnapshot> {
  27. readonly #options: EndpointScopedZCodeBuiltinSourceOptions;
  28. readonly #listeners = new Set<(reason: string) => void>();
  29. #current: CurrentEndpointSource | null = null;
  30. #ensureInFlight: Promise<CurrentEndpointSource> | null = null;
  31. #disposed = false;
  32. constructor(options: EndpointScopedZCodeBuiltinSourceOptions) {
  33. this.#options = options;
  34. }
  35. async read(): Promise<ProviderConfigLayerSnapshot> {
  36. return (await this.#ensureCurrent()).source.read();
  37. }
  38. onDidChange(listener: (reason: string) => void): () => void {
  39. this.#assertNotDisposed();
  40. this.#listeners.add(listener);
  41. return () => this.#listeners.delete(listener);
  42. }
  43. async refresh(options?: { readonly force?: boolean }): Promise<ZCodeBuiltinRefreshResult> {
  44. return (await this.#ensureCurrent()).synchronizer.refresh(options);
  45. }
  46. /** 返回当前 Environment Endpoint 对应、已完成物化的 Active Config 路径。 */
  47. async resolveActiveFilePath(): Promise<string> {
  48. return (await this.#ensureCurrent()).activeFilePath;
  49. }
  50. dispose(): void {
  51. if (this.#disposed) return;
  52. this.#disposed = true;
  53. this.#current?.dispose();
  54. this.#current = null;
  55. this.#listeners.clear();
  56. }
  57. async #ensureCurrent(): Promise<CurrentEndpointSource> {
  58. this.#assertNotDisposed();
  59. if (this.#ensureInFlight) return this.#ensureInFlight;
  60. const ensure = this.#resolveCurrent().finally(() => {
  61. if (this.#ensureInFlight === ensure) this.#ensureInFlight = null;
  62. });
  63. this.#ensureInFlight = ensure;
  64. return ensure;
  65. }
  66. async #resolveCurrent(): Promise<CurrentEndpointSource> {
  67. const endpointOrigin = normalizeZCodeBuiltinEndpointOrigin(
  68. await this.#options.resolveEndpointOrigin(),
  69. );
  70. const paths = resolveZCodeBuiltinCachePaths({
  71. environmentConfigRoot: this.#options.environmentConfigRoot,
  72. platform: this.#options.platform,
  73. appVersion: this.#options.appVersion,
  74. zcodeEndpointOrigin: endpointOrigin,
  75. });
  76. if (this.#current?.activeFilePath === paths.activeFilePath) return this.#current;
  77. const source = new NodeZCodeBuiltinProviderConfigSource({
  78. bundledFilePath: this.#options.bundledFilePath,
  79. activeFilePath: paths.activeFilePath,
  80. watch: this.#options.watch,
  81. });
  82. const sourceDispose = source.onDidChange((reason) => this.#emit(reason));
  83. const synchronizer = new ZCodeBuiltinRemoteSynchronizer({
  84. source,
  85. controlFilePath: paths.controlFilePath,
  86. resolveEndpointKey: async () =>
  87. normalizeZCodeBuiltinEndpointOrigin(await this.#options.resolveEndpointOrigin()),
  88. fetchRelease: this.#options.fetchRelease,
  89. onRefreshResult: this.#options.onRefreshResult,
  90. });
  91. try {
  92. await source.read();
  93. this.#assertNotDisposed();
  94. } catch (error) {
  95. sourceDispose();
  96. synchronizer.dispose();
  97. source.dispose();
  98. throw error;
  99. }
  100. const previous = this.#current;
  101. const current = new CurrentEndpointSource(
  102. paths.activeFilePath,
  103. source,
  104. synchronizer,
  105. sourceDispose,
  106. );
  107. this.#current = current;
  108. previous?.dispose();
  109. if (previous) this.#emit("endpoint-changed");
  110. return current;
  111. }
  112. #emit(reason: string): void {
  113. if (this.#disposed) return;
  114. for (const listener of this.#listeners) listener(reason);
  115. }
  116. #assertNotDisposed(): void {
  117. if (this.#disposed) throw new Error("EndpointScopedZCodeBuiltinSource 已 dispose");
  118. }
  119. }
  120. class CurrentEndpointSource {
  121. constructor(
  122. readonly activeFilePath: string,
  123. readonly source: NodeZCodeBuiltinProviderConfigSource,
  124. readonly synchronizer: ZCodeBuiltinRemoteSynchronizer,
  125. readonly sourceDispose: () => void,
  126. ) {}
  127. dispose(): void {
  128. this.sourceDispose();
  129. this.synchronizer.dispose();
  130. this.source.dispose();
  131. }
  132. }