File size: 5,751 Bytes
0e239c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
diff --git a/packages/server/src/provider/multi-provider/multi-provider.ts b/packages/server/src/provider/multi-provider/multi-provider.ts
index 783db72..e4eeafa 100644
--- a/packages/server/src/provider/multi-provider/multi-provider.ts
+++ b/packages/server/src/provider/multi-provider/multi-provider.ts
@@ -104,9 +104,9 @@ export class MultiProvider implements Provider {
     Object.freeze(this.providerEntriesByName);
   }
 
-  async initialize(context?: EvaluationContext): Promise<void> {
+  async initialize(context?: EvaluationContext, domain?: string): Promise<void> {
     const result = await Promise.allSettled(
-      this.providerEntries.map((provider) => provider.provider.initialize?.(context)),
+      this.providerEntries.map((provider) => provider.provider.initialize?.(context, domain)),
     );
     throwAggregateErrorFromPromiseResults(result, this.providerEntries);
   }
diff --git a/packages/shared/src/open-feature.ts b/packages/shared/src/open-feature.ts
index 00142b5..78ac596 100644
--- a/packages/shared/src/open-feature.ts
+++ b/packages/shared/src/open-feature.ts
@@ -229,6 +229,8 @@ export abstract class OpenFeatureCommonAPI<
     const oldProvider = this.getProviderForClient(domain);
     const providerName = provider.metadata.name;
 
+    this.assertDomainScopedBindingAllowed(provider, domain);
+
     // ignore no-ops
     if (oldProvider === provider) {
       this._logger.debug('Provider is already set, ignoring setProvider call');
@@ -252,8 +254,9 @@ export abstract class OpenFeatureCommonAPI<
 
     // initialize the provider if it implements "initialize" and it's not already registered
     if (typeof provider.initialize === 'function' && !this.allProviders.includes(provider)) {
+      const initContext = domain ? (this._domainScopedContext.get(domain) ?? this._context) : this._context;
       initializationPromise = provider
-        .initialize?.(domain ? (this._domainScopedContext.get(domain) ?? this._context) : this._context)
+        .initialize?.(initContext, domain)
         ?.then(() => {
           wrappedProvider.status = this._statusEnumType.READY;
           // fetch the most recent event emitters, some may have been added during init
@@ -429,6 +432,39 @@ export abstract class OpenFeatureCommonAPI<
     }
   }
 
+  private getProviderInstanceBinding(provider: P): string | null {
+    if (this._defaultProvider.provider === provider) {
+      // Empty string represents the default (unbound) provider.
+      return '';
+    }
+
+    for (const [domain, wrapper] of this._domainScopedProviders) {
+      if (wrapper.provider === provider) {
+        return domain;
+      }
+    }
+
+    return null;
+  }
+
+  private assertDomainScopedBindingAllowed(provider: P, domain?: string): void {
+    if (!provider.domainScoped) {
+      return;
+    }
+
+    const existingBinding = this.getProviderInstanceBinding(provider);
+    if (existingBinding === null) {
+      return;
+    }
+
+    const requestedBinding = domain ?? '';
+    if (existingBinding === requestedBinding) {
+      return;
+    }
+
+    throw new GeneralError(`Cannot bind domain-scoped provider '${provider.metadata.name}' to more than one domain.`);
+  }
+
   private get allProviders(): P[] {
     return [
       ...[...this._domainScopedProviders.values()].map((wrappers) => wrappers.provider),
diff --git a/packages/shared/src/provider/provider.ts b/packages/shared/src/provider/provider.ts
index 74ae860..bab76c1 100644
--- a/packages/shared/src/provider/provider.ts
+++ b/packages/shared/src/provider/provider.ts
@@ -96,6 +96,12 @@ export interface CommonProvider<S extends ClientProviderStatus | ServerProviderS
    */
   readonly runsOn?: Paradigm;
 
+  /**
+   * When true, the provider maintains state specific to a single domain (such as a
+   * persistent cache) and the SDK will bind it to at most one domain.
+   */
+  readonly domainScoped?: boolean;
+
   // TODO: in the future we could make this a never to force provider to remove it.
   /**
    * @deprecated the SDK now maintains the provider's state; there's no need for providers to implement this field.
@@ -123,9 +129,10 @@ export interface CommonProvider<S extends ClientProviderStatus | ServerProviderS
    * When the returned promise resolves, the SDK fires the ProviderEvents.Ready event.
    * If the returned promise rejects, the SDK fires the ProviderEvents.Error event.
    * Use this function to perform any context-dependent setup within the provider.
-   * @param context
+   * @param context the global evaluation context
+   * @param domain the bound domain, if any
    */
-  initialize?(context?: EvaluationContext): Promise<void>;
+  initialize?(context?: EvaluationContext, domain?: string): Promise<void>;
 
   /**
    * Track a user action or application state, usually representing a business objective or outcome.
diff --git a/packages/web/src/provider/multi-provider/multi-provider-web.ts b/packages/web/src/provider/multi-provider/multi-provider-web.ts
index e346efe..0a41a30 100644
--- a/packages/web/src/provider/multi-provider/multi-provider-web.ts
+++ b/packages/web/src/provider/multi-provider/multi-provider-web.ts
@@ -103,9 +103,9 @@ export class MultiProvider implements Provider {
     Object.freeze(this.providerEntriesByName);
   }
 
-  async initialize(context?: EvaluationContext): Promise<void> {
+  async initialize(context?: EvaluationContext, domain?: string): Promise<void> {
     const result = await Promise.allSettled(
-      this.providerEntries.map((provider) => provider.provider.initialize?.(context)),
+      this.providerEntries.map((provider) => provider.provider.initialize?.(context, domain)),
     );
     throwAggregateErrorFromPromiseResults(result, this.providerEntries);
   }