featurehub-openfeature-common 0.0.0-stage → 1.0.0
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 +70 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
// src/context-mapper.ts
|
|
2
|
+
import {
|
|
3
|
+
StrategyAttributeWellKnownNames
|
|
4
|
+
} from "featurehub-javascript-core-sdk";
|
|
5
|
+
var FHUB_WHEN_ACTIVE = StrategyAttributeWellKnownNames.FhubWhenActive.toLowerCase();
|
|
6
|
+
var WELL_KNOWN_KEYS = new Set(
|
|
7
|
+
Object.values(StrategyAttributeWellKnownNames).map((k) => k.toLowerCase()).filter((k) => k !== "userkey" && k !== FHUB_WHEN_ACTIVE)
|
|
8
|
+
);
|
|
9
|
+
var InvalidContextError = class extends Error {
|
|
10
|
+
key;
|
|
11
|
+
constructor(key) {
|
|
12
|
+
super(
|
|
13
|
+
`Context attribute '${key}' is an object or contains nested objects; FeatureHub attributes must be strings, numbers, booleans, date-times, or lists of those.`
|
|
14
|
+
);
|
|
15
|
+
this.name = "InvalidContextError";
|
|
16
|
+
this.key = key;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var ContextConflictError = class extends Error {
|
|
20
|
+
constructor() {
|
|
21
|
+
super(
|
|
22
|
+
"A server-evaluated API key holds one evaluation context for the whole process, and a resolution for a different context arrived while another context's was in progress. Use the same context everywhere, or a client-evaluated key (containing '*') to resolve many contexts at once."
|
|
23
|
+
);
|
|
24
|
+
this.name = "ContextConflictError";
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
function isClientEvaluatedKey(apiKey) {
|
|
28
|
+
return apiKey.includes("*");
|
|
29
|
+
}
|
|
30
|
+
function isValue(v) {
|
|
31
|
+
return typeof v === "string" || typeof v === "number" || typeof v === "boolean" || v instanceof Date;
|
|
32
|
+
}
|
|
33
|
+
function isAllowedList(v) {
|
|
34
|
+
return Array.isArray(v) && v.every(isValue);
|
|
35
|
+
}
|
|
36
|
+
function mapEvaluationContext(context) {
|
|
37
|
+
const attributes = {};
|
|
38
|
+
let userkey;
|
|
39
|
+
if (context) {
|
|
40
|
+
for (const [key, value] of Object.entries(context)) {
|
|
41
|
+
if (value === void 0 || value === null) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const lower = key.toLowerCase();
|
|
45
|
+
if (lower === "targetingkey" || lower === "userkey") {
|
|
46
|
+
userkey = String(value);
|
|
47
|
+
} else if (lower === FHUB_WHEN_ACTIVE) {
|
|
48
|
+
continue;
|
|
49
|
+
} else if (isValue(value) || isAllowedList(value)) {
|
|
50
|
+
attributes[WELL_KNOWN_KEYS.has(lower) ? lower : key] = value;
|
|
51
|
+
} else {
|
|
52
|
+
throw new InvalidContextError(key);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return { userkey, attributes };
|
|
57
|
+
}
|
|
58
|
+
function toContextRecord(context) {
|
|
59
|
+
const { userkey, attributes } = mapEvaluationContext(context);
|
|
60
|
+
return userkey === void 0 ? { ...attributes } : { userkey, ...attributes };
|
|
61
|
+
}
|
|
62
|
+
function contextSignature(context) {
|
|
63
|
+
const { userkey, attributes } = mapEvaluationContext(context);
|
|
64
|
+
const json = (v) => v instanceof Date ? { date: v.toISOString() } : v;
|
|
65
|
+
const entries = Object.entries(attributes).map(([key, value]) => [
|
|
66
|
+
key,
|
|
67
|
+
Array.isArray(value) ? value.map(json) : json(value)
|
|
68
|
+
]);
|
|
69
|
+
if (userkey !== void 0) {
|
|
70
|
+
entries.push(["userkey", userkey]);
|
|
71
|
+
}
|
|
72
|
+
return JSON.stringify(entries.sort((a, b) => a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/lifecycle.ts
|
|
76
|
+
import {
|
|
77
|
+
ProviderFatalError
|
|
78
|
+
} from "@openfeature/core";
|
|
79
|
+
import { ClientEvalFeatureContext, Readyness } from "featurehub-javascript-core-sdk";
|
|
80
|
+
|
|
81
|
+
// src/resolution.ts
|
|
82
|
+
import {
|
|
83
|
+
ErrorCode,
|
|
84
|
+
StandardResolutionReasons
|
|
85
|
+
} from "@openfeature/core";
|
|
86
|
+
function captureFeature(holder) {
|
|
87
|
+
if (holder === void 0 || !holder.exists) {
|
|
88
|
+
return { exists: false };
|
|
89
|
+
}
|
|
90
|
+
const evaluated = holder.getValue();
|
|
91
|
+
return {
|
|
92
|
+
exists: true,
|
|
93
|
+
featureType: holder.getType()?.toString(),
|
|
94
|
+
version: holder.getVersion(),
|
|
95
|
+
value: evaluated?.value,
|
|
96
|
+
strategyId: evaluated?.strategyId,
|
|
97
|
+
split: evaluated?.split === true,
|
|
98
|
+
properties: holder.featureProperties
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
var FEATURE_TYPES = {
|
|
102
|
+
boolean: "BOOLEAN",
|
|
103
|
+
string: "STRING",
|
|
104
|
+
number: "NUMBER",
|
|
105
|
+
object: "JSON"
|
|
106
|
+
};
|
|
107
|
+
function expectedFeatureType(kind) {
|
|
108
|
+
return FEATURE_TYPES[kind];
|
|
109
|
+
}
|
|
110
|
+
function buildFlagMetadata(featureType, version, properties) {
|
|
111
|
+
const metadata = { ...properties ?? {} };
|
|
112
|
+
if (typeof version === "number" && version >= 0) {
|
|
113
|
+
metadata["featureVersion"] = version;
|
|
114
|
+
}
|
|
115
|
+
if (featureType) {
|
|
116
|
+
metadata["featureType"] = featureType;
|
|
117
|
+
}
|
|
118
|
+
return metadata;
|
|
119
|
+
}
|
|
120
|
+
function notReadyResolution(defaultValue) {
|
|
121
|
+
return {
|
|
122
|
+
value: defaultValue,
|
|
123
|
+
reason: StandardResolutionReasons.ERROR,
|
|
124
|
+
errorCode: ErrorCode.PROVIDER_NOT_READY,
|
|
125
|
+
errorMessage: "FeatureHub provider has not completed initialization."
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function flagNotFoundResolution(flagKey, defaultValue) {
|
|
129
|
+
return {
|
|
130
|
+
value: defaultValue,
|
|
131
|
+
reason: StandardResolutionReasons.ERROR,
|
|
132
|
+
errorCode: ErrorCode.FLAG_NOT_FOUND,
|
|
133
|
+
errorMessage: `Flag '${flagKey}' was not found in this FeatureHub environment.`
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function invalidContextResolution(defaultValue, error) {
|
|
137
|
+
return {
|
|
138
|
+
value: defaultValue,
|
|
139
|
+
reason: StandardResolutionReasons.ERROR,
|
|
140
|
+
errorCode: ErrorCode.INVALID_CONTEXT,
|
|
141
|
+
errorMessage: error instanceof Error ? error.message : String(error)
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function generalErrorResolution(defaultValue, error) {
|
|
145
|
+
return {
|
|
146
|
+
value: defaultValue,
|
|
147
|
+
reason: StandardResolutionReasons.ERROR,
|
|
148
|
+
errorCode: ErrorCode.GENERAL,
|
|
149
|
+
errorMessage: error instanceof Error ? error.message : String(error)
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
var NUMERIC_STRING = /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/;
|
|
153
|
+
function convert(flagKey, kind, value) {
|
|
154
|
+
const mismatch = () => ({
|
|
155
|
+
errorCode: ErrorCode.TYPE_MISMATCH,
|
|
156
|
+
errorMessage: `Flag '${flagKey}' has a value that cannot be resolved as ${kind}.`
|
|
157
|
+
});
|
|
158
|
+
switch (kind) {
|
|
159
|
+
case "boolean":
|
|
160
|
+
if (typeof value === "boolean") return { value };
|
|
161
|
+
if (value === "true") return { value: true };
|
|
162
|
+
if (value === "false") return { value: false };
|
|
163
|
+
return mismatch();
|
|
164
|
+
case "string":
|
|
165
|
+
if (typeof value === "string") return { value };
|
|
166
|
+
if (typeof value === "number" || typeof value === "boolean") return { value: String(value) };
|
|
167
|
+
return mismatch();
|
|
168
|
+
case "number":
|
|
169
|
+
if (typeof value === "number") return Number.isFinite(value) ? { value } : mismatch();
|
|
170
|
+
if (typeof value === "string" && NUMERIC_STRING.test(value)) {
|
|
171
|
+
const n = Number(value);
|
|
172
|
+
return Number.isFinite(n) ? { value: n } : mismatch();
|
|
173
|
+
}
|
|
174
|
+
return mismatch();
|
|
175
|
+
case "object": {
|
|
176
|
+
let decoded = value;
|
|
177
|
+
if (typeof value === "string") {
|
|
178
|
+
try {
|
|
179
|
+
decoded = JSON.parse(value);
|
|
180
|
+
} catch {
|
|
181
|
+
return {
|
|
182
|
+
errorCode: ErrorCode.PARSE_ERROR,
|
|
183
|
+
errorMessage: `Flag '${flagKey}' contains invalid JSON.`
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (typeof decoded === "object" && decoded !== null && !Array.isArray(decoded)) {
|
|
188
|
+
return { value: decoded };
|
|
189
|
+
}
|
|
190
|
+
return mismatch();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function resolveFromEvaluation(flagKey, kind, defaultValue, evaluation) {
|
|
195
|
+
if (!evaluation.exists) {
|
|
196
|
+
return flagNotFoundResolution(flagKey, defaultValue);
|
|
197
|
+
}
|
|
198
|
+
const metadata = buildFlagMetadata(
|
|
199
|
+
evaluation.featureType,
|
|
200
|
+
evaluation.version,
|
|
201
|
+
evaluation.properties
|
|
202
|
+
);
|
|
203
|
+
if (evaluation.featureType && evaluation.featureType !== expectedFeatureType(kind)) {
|
|
204
|
+
return {
|
|
205
|
+
value: defaultValue,
|
|
206
|
+
reason: StandardResolutionReasons.ERROR,
|
|
207
|
+
errorCode: ErrorCode.TYPE_MISMATCH,
|
|
208
|
+
errorMessage: `Flag '${flagKey}' is of type ${evaluation.featureType} and cannot be resolved as ${kind}.`,
|
|
209
|
+
flagMetadata: metadata
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
if (evaluation.value === void 0 || evaluation.value === null) {
|
|
213
|
+
return {
|
|
214
|
+
value: defaultValue,
|
|
215
|
+
reason: StandardResolutionReasons.DISABLED,
|
|
216
|
+
variant: evaluation.strategyId,
|
|
217
|
+
flagMetadata: metadata
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
const converted = convert(flagKey, kind, evaluation.value);
|
|
221
|
+
if (!("value" in converted)) {
|
|
222
|
+
return {
|
|
223
|
+
value: defaultValue,
|
|
224
|
+
reason: StandardResolutionReasons.ERROR,
|
|
225
|
+
errorCode: converted.errorCode,
|
|
226
|
+
errorMessage: converted.errorMessage,
|
|
227
|
+
flagMetadata: metadata
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
value: converted.value,
|
|
232
|
+
reason: !evaluation.strategyId ? StandardResolutionReasons.STATIC : evaluation.split ? StandardResolutionReasons.SPLIT : StandardResolutionReasons.TARGETING_MATCH,
|
|
233
|
+
variant: evaluation.strategyId,
|
|
234
|
+
flagMetadata: metadata
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/lifecycle.ts
|
|
239
|
+
var noEdge = {
|
|
240
|
+
contextChange: () => Promise.resolve(),
|
|
241
|
+
clientEvaluated: () => true,
|
|
242
|
+
requiresReplacementOnHeaderChange: () => false,
|
|
243
|
+
close: () => {
|
|
244
|
+
},
|
|
245
|
+
poll: () => Promise.resolve()
|
|
246
|
+
};
|
|
247
|
+
var given = (value) => value !== void 0 && value.trim().length > 0 ? value.trim() : void 0;
|
|
248
|
+
var PROVIDER_READY = "PROVIDER_READY";
|
|
249
|
+
var PROVIDER_ERROR = "PROVIDER_ERROR";
|
|
250
|
+
var PROVIDER_CONFIGURATION_CHANGED = "PROVIDER_CONFIGURATION_CHANGED";
|
|
251
|
+
var PROVIDER_STALE = "PROVIDER_STALE";
|
|
252
|
+
function applyTransport(cfg, options, defaultTransport) {
|
|
253
|
+
if (options.cacheTimeToLiveMs !== void 0) {
|
|
254
|
+
cfg.restPassive(options.cacheTimeToLiveMs);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
if (options.pollIntervalMs !== void 0) {
|
|
258
|
+
cfg.restActive(options.pollIntervalMs);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const transport = options.transport ?? defaultTransport;
|
|
262
|
+
if (transport === void 0) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
switch (transport) {
|
|
266
|
+
case "passive":
|
|
267
|
+
cfg.restPassive();
|
|
268
|
+
break;
|
|
269
|
+
case "polling":
|
|
270
|
+
cfg.restActive();
|
|
271
|
+
break;
|
|
272
|
+
default:
|
|
273
|
+
cfg.streaming();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
var FeatureHubProviderCore = class {
|
|
277
|
+
options;
|
|
278
|
+
/** Edge's URL and the API key, as given in code or found in the environment; neither for stores only */
|
|
279
|
+
host;
|
|
280
|
+
apiKey;
|
|
281
|
+
clientEval;
|
|
282
|
+
waitForReadyMs;
|
|
283
|
+
config;
|
|
284
|
+
/**
|
|
285
|
+
* Features are held and resolve: readiness is Ready, or Stale (a context change in progress, or a
|
|
286
|
+
* connection that stopped for good after delivering features; featurehub-protocol.yaml repository.readiness).
|
|
287
|
+
*/
|
|
288
|
+
ready = false;
|
|
289
|
+
started = false;
|
|
290
|
+
/** the connection has stopped for good (connection failed, or environment stale) */
|
|
291
|
+
stopped = false;
|
|
292
|
+
staleEmitted = false;
|
|
293
|
+
lastReadiness = Readyness.NotReady;
|
|
294
|
+
/** key → last seen feature version, so polls that change nothing stay silent. */
|
|
295
|
+
featureVersions = /* @__PURE__ */ new Map();
|
|
296
|
+
seenPayload = false;
|
|
297
|
+
/**
|
|
298
|
+
* featurehub-protocol.yaml providers.source_selection and validation_errors: a connection needs both a host
|
|
299
|
+
* and an API key; with neither, a `configFactory` can supply a config whose features come from stores alone.
|
|
300
|
+
*/
|
|
301
|
+
constructor(providerName, options, connection = options ?? {}) {
|
|
302
|
+
if (!options) {
|
|
303
|
+
throw new Error(`${providerName} requires its options.`);
|
|
304
|
+
}
|
|
305
|
+
const host = given(connection.host);
|
|
306
|
+
const apiKey = given(connection.apiKey);
|
|
307
|
+
if (host === void 0 && apiKey === void 0) {
|
|
308
|
+
if (!options.configFactory) {
|
|
309
|
+
throw new Error(
|
|
310
|
+
`${providerName} requires \`host\` and \`apiKey\`, or a \`configFactory\` whose config gets its features from stores.`
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
} else if (host === void 0 || apiKey === void 0) {
|
|
314
|
+
throw new Error(`${providerName} requires both \`host\` and \`apiKey\`: only one was given.`);
|
|
315
|
+
}
|
|
316
|
+
this.options = options;
|
|
317
|
+
this.host = host;
|
|
318
|
+
this.apiKey = apiKey;
|
|
319
|
+
this.clientEval = apiKey === void 0 || isClientEvaluatedKey(apiKey);
|
|
320
|
+
this.waitForReadyMs = options.waitForReadyMs ?? 1e4;
|
|
321
|
+
}
|
|
322
|
+
/** Creates the config, wires its listeners, applies the transport and calls `init()`. */
|
|
323
|
+
startConfig() {
|
|
324
|
+
const cfg = this.options.configFactory ? this.options.configFactory({ ...this.options, host: this.host, apiKey: this.apiKey }) : this.createConfig();
|
|
325
|
+
this.config = cfg;
|
|
326
|
+
if (this.apiKey === void 0) {
|
|
327
|
+
cfg.isClientEvaluated = true;
|
|
328
|
+
}
|
|
329
|
+
cfg.addReadinessListener((readiness) => this.onReadiness(readiness));
|
|
330
|
+
cfg.addConnectionEventListener?.(() => this.onConnectionStopped());
|
|
331
|
+
cfg.registerRawUpdateFeatureListener({
|
|
332
|
+
get connected() {
|
|
333
|
+
return cfg.initialized;
|
|
334
|
+
},
|
|
335
|
+
processUpdate: (feature) => this.emitChanged(this.noteVersions([feature], false)),
|
|
336
|
+
processUpdates: (features) => this.emitChanged(this.noteVersions(features, true)),
|
|
337
|
+
deleteFeature: (feature) => {
|
|
338
|
+
if (!feature?.key) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
this.featureVersions.delete(feature.key);
|
|
342
|
+
this.emitChanged([feature.key]);
|
|
343
|
+
},
|
|
344
|
+
close: () => {
|
|
345
|
+
},
|
|
346
|
+
configChanged: () => {
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
applyTransport(cfg, this.options, this.defaultTransport);
|
|
350
|
+
cfg.init();
|
|
351
|
+
return cfg;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Waits for the repository to become ready, then allows lifecycle events through
|
|
355
|
+
* (featurehub-protocol.yaml providers.events):
|
|
356
|
+
* - Failed (the connection was rejected before any features): initialisation fails, PROVIDER_FATAL
|
|
357
|
+
* - Stale with features, because the connection stopped for good with the first response: initialisation
|
|
358
|
+
* succeeds and STALE follows READY
|
|
359
|
+
* - anything else not Ready in time: initialisation fails
|
|
360
|
+
*/
|
|
361
|
+
async awaitReady(cfg, waitMs = this.waitForReadyMs) {
|
|
362
|
+
const readiness = await cfg.waitForReady(waitMs);
|
|
363
|
+
const stopped = this.stopped || cfg.connectionFailed === true || cfg.environmentStale === true;
|
|
364
|
+
if (readiness === Readyness.Failed) {
|
|
365
|
+
await this.onClose();
|
|
366
|
+
throw new ProviderFatalError(
|
|
367
|
+
"FeatureHub rejected the connection before sending any features (readiness: Failed)."
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
if (readiness !== Readyness.Ready && !(readiness === Readyness.Stale && stopped)) {
|
|
371
|
+
this.started = true;
|
|
372
|
+
throw new Error(
|
|
373
|
+
`FeatureHub did not become ready within ${this.waitForReadyMs}ms (readiness: ${readiness}).`
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
this.ready = true;
|
|
377
|
+
this.started = true;
|
|
378
|
+
if (stopped) {
|
|
379
|
+
this.stopped = true;
|
|
380
|
+
setTimeout(() => this.emitStale(), 0);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* OpenFeature tracking: records a FeatureHub named usage event (ClientContext.recordNamedUsage) with the
|
|
385
|
+
* evaluation context and the event's details, for the registered usage plugins (Segment, say). It needs a
|
|
386
|
+
* usage plugin to go anywhere, and never fails the caller: an unusable context records nothing.
|
|
387
|
+
*/
|
|
388
|
+
track(trackingEventName, context, trackingEventDetails) {
|
|
389
|
+
const cfg = this.config;
|
|
390
|
+
if (!cfg) {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
try {
|
|
394
|
+
const repository = cfg.repository();
|
|
395
|
+
const ctx = new ClientEvalFeatureContext(repository, noEdge);
|
|
396
|
+
ctx.attributes = toContextRecord(context);
|
|
397
|
+
ctx.recordNamedUsage(
|
|
398
|
+
trackingEventName,
|
|
399
|
+
trackingEventDetails ? { ...trackingEventDetails } : void 0
|
|
400
|
+
);
|
|
401
|
+
} catch {
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
async onClose() {
|
|
405
|
+
this.ready = false;
|
|
406
|
+
this.started = false;
|
|
407
|
+
this.stopped = false;
|
|
408
|
+
this.staleEmitted = false;
|
|
409
|
+
this.lastReadiness = Readyness.NotReady;
|
|
410
|
+
this.featureVersions.clear();
|
|
411
|
+
this.seenPayload = false;
|
|
412
|
+
const cfg = this.config;
|
|
413
|
+
this.config = void 0;
|
|
414
|
+
try {
|
|
415
|
+
cfg?.forceClose();
|
|
416
|
+
} catch {
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
/** Synchronous resolution helper (web providers). */
|
|
420
|
+
resolveSync(flagKey, kind, defaultValue, logger, take) {
|
|
421
|
+
if (!this.ready || !this.config) {
|
|
422
|
+
return notReadyResolution(defaultValue);
|
|
423
|
+
}
|
|
424
|
+
try {
|
|
425
|
+
return resolveFromEvaluation(flagKey, kind, defaultValue, take());
|
|
426
|
+
} catch (err) {
|
|
427
|
+
return this.failedResolution(flagKey, defaultValue, err, logger);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
/** Asynchronous resolution helper (node providers). */
|
|
431
|
+
async resolveAsync(flagKey, kind, defaultValue, logger, take) {
|
|
432
|
+
if (!this.ready || !this.config) {
|
|
433
|
+
return notReadyResolution(defaultValue);
|
|
434
|
+
}
|
|
435
|
+
try {
|
|
436
|
+
return resolveFromEvaluation(flagKey, kind, defaultValue, await take());
|
|
437
|
+
} catch (err) {
|
|
438
|
+
return this.failedResolution(flagKey, defaultValue, err, logger);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
failedResolution(flagKey, defaultValue, err, logger) {
|
|
442
|
+
if (err instanceof InvalidContextError || err instanceof ContextConflictError) {
|
|
443
|
+
return invalidContextResolution(defaultValue, err);
|
|
444
|
+
}
|
|
445
|
+
logger?.error(`FeatureHub evaluation of '${flagKey}' failed`, err);
|
|
446
|
+
return generalErrorResolution(defaultValue, err);
|
|
447
|
+
}
|
|
448
|
+
onReadiness(readiness) {
|
|
449
|
+
if (readiness === this.lastReadiness) {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
const wasReady = this.ready;
|
|
453
|
+
this.lastReadiness = readiness;
|
|
454
|
+
switch (readiness) {
|
|
455
|
+
case Readyness.Ready:
|
|
456
|
+
this.ready = true;
|
|
457
|
+
if (this.started && !wasReady) {
|
|
458
|
+
this.eventSink.emit(PROVIDER_READY, { message: "FeatureHub repository is ready" });
|
|
459
|
+
}
|
|
460
|
+
break;
|
|
461
|
+
case Readyness.Stale:
|
|
462
|
+
this.ready = wasReady || this.started;
|
|
463
|
+
break;
|
|
464
|
+
case Readyness.Failed:
|
|
465
|
+
this.ready = false;
|
|
466
|
+
if (this.started) {
|
|
467
|
+
this.eventSink.emit(PROVIDER_ERROR, { message: "FeatureHub repository failed" });
|
|
468
|
+
}
|
|
469
|
+
break;
|
|
470
|
+
default:
|
|
471
|
+
this.ready = false;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
onConnectionStopped() {
|
|
475
|
+
this.stopped = true;
|
|
476
|
+
if (this.started) {
|
|
477
|
+
this.emitStale();
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
/** STALE once, after READY, while features are held; resolutions continue from the last features. */
|
|
481
|
+
emitStale() {
|
|
482
|
+
if (this.staleEmitted || !this.ready || !this.config) {
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
this.staleEmitted = true;
|
|
486
|
+
this.eventSink.emit(PROVIDER_STALE, {
|
|
487
|
+
message: "FeatureHub will send no more updates: resolving from the last features"
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Records the versions carried by a payload and returns the keys that
|
|
492
|
+
* actually changed. The first payload after construction is a baseline: the
|
|
493
|
+
* repository replays the full feature set on every poll (and on every SSE
|
|
494
|
+
* reconnect), so without this comparison ConfigurationChanged would fire on
|
|
495
|
+
* every poll even when nothing changed. A full set also removes every feature
|
|
496
|
+
* it does not hold, which is a change too.
|
|
497
|
+
*/
|
|
498
|
+
noteVersions(features, fullSet) {
|
|
499
|
+
const baseline = !this.seenPayload;
|
|
500
|
+
const changed = [];
|
|
501
|
+
if (fullSet) {
|
|
502
|
+
const delivered = new Set(features.map((f) => f?.key).filter((k) => !!k));
|
|
503
|
+
for (const key of [...this.featureVersions.keys()]) {
|
|
504
|
+
if (!delivered.has(key)) {
|
|
505
|
+
this.featureVersions.delete(key);
|
|
506
|
+
if (!baseline) {
|
|
507
|
+
changed.push(key);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
for (const feature of features) {
|
|
513
|
+
const key = feature?.key;
|
|
514
|
+
if (!key) {
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
const had = this.featureVersions.has(key);
|
|
518
|
+
if (had ? this.featureVersions.get(key) !== feature.version : !baseline) {
|
|
519
|
+
changed.push(key);
|
|
520
|
+
}
|
|
521
|
+
this.featureVersions.set(key, feature.version);
|
|
522
|
+
}
|
|
523
|
+
this.seenPayload = true;
|
|
524
|
+
return changed;
|
|
525
|
+
}
|
|
526
|
+
emitChanged(keys) {
|
|
527
|
+
if (!this.started || keys.length === 0) {
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
this.eventSink.emit(PROVIDER_CONFIGURATION_CHANGED, { flagsChanged: keys });
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
export {
|
|
534
|
+
ContextConflictError,
|
|
535
|
+
FeatureHubProviderCore,
|
|
536
|
+
InvalidContextError,
|
|
537
|
+
applyTransport,
|
|
538
|
+
buildFlagMetadata,
|
|
539
|
+
captureFeature,
|
|
540
|
+
contextSignature,
|
|
541
|
+
expectedFeatureType,
|
|
542
|
+
flagNotFoundResolution,
|
|
543
|
+
generalErrorResolution,
|
|
544
|
+
invalidContextResolution,
|
|
545
|
+
isClientEvaluatedKey,
|
|
546
|
+
mapEvaluationContext,
|
|
547
|
+
notReadyResolution,
|
|
548
|
+
resolveFromEvaluation,
|
|
549
|
+
toContextRecord
|
|
550
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "featurehub-openfeature-common",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Shared context mapping and resolution helpers for the official FeatureHub OpenFeature providers (Node.js and Web). Not intended to be used directly.",
|
|
5
|
+
"author": "info@featurehub.io",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"module": "lib/index.js",
|
|
10
|
+
"homepage": "https://featurehub.io",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "github:featurehub-io/featurehub-javascript-sdk"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"feature-flag-api",
|
|
21
|
+
"feature-flag-sdk",
|
|
22
|
+
"feature-experimentation",
|
|
23
|
+
"feature-flag",
|
|
24
|
+
"flag",
|
|
25
|
+
"toggle",
|
|
26
|
+
"feature-toggle-api",
|
|
27
|
+
"remote-configuration",
|
|
28
|
+
"ab-testing",
|
|
29
|
+
"openfeature",
|
|
30
|
+
"openfeature-provider",
|
|
31
|
+
"featurehub"
|
|
32
|
+
],
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"import": "./dist/index.js",
|
|
37
|
+
"require": "./dist/index.cjs"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"provenance": false,
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@openfeature/core": ">=1.12.0 <2"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"featurehub-javascript-core-sdk": "^2.1.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@openfeature/core": "^1.12.0",
|
|
52
|
+
"rimraf": "6.1.3",
|
|
53
|
+
"tsup": "8.5.0",
|
|
54
|
+
"typescript": "5.9.3",
|
|
55
|
+
"vite": "^7.3.8",
|
|
56
|
+
"vitest": "^3.2.7",
|
|
57
|
+
"featurehub-tsconfig": "0.0.0"
|
|
58
|
+
},
|
|
59
|
+
"engines": {
|
|
60
|
+
"node": ">=20.0.0"
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "tsup",
|
|
64
|
+
"build:watch": "tsup --watch",
|
|
65
|
+
"clean": "rimraf node_modules dist",
|
|
66
|
+
"lint": "eslint .",
|
|
67
|
+
"lint:fix": "eslint --fix .",
|
|
68
|
+
"typecheck": "tsc --noEmit",
|
|
69
|
+
"test": "vitest run",
|
|
70
|
+
"test:watch": "vitest",
|
|
71
|
+
"test:coverage": "vitest run --coverage"
|
|
72
|
+
}
|
|
6
73
|
}
|