featurehub-openfeature-common 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +17 -2
- package/dist/index.cjs +585 -0
- package/dist/index.d.cts +217 -0
- package/dist/index.d.ts +217 -0
- package/dist/index.js +550 -0
- package/package.json +67 -3
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { EvaluationContext, FlagMetadata, ResolutionDetails, TrackingEventDetails, Logger } from '@openfeature/core';
|
|
2
|
+
import { ContextAttribute, FeatureStateHolder, FeatureHubConfig } from 'featurehub-javascript-core-sdk';
|
|
3
|
+
export { ContextAttribute } from 'featurehub-javascript-core-sdk';
|
|
4
|
+
|
|
5
|
+
interface MappedContext {
|
|
6
|
+
/** FeatureHub's user key (OpenFeature `targetingKey`). */
|
|
7
|
+
userkey?: string;
|
|
8
|
+
/** All remaining context attributes, keyed by FeatureHub attribute key. */
|
|
9
|
+
attributes: Record<string, ContextAttribute>;
|
|
10
|
+
}
|
|
11
|
+
/** Raised when an OpenFeature context contains a value FeatureHub cannot represent. */
|
|
12
|
+
declare class InvalidContextError extends Error {
|
|
13
|
+
readonly key: string;
|
|
14
|
+
constructor(key: string);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Raised when a server-evaluated key is asked to resolve a context other than the one it is resolving. Such
|
|
18
|
+
* a key holds one context, evaluated by Edge, for every OpenFeature client in the process: two code paths
|
|
19
|
+
* resolving different contexts at the same time would each be given the other's values.
|
|
20
|
+
*/
|
|
21
|
+
declare class ContextConflictError extends Error {
|
|
22
|
+
constructor();
|
|
23
|
+
}
|
|
24
|
+
/** True when the API key is client-evaluated (FeatureHub marks these with a `*`). */
|
|
25
|
+
declare function isClientEvaluatedKey(apiKey: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Maps an OpenFeature EvaluationContext onto FeatureHub's context model
|
|
28
|
+
* (featurehub-protocol.yaml encoding.evaluation_context):
|
|
29
|
+
*
|
|
30
|
+
* - `targetingKey` (or `userkey`) maps to FeatureHub's `userkey`.
|
|
31
|
+
* - The well-known keys (`device`, `country`, `platform`, `session`, `version`) are recognised
|
|
32
|
+
* case-insensitively and written lowercase.
|
|
33
|
+
* - `fhubWhenActive` is dropped: it is a strategy attribute, never supplied by the application.
|
|
34
|
+
* - Everything else passes through as a custom attribute, its case preserved.
|
|
35
|
+
* - Values are strings, numbers, booleans or date-times (a `Date`), or lists of those; null values are
|
|
36
|
+
* skipped. A date-time is sent to FeatureHub as UTC ISO-8601 to the second.
|
|
37
|
+
*
|
|
38
|
+
* @throws InvalidContextError when a value cannot be represented (an object, or a list holding one).
|
|
39
|
+
*/
|
|
40
|
+
declare function mapEvaluationContext(context: EvaluationContext | undefined): MappedContext;
|
|
41
|
+
/** Builds a FeatureHub `ContextRecord` (for the SDK's context API) from an OpenFeature EvaluationContext. */
|
|
42
|
+
declare function toContextRecord(context: EvaluationContext | undefined): Record<string, ContextAttribute>;
|
|
43
|
+
/**
|
|
44
|
+
* A stable signature of a mapped context, used only to detect context changes
|
|
45
|
+
* (it is never sent anywhere, so it deliberately does not reproduce the
|
|
46
|
+
* `x-featurehub` wire format — the SDK builds that from the context itself).
|
|
47
|
+
*/
|
|
48
|
+
declare function contextSignature(context: EvaluationContext | undefined): string;
|
|
49
|
+
|
|
50
|
+
type FeatureKind = "boolean" | "string" | "number" | "object";
|
|
51
|
+
/** A plain copy of one FeatureHub feature evaluation, safe to pass between async boundaries. */
|
|
52
|
+
interface FeatureEvaluation {
|
|
53
|
+
exists: boolean;
|
|
54
|
+
featureType?: string;
|
|
55
|
+
version?: number;
|
|
56
|
+
value?: unknown;
|
|
57
|
+
/** the strategy that supplied the value: matched locally, or the server's `v` */
|
|
58
|
+
strategyId?: string;
|
|
59
|
+
/** the strategy is a percentage rollout matched locally */
|
|
60
|
+
split?: boolean;
|
|
61
|
+
/** the feature's properties (`fp`) */
|
|
62
|
+
properties?: Record<string, string>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Captures one evaluation of a (live) FeatureHub feature state holder. The holder is
|
|
66
|
+
* bound to a shared repository, so callers that resolve against a shared
|
|
67
|
+
* context must capture it while they still hold their lock.
|
|
68
|
+
*/
|
|
69
|
+
declare function captureFeature(holder: FeatureStateHolder | undefined): FeatureEvaluation;
|
|
70
|
+
declare function expectedFeatureType(kind: FeatureKind): "BOOLEAN" | "STRING" | "NUMBER" | "JSON";
|
|
71
|
+
/**
|
|
72
|
+
* openfeature-resolution.yaml flag_metadata: the feature's properties, then featureVersion and featureType,
|
|
73
|
+
* which replace a property of the same name.
|
|
74
|
+
*/
|
|
75
|
+
declare function buildFlagMetadata(featureType: string | undefined, version: number | undefined, properties?: Record<string, string>): FlagMetadata;
|
|
76
|
+
declare function notReadyResolution(defaultValue: unknown): ResolutionDetails<unknown>;
|
|
77
|
+
declare function flagNotFoundResolution(flagKey: string, defaultValue: unknown): ResolutionDetails<unknown>;
|
|
78
|
+
declare function invalidContextResolution(defaultValue: unknown, error: unknown): ResolutionDetails<unknown>;
|
|
79
|
+
declare function generalErrorResolution(defaultValue: unknown, error: unknown): ResolutionDetails<unknown>;
|
|
80
|
+
/**
|
|
81
|
+
* Applies OpenFeature resolution semantics to a captured FeatureHub evaluation (openfeature-resolution.yaml
|
|
82
|
+
* resolution.steps, in order): not found, a feature of the wrong type, no value (DISABLED), conversion,
|
|
83
|
+
* then the reason - STATIC with no strategy, SPLIT for a percentage rollout matched locally,
|
|
84
|
+
* TARGETING_MATCH for any other strategy (the server's `v` included) - with the strategy id as variant.
|
|
85
|
+
*
|
|
86
|
+
* This is the single implementation shared by the Node and Web providers.
|
|
87
|
+
*/
|
|
88
|
+
declare function resolveFromEvaluation(flagKey: string, kind: FeatureKind, defaultValue: unknown, evaluation: FeatureEvaluation): ResolutionDetails<unknown>;
|
|
89
|
+
|
|
90
|
+
/** How the provider keeps its local repository up to date. */
|
|
91
|
+
type Transport = "streaming" | "polling" | "passive";
|
|
92
|
+
/** The transport-related slice of the provider options. */
|
|
93
|
+
interface TransportSelection {
|
|
94
|
+
/** Poll Edge every N milliseconds (REST active). Implies `transport: "polling"`. */
|
|
95
|
+
pollIntervalMs?: number;
|
|
96
|
+
/** Only contact Edge when a resolution happens after this many milliseconds (REST passive). Implies `transport: "passive"`. */
|
|
97
|
+
cacheTimeToLiveMs?: number;
|
|
98
|
+
/** Explicit transport; overrides the platform default but not the two options above. */
|
|
99
|
+
transport?: Transport;
|
|
100
|
+
}
|
|
101
|
+
interface BaseProviderOptions<TOptions> extends TransportSelection {
|
|
102
|
+
/** Base URL of FeatureHub Edge, e.g. `https://edge.featurehub.io`. */
|
|
103
|
+
host?: string;
|
|
104
|
+
/** A FeatureHub SDK API key (see the provider docs for eval-mode guidance). */
|
|
105
|
+
apiKey?: string;
|
|
106
|
+
/** How long `initialize` waits for readiness. Default 10s. */
|
|
107
|
+
waitForReadyMs?: number;
|
|
108
|
+
/**
|
|
109
|
+
* Build the FeatureHub config yourself, to attach value interceptors, feature stores and usage plugins. It
|
|
110
|
+
* is given the options with `host` and `apiKey` as the provider resolved them. With neither, the config's
|
|
111
|
+
* features come from its stores alone, and are evaluated locally.
|
|
112
|
+
*/
|
|
113
|
+
configFactory?: (options: TOptions) => FeatureHubConfig;
|
|
114
|
+
}
|
|
115
|
+
/** The connection a provider was given, in code or (server-side) from the environment. */
|
|
116
|
+
interface ProviderConnection {
|
|
117
|
+
host?: string | undefined;
|
|
118
|
+
apiKey?: string | undefined;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The minimal shape of an OpenFeature event emitter. Each SDK types its
|
|
122
|
+
* emitter with its own event enum, whose string values are identical across
|
|
123
|
+
* the server and web SDKs, so the shared base stays SDK-agnostic.
|
|
124
|
+
*/
|
|
125
|
+
interface ProviderEventSink {
|
|
126
|
+
emit(event: string, details?: {
|
|
127
|
+
message?: string;
|
|
128
|
+
flagsChanged?: string[];
|
|
129
|
+
}): void;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Applies the transport selected by the provider options. Explicit
|
|
133
|
+
* `pollIntervalMs` / `cacheTimeToLiveMs` win over `transport`, which in turn
|
|
134
|
+
* wins over the platform default. With none of them and no platform default,
|
|
135
|
+
* the config keeps its own mode (the node SDK's comes from the environment).
|
|
136
|
+
*/
|
|
137
|
+
declare function applyTransport(cfg: FeatureHubConfig, options: TransportSelection, defaultTransport: Transport | undefined): void;
|
|
138
|
+
/**
|
|
139
|
+
* Shared provider lifecycle: config creation, event wiring (with change
|
|
140
|
+
* detection), transport selection, readiness and teardown, plus the resolution
|
|
141
|
+
* error mapping. Subclasses contribute only what is genuinely platform
|
|
142
|
+
* specific: the event emitter, the config constructor, the default transport,
|
|
143
|
+
* and their resolve methods.
|
|
144
|
+
*/
|
|
145
|
+
declare abstract class FeatureHubProviderCore<TOptions extends BaseProviderOptions<TOptions>> {
|
|
146
|
+
protected readonly options: TOptions;
|
|
147
|
+
/** Edge's URL and the API key, as given in code or found in the environment; neither for stores only */
|
|
148
|
+
protected readonly host: string | undefined;
|
|
149
|
+
protected readonly apiKey: string | undefined;
|
|
150
|
+
protected readonly clientEval: boolean;
|
|
151
|
+
protected readonly waitForReadyMs: number;
|
|
152
|
+
protected config?: FeatureHubConfig;
|
|
153
|
+
/**
|
|
154
|
+
* Features are held and resolve: readiness is Ready, or Stale (a context change in progress, or a
|
|
155
|
+
* connection that stopped for good after delivering features; featurehub-protocol.yaml repository.readiness).
|
|
156
|
+
*/
|
|
157
|
+
protected ready: boolean;
|
|
158
|
+
private started;
|
|
159
|
+
/** the connection has stopped for good (connection failed, or environment stale) */
|
|
160
|
+
private stopped;
|
|
161
|
+
private staleEmitted;
|
|
162
|
+
private lastReadiness;
|
|
163
|
+
/** key → last seen feature version, so polls that change nothing stay silent. */
|
|
164
|
+
private readonly featureVersions;
|
|
165
|
+
private seenPayload;
|
|
166
|
+
/**
|
|
167
|
+
* featurehub-protocol.yaml providers.source_selection and validation_errors: a connection needs both a host
|
|
168
|
+
* and an API key; with neither, a `configFactory` can supply a config whose features come from stores alone.
|
|
169
|
+
*/
|
|
170
|
+
protected constructor(providerName: string, options: TOptions, connection?: ProviderConnection);
|
|
171
|
+
/** This platform's OpenFeature event emitter. */
|
|
172
|
+
protected abstract get eventSink(): ProviderEventSink;
|
|
173
|
+
/** Create the config from this platform's FeatureHub SDK. */
|
|
174
|
+
protected abstract createConfig(): FeatureHubConfig;
|
|
175
|
+
/** Transport used when the options do not select one; undefined keeps the config's own mode. */
|
|
176
|
+
protected abstract get defaultTransport(): Transport | undefined;
|
|
177
|
+
/** Creates the config, wires its listeners, applies the transport and calls `init()`. */
|
|
178
|
+
protected startConfig(): FeatureHubConfig;
|
|
179
|
+
/**
|
|
180
|
+
* Waits for the repository to become ready, then allows lifecycle events through
|
|
181
|
+
* (featurehub-protocol.yaml providers.events):
|
|
182
|
+
* - Failed (the connection was rejected before any features): initialisation fails, PROVIDER_FATAL
|
|
183
|
+
* - Stale with features, because the connection stopped for good with the first response: initialisation
|
|
184
|
+
* succeeds and STALE follows READY
|
|
185
|
+
* - anything else not Ready in time: initialisation fails
|
|
186
|
+
*/
|
|
187
|
+
protected awaitReady(cfg: FeatureHubConfig, waitMs?: number): Promise<void>;
|
|
188
|
+
/**
|
|
189
|
+
* OpenFeature tracking: records a FeatureHub named usage event (ClientContext.recordNamedUsage) with the
|
|
190
|
+
* evaluation context and the event's details, for the registered usage plugins (Segment, say). It needs a
|
|
191
|
+
* usage plugin to go anywhere, and never fails the caller: an unusable context records nothing.
|
|
192
|
+
*/
|
|
193
|
+
track(trackingEventName: string, context?: EvaluationContext, trackingEventDetails?: TrackingEventDetails): void;
|
|
194
|
+
onClose(): Promise<void>;
|
|
195
|
+
/** Synchronous resolution helper (web providers). */
|
|
196
|
+
protected resolveSync(flagKey: string, kind: FeatureKind, defaultValue: unknown, logger: Logger | undefined, take: () => FeatureEvaluation): ResolutionDetails<unknown>;
|
|
197
|
+
/** Asynchronous resolution helper (node providers). */
|
|
198
|
+
protected resolveAsync(flagKey: string, kind: FeatureKind, defaultValue: unknown, logger: Logger | undefined, take: () => FeatureEvaluation | Promise<FeatureEvaluation>): Promise<ResolutionDetails<unknown>>;
|
|
199
|
+
private failedResolution;
|
|
200
|
+
private onReadiness;
|
|
201
|
+
private onConnectionStopped;
|
|
202
|
+
/** STALE once, after READY, while features are held; resolutions continue from the last features. */
|
|
203
|
+
private emitStale;
|
|
204
|
+
/**
|
|
205
|
+
* Records the versions carried by a payload and returns the keys that
|
|
206
|
+
* actually changed. The first payload after construction is a baseline: the
|
|
207
|
+
* repository replays the full feature set on every poll (and on every SSE
|
|
208
|
+
* reconnect), so without this comparison ConfigurationChanged would fire on
|
|
209
|
+
* every poll even when nothing changed. A full set also removes every feature
|
|
210
|
+
* it does not hold, which is a change too.
|
|
211
|
+
*/
|
|
212
|
+
private noteVersions;
|
|
213
|
+
private emitChanged;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export { type BaseProviderOptions, ContextConflictError, type FeatureEvaluation, FeatureHubProviderCore, type FeatureKind, InvalidContextError, type MappedContext, type ProviderEventSink, type Transport, type TransportSelection, applyTransport, buildFlagMetadata, captureFeature, contextSignature, expectedFeatureType, flagNotFoundResolution, generalErrorResolution, invalidContextResolution, isClientEvaluatedKey, mapEvaluationContext, notReadyResolution, resolveFromEvaluation, toContextRecord };
|
|
217
|
+
declare module 'featurehub-openfeature-common'
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { EvaluationContext, FlagMetadata, ResolutionDetails, TrackingEventDetails, Logger } from '@openfeature/core';
|
|
2
|
+
import { ContextAttribute, FeatureStateHolder, FeatureHubConfig } from 'featurehub-javascript-core-sdk';
|
|
3
|
+
export { ContextAttribute } from 'featurehub-javascript-core-sdk';
|
|
4
|
+
|
|
5
|
+
interface MappedContext {
|
|
6
|
+
/** FeatureHub's user key (OpenFeature `targetingKey`). */
|
|
7
|
+
userkey?: string;
|
|
8
|
+
/** All remaining context attributes, keyed by FeatureHub attribute key. */
|
|
9
|
+
attributes: Record<string, ContextAttribute>;
|
|
10
|
+
}
|
|
11
|
+
/** Raised when an OpenFeature context contains a value FeatureHub cannot represent. */
|
|
12
|
+
declare class InvalidContextError extends Error {
|
|
13
|
+
readonly key: string;
|
|
14
|
+
constructor(key: string);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Raised when a server-evaluated key is asked to resolve a context other than the one it is resolving. Such
|
|
18
|
+
* a key holds one context, evaluated by Edge, for every OpenFeature client in the process: two code paths
|
|
19
|
+
* resolving different contexts at the same time would each be given the other's values.
|
|
20
|
+
*/
|
|
21
|
+
declare class ContextConflictError extends Error {
|
|
22
|
+
constructor();
|
|
23
|
+
}
|
|
24
|
+
/** True when the API key is client-evaluated (FeatureHub marks these with a `*`). */
|
|
25
|
+
declare function isClientEvaluatedKey(apiKey: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Maps an OpenFeature EvaluationContext onto FeatureHub's context model
|
|
28
|
+
* (featurehub-protocol.yaml encoding.evaluation_context):
|
|
29
|
+
*
|
|
30
|
+
* - `targetingKey` (or `userkey`) maps to FeatureHub's `userkey`.
|
|
31
|
+
* - The well-known keys (`device`, `country`, `platform`, `session`, `version`) are recognised
|
|
32
|
+
* case-insensitively and written lowercase.
|
|
33
|
+
* - `fhubWhenActive` is dropped: it is a strategy attribute, never supplied by the application.
|
|
34
|
+
* - Everything else passes through as a custom attribute, its case preserved.
|
|
35
|
+
* - Values are strings, numbers, booleans or date-times (a `Date`), or lists of those; null values are
|
|
36
|
+
* skipped. A date-time is sent to FeatureHub as UTC ISO-8601 to the second.
|
|
37
|
+
*
|
|
38
|
+
* @throws InvalidContextError when a value cannot be represented (an object, or a list holding one).
|
|
39
|
+
*/
|
|
40
|
+
declare function mapEvaluationContext(context: EvaluationContext | undefined): MappedContext;
|
|
41
|
+
/** Builds a FeatureHub `ContextRecord` (for the SDK's context API) from an OpenFeature EvaluationContext. */
|
|
42
|
+
declare function toContextRecord(context: EvaluationContext | undefined): Record<string, ContextAttribute>;
|
|
43
|
+
/**
|
|
44
|
+
* A stable signature of a mapped context, used only to detect context changes
|
|
45
|
+
* (it is never sent anywhere, so it deliberately does not reproduce the
|
|
46
|
+
* `x-featurehub` wire format — the SDK builds that from the context itself).
|
|
47
|
+
*/
|
|
48
|
+
declare function contextSignature(context: EvaluationContext | undefined): string;
|
|
49
|
+
|
|
50
|
+
type FeatureKind = "boolean" | "string" | "number" | "object";
|
|
51
|
+
/** A plain copy of one FeatureHub feature evaluation, safe to pass between async boundaries. */
|
|
52
|
+
interface FeatureEvaluation {
|
|
53
|
+
exists: boolean;
|
|
54
|
+
featureType?: string;
|
|
55
|
+
version?: number;
|
|
56
|
+
value?: unknown;
|
|
57
|
+
/** the strategy that supplied the value: matched locally, or the server's `v` */
|
|
58
|
+
strategyId?: string;
|
|
59
|
+
/** the strategy is a percentage rollout matched locally */
|
|
60
|
+
split?: boolean;
|
|
61
|
+
/** the feature's properties (`fp`) */
|
|
62
|
+
properties?: Record<string, string>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Captures one evaluation of a (live) FeatureHub feature state holder. The holder is
|
|
66
|
+
* bound to a shared repository, so callers that resolve against a shared
|
|
67
|
+
* context must capture it while they still hold their lock.
|
|
68
|
+
*/
|
|
69
|
+
declare function captureFeature(holder: FeatureStateHolder | undefined): FeatureEvaluation;
|
|
70
|
+
declare function expectedFeatureType(kind: FeatureKind): "BOOLEAN" | "STRING" | "NUMBER" | "JSON";
|
|
71
|
+
/**
|
|
72
|
+
* openfeature-resolution.yaml flag_metadata: the feature's properties, then featureVersion and featureType,
|
|
73
|
+
* which replace a property of the same name.
|
|
74
|
+
*/
|
|
75
|
+
declare function buildFlagMetadata(featureType: string | undefined, version: number | undefined, properties?: Record<string, string>): FlagMetadata;
|
|
76
|
+
declare function notReadyResolution(defaultValue: unknown): ResolutionDetails<unknown>;
|
|
77
|
+
declare function flagNotFoundResolution(flagKey: string, defaultValue: unknown): ResolutionDetails<unknown>;
|
|
78
|
+
declare function invalidContextResolution(defaultValue: unknown, error: unknown): ResolutionDetails<unknown>;
|
|
79
|
+
declare function generalErrorResolution(defaultValue: unknown, error: unknown): ResolutionDetails<unknown>;
|
|
80
|
+
/**
|
|
81
|
+
* Applies OpenFeature resolution semantics to a captured FeatureHub evaluation (openfeature-resolution.yaml
|
|
82
|
+
* resolution.steps, in order): not found, a feature of the wrong type, no value (DISABLED), conversion,
|
|
83
|
+
* then the reason - STATIC with no strategy, SPLIT for a percentage rollout matched locally,
|
|
84
|
+
* TARGETING_MATCH for any other strategy (the server's `v` included) - with the strategy id as variant.
|
|
85
|
+
*
|
|
86
|
+
* This is the single implementation shared by the Node and Web providers.
|
|
87
|
+
*/
|
|
88
|
+
declare function resolveFromEvaluation(flagKey: string, kind: FeatureKind, defaultValue: unknown, evaluation: FeatureEvaluation): ResolutionDetails<unknown>;
|
|
89
|
+
|
|
90
|
+
/** How the provider keeps its local repository up to date. */
|
|
91
|
+
type Transport = "streaming" | "polling" | "passive";
|
|
92
|
+
/** The transport-related slice of the provider options. */
|
|
93
|
+
interface TransportSelection {
|
|
94
|
+
/** Poll Edge every N milliseconds (REST active). Implies `transport: "polling"`. */
|
|
95
|
+
pollIntervalMs?: number;
|
|
96
|
+
/** Only contact Edge when a resolution happens after this many milliseconds (REST passive). Implies `transport: "passive"`. */
|
|
97
|
+
cacheTimeToLiveMs?: number;
|
|
98
|
+
/** Explicit transport; overrides the platform default but not the two options above. */
|
|
99
|
+
transport?: Transport;
|
|
100
|
+
}
|
|
101
|
+
interface BaseProviderOptions<TOptions> extends TransportSelection {
|
|
102
|
+
/** Base URL of FeatureHub Edge, e.g. `https://edge.featurehub.io`. */
|
|
103
|
+
host?: string;
|
|
104
|
+
/** A FeatureHub SDK API key (see the provider docs for eval-mode guidance). */
|
|
105
|
+
apiKey?: string;
|
|
106
|
+
/** How long `initialize` waits for readiness. Default 10s. */
|
|
107
|
+
waitForReadyMs?: number;
|
|
108
|
+
/**
|
|
109
|
+
* Build the FeatureHub config yourself, to attach value interceptors, feature stores and usage plugins. It
|
|
110
|
+
* is given the options with `host` and `apiKey` as the provider resolved them. With neither, the config's
|
|
111
|
+
* features come from its stores alone, and are evaluated locally.
|
|
112
|
+
*/
|
|
113
|
+
configFactory?: (options: TOptions) => FeatureHubConfig;
|
|
114
|
+
}
|
|
115
|
+
/** The connection a provider was given, in code or (server-side) from the environment. */
|
|
116
|
+
interface ProviderConnection {
|
|
117
|
+
host?: string | undefined;
|
|
118
|
+
apiKey?: string | undefined;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The minimal shape of an OpenFeature event emitter. Each SDK types its
|
|
122
|
+
* emitter with its own event enum, whose string values are identical across
|
|
123
|
+
* the server and web SDKs, so the shared base stays SDK-agnostic.
|
|
124
|
+
*/
|
|
125
|
+
interface ProviderEventSink {
|
|
126
|
+
emit(event: string, details?: {
|
|
127
|
+
message?: string;
|
|
128
|
+
flagsChanged?: string[];
|
|
129
|
+
}): void;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Applies the transport selected by the provider options. Explicit
|
|
133
|
+
* `pollIntervalMs` / `cacheTimeToLiveMs` win over `transport`, which in turn
|
|
134
|
+
* wins over the platform default. With none of them and no platform default,
|
|
135
|
+
* the config keeps its own mode (the node SDK's comes from the environment).
|
|
136
|
+
*/
|
|
137
|
+
declare function applyTransport(cfg: FeatureHubConfig, options: TransportSelection, defaultTransport: Transport | undefined): void;
|
|
138
|
+
/**
|
|
139
|
+
* Shared provider lifecycle: config creation, event wiring (with change
|
|
140
|
+
* detection), transport selection, readiness and teardown, plus the resolution
|
|
141
|
+
* error mapping. Subclasses contribute only what is genuinely platform
|
|
142
|
+
* specific: the event emitter, the config constructor, the default transport,
|
|
143
|
+
* and their resolve methods.
|
|
144
|
+
*/
|
|
145
|
+
declare abstract class FeatureHubProviderCore<TOptions extends BaseProviderOptions<TOptions>> {
|
|
146
|
+
protected readonly options: TOptions;
|
|
147
|
+
/** Edge's URL and the API key, as given in code or found in the environment; neither for stores only */
|
|
148
|
+
protected readonly host: string | undefined;
|
|
149
|
+
protected readonly apiKey: string | undefined;
|
|
150
|
+
protected readonly clientEval: boolean;
|
|
151
|
+
protected readonly waitForReadyMs: number;
|
|
152
|
+
protected config?: FeatureHubConfig;
|
|
153
|
+
/**
|
|
154
|
+
* Features are held and resolve: readiness is Ready, or Stale (a context change in progress, or a
|
|
155
|
+
* connection that stopped for good after delivering features; featurehub-protocol.yaml repository.readiness).
|
|
156
|
+
*/
|
|
157
|
+
protected ready: boolean;
|
|
158
|
+
private started;
|
|
159
|
+
/** the connection has stopped for good (connection failed, or environment stale) */
|
|
160
|
+
private stopped;
|
|
161
|
+
private staleEmitted;
|
|
162
|
+
private lastReadiness;
|
|
163
|
+
/** key → last seen feature version, so polls that change nothing stay silent. */
|
|
164
|
+
private readonly featureVersions;
|
|
165
|
+
private seenPayload;
|
|
166
|
+
/**
|
|
167
|
+
* featurehub-protocol.yaml providers.source_selection and validation_errors: a connection needs both a host
|
|
168
|
+
* and an API key; with neither, a `configFactory` can supply a config whose features come from stores alone.
|
|
169
|
+
*/
|
|
170
|
+
protected constructor(providerName: string, options: TOptions, connection?: ProviderConnection);
|
|
171
|
+
/** This platform's OpenFeature event emitter. */
|
|
172
|
+
protected abstract get eventSink(): ProviderEventSink;
|
|
173
|
+
/** Create the config from this platform's FeatureHub SDK. */
|
|
174
|
+
protected abstract createConfig(): FeatureHubConfig;
|
|
175
|
+
/** Transport used when the options do not select one; undefined keeps the config's own mode. */
|
|
176
|
+
protected abstract get defaultTransport(): Transport | undefined;
|
|
177
|
+
/** Creates the config, wires its listeners, applies the transport and calls `init()`. */
|
|
178
|
+
protected startConfig(): FeatureHubConfig;
|
|
179
|
+
/**
|
|
180
|
+
* Waits for the repository to become ready, then allows lifecycle events through
|
|
181
|
+
* (featurehub-protocol.yaml providers.events):
|
|
182
|
+
* - Failed (the connection was rejected before any features): initialisation fails, PROVIDER_FATAL
|
|
183
|
+
* - Stale with features, because the connection stopped for good with the first response: initialisation
|
|
184
|
+
* succeeds and STALE follows READY
|
|
185
|
+
* - anything else not Ready in time: initialisation fails
|
|
186
|
+
*/
|
|
187
|
+
protected awaitReady(cfg: FeatureHubConfig, waitMs?: number): Promise<void>;
|
|
188
|
+
/**
|
|
189
|
+
* OpenFeature tracking: records a FeatureHub named usage event (ClientContext.recordNamedUsage) with the
|
|
190
|
+
* evaluation context and the event's details, for the registered usage plugins (Segment, say). It needs a
|
|
191
|
+
* usage plugin to go anywhere, and never fails the caller: an unusable context records nothing.
|
|
192
|
+
*/
|
|
193
|
+
track(trackingEventName: string, context?: EvaluationContext, trackingEventDetails?: TrackingEventDetails): void;
|
|
194
|
+
onClose(): Promise<void>;
|
|
195
|
+
/** Synchronous resolution helper (web providers). */
|
|
196
|
+
protected resolveSync(flagKey: string, kind: FeatureKind, defaultValue: unknown, logger: Logger | undefined, take: () => FeatureEvaluation): ResolutionDetails<unknown>;
|
|
197
|
+
/** Asynchronous resolution helper (node providers). */
|
|
198
|
+
protected resolveAsync(flagKey: string, kind: FeatureKind, defaultValue: unknown, logger: Logger | undefined, take: () => FeatureEvaluation | Promise<FeatureEvaluation>): Promise<ResolutionDetails<unknown>>;
|
|
199
|
+
private failedResolution;
|
|
200
|
+
private onReadiness;
|
|
201
|
+
private onConnectionStopped;
|
|
202
|
+
/** STALE once, after READY, while features are held; resolutions continue from the last features. */
|
|
203
|
+
private emitStale;
|
|
204
|
+
/**
|
|
205
|
+
* Records the versions carried by a payload and returns the keys that
|
|
206
|
+
* actually changed. The first payload after construction is a baseline: the
|
|
207
|
+
* repository replays the full feature set on every poll (and on every SSE
|
|
208
|
+
* reconnect), so without this comparison ConfigurationChanged would fire on
|
|
209
|
+
* every poll even when nothing changed. A full set also removes every feature
|
|
210
|
+
* it does not hold, which is a change too.
|
|
211
|
+
*/
|
|
212
|
+
private noteVersions;
|
|
213
|
+
private emitChanged;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export { type BaseProviderOptions, ContextConflictError, type FeatureEvaluation, FeatureHubProviderCore, type FeatureKind, InvalidContextError, type MappedContext, type ProviderEventSink, type Transport, type TransportSelection, applyTransport, buildFlagMetadata, captureFeature, contextSignature, expectedFeatureType, flagNotFoundResolution, generalErrorResolution, invalidContextResolution, isClientEvaluatedKey, mapEvaluationContext, notReadyResolution, resolveFromEvaluation, toContextRecord };
|
|
217
|
+
declare module 'featurehub-openfeature-common'
|