|
| 1 | +import { mergeContexts } from "./mergeContexts"; |
| 2 | +import type { |
| 3 | + Contexts, |
| 4 | + ContextObj, |
| 5 | + LogLevel, |
| 6 | +} from "./types"; |
| 7 | +import type { makeLogger } from "./logger"; |
| 8 | +import type { |
| 9 | + ReforgeInterface, |
| 10 | + Telemetry, |
| 11 | + TypedNodeServerConfigurationRaw, |
| 12 | +} from "./reforge"; |
| 13 | +import type { GlobalListenerCallback } from "./configChangeNotifier"; |
| 14 | +import { contextObjToMap } from "./mergeContexts"; |
| 15 | + |
| 16 | +/** |
| 17 | + * Interface for the parent Reforge instance. |
| 18 | + * Used internally by ReforgeClient to avoid circular dependencies. |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +interface ReforgeParent { |
| 22 | + telemetry: Telemetry | undefined; |
| 23 | + get<K extends keyof TypedNodeServerConfigurationRaw>( |
| 24 | + key: K, |
| 25 | + contexts?: Contexts | ContextObj, |
| 26 | + defaultValue?: TypedNodeServerConfigurationRaw[K] |
| 27 | + ): TypedNodeServerConfigurationRaw[K]; |
| 28 | + isFeatureEnabled(key: string, contexts?: Contexts | ContextObj): boolean; |
| 29 | + logger( |
| 30 | + loggerName: string, |
| 31 | + defaultLevel?: LogLevel, |
| 32 | + contexts?: Contexts | ContextObj |
| 33 | + ): ReturnType<typeof makeLogger>; |
| 34 | + shouldLog(args: { |
| 35 | + loggerName: string; |
| 36 | + desiredLevel: LogLevel; |
| 37 | + defaultLevel?: LogLevel; |
| 38 | + contexts?: Contexts | ContextObj; |
| 39 | + }): boolean; |
| 40 | + getLogLevel(loggerName: string): LogLevel; |
| 41 | + updateIfStalerThan(durationInMs: number): Promise<void> | undefined; |
| 42 | + addConfigChangeListener(callback: GlobalListenerCallback): () => void; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * A context-scoped Reforge client that shares the underlying resolver |
| 47 | + * with its parent Reforge instance but applies a specific context to all operations. |
| 48 | + * |
| 49 | + * This allows creating request/call-scoped clients without duplicating |
| 50 | + * the resolver, polling, or SSE connections. All scoped clients share |
| 51 | + * the same parent Reforge's state. |
| 52 | + */ |
| 53 | +export class ReforgeClient implements ReforgeInterface { |
| 54 | + private readonly parent: ReforgeParent; |
| 55 | + private readonly context: Contexts; |
| 56 | + |
| 57 | + constructor(parent: ReforgeParent, contexts: Contexts | ContextObj) { |
| 58 | + this.parent = parent; |
| 59 | + this.context = |
| 60 | + contexts instanceof Map ? contexts : contextObjToMap(contexts); |
| 61 | + } |
| 62 | + |
| 63 | + get telemetry(): Telemetry | undefined { |
| 64 | + return this.parent.telemetry; |
| 65 | + } |
| 66 | + |
| 67 | + get<K extends keyof TypedNodeServerConfigurationRaw>( |
| 68 | + key: K, |
| 69 | + contexts?: Contexts | ContextObj, |
| 70 | + defaultValue?: TypedNodeServerConfigurationRaw[K] |
| 71 | + ): TypedNodeServerConfigurationRaw[K] { |
| 72 | + const mergedContexts = contexts |
| 73 | + ? mergeContexts(this.context, contexts) |
| 74 | + : this.context; |
| 75 | + return this.parent.get(key, mergedContexts, defaultValue); |
| 76 | + } |
| 77 | + |
| 78 | + isFeatureEnabled( |
| 79 | + key: string, |
| 80 | + contexts?: Contexts | ContextObj |
| 81 | + ): boolean { |
| 82 | + const mergedContexts = contexts |
| 83 | + ? mergeContexts(this.context, contexts) |
| 84 | + : this.context; |
| 85 | + return this.parent.isFeatureEnabled(key, mergedContexts); |
| 86 | + } |
| 87 | + |
| 88 | + logger( |
| 89 | + loggerName: string, |
| 90 | + defaultLevel?: LogLevel, |
| 91 | + contexts?: Contexts | ContextObj |
| 92 | + ): ReturnType<typeof makeLogger> { |
| 93 | + const mergedContexts = contexts |
| 94 | + ? mergeContexts(this.context, contexts) |
| 95 | + : this.context; |
| 96 | + return this.parent.logger(loggerName, defaultLevel, mergedContexts); |
| 97 | + } |
| 98 | + |
| 99 | + shouldLog({ |
| 100 | + loggerName, |
| 101 | + desiredLevel, |
| 102 | + defaultLevel, |
| 103 | + contexts, |
| 104 | + }: { |
| 105 | + loggerName: string; |
| 106 | + desiredLevel: LogLevel; |
| 107 | + defaultLevel?: LogLevel; |
| 108 | + contexts?: Contexts | ContextObj; |
| 109 | + }): boolean { |
| 110 | + const mergedContexts = contexts |
| 111 | + ? mergeContexts(this.context, contexts) |
| 112 | + : this.context; |
| 113 | + return this.parent.shouldLog({ |
| 114 | + loggerName, |
| 115 | + desiredLevel, |
| 116 | + defaultLevel, |
| 117 | + contexts: mergedContexts, |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + getLogLevel(loggerName: string): LogLevel { |
| 122 | + return this.parent.getLogLevel(loggerName); |
| 123 | + } |
| 124 | + |
| 125 | + updateIfStalerThan(durationInMs: number): Promise<void> | undefined { |
| 126 | + return this.parent.updateIfStalerThan(durationInMs); |
| 127 | + } |
| 128 | + |
| 129 | + withContext(contexts: Contexts | ContextObj): ReforgeInterface { |
| 130 | + const mergedContexts = mergeContexts(this.context, contexts); |
| 131 | + return new ReforgeClient(this.parent, mergedContexts); |
| 132 | + } |
| 133 | + |
| 134 | + inContext<T>( |
| 135 | + contexts: Contexts | ContextObj, |
| 136 | + func: (reforge: ReforgeInterface) => T |
| 137 | + ): T { |
| 138 | + const mergedContexts = mergeContexts(this.context, contexts); |
| 139 | + return func(new ReforgeClient(this.parent, mergedContexts)); |
| 140 | + } |
| 141 | + |
| 142 | + addConfigChangeListener(callback: GlobalListenerCallback): () => void { |
| 143 | + return this.parent.addConfigChangeListener(callback); |
| 144 | + } |
| 145 | +} |
0 commit comments