Silin-Chen's picture
CrossSWE-Bench: current 171-instance release
0e239c6
Raw History Blame Contribute Delete
5.75 kB
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);
}