File size: 1,787 Bytes
4c76b0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {
  runOutsidePluginMetadataSnapshotScope,
  withPluginMetadataSnapshotScope,
} from "../current-plugin-metadata-snapshot.js";
import type { PluginMetadataSnapshot } from "../plugin-metadata-snapshot.types.js";
import { createEmptyPluginRegistry } from "../registry-empty.js";
import type { PluginRegistry } from "../registry-types.js";
import {
  runOutsidePluginRuntimeRegistryScope,
  withPluginRuntimeRegistryScope,
} from "./gateway-request-scope.js";
import {
  withPluginRuntimeGenerationRegistryScope,
  runOutsidePluginRuntimeGenerationRegistryScope,
} from "./generation-state.js";

export { getPluginRuntimeGenerationRegistry } from "./generation-state.js";

/** Carries one prepared plugin generation through all nested runtime lookups. */
export function withPluginRuntimeGenerationScope<T>(
  generation: {
    metadataSnapshot: PluginMetadataSnapshot;
    pluginRegistry?: PluginRegistry;
  },
  run: () => T,
): T {
  const pluginRegistry = generation.pluginRegistry ?? createEmptyPluginRegistry();
  return withPluginMetadataSnapshotScope(
    generation.metadataSnapshot,
    () =>
      withPluginRuntimeGenerationRegistryScope(pluginRegistry, () =>
        withPluginRuntimeRegistryScope(
          pluginRegistry,
          run,
          generation.metadataSnapshot.declaredProviderOwners,
        ),
      ),
    // The prepared generation already owns discovery and policy compatibility.
    { trustConfigIdentity: true },
  );
}

/** Re-admission drops the old generation while retaining the exact Gateway caller. */
export function runOutsidePluginRuntimeGenerationScope<T>(run: () => T): T {
  return runOutsidePluginRuntimeGenerationRegistryScope(() =>
    runOutsidePluginMetadataSnapshotScope(() => runOutsidePluginRuntimeRegistryScope(run)),
  );
}