@launchdarkly/js-client-sdk-common 1.7.0 → 1.9.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/CHANGELOG.md +54 -0
- package/dist/DataManager.d.ts +64 -0
- package/dist/DataManager.d.ts.map +1 -0
- package/dist/HookRunner.d.ts +12 -0
- package/dist/HookRunner.d.ts.map +1 -0
- package/dist/LDClientImpl.d.ts +14 -48
- package/dist/LDClientImpl.d.ts.map +1 -1
- package/dist/LDEmitter.d.ts +13 -1
- package/dist/LDEmitter.d.ts.map +1 -1
- package/dist/api/LDClient.d.ts +13 -14
- package/dist/api/LDClient.d.ts.map +1 -1
- package/dist/api/LDOptions.d.ts +28 -12
- package/dist/api/LDOptions.d.ts.map +1 -1
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/integrations/Hooks.d.ts +133 -0
- package/dist/api/integrations/Hooks.d.ts.map +1 -0
- package/dist/api/integrations/index.d.ts +2 -0
- package/dist/api/integrations/index.d.ts.map +1 -0
- package/dist/configuration/Configuration.d.ts +59 -19
- package/dist/configuration/Configuration.d.ts.map +1 -1
- package/dist/configuration/index.d.ts +2 -2
- package/dist/configuration/index.d.ts.map +1 -1
- package/dist/configuration/validators.d.ts.map +1 -1
- package/dist/context/addAutoEnv.d.ts +3 -3
- package/dist/context/addAutoEnv.d.ts.map +1 -1
- package/dist/crypto/digest.d.ts +3 -0
- package/dist/crypto/digest.d.ts.map +1 -0
- package/dist/datasource/DataSourceEventHandler.d.ts +16 -0
- package/dist/datasource/DataSourceEventHandler.d.ts.map +1 -0
- package/dist/datasource/DataSourceStatus.d.ts +39 -0
- package/dist/datasource/DataSourceStatus.d.ts.map +1 -0
- package/dist/datasource/DataSourceStatusErrorInfo.d.ts +8 -0
- package/dist/datasource/DataSourceStatusErrorInfo.d.ts.map +1 -0
- package/dist/datasource/DataSourceStatusManager.d.ts +40 -0
- package/dist/datasource/DataSourceStatusManager.d.ts.map +1 -0
- package/dist/diagnostics/createDiagnosticsInitConfig.d.ts +1 -1
- package/dist/diagnostics/createDiagnosticsInitConfig.d.ts.map +1 -1
- package/dist/diagnostics/createDiagnosticsManager.d.ts +1 -1
- package/dist/diagnostics/createDiagnosticsManager.d.ts.map +1 -1
- package/dist/events/createEventProcessor.d.ts +3 -3
- package/dist/events/createEventProcessor.d.ts.map +1 -1
- package/dist/flag-manager/FlagManager.d.ts +39 -15
- package/dist/flag-manager/FlagManager.d.ts.map +1 -1
- package/dist/flag-manager/FlagPersistence.d.ts +2 -1
- package/dist/flag-manager/FlagPersistence.d.ts.map +1 -1
- package/dist/flag-manager/FlagStore.d.ts.map +1 -1
- package/dist/index.cjs +1686 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +16 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +1659 -0
- package/dist/index.mjs.map +1 -0
- package/dist/polling/PollingProcessor.d.ts.map +1 -1
- package/dist/polling/Requestor.d.ts +16 -1
- package/dist/polling/Requestor.d.ts.map +1 -1
- package/dist/storage/namespaceUtils.d.ts +7 -7
- package/dist/storage/namespaceUtils.d.ts.map +1 -1
- package/dist/streaming/DataSourceConfig.d.ts +24 -0
- package/dist/streaming/DataSourceConfig.d.ts.map +1 -0
- package/dist/streaming/StreamingProcessor.d.ts +33 -0
- package/dist/streaming/StreamingProcessor.d.ts.map +1 -0
- package/dist/streaming/index.d.ts +4 -0
- package/dist/streaming/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +18 -7
- package/dist/LDClientImpl.js +0 -460
- package/dist/LDClientImpl.js.map +0 -1
- package/dist/LDEmitter.js +0 -64
- package/dist/LDEmitter.js.map +0 -1
- package/dist/api/ConnectionMode.js +0 -3
- package/dist/api/ConnectionMode.js.map +0 -1
- package/dist/api/LDClient.js +0 -3
- package/dist/api/LDClient.js.map +0 -1
- package/dist/api/LDEvaluationDetail.js +0 -3
- package/dist/api/LDEvaluationDetail.js.map +0 -1
- package/dist/api/LDIdentifyOptions.js +0 -3
- package/dist/api/LDIdentifyOptions.js.map +0 -1
- package/dist/api/LDOptions.js +0 -3
- package/dist/api/LDOptions.js.map +0 -1
- package/dist/api/index.js +0 -20
- package/dist/api/index.js.map +0 -1
- package/dist/configuration/Configuration.js +0 -73
- package/dist/configuration/Configuration.js.map +0 -1
- package/dist/configuration/index.js +0 -5
- package/dist/configuration/index.js.map +0 -1
- package/dist/configuration/validators.js +0 -37
- package/dist/configuration/validators.js.map +0 -1
- package/dist/context/addAutoEnv.js +0 -114
- package/dist/context/addAutoEnv.js.map +0 -1
- package/dist/context/ensureKey.js +0 -72
- package/dist/context/ensureKey.js.map +0 -1
- package/dist/diagnostics/createDiagnosticsInitConfig.js +0 -19
- package/dist/diagnostics/createDiagnosticsInitConfig.js.map +0 -1
- package/dist/diagnostics/createDiagnosticsManager.js +0 -12
- package/dist/diagnostics/createDiagnosticsManager.js.map +0 -1
- package/dist/evaluation/evaluationDetail.js +0 -20
- package/dist/evaluation/evaluationDetail.js.map +0 -1
- package/dist/events/EventFactory.js +0 -25
- package/dist/events/EventFactory.js.map +0 -1
- package/dist/events/createEventProcessor.js +0 -11
- package/dist/events/createEventProcessor.js.map +0 -1
- package/dist/flag-manager/ContextIndex.js +0 -64
- package/dist/flag-manager/ContextIndex.js.map +0 -1
- package/dist/flag-manager/FlagManager.js +0 -74
- package/dist/flag-manager/FlagManager.js.map +0 -1
- package/dist/flag-manager/FlagPersistence.js +0 -120
- package/dist/flag-manager/FlagPersistence.js.map +0 -1
- package/dist/flag-manager/FlagStore.js +0 -28
- package/dist/flag-manager/FlagStore.js.map +0 -1
- package/dist/flag-manager/FlagUpdater.js +0 -69
- package/dist/flag-manager/FlagUpdater.js.map +0 -1
- package/dist/flag-manager/ItemDescriptor.js +0 -3
- package/dist/flag-manager/ItemDescriptor.js.map +0 -1
- package/dist/flag-manager/calculateChangedKeys.js +0 -22
- package/dist/flag-manager/calculateChangedKeys.js.map +0 -1
- package/dist/index.js +0 -22
- package/dist/index.js.map +0 -1
- package/dist/polling/PollingProcessor.js +0 -79
- package/dist/polling/PollingProcessor.js.map +0 -1
- package/dist/polling/Requestor.js +0 -50
- package/dist/polling/Requestor.js.map +0 -1
- package/dist/storage/getOrGenerateKey.js +0 -21
- package/dist/storage/getOrGenerateKey.js.map +0 -1
- package/dist/storage/namespaceUtils.js +0 -61
- package/dist/storage/namespaceUtils.js.map +0 -1
- package/dist/types/index.js +0 -3
- package/dist/types/index.js.map +0 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1686 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsSdkCommon = require('@launchdarkly/js-sdk-common');
|
|
4
|
+
|
|
5
|
+
function _interopNamespaceDefault(e) {
|
|
6
|
+
var n = Object.create(null);
|
|
7
|
+
if (e) {
|
|
8
|
+
Object.keys(e).forEach(function (k) {
|
|
9
|
+
if (k !== 'default') {
|
|
10
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
11
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return e[k]; }
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
n.default = e;
|
|
19
|
+
return Object.freeze(n);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
var jsSdkCommon__namespace = /*#__PURE__*/_interopNamespaceDefault(jsSdkCommon);
|
|
23
|
+
|
|
24
|
+
exports.DataSourceState = void 0;
|
|
25
|
+
(function (DataSourceState) {
|
|
26
|
+
DataSourceState["Initializing"] = "INITIALIZING";
|
|
27
|
+
DataSourceState["Valid"] = "VALID";
|
|
28
|
+
DataSourceState["Interrupted"] = "INTERRUPTED";
|
|
29
|
+
DataSourceState["SetOffline"] = "SET_OFFLINE";
|
|
30
|
+
DataSourceState["Closed"] = "CLOSED";
|
|
31
|
+
// TODO: SDK-702 - Implement network availability behaviors
|
|
32
|
+
// NetworkUnavailable,
|
|
33
|
+
})(exports.DataSourceState || (exports.DataSourceState = {}));
|
|
34
|
+
|
|
35
|
+
// eslint-disable-next-line max-classes-per-file
|
|
36
|
+
const validators = {
|
|
37
|
+
logger: jsSdkCommon.TypeValidators.Object,
|
|
38
|
+
maxCachedContexts: jsSdkCommon.TypeValidators.numberWithMin(0),
|
|
39
|
+
baseUri: jsSdkCommon.TypeValidators.String,
|
|
40
|
+
streamUri: jsSdkCommon.TypeValidators.String,
|
|
41
|
+
eventsUri: jsSdkCommon.TypeValidators.String,
|
|
42
|
+
capacity: jsSdkCommon.TypeValidators.numberWithMin(1),
|
|
43
|
+
diagnosticRecordingInterval: jsSdkCommon.TypeValidators.numberWithMin(2),
|
|
44
|
+
flushInterval: jsSdkCommon.TypeValidators.numberWithMin(2),
|
|
45
|
+
streamInitialReconnectDelay: jsSdkCommon.TypeValidators.numberWithMin(0),
|
|
46
|
+
allAttributesPrivate: jsSdkCommon.TypeValidators.Boolean,
|
|
47
|
+
debug: jsSdkCommon.TypeValidators.Boolean,
|
|
48
|
+
diagnosticOptOut: jsSdkCommon.TypeValidators.Boolean,
|
|
49
|
+
withReasons: jsSdkCommon.TypeValidators.Boolean,
|
|
50
|
+
sendEvents: jsSdkCommon.TypeValidators.Boolean,
|
|
51
|
+
pollInterval: jsSdkCommon.TypeValidators.numberWithMin(30),
|
|
52
|
+
useReport: jsSdkCommon.TypeValidators.Boolean,
|
|
53
|
+
privateAttributes: jsSdkCommon.TypeValidators.StringArray,
|
|
54
|
+
applicationInfo: jsSdkCommon.TypeValidators.Object,
|
|
55
|
+
wrapperName: jsSdkCommon.TypeValidators.String,
|
|
56
|
+
wrapperVersion: jsSdkCommon.TypeValidators.String,
|
|
57
|
+
payloadFilterKey: jsSdkCommon.TypeValidators.stringMatchingRegex(/^[a-zA-Z0-9](\w|\.|-)*$/),
|
|
58
|
+
hooks: jsSdkCommon.TypeValidators.createTypeArray('Hook[]', {}),
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const DEFAULT_POLLING_INTERVAL = 60 * 5;
|
|
62
|
+
const DEFAULT_POLLING = 'https://clientsdk.launchdarkly.com';
|
|
63
|
+
const DEFAULT_STREAM = 'https://clientstream.launchdarkly.com';
|
|
64
|
+
function ensureSafeLogger(logger) {
|
|
65
|
+
if (logger instanceof jsSdkCommon.SafeLogger) {
|
|
66
|
+
return logger;
|
|
67
|
+
}
|
|
68
|
+
// Even if logger is not defined this will produce a valid logger.
|
|
69
|
+
return jsSdkCommon.createSafeLogger(logger);
|
|
70
|
+
}
|
|
71
|
+
class ConfigurationImpl {
|
|
72
|
+
constructor(pristineOptions = {}, internalOptions = {}) {
|
|
73
|
+
this.logger = jsSdkCommon.createSafeLogger();
|
|
74
|
+
this.baseUri = DEFAULT_POLLING;
|
|
75
|
+
this.eventsUri = jsSdkCommon.ServiceEndpoints.DEFAULT_EVENTS;
|
|
76
|
+
this.streamUri = DEFAULT_STREAM;
|
|
77
|
+
this.maxCachedContexts = 5;
|
|
78
|
+
this.capacity = 100;
|
|
79
|
+
this.diagnosticRecordingInterval = 900;
|
|
80
|
+
this.flushInterval = 30;
|
|
81
|
+
this.streamInitialReconnectDelay = 1;
|
|
82
|
+
this.allAttributesPrivate = false;
|
|
83
|
+
this.debug = false;
|
|
84
|
+
this.diagnosticOptOut = false;
|
|
85
|
+
this.sendEvents = true;
|
|
86
|
+
this.sendLDHeaders = true;
|
|
87
|
+
this.useReport = false;
|
|
88
|
+
this.withReasons = false;
|
|
89
|
+
this.privateAttributes = [];
|
|
90
|
+
this.pollInterval = DEFAULT_POLLING_INTERVAL;
|
|
91
|
+
this.hooks = [];
|
|
92
|
+
this.logger = ensureSafeLogger(pristineOptions.logger);
|
|
93
|
+
const errors = this.validateTypesAndNames(pristineOptions);
|
|
94
|
+
errors.forEach((e) => this.logger.warn(e));
|
|
95
|
+
this.serviceEndpoints = new jsSdkCommon.ServiceEndpoints(this.streamUri, this.baseUri, this.eventsUri, internalOptions.analyticsEventPath, internalOptions.diagnosticEventPath, internalOptions.includeAuthorizationHeader, pristineOptions.payloadFilterKey);
|
|
96
|
+
this.useReport = pristineOptions.useReport ?? false;
|
|
97
|
+
this.tags = new jsSdkCommon.ApplicationTags({ application: this.applicationInfo, logger: this.logger });
|
|
98
|
+
this.userAgentHeaderName = internalOptions.userAgentHeaderName ?? 'user-agent';
|
|
99
|
+
this.trackEventModifier = internalOptions.trackEventModifier ?? ((event) => event);
|
|
100
|
+
}
|
|
101
|
+
validateTypesAndNames(pristineOptions) {
|
|
102
|
+
const errors = [];
|
|
103
|
+
Object.entries(pristineOptions).forEach(([k, v]) => {
|
|
104
|
+
const validator = validators[k];
|
|
105
|
+
if (validator) {
|
|
106
|
+
if (!validator.is(v)) {
|
|
107
|
+
const validatorType = validator.getType();
|
|
108
|
+
if (validatorType === 'boolean') {
|
|
109
|
+
errors.push(jsSdkCommon.OptionMessages.wrongOptionTypeBoolean(k, typeof v));
|
|
110
|
+
this[k] = !!v;
|
|
111
|
+
}
|
|
112
|
+
else if (validatorType === 'boolean | undefined | null') {
|
|
113
|
+
errors.push(jsSdkCommon.OptionMessages.wrongOptionTypeBoolean(k, typeof v));
|
|
114
|
+
if (typeof v !== 'boolean' && typeof v !== 'undefined' && v !== null) {
|
|
115
|
+
this[k] = !!v;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else if (validator instanceof jsSdkCommon.NumberWithMinimum && jsSdkCommon.TypeValidators.Number.is(v)) {
|
|
119
|
+
const { min } = validator;
|
|
120
|
+
errors.push(jsSdkCommon.OptionMessages.optionBelowMinimum(k, v, min));
|
|
121
|
+
this[k] = min;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
errors.push(jsSdkCommon.OptionMessages.wrongOptionType(k, validator.getType(), typeof v));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if (k === 'logger') ;
|
|
128
|
+
else {
|
|
129
|
+
// if an option is explicitly null, coerce to undefined
|
|
130
|
+
this[k] = v ?? undefined;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
errors.push(jsSdkCommon.OptionMessages.unknownOption(k));
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
return errors;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function digest(hasher, encoding) {
|
|
142
|
+
if (hasher.digest) {
|
|
143
|
+
return hasher.digest(encoding);
|
|
144
|
+
}
|
|
145
|
+
if (hasher.asyncDigest) {
|
|
146
|
+
return hasher.asyncDigest(encoding);
|
|
147
|
+
}
|
|
148
|
+
// This represents an error in platform implementation.
|
|
149
|
+
throw new Error('Platform must implement digest or asyncDigest');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* This function will retrieve a previously generated key for the given {@link storageKey} if it
|
|
154
|
+
* exists or generate and store one on the fly if it does not already exist.
|
|
155
|
+
* @param storageKey keyed storage location where the generated key should live. See {@link namespaceForGeneratedContextKey}
|
|
156
|
+
* for related exmaples of generating a storage key and usage.
|
|
157
|
+
* @param platform crypto and storage implementations for necessary operations
|
|
158
|
+
* @returns the generated key
|
|
159
|
+
*/
|
|
160
|
+
const getOrGenerateKey = async (storageKey, { crypto, storage }) => {
|
|
161
|
+
let generatedKey = await storage?.get(storageKey);
|
|
162
|
+
if (!generatedKey) {
|
|
163
|
+
generatedKey = crypto.randomUUID();
|
|
164
|
+
await storage?.set(storageKey, generatedKey);
|
|
165
|
+
}
|
|
166
|
+
return generatedKey;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Hashes the input and encodes it as base64
|
|
171
|
+
*/
|
|
172
|
+
function hashAndBase64Encode(crypto) {
|
|
173
|
+
return async (input) => digest(crypto.createHash('sha256').update(input), 'base64');
|
|
174
|
+
}
|
|
175
|
+
const noop = async (input) => input; // no-op transform
|
|
176
|
+
async function concatNamespacesAndValues(parts) {
|
|
177
|
+
const processedParts = await Promise.all(parts.map((part) => part.transform(part.value))); // use the transform from each part to transform the value
|
|
178
|
+
return processedParts.join('_');
|
|
179
|
+
}
|
|
180
|
+
async function namespaceForEnvironment(crypto, sdkKey) {
|
|
181
|
+
return concatNamespacesAndValues([
|
|
182
|
+
{ value: 'LaunchDarkly', transform: noop },
|
|
183
|
+
{ value: sdkKey, transform: hashAndBase64Encode(crypto) }, // hash sdk key and encode it
|
|
184
|
+
]);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* @deprecated prefer {@link namespaceForGeneratedContextKey}. At one time we only generated keys for
|
|
188
|
+
* anonymous contexts and they were namespaced in LaunchDarkly_AnonymousKeys. Eventually we started
|
|
189
|
+
* generating context keys for non-anonymous contexts such as for the Auto Environment Attributes
|
|
190
|
+
* feature and those were namespaced in LaunchDarkly_ContextKeys. This function can be removed
|
|
191
|
+
* when the data under the LaunchDarkly_AnonymousKeys namespace is merged with data under the
|
|
192
|
+
* LaunchDarkly_ContextKeys namespace.
|
|
193
|
+
*/
|
|
194
|
+
async function namespaceForAnonymousGeneratedContextKey(kind) {
|
|
195
|
+
return concatNamespacesAndValues([
|
|
196
|
+
{ value: 'LaunchDarkly', transform: noop },
|
|
197
|
+
{ value: 'AnonymousKeys', transform: noop },
|
|
198
|
+
{ value: kind, transform: noop }, // existing SDKs are not hashing or encoding this kind, though they should have
|
|
199
|
+
]);
|
|
200
|
+
}
|
|
201
|
+
async function namespaceForGeneratedContextKey(kind) {
|
|
202
|
+
return concatNamespacesAndValues([
|
|
203
|
+
{ value: 'LaunchDarkly', transform: noop },
|
|
204
|
+
{ value: 'ContextKeys', transform: noop },
|
|
205
|
+
{ value: kind, transform: noop }, // existing SDKs are not hashing or encoding this kind, though they should have
|
|
206
|
+
]);
|
|
207
|
+
}
|
|
208
|
+
async function namespaceForContextIndex(environmentNamespace) {
|
|
209
|
+
return concatNamespacesAndValues([
|
|
210
|
+
{ value: environmentNamespace, transform: noop },
|
|
211
|
+
{ value: 'ContextIndex', transform: noop },
|
|
212
|
+
]);
|
|
213
|
+
}
|
|
214
|
+
async function namespaceForContextData(crypto, environmentNamespace, context) {
|
|
215
|
+
return concatNamespacesAndValues([
|
|
216
|
+
{ value: environmentNamespace, transform: noop },
|
|
217
|
+
{ value: context.canonicalKey, transform: hashAndBase64Encode(crypto) }, // hash and encode canonical key
|
|
218
|
+
]);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
222
|
+
const { isLegacyUser: isLegacyUser$1, isSingleKind: isSingleKind$1, isMultiKind: isMultiKind$1 } = jsSdkCommon.internal;
|
|
223
|
+
const defaultAutoEnvSchemaVersion = '1.0';
|
|
224
|
+
const toMulti = (c) => {
|
|
225
|
+
const { kind, ...contextCommon } = c;
|
|
226
|
+
return {
|
|
227
|
+
kind: 'multi',
|
|
228
|
+
[kind]: contextCommon,
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
/**
|
|
232
|
+
* Clones the LDApplication object and populates the key, envAttributesVersion, id and version fields.
|
|
233
|
+
*
|
|
234
|
+
* @param crypto
|
|
235
|
+
* @param info
|
|
236
|
+
* @param applicationInfo
|
|
237
|
+
* @param config
|
|
238
|
+
* @return An LDApplication object with populated key, envAttributesVersion, id and version.
|
|
239
|
+
*/
|
|
240
|
+
const addApplicationInfo = async ({ crypto, info }, { applicationInfo }) => {
|
|
241
|
+
const { ld_application } = info.platformData();
|
|
242
|
+
let app = jsSdkCommon.deepCompact(ld_application) ?? {};
|
|
243
|
+
const id = applicationInfo?.id || app?.id;
|
|
244
|
+
if (id) {
|
|
245
|
+
const version = applicationInfo?.version || app?.version;
|
|
246
|
+
const name = applicationInfo?.name || app?.name;
|
|
247
|
+
const versionName = applicationInfo?.versionName || app?.versionName;
|
|
248
|
+
app = {
|
|
249
|
+
...app,
|
|
250
|
+
id,
|
|
251
|
+
// only add props if they are defined
|
|
252
|
+
...(version ? { version } : {}),
|
|
253
|
+
...(name ? { name } : {}),
|
|
254
|
+
...(versionName ? { versionName } : {}),
|
|
255
|
+
};
|
|
256
|
+
app.key = await digest(crypto.createHash('sha256').update(id), 'base64');
|
|
257
|
+
app.envAttributesVersion = app.envAttributesVersion || defaultAutoEnvSchemaVersion;
|
|
258
|
+
return app;
|
|
259
|
+
}
|
|
260
|
+
return undefined;
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* Clones the LDDevice object and populates the key and envAttributesVersion field.
|
|
264
|
+
*
|
|
265
|
+
* @param platform
|
|
266
|
+
* @return An LDDevice object with populated key and envAttributesVersion.
|
|
267
|
+
*/
|
|
268
|
+
const addDeviceInfo = async (platform) => {
|
|
269
|
+
const { ld_device, os } = platform.info.platformData();
|
|
270
|
+
const device = jsSdkCommon.deepCompact(ld_device) ?? {};
|
|
271
|
+
const name = os?.name || device.os?.name;
|
|
272
|
+
const version = os?.version || device.os?.version;
|
|
273
|
+
const family = device.os?.family;
|
|
274
|
+
// only add device.os if there's data
|
|
275
|
+
if (name || version || family) {
|
|
276
|
+
device.os = {
|
|
277
|
+
// only add props if they are defined
|
|
278
|
+
...(name ? { name } : {}),
|
|
279
|
+
...(version ? { version } : {}),
|
|
280
|
+
...(family ? { family } : {}),
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
// Check if device has any meaningful data before we return it.
|
|
284
|
+
if (Object.keys(device).filter((k) => k !== 'key' && k !== 'envAttributesVersion').length) {
|
|
285
|
+
const ldDeviceNamespace = await namespaceForGeneratedContextKey('ld_device');
|
|
286
|
+
device.key = await getOrGenerateKey(ldDeviceNamespace, platform);
|
|
287
|
+
device.envAttributesVersion = device.envAttributesVersion || defaultAutoEnvSchemaVersion;
|
|
288
|
+
return device;
|
|
289
|
+
}
|
|
290
|
+
return undefined;
|
|
291
|
+
};
|
|
292
|
+
const addAutoEnv = async (context, platform, config) => {
|
|
293
|
+
// LDUser is not supported for auto env reporting
|
|
294
|
+
if (isLegacyUser$1(context)) {
|
|
295
|
+
return context;
|
|
296
|
+
}
|
|
297
|
+
let ld_application;
|
|
298
|
+
let ld_device;
|
|
299
|
+
// Check if customer contexts exist. Only override if they are not provided.
|
|
300
|
+
if ((isSingleKind$1(context) && context.kind !== 'ld_application') ||
|
|
301
|
+
(isMultiKind$1(context) && !context.ld_application)) {
|
|
302
|
+
ld_application = await addApplicationInfo(platform, config);
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
config.logger.warn('Not adding ld_application environment attributes because it already exists.');
|
|
306
|
+
}
|
|
307
|
+
if ((isSingleKind$1(context) && context.kind !== 'ld_device') ||
|
|
308
|
+
(isMultiKind$1(context) && !context.ld_device)) {
|
|
309
|
+
ld_device = await addDeviceInfo(platform);
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
config.logger.warn('Not adding ld_device environment attributes because it already exists.');
|
|
313
|
+
}
|
|
314
|
+
// Unable to automatically add environment attributes for kind: {}. {} already exists.
|
|
315
|
+
if (ld_application || ld_device) {
|
|
316
|
+
const multi = isSingleKind$1(context) ? toMulti(context) : context;
|
|
317
|
+
return {
|
|
318
|
+
...multi,
|
|
319
|
+
...(ld_application ? { ld_application } : {}),
|
|
320
|
+
...(ld_device ? { ld_device } : {}),
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
return context;
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const { isLegacyUser, isMultiKind, isSingleKind } = jsSdkCommon.internal;
|
|
327
|
+
/**
|
|
328
|
+
* This is the root ensureKey function. All other ensureKey functions reduce to this.
|
|
329
|
+
*
|
|
330
|
+
* - ensureKeyCommon // private root function
|
|
331
|
+
* - ensureKeySingle
|
|
332
|
+
* - ensureKeyMulti
|
|
333
|
+
* - ensureKeyLegacy
|
|
334
|
+
* - ensureKey // exported for external use
|
|
335
|
+
*
|
|
336
|
+
* @param kind The LDContext kind
|
|
337
|
+
* @param c The LDContext object
|
|
338
|
+
* @param platform Platform containing crypto and storage needed for storing and querying keys.
|
|
339
|
+
*/
|
|
340
|
+
const ensureKeyCommon = async (kind, c, platform) => {
|
|
341
|
+
const { anonymous, key } = c;
|
|
342
|
+
if (anonymous && !key) {
|
|
343
|
+
const storageKey = await namespaceForAnonymousGeneratedContextKey(kind);
|
|
344
|
+
// This mutates a cloned copy of the original context from ensureyKey so this is safe.
|
|
345
|
+
// eslint-disable-next-line no-param-reassign
|
|
346
|
+
c.key = await getOrGenerateKey(storageKey, platform);
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
const ensureKeySingle = async (c, platform) => {
|
|
350
|
+
await ensureKeyCommon(c.kind, c, platform);
|
|
351
|
+
};
|
|
352
|
+
const ensureKeyMulti = async (multiContext, platform) => {
|
|
353
|
+
const { kind, ...singleContexts } = multiContext;
|
|
354
|
+
return Promise.all(Object.entries(singleContexts).map(([k, c]) => ensureKeyCommon(k, c, platform)));
|
|
355
|
+
};
|
|
356
|
+
const ensureKeyLegacy = async (c, platform) => {
|
|
357
|
+
await ensureKeyCommon('user', c, platform);
|
|
358
|
+
};
|
|
359
|
+
/**
|
|
360
|
+
* Ensure a key is always present in anonymous contexts. Non-anonymous contexts
|
|
361
|
+
* are not processed and will just be returned as is.
|
|
362
|
+
*
|
|
363
|
+
* @param context
|
|
364
|
+
* @param platform
|
|
365
|
+
*/
|
|
366
|
+
const ensureKey = async (context, platform) => {
|
|
367
|
+
const cloned = jsSdkCommon.clone(context);
|
|
368
|
+
if (isSingleKind(cloned)) {
|
|
369
|
+
await ensureKeySingle(cloned, platform);
|
|
370
|
+
}
|
|
371
|
+
if (isMultiKind(cloned)) {
|
|
372
|
+
await ensureKeyMulti(cloned, platform);
|
|
373
|
+
}
|
|
374
|
+
if (isLegacyUser(cloned)) {
|
|
375
|
+
await ensureKeyLegacy(cloned, platform);
|
|
376
|
+
}
|
|
377
|
+
return cloned;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const createDiagnosticsInitConfig = (config) => ({
|
|
381
|
+
customBaseURI: config.serviceEndpoints.polling !== DEFAULT_POLLING,
|
|
382
|
+
customStreamURI: config.serviceEndpoints.streaming !== DEFAULT_STREAM,
|
|
383
|
+
customEventsURI: config.serviceEndpoints.events !== jsSdkCommon.ServiceEndpoints.DEFAULT_EVENTS,
|
|
384
|
+
eventsCapacity: config.capacity,
|
|
385
|
+
eventsFlushIntervalMillis: jsSdkCommon.secondsToMillis(config.flushInterval),
|
|
386
|
+
reconnectTimeMillis: jsSdkCommon.secondsToMillis(config.streamInitialReconnectDelay),
|
|
387
|
+
diagnosticRecordingIntervalMillis: jsSdkCommon.secondsToMillis(config.diagnosticRecordingInterval),
|
|
388
|
+
allAttributesPrivate: config.allAttributesPrivate,
|
|
389
|
+
// TODO: Implement when corresponding features are implemented.
|
|
390
|
+
usingSecureMode: false,
|
|
391
|
+
bootstrapMode: false,
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
const createDiagnosticsManager = (clientSideID, config, platform) => {
|
|
395
|
+
if (config.sendEvents && !config.diagnosticOptOut) {
|
|
396
|
+
return new jsSdkCommon.internal.DiagnosticsManager(clientSideID, platform, createDiagnosticsInitConfig(config));
|
|
397
|
+
}
|
|
398
|
+
return undefined;
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
function createErrorEvaluationDetail(errorKind, def) {
|
|
402
|
+
return {
|
|
403
|
+
value: def ?? null,
|
|
404
|
+
variationIndex: null,
|
|
405
|
+
reason: { kind: 'ERROR', errorKind },
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
function createSuccessEvaluationDetail(value, variationIndex, reason) {
|
|
409
|
+
return {
|
|
410
|
+
value,
|
|
411
|
+
variationIndex: variationIndex ?? null,
|
|
412
|
+
reason: reason ?? null,
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const createEventProcessor = (clientSideID, config, platform, baseHeaders, diagnosticsManager) => {
|
|
417
|
+
if (config.sendEvents) {
|
|
418
|
+
return new jsSdkCommon.internal.EventProcessor({ ...config, eventsCapacity: config.capacity }, new jsSdkCommon.ClientContext(clientSideID, config, platform), baseHeaders, undefined, diagnosticsManager, false);
|
|
419
|
+
}
|
|
420
|
+
return undefined;
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* @internal
|
|
425
|
+
*/
|
|
426
|
+
class EventFactory extends jsSdkCommon.internal.EventFactoryBase {
|
|
427
|
+
evalEventClient(flagKey, value, defaultVal, flag, context, reason) {
|
|
428
|
+
const { trackEvents, debugEventsUntilDate, trackReason, flagVersion, version, variation } = flag;
|
|
429
|
+
return super.evalEvent({
|
|
430
|
+
addExperimentData: trackReason,
|
|
431
|
+
context,
|
|
432
|
+
debugEventsUntilDate,
|
|
433
|
+
defaultVal,
|
|
434
|
+
flagKey,
|
|
435
|
+
reason,
|
|
436
|
+
trackEvents: !!trackEvents,
|
|
437
|
+
value,
|
|
438
|
+
variation,
|
|
439
|
+
version: flagVersion ?? version,
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* An index for tracking the most recently used contexts by timestamp with the ability to
|
|
446
|
+
* update entry timestamps and prune out least used contexts above a max capacity provided.
|
|
447
|
+
*/
|
|
448
|
+
class ContextIndex {
|
|
449
|
+
constructor() {
|
|
450
|
+
this.container = { index: new Array() };
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Creates a {@link ContextIndex} from its JSON representation (likely retrieved from persistence).
|
|
454
|
+
* @param json representation of the {@link ContextIndex}
|
|
455
|
+
* @returns the {@link ContextIndex}
|
|
456
|
+
*/
|
|
457
|
+
static fromJson(json) {
|
|
458
|
+
const contextIndex = new ContextIndex();
|
|
459
|
+
try {
|
|
460
|
+
contextIndex.container = JSON.parse(json);
|
|
461
|
+
}
|
|
462
|
+
catch (e) {
|
|
463
|
+
/* ignoring error and returning empty index */
|
|
464
|
+
}
|
|
465
|
+
return contextIndex;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* @returns the JSON representation of the {@link ContextIndex} (like for saving to persistence)
|
|
469
|
+
*/
|
|
470
|
+
toJson() {
|
|
471
|
+
return JSON.stringify(this.container);
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Notice that a context has been used and when it was used. This will update an existing record
|
|
475
|
+
* with the given timestamp, or create a new record if one doesn't exist.
|
|
476
|
+
* @param id of the corresponding context
|
|
477
|
+
* @param timestamp in millis since epoch
|
|
478
|
+
*/
|
|
479
|
+
notice(id, timestamp) {
|
|
480
|
+
const entry = this.container.index.find((it) => it.id === id);
|
|
481
|
+
if (entry === undefined) {
|
|
482
|
+
this.container.index.push({ id, timestamp });
|
|
483
|
+
}
|
|
484
|
+
else {
|
|
485
|
+
entry.timestamp = timestamp;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Prune the index to the specified max size and then return the IDs
|
|
490
|
+
* @param maxContexts the maximum number of contexts to retain after this prune
|
|
491
|
+
* @returns an array of removed entries
|
|
492
|
+
*/
|
|
493
|
+
prune(maxContexts) {
|
|
494
|
+
const clampedMax = Math.max(maxContexts, 0); // clamp to [0, infinity)
|
|
495
|
+
if (this.container.index.length > clampedMax) {
|
|
496
|
+
// sort by timestamp so that older timestamps appear first in the array
|
|
497
|
+
this.container.index.sort((a, b) => a.timestamp - b.timestamp);
|
|
498
|
+
// delete the first N many elements above capacity. splice returns removed elements
|
|
499
|
+
return this.container.index.splice(0, this.container.index.length - clampedMax);
|
|
500
|
+
}
|
|
501
|
+
return [];
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* This class handles persisting and loading flag values from a persistent
|
|
507
|
+
* store. It intercepts updates and forwards them to the flag updater and
|
|
508
|
+
* then persists changes after the updater has completed.
|
|
509
|
+
*/
|
|
510
|
+
class FlagPersistence {
|
|
511
|
+
constructor(platform, environmentNamespace, maxCachedContexts, flagStore, flagUpdater, logger, timeStamper = () => Date.now()) {
|
|
512
|
+
this.platform = platform;
|
|
513
|
+
this.environmentNamespace = environmentNamespace;
|
|
514
|
+
this.maxCachedContexts = maxCachedContexts;
|
|
515
|
+
this.flagStore = flagStore;
|
|
516
|
+
this.flagUpdater = flagUpdater;
|
|
517
|
+
this.logger = logger;
|
|
518
|
+
this.timeStamper = timeStamper;
|
|
519
|
+
this.indexKeyPromise = namespaceForContextIndex(this.environmentNamespace);
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Inits flag persistence for the provided context with the provided flags. This will result
|
|
523
|
+
* in the underlying {@link FlagUpdater} switching its active context.
|
|
524
|
+
*/
|
|
525
|
+
async init(context, newFlags) {
|
|
526
|
+
this.flagUpdater.init(context, newFlags);
|
|
527
|
+
await this.storeCache(context);
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Upserts a flag into the {@link FlagUpdater} and stores that to persistence if the upsert
|
|
531
|
+
* was successful / accepted. An upsert may be rejected if the provided context is not
|
|
532
|
+
* the active context.
|
|
533
|
+
*/
|
|
534
|
+
async upsert(context, key, item) {
|
|
535
|
+
if (this.flagUpdater.upsert(context, key, item)) {
|
|
536
|
+
await this.storeCache(context);
|
|
537
|
+
return true;
|
|
538
|
+
}
|
|
539
|
+
return false;
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Loads the flags from persistence for the provided context and gives those to the
|
|
543
|
+
* {@link FlagUpdater} this {@link FlagPersistence} was constructed with.
|
|
544
|
+
*/
|
|
545
|
+
async loadCached(context) {
|
|
546
|
+
const storageKey = await namespaceForContextData(this.platform.crypto, this.environmentNamespace, context);
|
|
547
|
+
let flagsJson = await this.platform.storage?.get(storageKey);
|
|
548
|
+
if (flagsJson === null || flagsJson === undefined) {
|
|
549
|
+
// Fallback: in version <10.3.1 flag data was stored under the canonical key, check
|
|
550
|
+
// to see if data is present and migrate the data if present.
|
|
551
|
+
flagsJson = await this.platform.storage?.get(context.canonicalKey);
|
|
552
|
+
if (flagsJson === null || flagsJson === undefined) {
|
|
553
|
+
// return false indicating cache did not load if flag json is still absent
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
// migrate data from version <10.3.1 and cleanup data that was under canonical key
|
|
557
|
+
await this.platform.storage?.set(storageKey, flagsJson);
|
|
558
|
+
await this.platform.storage?.clear(context.canonicalKey);
|
|
559
|
+
}
|
|
560
|
+
try {
|
|
561
|
+
const flags = JSON.parse(flagsJson);
|
|
562
|
+
// mapping flags to item descriptors
|
|
563
|
+
const descriptors = Object.entries(flags).reduce((acc, [key, flag]) => {
|
|
564
|
+
acc[key] = { version: flag.version, flag };
|
|
565
|
+
return acc;
|
|
566
|
+
}, {});
|
|
567
|
+
this.flagUpdater.initCached(context, descriptors);
|
|
568
|
+
this.logger.debug('Loaded cached flag evaluations from persistent storage');
|
|
569
|
+
return true;
|
|
570
|
+
}
|
|
571
|
+
catch (e) {
|
|
572
|
+
this.logger.warn(`Could not load cached flag evaluations from persistent storage: ${e.message}`);
|
|
573
|
+
return false;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
async loadIndex() {
|
|
577
|
+
if (this.contextIndex !== undefined) {
|
|
578
|
+
return this.contextIndex;
|
|
579
|
+
}
|
|
580
|
+
const json = await this.platform.storage?.get(await this.indexKeyPromise);
|
|
581
|
+
if (!json) {
|
|
582
|
+
this.contextIndex = new ContextIndex();
|
|
583
|
+
return this.contextIndex;
|
|
584
|
+
}
|
|
585
|
+
try {
|
|
586
|
+
this.contextIndex = ContextIndex.fromJson(json);
|
|
587
|
+
this.logger.debug('Loaded context index from persistent storage');
|
|
588
|
+
}
|
|
589
|
+
catch (e) {
|
|
590
|
+
this.logger.warn(`Could not load index from persistent storage: ${e.message}`);
|
|
591
|
+
this.contextIndex = new ContextIndex();
|
|
592
|
+
}
|
|
593
|
+
return this.contextIndex;
|
|
594
|
+
}
|
|
595
|
+
async storeCache(context) {
|
|
596
|
+
const index = await this.loadIndex();
|
|
597
|
+
const storageKey = await namespaceForContextData(this.platform.crypto, this.environmentNamespace, context);
|
|
598
|
+
index.notice(storageKey, this.timeStamper());
|
|
599
|
+
const pruned = index.prune(this.maxCachedContexts);
|
|
600
|
+
await Promise.all(pruned.map(async (it) => this.platform.storage?.clear(it.id)));
|
|
601
|
+
// store index
|
|
602
|
+
await this.platform.storage?.set(await this.indexKeyPromise, index.toJson());
|
|
603
|
+
const allFlags = this.flagStore.getAll();
|
|
604
|
+
// mapping item descriptors to flags
|
|
605
|
+
const flags = Object.entries(allFlags).reduce((acc, [key, descriptor]) => {
|
|
606
|
+
if (descriptor.flag !== null && descriptor.flag !== undefined) {
|
|
607
|
+
acc[key] = descriptor.flag;
|
|
608
|
+
}
|
|
609
|
+
return acc;
|
|
610
|
+
}, {});
|
|
611
|
+
const jsonAll = JSON.stringify(flags);
|
|
612
|
+
// store flag data
|
|
613
|
+
await this.platform.storage?.set(storageKey, jsonAll);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* In memory flag store.
|
|
619
|
+
*/
|
|
620
|
+
class DefaultFlagStore {
|
|
621
|
+
constructor() {
|
|
622
|
+
this.flags = {};
|
|
623
|
+
}
|
|
624
|
+
init(newFlags) {
|
|
625
|
+
this.flags = Object.entries(newFlags).reduce((acc, [key, flag]) => {
|
|
626
|
+
acc[key] = flag;
|
|
627
|
+
return acc;
|
|
628
|
+
}, {});
|
|
629
|
+
}
|
|
630
|
+
insertOrUpdate(key, update) {
|
|
631
|
+
this.flags[key] = update;
|
|
632
|
+
}
|
|
633
|
+
get(key) {
|
|
634
|
+
if (Object.prototype.hasOwnProperty.call(this.flags, key)) {
|
|
635
|
+
return this.flags[key];
|
|
636
|
+
}
|
|
637
|
+
return undefined;
|
|
638
|
+
}
|
|
639
|
+
getAll() {
|
|
640
|
+
return this.flags;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function calculateChangedKeys(existingObject, newObject) {
|
|
645
|
+
const changedKeys = [];
|
|
646
|
+
// property deleted or updated
|
|
647
|
+
Object.entries(existingObject).forEach(([k, f]) => {
|
|
648
|
+
const subObject = newObject[k];
|
|
649
|
+
if (!subObject || !jsSdkCommon.fastDeepEqual(f, subObject)) {
|
|
650
|
+
changedKeys.push(k);
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
// property added
|
|
654
|
+
Object.keys(newObject).forEach((k) => {
|
|
655
|
+
if (!existingObject[k]) {
|
|
656
|
+
changedKeys.push(k);
|
|
657
|
+
}
|
|
658
|
+
});
|
|
659
|
+
return changedKeys;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* The flag updater handles logic required during the flag update process.
|
|
664
|
+
* It handles versions checking to handle out of order flag updates and
|
|
665
|
+
* also handles flag comparisons for change notification.
|
|
666
|
+
*/
|
|
667
|
+
class FlagUpdater {
|
|
668
|
+
constructor(flagStore, logger) {
|
|
669
|
+
this.changeCallbacks = new Array();
|
|
670
|
+
this.flagStore = flagStore;
|
|
671
|
+
this.logger = logger;
|
|
672
|
+
}
|
|
673
|
+
init(context, newFlags) {
|
|
674
|
+
this.activeContextKey = context.canonicalKey;
|
|
675
|
+
const oldFlags = this.flagStore.getAll();
|
|
676
|
+
this.flagStore.init(newFlags);
|
|
677
|
+
const changed = calculateChangedKeys(oldFlags, newFlags);
|
|
678
|
+
if (changed.length > 0) {
|
|
679
|
+
this.changeCallbacks.forEach((callback) => {
|
|
680
|
+
try {
|
|
681
|
+
callback(context, changed);
|
|
682
|
+
}
|
|
683
|
+
catch (err) {
|
|
684
|
+
/* intentionally empty */
|
|
685
|
+
}
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
initCached(context, newFlags) {
|
|
690
|
+
if (this.activeContextKey === context.canonicalKey) {
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
this.init(context, newFlags);
|
|
694
|
+
}
|
|
695
|
+
upsert(context, key, item) {
|
|
696
|
+
if (this.activeContextKey !== context.canonicalKey) {
|
|
697
|
+
this.logger.warn('Received an update for an inactive context.');
|
|
698
|
+
return false;
|
|
699
|
+
}
|
|
700
|
+
const currentValue = this.flagStore.get(key);
|
|
701
|
+
if (currentValue !== undefined && currentValue.version >= item.version) {
|
|
702
|
+
// this is an out of order update that can be ignored
|
|
703
|
+
return false;
|
|
704
|
+
}
|
|
705
|
+
this.flagStore.insertOrUpdate(key, item);
|
|
706
|
+
this.changeCallbacks.forEach((callback) => {
|
|
707
|
+
try {
|
|
708
|
+
callback(context, [key]);
|
|
709
|
+
}
|
|
710
|
+
catch (err) {
|
|
711
|
+
/* intentionally empty */
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
return true;
|
|
715
|
+
}
|
|
716
|
+
on(callback) {
|
|
717
|
+
this.changeCallbacks.push(callback);
|
|
718
|
+
}
|
|
719
|
+
off(callback) {
|
|
720
|
+
const index = this.changeCallbacks.indexOf(callback);
|
|
721
|
+
if (index > -1) {
|
|
722
|
+
this.changeCallbacks.splice(index, 1);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
class DefaultFlagManager {
|
|
728
|
+
/**
|
|
729
|
+
* @param platform implementation of various platform provided functionality
|
|
730
|
+
* @param sdkKey that will be used to distinguish different environments
|
|
731
|
+
* @param maxCachedContexts that specifies the max number of contexts that will be cached in persistence
|
|
732
|
+
* @param logger used for logging various messages
|
|
733
|
+
* @param timeStamper exists for testing purposes
|
|
734
|
+
*/
|
|
735
|
+
constructor(platform, sdkKey, maxCachedContexts, logger, timeStamper = () => Date.now()) {
|
|
736
|
+
this.timeStamper = timeStamper;
|
|
737
|
+
this.flagStore = new DefaultFlagStore();
|
|
738
|
+
this.flagUpdater = new FlagUpdater(this.flagStore, logger);
|
|
739
|
+
this.flagPersistencePromise = this.initPersistence(platform, sdkKey, maxCachedContexts, logger, timeStamper);
|
|
740
|
+
}
|
|
741
|
+
async initPersistence(platform, sdkKey, maxCachedContexts, logger, timeStamper = () => Date.now()) {
|
|
742
|
+
const environmentNamespace = await namespaceForEnvironment(platform.crypto, sdkKey);
|
|
743
|
+
return new FlagPersistence(platform, environmentNamespace, maxCachedContexts, this.flagStore, this.flagUpdater, logger, timeStamper);
|
|
744
|
+
}
|
|
745
|
+
get(key) {
|
|
746
|
+
return this.flagStore.get(key);
|
|
747
|
+
}
|
|
748
|
+
getAll() {
|
|
749
|
+
return this.flagStore.getAll();
|
|
750
|
+
}
|
|
751
|
+
setBootstrap(context, newFlags) {
|
|
752
|
+
// Bypasses the persistence as we do not want to put these flags into any cache.
|
|
753
|
+
// Generally speaking persistence likely *SHOULD* be disabled when using bootstrap.
|
|
754
|
+
this.flagUpdater.init(context, newFlags);
|
|
755
|
+
}
|
|
756
|
+
async init(context, newFlags) {
|
|
757
|
+
return (await this.flagPersistencePromise).init(context, newFlags);
|
|
758
|
+
}
|
|
759
|
+
async upsert(context, key, item) {
|
|
760
|
+
return (await this.flagPersistencePromise).upsert(context, key, item);
|
|
761
|
+
}
|
|
762
|
+
async loadCached(context) {
|
|
763
|
+
return (await this.flagPersistencePromise).loadCached(context);
|
|
764
|
+
}
|
|
765
|
+
on(callback) {
|
|
766
|
+
this.flagUpdater.on(callback);
|
|
767
|
+
}
|
|
768
|
+
off(callback) {
|
|
769
|
+
this.flagUpdater.off(callback);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
const UNKNOWN_HOOK_NAME = 'unknown hook';
|
|
774
|
+
const BEFORE_EVALUATION_STAGE_NAME = 'beforeEvaluation';
|
|
775
|
+
const AFTER_EVALUATION_STAGE_NAME = 'afterEvaluation';
|
|
776
|
+
function tryExecuteStage(logger, method, hookName, stage, def) {
|
|
777
|
+
try {
|
|
778
|
+
return stage();
|
|
779
|
+
}
|
|
780
|
+
catch (err) {
|
|
781
|
+
logger?.error(`An error was encountered in "${method}" of the "${hookName}" hook: ${err}`);
|
|
782
|
+
return def;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
function getHookName(logger, hook) {
|
|
786
|
+
try {
|
|
787
|
+
return hook.getMetadata().name || UNKNOWN_HOOK_NAME;
|
|
788
|
+
}
|
|
789
|
+
catch {
|
|
790
|
+
logger.error(`Exception thrown getting metadata for hook. Unable to get hook name.`);
|
|
791
|
+
return UNKNOWN_HOOK_NAME;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
function executeBeforeEvaluation(logger, hooks, hookContext) {
|
|
795
|
+
return hooks.map((hook) => tryExecuteStage(logger, BEFORE_EVALUATION_STAGE_NAME, getHookName(logger, hook), () => hook?.beforeEvaluation?.(hookContext, {}) ?? {}, {}));
|
|
796
|
+
}
|
|
797
|
+
function executeAfterEvaluation(logger, hooks, hookContext, updatedData, result) {
|
|
798
|
+
// This iterates in reverse, versus reversing a shallow copy of the hooks,
|
|
799
|
+
// for efficiency.
|
|
800
|
+
for (let hookIndex = hooks.length - 1; hookIndex >= 0; hookIndex -= 1) {
|
|
801
|
+
const hook = hooks[hookIndex];
|
|
802
|
+
const data = updatedData[hookIndex];
|
|
803
|
+
tryExecuteStage(logger, AFTER_EVALUATION_STAGE_NAME, getHookName(logger, hook), () => hook?.afterEvaluation?.(hookContext, data, result) ?? {}, {});
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
function executeBeforeIdentify(logger, hooks, hookContext) {
|
|
807
|
+
return hooks.map((hook) => tryExecuteStage(logger, BEFORE_EVALUATION_STAGE_NAME, getHookName(logger, hook), () => hook?.beforeIdentify?.(hookContext, {}) ?? {}, {}));
|
|
808
|
+
}
|
|
809
|
+
function executeAfterIdentify(logger, hooks, hookContext, updatedData, result) {
|
|
810
|
+
// This iterates in reverse, versus reversing a shallow copy of the hooks,
|
|
811
|
+
// for efficiency.
|
|
812
|
+
for (let hookIndex = hooks.length - 1; hookIndex >= 0; hookIndex -= 1) {
|
|
813
|
+
const hook = hooks[hookIndex];
|
|
814
|
+
const data = updatedData[hookIndex];
|
|
815
|
+
tryExecuteStage(logger, AFTER_EVALUATION_STAGE_NAME, getHookName(logger, hook), () => hook?.afterIdentify?.(hookContext, data, result) ?? {}, {});
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
class HookRunner {
|
|
819
|
+
constructor(logger, initialHooks) {
|
|
820
|
+
this.logger = logger;
|
|
821
|
+
this.hooks = [];
|
|
822
|
+
this.hooks.push(...initialHooks);
|
|
823
|
+
}
|
|
824
|
+
withEvaluation(key, context, defaultValue, method) {
|
|
825
|
+
if (this.hooks.length === 0) {
|
|
826
|
+
return method();
|
|
827
|
+
}
|
|
828
|
+
const hooks = [...this.hooks];
|
|
829
|
+
const hookContext = {
|
|
830
|
+
flagKey: key,
|
|
831
|
+
context,
|
|
832
|
+
defaultValue,
|
|
833
|
+
};
|
|
834
|
+
const hookData = executeBeforeEvaluation(this.logger, hooks, hookContext);
|
|
835
|
+
const result = method();
|
|
836
|
+
executeAfterEvaluation(this.logger, hooks, hookContext, hookData, result);
|
|
837
|
+
return result;
|
|
838
|
+
}
|
|
839
|
+
identify(context, timeout) {
|
|
840
|
+
const hooks = [...this.hooks];
|
|
841
|
+
const hookContext = {
|
|
842
|
+
context,
|
|
843
|
+
timeout,
|
|
844
|
+
};
|
|
845
|
+
const hookData = executeBeforeIdentify(this.logger, hooks, hookContext);
|
|
846
|
+
return (result) => {
|
|
847
|
+
executeAfterIdentify(this.logger, hooks, hookContext, hookData, result);
|
|
848
|
+
};
|
|
849
|
+
}
|
|
850
|
+
addHook(hook) {
|
|
851
|
+
this.hooks.push(hook);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
/**
|
|
856
|
+
* Implementation Note: There should not be any default listeners for change events in a client
|
|
857
|
+
* implementation. Default listeners mean a client cannot determine when there are actual
|
|
858
|
+
* application developer provided listeners. If we require default listeners, then we should add
|
|
859
|
+
* a system to allow listeners which have counts independent of the primary listener counts.
|
|
860
|
+
*/
|
|
861
|
+
class LDEmitter {
|
|
862
|
+
constructor(logger) {
|
|
863
|
+
this.logger = logger;
|
|
864
|
+
this.listeners = new Map();
|
|
865
|
+
}
|
|
866
|
+
on(name, listener) {
|
|
867
|
+
if (!this.listeners.has(name)) {
|
|
868
|
+
this.listeners.set(name, [listener]);
|
|
869
|
+
}
|
|
870
|
+
else {
|
|
871
|
+
this.listeners.get(name)?.push(listener);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Unsubscribe one or all events.
|
|
876
|
+
*
|
|
877
|
+
* @param name
|
|
878
|
+
* @param listener Optional. If unspecified, all listeners for the event will be removed.
|
|
879
|
+
*/
|
|
880
|
+
off(name, listener) {
|
|
881
|
+
const existingListeners = this.listeners.get(name);
|
|
882
|
+
if (!existingListeners) {
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
if (listener) {
|
|
886
|
+
// remove from internal cache
|
|
887
|
+
const updated = existingListeners.filter((fn) => fn !== listener);
|
|
888
|
+
if (updated.length === 0) {
|
|
889
|
+
this.listeners.delete(name);
|
|
890
|
+
}
|
|
891
|
+
else {
|
|
892
|
+
this.listeners.set(name, updated);
|
|
893
|
+
}
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
// listener was not specified, so remove them all for that event
|
|
897
|
+
this.listeners.delete(name);
|
|
898
|
+
}
|
|
899
|
+
invokeListener(listener, name, ...detail) {
|
|
900
|
+
try {
|
|
901
|
+
listener(...detail);
|
|
902
|
+
}
|
|
903
|
+
catch (err) {
|
|
904
|
+
this.logger?.error(`Encountered error invoking handler for "${name}", detail: "${err}"`);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
emit(name, ...detail) {
|
|
908
|
+
const listeners = this.listeners.get(name);
|
|
909
|
+
listeners?.forEach((listener) => this.invokeListener(listener, name, ...detail));
|
|
910
|
+
}
|
|
911
|
+
eventNames() {
|
|
912
|
+
return [...this.listeners.keys()];
|
|
913
|
+
}
|
|
914
|
+
listenerCount(name) {
|
|
915
|
+
return this.listeners.get(name)?.length ?? 0;
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
const { ClientMessages, ErrorKinds } = jsSdkCommon.internal;
|
|
920
|
+
class LDClientImpl {
|
|
921
|
+
/**
|
|
922
|
+
* Creates the client object synchronously. No async, no network calls.
|
|
923
|
+
*/
|
|
924
|
+
constructor(sdkKey, autoEnvAttributes, platform, options, dataManagerFactory, internalOptions) {
|
|
925
|
+
this.sdkKey = sdkKey;
|
|
926
|
+
this.autoEnvAttributes = autoEnvAttributes;
|
|
927
|
+
this.platform = platform;
|
|
928
|
+
this.identifyTimeout = 5;
|
|
929
|
+
this.highTimeoutThreshold = 15;
|
|
930
|
+
this.eventFactoryDefault = new EventFactory(false);
|
|
931
|
+
this.eventFactoryWithReasons = new EventFactory(true);
|
|
932
|
+
this.eventSendingEnabled = false;
|
|
933
|
+
if (!sdkKey) {
|
|
934
|
+
throw new Error('You must configure the client with a client-side SDK key');
|
|
935
|
+
}
|
|
936
|
+
if (!platform.encoding) {
|
|
937
|
+
throw new Error('Platform must implement Encoding because btoa is required.');
|
|
938
|
+
}
|
|
939
|
+
this.config = new ConfigurationImpl(options, internalOptions);
|
|
940
|
+
this.logger = this.config.logger;
|
|
941
|
+
this.baseHeaders = jsSdkCommon.defaultHeaders(this.sdkKey, this.platform.info, this.config.tags, this.config.serviceEndpoints.includeAuthorizationHeader, this.config.userAgentHeaderName);
|
|
942
|
+
this.flagManager = new DefaultFlagManager(this.platform, sdkKey, this.config.maxCachedContexts, this.config.logger);
|
|
943
|
+
this.diagnosticsManager = createDiagnosticsManager(sdkKey, this.config, platform);
|
|
944
|
+
this.eventProcessor = createEventProcessor(sdkKey, this.config, platform, this.baseHeaders, this.diagnosticsManager);
|
|
945
|
+
this.emitter = new LDEmitter();
|
|
946
|
+
this.emitter.on('error', (c, err) => {
|
|
947
|
+
this.logger.error(`error: ${err}, context: ${JSON.stringify(c)}`);
|
|
948
|
+
});
|
|
949
|
+
this.flagManager.on((context, flagKeys) => {
|
|
950
|
+
const ldContext = jsSdkCommon.Context.toLDContext(context);
|
|
951
|
+
this.emitter.emit('change', ldContext, flagKeys);
|
|
952
|
+
flagKeys.forEach((it) => {
|
|
953
|
+
this.emitter.emit(`change:${it}`, ldContext);
|
|
954
|
+
});
|
|
955
|
+
});
|
|
956
|
+
this.dataManager = dataManagerFactory(this.flagManager, this.config, this.baseHeaders, this.emitter, this.diagnosticsManager);
|
|
957
|
+
this.hookRunner = new HookRunner(this.logger, this.config.hooks);
|
|
958
|
+
}
|
|
959
|
+
allFlags() {
|
|
960
|
+
// extracting all flag values
|
|
961
|
+
const result = Object.entries(this.flagManager.getAll()).reduce((acc, [key, descriptor]) => {
|
|
962
|
+
if (descriptor.flag !== null && descriptor.flag !== undefined && !descriptor.flag.deleted) {
|
|
963
|
+
acc[key] = descriptor.flag.value;
|
|
964
|
+
}
|
|
965
|
+
return acc;
|
|
966
|
+
}, {});
|
|
967
|
+
return result;
|
|
968
|
+
}
|
|
969
|
+
async close() {
|
|
970
|
+
await this.flush();
|
|
971
|
+
this.eventProcessor?.close();
|
|
972
|
+
this.updateProcessor?.close();
|
|
973
|
+
this.logger.debug('Closed event processor and data source.');
|
|
974
|
+
}
|
|
975
|
+
async flush() {
|
|
976
|
+
try {
|
|
977
|
+
await this.eventProcessor?.flush();
|
|
978
|
+
this.logger.debug('Successfully flushed event processor.');
|
|
979
|
+
}
|
|
980
|
+
catch (e) {
|
|
981
|
+
this.logger.error(`Error flushing event processor: ${e}.`);
|
|
982
|
+
return { error: e, result: false };
|
|
983
|
+
}
|
|
984
|
+
return { result: true };
|
|
985
|
+
}
|
|
986
|
+
getContext() {
|
|
987
|
+
// The LDContext returned here may have been modified by the SDK (for example: adding auto env attributes).
|
|
988
|
+
// We are returning an LDContext here to maintain a consistent represetnation of context to the consuming
|
|
989
|
+
// code. We are returned the unchecked context so that if a consumer identifies with an invalid context
|
|
990
|
+
// and then calls getContext, they get back the same context they provided, without any assertion about
|
|
991
|
+
// validity.
|
|
992
|
+
return this.uncheckedContext ? jsSdkCommon.clone(this.uncheckedContext) : undefined;
|
|
993
|
+
}
|
|
994
|
+
getInternalContext() {
|
|
995
|
+
return this.checkedContext;
|
|
996
|
+
}
|
|
997
|
+
createIdentifyPromise(timeout) {
|
|
998
|
+
let res;
|
|
999
|
+
let rej;
|
|
1000
|
+
const slow = new Promise((resolve, reject) => {
|
|
1001
|
+
res = resolve;
|
|
1002
|
+
rej = reject;
|
|
1003
|
+
});
|
|
1004
|
+
const timed = jsSdkCommon.timedPromise(timeout, 'identify');
|
|
1005
|
+
const raced = Promise.race([timed, slow]).catch((e) => {
|
|
1006
|
+
if (e.message.includes('timed out')) {
|
|
1007
|
+
this.logger.error(`identify error: ${e}`);
|
|
1008
|
+
}
|
|
1009
|
+
throw e;
|
|
1010
|
+
});
|
|
1011
|
+
return { identifyPromise: raced, identifyResolve: res, identifyReject: rej };
|
|
1012
|
+
}
|
|
1013
|
+
/**
|
|
1014
|
+
* Identifies a context to LaunchDarkly. See {@link LDClient.identify}.
|
|
1015
|
+
*
|
|
1016
|
+
* @param pristineContext The LDContext object to be identified.
|
|
1017
|
+
* @param identifyOptions Optional configuration. See {@link LDIdentifyOptions}.
|
|
1018
|
+
* @returns A Promise which resolves when the flag values for the specified
|
|
1019
|
+
* context are available. It rejects when:
|
|
1020
|
+
*
|
|
1021
|
+
* 1. The context is unspecified or has no key.
|
|
1022
|
+
*
|
|
1023
|
+
* 2. The identify timeout is exceeded. In client SDKs this defaults to 5s.
|
|
1024
|
+
* You can customize this timeout with {@link LDIdentifyOptions | identifyOptions}.
|
|
1025
|
+
*
|
|
1026
|
+
* 3. A network error is encountered during initialization.
|
|
1027
|
+
*/
|
|
1028
|
+
async identify(pristineContext, identifyOptions) {
|
|
1029
|
+
if (identifyOptions?.timeout) {
|
|
1030
|
+
this.identifyTimeout = identifyOptions.timeout;
|
|
1031
|
+
}
|
|
1032
|
+
if (this.identifyTimeout > this.highTimeoutThreshold) {
|
|
1033
|
+
this.logger.warn('The identify function was called with a timeout greater than ' +
|
|
1034
|
+
`${this.highTimeoutThreshold} seconds. We recommend a timeout of less than ` +
|
|
1035
|
+
`${this.highTimeoutThreshold} seconds.`);
|
|
1036
|
+
}
|
|
1037
|
+
let context = await ensureKey(pristineContext, this.platform);
|
|
1038
|
+
if (this.autoEnvAttributes === jsSdkCommon.AutoEnvAttributes.Enabled) {
|
|
1039
|
+
context = await addAutoEnv(context, this.platform, this.config);
|
|
1040
|
+
}
|
|
1041
|
+
const checkedContext = jsSdkCommon.Context.fromLDContext(context);
|
|
1042
|
+
if (!checkedContext.valid) {
|
|
1043
|
+
const error = new Error('Context was unspecified or had no key');
|
|
1044
|
+
this.emitter.emit('error', context, error);
|
|
1045
|
+
return Promise.reject(error);
|
|
1046
|
+
}
|
|
1047
|
+
this.uncheckedContext = context;
|
|
1048
|
+
this.checkedContext = checkedContext;
|
|
1049
|
+
this.eventProcessor?.sendEvent(this.eventFactoryDefault.identifyEvent(this.checkedContext));
|
|
1050
|
+
const { identifyPromise, identifyResolve, identifyReject } = this.createIdentifyPromise(this.identifyTimeout);
|
|
1051
|
+
this.logger.debug(`Identifying ${JSON.stringify(this.checkedContext)}`);
|
|
1052
|
+
const afterIdentify = this.hookRunner.identify(context, identifyOptions?.timeout);
|
|
1053
|
+
await this.dataManager.identify(identifyResolve, identifyReject, checkedContext, identifyOptions);
|
|
1054
|
+
return identifyPromise.then((res) => {
|
|
1055
|
+
afterIdentify({ status: 'completed' });
|
|
1056
|
+
return res;
|
|
1057
|
+
}, (e) => {
|
|
1058
|
+
afterIdentify({ status: 'error' });
|
|
1059
|
+
throw e;
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
on(eventName, listener) {
|
|
1063
|
+
this.emitter.on(eventName, listener);
|
|
1064
|
+
}
|
|
1065
|
+
off(eventName, listener) {
|
|
1066
|
+
this.emitter.off(eventName, listener);
|
|
1067
|
+
}
|
|
1068
|
+
track(key, data, metricValue) {
|
|
1069
|
+
if (!this.checkedContext || !this.checkedContext.valid) {
|
|
1070
|
+
this.logger.warn(ClientMessages.missingContextKeyNoEvent);
|
|
1071
|
+
return;
|
|
1072
|
+
}
|
|
1073
|
+
// 0 is valid, so do not truthy check the metric value
|
|
1074
|
+
if (metricValue !== undefined && !jsSdkCommon.TypeValidators.Number.is(metricValue)) {
|
|
1075
|
+
this.logger?.warn(ClientMessages.invalidMetricValue(typeof metricValue));
|
|
1076
|
+
}
|
|
1077
|
+
this.eventProcessor?.sendEvent(this.config.trackEventModifier(this.eventFactoryDefault.customEvent(key, this.checkedContext, data, metricValue)));
|
|
1078
|
+
}
|
|
1079
|
+
variationInternal(flagKey, defaultValue, eventFactory, typeChecker) {
|
|
1080
|
+
if (!this.uncheckedContext) {
|
|
1081
|
+
this.logger.debug(ClientMessages.missingContextKeyNoEvent);
|
|
1082
|
+
return createErrorEvaluationDetail(ErrorKinds.UserNotSpecified, defaultValue);
|
|
1083
|
+
}
|
|
1084
|
+
const evalContext = jsSdkCommon.Context.fromLDContext(this.uncheckedContext);
|
|
1085
|
+
const foundItem = this.flagManager.get(flagKey);
|
|
1086
|
+
if (foundItem === undefined || foundItem.flag.deleted) {
|
|
1087
|
+
const defVal = defaultValue ?? null;
|
|
1088
|
+
const error = new jsSdkCommon.LDClientError(`Unknown feature flag "${flagKey}"; returning default value ${defVal}.`);
|
|
1089
|
+
this.emitter.emit('error', this.uncheckedContext, error);
|
|
1090
|
+
this.eventProcessor?.sendEvent(this.eventFactoryDefault.unknownFlagEvent(flagKey, defVal, evalContext));
|
|
1091
|
+
return createErrorEvaluationDetail(ErrorKinds.FlagNotFound, defaultValue);
|
|
1092
|
+
}
|
|
1093
|
+
const { reason, value, variation } = foundItem.flag;
|
|
1094
|
+
if (typeChecker) {
|
|
1095
|
+
const [matched, type] = typeChecker(value);
|
|
1096
|
+
if (!matched) {
|
|
1097
|
+
this.eventProcessor?.sendEvent(eventFactory.evalEventClient(flagKey, defaultValue, // track default value on type errors
|
|
1098
|
+
defaultValue, foundItem.flag, evalContext, reason));
|
|
1099
|
+
const error = new jsSdkCommon.LDClientError(`Wrong type "${type}" for feature flag "${flagKey}"; returning default value`);
|
|
1100
|
+
this.emitter.emit('error', this.uncheckedContext, error);
|
|
1101
|
+
return createErrorEvaluationDetail(ErrorKinds.WrongType, defaultValue);
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
const successDetail = createSuccessEvaluationDetail(value, variation, reason);
|
|
1105
|
+
if (value === undefined || value === null) {
|
|
1106
|
+
this.logger.debug('Result value is null. Providing default value.');
|
|
1107
|
+
successDetail.value = defaultValue;
|
|
1108
|
+
}
|
|
1109
|
+
this.eventProcessor?.sendEvent(eventFactory.evalEventClient(flagKey, value, defaultValue, foundItem.flag, evalContext, reason));
|
|
1110
|
+
return successDetail;
|
|
1111
|
+
}
|
|
1112
|
+
variation(flagKey, defaultValue) {
|
|
1113
|
+
const { value } = this.hookRunner.withEvaluation(flagKey, this.uncheckedContext, defaultValue, () => this.variationInternal(flagKey, defaultValue, this.eventFactoryDefault));
|
|
1114
|
+
return value;
|
|
1115
|
+
}
|
|
1116
|
+
variationDetail(flagKey, defaultValue) {
|
|
1117
|
+
return this.hookRunner.withEvaluation(flagKey, this.uncheckedContext, defaultValue, () => this.variationInternal(flagKey, defaultValue, this.eventFactoryWithReasons));
|
|
1118
|
+
}
|
|
1119
|
+
typedEval(key, defaultValue, eventFactory, typeChecker) {
|
|
1120
|
+
return this.hookRunner.withEvaluation(key, this.uncheckedContext, defaultValue, () => this.variationInternal(key, defaultValue, eventFactory, typeChecker));
|
|
1121
|
+
}
|
|
1122
|
+
boolVariation(key, defaultValue) {
|
|
1123
|
+
return this.typedEval(key, defaultValue, this.eventFactoryDefault, (value) => [
|
|
1124
|
+
jsSdkCommon.TypeValidators.Boolean.is(value),
|
|
1125
|
+
jsSdkCommon.TypeValidators.Boolean.getType(),
|
|
1126
|
+
]).value;
|
|
1127
|
+
}
|
|
1128
|
+
jsonVariation(key, defaultValue) {
|
|
1129
|
+
return this.variation(key, defaultValue);
|
|
1130
|
+
}
|
|
1131
|
+
numberVariation(key, defaultValue) {
|
|
1132
|
+
return this.typedEval(key, defaultValue, this.eventFactoryDefault, (value) => [
|
|
1133
|
+
jsSdkCommon.TypeValidators.Number.is(value),
|
|
1134
|
+
jsSdkCommon.TypeValidators.Number.getType(),
|
|
1135
|
+
]).value;
|
|
1136
|
+
}
|
|
1137
|
+
stringVariation(key, defaultValue) {
|
|
1138
|
+
return this.typedEval(key, defaultValue, this.eventFactoryDefault, (value) => [
|
|
1139
|
+
jsSdkCommon.TypeValidators.String.is(value),
|
|
1140
|
+
jsSdkCommon.TypeValidators.String.getType(),
|
|
1141
|
+
]).value;
|
|
1142
|
+
}
|
|
1143
|
+
boolVariationDetail(key, defaultValue) {
|
|
1144
|
+
return this.typedEval(key, defaultValue, this.eventFactoryWithReasons, (value) => [
|
|
1145
|
+
jsSdkCommon.TypeValidators.Boolean.is(value),
|
|
1146
|
+
jsSdkCommon.TypeValidators.Boolean.getType(),
|
|
1147
|
+
]);
|
|
1148
|
+
}
|
|
1149
|
+
numberVariationDetail(key, defaultValue) {
|
|
1150
|
+
return this.typedEval(key, defaultValue, this.eventFactoryWithReasons, (value) => [
|
|
1151
|
+
jsSdkCommon.TypeValidators.Number.is(value),
|
|
1152
|
+
jsSdkCommon.TypeValidators.Number.getType(),
|
|
1153
|
+
]);
|
|
1154
|
+
}
|
|
1155
|
+
stringVariationDetail(key, defaultValue) {
|
|
1156
|
+
return this.typedEval(key, defaultValue, this.eventFactoryWithReasons, (value) => [
|
|
1157
|
+
jsSdkCommon.TypeValidators.String.is(value),
|
|
1158
|
+
jsSdkCommon.TypeValidators.String.getType(),
|
|
1159
|
+
]);
|
|
1160
|
+
}
|
|
1161
|
+
jsonVariationDetail(key, defaultValue) {
|
|
1162
|
+
return this.variationDetail(key, defaultValue);
|
|
1163
|
+
}
|
|
1164
|
+
addHook(hook) {
|
|
1165
|
+
this.hookRunner.addHook(hook);
|
|
1166
|
+
}
|
|
1167
|
+
/**
|
|
1168
|
+
* Enable/Disable event sending.
|
|
1169
|
+
* @param enabled True to enable event processing, false to disable.
|
|
1170
|
+
* @param flush True to flush while disabling. Useful to flush on certain state transitions.
|
|
1171
|
+
*/
|
|
1172
|
+
setEventSendingEnabled(enabled, flush) {
|
|
1173
|
+
if (this.eventSendingEnabled === enabled) {
|
|
1174
|
+
return;
|
|
1175
|
+
}
|
|
1176
|
+
this.eventSendingEnabled = enabled;
|
|
1177
|
+
if (enabled) {
|
|
1178
|
+
this.logger.debug('Starting event processor');
|
|
1179
|
+
this.eventProcessor?.start();
|
|
1180
|
+
}
|
|
1181
|
+
else if (flush) {
|
|
1182
|
+
this.logger?.debug('Flushing event processor before disabling.');
|
|
1183
|
+
// Disable and flush.
|
|
1184
|
+
this.flush().then(() => {
|
|
1185
|
+
// While waiting for the flush event sending could be re-enabled, in which case
|
|
1186
|
+
// we do not want to close the event processor.
|
|
1187
|
+
if (!this.eventSendingEnabled) {
|
|
1188
|
+
this.logger?.debug('Stopping event processor.');
|
|
1189
|
+
this.eventProcessor?.close();
|
|
1190
|
+
}
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1193
|
+
else {
|
|
1194
|
+
// Just disabled.
|
|
1195
|
+
this.logger?.debug('Stopping event processor.');
|
|
1196
|
+
this.eventProcessor?.close();
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
sendEvent(event) {
|
|
1200
|
+
this.eventProcessor?.sendEvent(event);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
function isOk(status) {
|
|
1205
|
+
return status >= 200 && status <= 299;
|
|
1206
|
+
}
|
|
1207
|
+
class LDRequestError extends Error {
|
|
1208
|
+
constructor(message, status) {
|
|
1209
|
+
super(message);
|
|
1210
|
+
this.status = status;
|
|
1211
|
+
this.name = 'LaunchDarklyRequestError';
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* Note: The requestor is implemented independently from polling such that it can be used to
|
|
1216
|
+
* make a one-off request.
|
|
1217
|
+
*/
|
|
1218
|
+
class Requestor {
|
|
1219
|
+
constructor(requests, uri, headers, method, body) {
|
|
1220
|
+
this.requests = requests;
|
|
1221
|
+
this.uri = uri;
|
|
1222
|
+
this.headers = headers;
|
|
1223
|
+
this.method = method;
|
|
1224
|
+
this.body = body;
|
|
1225
|
+
}
|
|
1226
|
+
async requestPayload() {
|
|
1227
|
+
let status;
|
|
1228
|
+
try {
|
|
1229
|
+
const res = await this.requests.fetch(this.uri, {
|
|
1230
|
+
method: this.method,
|
|
1231
|
+
headers: this.headers,
|
|
1232
|
+
body: this.body,
|
|
1233
|
+
});
|
|
1234
|
+
if (isOk(res.status)) {
|
|
1235
|
+
return await res.text();
|
|
1236
|
+
}
|
|
1237
|
+
// Assigning so it can be thrown after the try/catch.
|
|
1238
|
+
status = res.status;
|
|
1239
|
+
}
|
|
1240
|
+
catch (err) {
|
|
1241
|
+
throw new LDRequestError(err?.message);
|
|
1242
|
+
}
|
|
1243
|
+
throw new LDRequestError(`Unexpected status code: ${status}`, status);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
class DataSourceEventHandler {
|
|
1248
|
+
constructor(flagManager, statusManager, logger) {
|
|
1249
|
+
this.flagManager = flagManager;
|
|
1250
|
+
this.statusManager = statusManager;
|
|
1251
|
+
this.logger = logger;
|
|
1252
|
+
}
|
|
1253
|
+
async handlePut(context, flags) {
|
|
1254
|
+
this.logger.debug(`Got PUT: ${Object.keys(flags)}`);
|
|
1255
|
+
// mapping flags to item descriptors
|
|
1256
|
+
const descriptors = Object.entries(flags).reduce((acc, [key, flag]) => {
|
|
1257
|
+
acc[key] = { version: flag.version, flag };
|
|
1258
|
+
return acc;
|
|
1259
|
+
}, {});
|
|
1260
|
+
await this.flagManager.init(context, descriptors);
|
|
1261
|
+
this.statusManager.requestStateUpdate(exports.DataSourceState.Valid);
|
|
1262
|
+
}
|
|
1263
|
+
async handlePatch(context, patchFlag) {
|
|
1264
|
+
this.logger.debug(`Got PATCH ${JSON.stringify(patchFlag, null, 2)}`);
|
|
1265
|
+
this.flagManager.upsert(context, patchFlag.key, {
|
|
1266
|
+
version: patchFlag.version,
|
|
1267
|
+
flag: patchFlag,
|
|
1268
|
+
});
|
|
1269
|
+
}
|
|
1270
|
+
async handleDelete(context, deleteFlag) {
|
|
1271
|
+
this.logger.debug(`Got DELETE ${JSON.stringify(deleteFlag, null, 2)}`);
|
|
1272
|
+
this.flagManager.upsert(context, deleteFlag.key, {
|
|
1273
|
+
version: deleteFlag.version,
|
|
1274
|
+
flag: {
|
|
1275
|
+
...deleteFlag,
|
|
1276
|
+
deleted: true,
|
|
1277
|
+
// props below are set to sensible defaults. they are irrelevant
|
|
1278
|
+
// because this flag has been deleted.
|
|
1279
|
+
flagVersion: 0,
|
|
1280
|
+
value: undefined,
|
|
1281
|
+
variation: 0,
|
|
1282
|
+
trackEvents: false,
|
|
1283
|
+
},
|
|
1284
|
+
});
|
|
1285
|
+
}
|
|
1286
|
+
handleStreamingError(error) {
|
|
1287
|
+
this.statusManager.reportError(error.kind, error.message, error.code, error.recoverable);
|
|
1288
|
+
}
|
|
1289
|
+
handlePollingError(error) {
|
|
1290
|
+
this.statusManager.reportError(error.kind, error.message, error.status, error.recoverable);
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
/**
|
|
1295
|
+
* Tracks the current data source status and emits updates when the status changes.
|
|
1296
|
+
*/
|
|
1297
|
+
class DataSourceStatusManager {
|
|
1298
|
+
constructor(emitter, timeStamper = () => Date.now()) {
|
|
1299
|
+
this.emitter = emitter;
|
|
1300
|
+
this.state = exports.DataSourceState.Closed;
|
|
1301
|
+
this.stateSinceMillis = timeStamper();
|
|
1302
|
+
this.timeStamper = timeStamper;
|
|
1303
|
+
}
|
|
1304
|
+
get status() {
|
|
1305
|
+
return {
|
|
1306
|
+
state: this.state,
|
|
1307
|
+
stateSince: this.stateSinceMillis,
|
|
1308
|
+
lastError: this.errorInfo,
|
|
1309
|
+
};
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* Updates the state of the manager.
|
|
1313
|
+
*
|
|
1314
|
+
* @param requestedState to track
|
|
1315
|
+
* @param isError to indicate that the state update is a result of an error occurring.
|
|
1316
|
+
*/
|
|
1317
|
+
updateState(requestedState, isError = false) {
|
|
1318
|
+
const newState = requestedState === exports.DataSourceState.Interrupted && this.state === exports.DataSourceState.Initializing // don't go to interrupted from initializing (recoverable errors when initializing are not noteworthy)
|
|
1319
|
+
? exports.DataSourceState.Initializing
|
|
1320
|
+
: requestedState;
|
|
1321
|
+
const changedState = this.state !== newState;
|
|
1322
|
+
if (changedState) {
|
|
1323
|
+
this.state = newState;
|
|
1324
|
+
this.stateSinceMillis = this.timeStamper();
|
|
1325
|
+
}
|
|
1326
|
+
if (changedState || isError) {
|
|
1327
|
+
this.emitter.emit('dataSourceStatus', this.status);
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
/**
|
|
1331
|
+
* Requests the manager move to the provided state. This request may be ignored
|
|
1332
|
+
* if the current state cannot transition to the requested state.
|
|
1333
|
+
* @param state that is requested
|
|
1334
|
+
*/
|
|
1335
|
+
requestStateUpdate(state) {
|
|
1336
|
+
this.updateState(state);
|
|
1337
|
+
}
|
|
1338
|
+
/**
|
|
1339
|
+
* Reports a datasource error to this manager. Since the {@link DataSourceStatus} includes error
|
|
1340
|
+
* information, it is possible that that a {@link DataSourceStatus} update is emitted with
|
|
1341
|
+
* the same {@link DataSourceState}.
|
|
1342
|
+
*
|
|
1343
|
+
* @param kind of the error
|
|
1344
|
+
* @param message for the error
|
|
1345
|
+
* @param statusCode of the error if there was one
|
|
1346
|
+
* @param recoverable to indicate that the error is anticipated to be recoverable
|
|
1347
|
+
*/
|
|
1348
|
+
reportError(kind, message, statusCode, recoverable = false) {
|
|
1349
|
+
const errorInfo = {
|
|
1350
|
+
kind,
|
|
1351
|
+
message,
|
|
1352
|
+
statusCode,
|
|
1353
|
+
time: this.timeStamper(),
|
|
1354
|
+
};
|
|
1355
|
+
this.errorInfo = errorInfo;
|
|
1356
|
+
this.updateState(recoverable ? exports.DataSourceState.Interrupted : exports.DataSourceState.Closed, true);
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
/**
|
|
1361
|
+
* @internal
|
|
1362
|
+
*/
|
|
1363
|
+
class PollingProcessor {
|
|
1364
|
+
constructor(plainContextString, dataSourceConfig, requests, encoding, dataHandler, errorHandler, logger) {
|
|
1365
|
+
this.plainContextString = plainContextString;
|
|
1366
|
+
this.dataSourceConfig = dataSourceConfig;
|
|
1367
|
+
this.dataHandler = dataHandler;
|
|
1368
|
+
this.errorHandler = errorHandler;
|
|
1369
|
+
this.logger = logger;
|
|
1370
|
+
this.stopped = false;
|
|
1371
|
+
const path = dataSourceConfig.useReport
|
|
1372
|
+
? dataSourceConfig.paths.pathReport(encoding, plainContextString)
|
|
1373
|
+
: dataSourceConfig.paths.pathGet(encoding, plainContextString);
|
|
1374
|
+
const parameters = [
|
|
1375
|
+
...(dataSourceConfig.queryParameters ?? []),
|
|
1376
|
+
];
|
|
1377
|
+
if (this.dataSourceConfig.withReasons) {
|
|
1378
|
+
parameters.push({ key: 'withReasons', value: 'true' });
|
|
1379
|
+
}
|
|
1380
|
+
const uri = jsSdkCommon.getPollingUri(dataSourceConfig.serviceEndpoints, path, parameters);
|
|
1381
|
+
this.pollInterval = dataSourceConfig.pollInterval;
|
|
1382
|
+
let method = 'GET';
|
|
1383
|
+
const headers = { ...dataSourceConfig.baseHeaders };
|
|
1384
|
+
let body;
|
|
1385
|
+
if (dataSourceConfig.useReport) {
|
|
1386
|
+
method = 'REPORT';
|
|
1387
|
+
headers['content-type'] = 'application/json';
|
|
1388
|
+
body = plainContextString; // context is in body for REPORT
|
|
1389
|
+
}
|
|
1390
|
+
this.requestor = new Requestor(requests, uri, headers, method, body);
|
|
1391
|
+
}
|
|
1392
|
+
async poll() {
|
|
1393
|
+
if (this.stopped) {
|
|
1394
|
+
return;
|
|
1395
|
+
}
|
|
1396
|
+
const reportJsonError = (data) => {
|
|
1397
|
+
this.logger?.error('Polling received invalid data');
|
|
1398
|
+
this.logger?.debug(`Invalid JSON follows: ${data}`);
|
|
1399
|
+
this.errorHandler?.(new jsSdkCommon.LDPollingError(jsSdkCommon.DataSourceErrorKind.InvalidData, 'Malformed JSON data in polling response'));
|
|
1400
|
+
};
|
|
1401
|
+
this.logger?.debug('Polling LaunchDarkly for feature flag updates');
|
|
1402
|
+
const startTime = Date.now();
|
|
1403
|
+
try {
|
|
1404
|
+
const res = await this.requestor.requestPayload();
|
|
1405
|
+
try {
|
|
1406
|
+
const flags = JSON.parse(res);
|
|
1407
|
+
try {
|
|
1408
|
+
this.dataHandler?.(flags);
|
|
1409
|
+
}
|
|
1410
|
+
catch (err) {
|
|
1411
|
+
this.logger?.error(`Exception from data handler: ${err}`);
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
catch {
|
|
1415
|
+
reportJsonError(res);
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
catch (err) {
|
|
1419
|
+
const requestError = err;
|
|
1420
|
+
if (requestError.status !== undefined) {
|
|
1421
|
+
if (!jsSdkCommon.isHttpRecoverable(requestError.status)) {
|
|
1422
|
+
this.logger?.error(jsSdkCommon.httpErrorMessage(err, 'polling request'));
|
|
1423
|
+
this.errorHandler?.(new jsSdkCommon.LDPollingError(jsSdkCommon.DataSourceErrorKind.ErrorResponse, requestError.message, requestError.status));
|
|
1424
|
+
return;
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
this.logger?.error(jsSdkCommon.httpErrorMessage(err, 'polling request', 'will retry'));
|
|
1428
|
+
}
|
|
1429
|
+
const elapsed = Date.now() - startTime;
|
|
1430
|
+
const sleepFor = Math.max(this.pollInterval * 1000 - elapsed, 0);
|
|
1431
|
+
this.logger?.debug('Elapsed: %d ms, sleeping for %d ms', elapsed, sleepFor);
|
|
1432
|
+
this.timeoutHandle = setTimeout(() => {
|
|
1433
|
+
this.poll();
|
|
1434
|
+
}, sleepFor);
|
|
1435
|
+
}
|
|
1436
|
+
start() {
|
|
1437
|
+
this.poll();
|
|
1438
|
+
}
|
|
1439
|
+
stop() {
|
|
1440
|
+
if (this.timeoutHandle) {
|
|
1441
|
+
clearTimeout(this.timeoutHandle);
|
|
1442
|
+
this.timeoutHandle = undefined;
|
|
1443
|
+
}
|
|
1444
|
+
this.stopped = true;
|
|
1445
|
+
}
|
|
1446
|
+
close() {
|
|
1447
|
+
this.stop();
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
const reportJsonError = (type, data, logger, errorHandler) => {
|
|
1452
|
+
logger?.error(`Stream received invalid data in "${type}" message`);
|
|
1453
|
+
logger?.debug(`Invalid JSON follows: ${data}`);
|
|
1454
|
+
errorHandler?.(new jsSdkCommon.LDStreamingError(jsSdkCommon.DataSourceErrorKind.InvalidData, 'Malformed JSON data in event stream'));
|
|
1455
|
+
};
|
|
1456
|
+
class StreamingProcessor {
|
|
1457
|
+
constructor(plainContextString, dataSourceConfig, listeners, requests, encoding, diagnosticsManager, errorHandler, logger) {
|
|
1458
|
+
this.plainContextString = plainContextString;
|
|
1459
|
+
this.dataSourceConfig = dataSourceConfig;
|
|
1460
|
+
this.listeners = listeners;
|
|
1461
|
+
this.requests = requests;
|
|
1462
|
+
this.diagnosticsManager = diagnosticsManager;
|
|
1463
|
+
this.errorHandler = errorHandler;
|
|
1464
|
+
this.logger = logger;
|
|
1465
|
+
// TODO: SC-255969 Implement better REPORT fallback logic
|
|
1466
|
+
if (dataSourceConfig.useReport && !requests.getEventSourceCapabilities().customMethod) {
|
|
1467
|
+
logger?.error("Configuration option useReport is true, but platform's EventSource does not support custom HTTP methods. Streaming may not work.");
|
|
1468
|
+
}
|
|
1469
|
+
const path = dataSourceConfig.useReport
|
|
1470
|
+
? dataSourceConfig.paths.pathReport(encoding, plainContextString)
|
|
1471
|
+
: dataSourceConfig.paths.pathGet(encoding, plainContextString);
|
|
1472
|
+
const parameters = [
|
|
1473
|
+
...(dataSourceConfig.queryParameters ?? []),
|
|
1474
|
+
];
|
|
1475
|
+
if (this.dataSourceConfig.withReasons) {
|
|
1476
|
+
parameters.push({ key: 'withReasons', value: 'true' });
|
|
1477
|
+
}
|
|
1478
|
+
this.requests = requests;
|
|
1479
|
+
this.headers = { ...dataSourceConfig.baseHeaders };
|
|
1480
|
+
this.logger = logger;
|
|
1481
|
+
this.streamUri = jsSdkCommon.getStreamingUri(dataSourceConfig.serviceEndpoints, path, parameters);
|
|
1482
|
+
}
|
|
1483
|
+
logConnectionStarted() {
|
|
1484
|
+
this.connectionAttemptStartTime = Date.now();
|
|
1485
|
+
}
|
|
1486
|
+
logConnectionResult(success) {
|
|
1487
|
+
if (this.connectionAttemptStartTime && this.diagnosticsManager) {
|
|
1488
|
+
this.diagnosticsManager.recordStreamInit(this.connectionAttemptStartTime, !success, Date.now() - this.connectionAttemptStartTime);
|
|
1489
|
+
}
|
|
1490
|
+
this.connectionAttemptStartTime = undefined;
|
|
1491
|
+
}
|
|
1492
|
+
/**
|
|
1493
|
+
* This is a wrapper around the passed errorHandler which adds additional
|
|
1494
|
+
* diagnostics and logging logic.
|
|
1495
|
+
*
|
|
1496
|
+
* @param err The error to be logged and handled.
|
|
1497
|
+
* @return boolean whether to retry the connection.
|
|
1498
|
+
*
|
|
1499
|
+
* @private
|
|
1500
|
+
*/
|
|
1501
|
+
retryAndHandleError(err) {
|
|
1502
|
+
if (!jsSdkCommon.shouldRetry(err)) {
|
|
1503
|
+
this.logConnectionResult(false);
|
|
1504
|
+
this.errorHandler?.(new jsSdkCommon.LDStreamingError(jsSdkCommon.DataSourceErrorKind.ErrorResponse, err.message, err.status, false));
|
|
1505
|
+
this.logger?.error(jsSdkCommon.httpErrorMessage(err, 'streaming request'));
|
|
1506
|
+
return false;
|
|
1507
|
+
}
|
|
1508
|
+
this.logger?.warn(jsSdkCommon.httpErrorMessage(err, 'streaming request', 'will retry'));
|
|
1509
|
+
this.logConnectionResult(false);
|
|
1510
|
+
this.logConnectionStarted();
|
|
1511
|
+
return true;
|
|
1512
|
+
}
|
|
1513
|
+
start() {
|
|
1514
|
+
this.logConnectionStarted();
|
|
1515
|
+
let methodAndBodyOverrides;
|
|
1516
|
+
if (this.dataSourceConfig.useReport) {
|
|
1517
|
+
// REPORT will include a body, so content type is required.
|
|
1518
|
+
this.headers['content-type'] = 'application/json';
|
|
1519
|
+
// orverrides default method with REPORT and adds body.
|
|
1520
|
+
methodAndBodyOverrides = { method: 'REPORT', body: this.plainContextString };
|
|
1521
|
+
}
|
|
1522
|
+
else {
|
|
1523
|
+
// no method or body override
|
|
1524
|
+
methodAndBodyOverrides = {};
|
|
1525
|
+
}
|
|
1526
|
+
// TLS is handled by the platform implementation.
|
|
1527
|
+
const eventSource = this.requests.createEventSource(this.streamUri, {
|
|
1528
|
+
headers: this.headers,
|
|
1529
|
+
...methodAndBodyOverrides,
|
|
1530
|
+
errorFilter: (error) => this.retryAndHandleError(error),
|
|
1531
|
+
initialRetryDelayMillis: this.dataSourceConfig.initialRetryDelayMillis,
|
|
1532
|
+
readTimeoutMillis: 5 * 60 * 1000,
|
|
1533
|
+
retryResetIntervalMillis: 60 * 1000,
|
|
1534
|
+
});
|
|
1535
|
+
this.eventSource = eventSource;
|
|
1536
|
+
eventSource.onclose = () => {
|
|
1537
|
+
this.logger?.info('Closed LaunchDarkly stream connection');
|
|
1538
|
+
};
|
|
1539
|
+
eventSource.onerror = () => {
|
|
1540
|
+
// The work is done by `errorFilter`.
|
|
1541
|
+
};
|
|
1542
|
+
eventSource.onopen = () => {
|
|
1543
|
+
this.logger?.info('Opened LaunchDarkly stream connection');
|
|
1544
|
+
};
|
|
1545
|
+
eventSource.onretrying = (e) => {
|
|
1546
|
+
this.logger?.info(`Will retry stream connection in ${e.delayMillis} milliseconds`);
|
|
1547
|
+
};
|
|
1548
|
+
this.listeners.forEach(({ deserializeData, processJson }, eventName) => {
|
|
1549
|
+
eventSource.addEventListener(eventName, (event) => {
|
|
1550
|
+
this.logger?.debug(`Received ${eventName} event`);
|
|
1551
|
+
if (event?.data) {
|
|
1552
|
+
this.logConnectionResult(true);
|
|
1553
|
+
const { data } = event;
|
|
1554
|
+
const dataJson = deserializeData(data);
|
|
1555
|
+
if (!dataJson) {
|
|
1556
|
+
reportJsonError(eventName, data, this.logger, this.errorHandler);
|
|
1557
|
+
return;
|
|
1558
|
+
}
|
|
1559
|
+
processJson(dataJson);
|
|
1560
|
+
}
|
|
1561
|
+
else {
|
|
1562
|
+
this.errorHandler?.(new jsSdkCommon.LDStreamingError(jsSdkCommon.DataSourceErrorKind.InvalidData, 'Unexpected payload from event stream'));
|
|
1563
|
+
}
|
|
1564
|
+
});
|
|
1565
|
+
});
|
|
1566
|
+
}
|
|
1567
|
+
stop() {
|
|
1568
|
+
this.eventSource?.close();
|
|
1569
|
+
this.eventSource = undefined;
|
|
1570
|
+
}
|
|
1571
|
+
close() {
|
|
1572
|
+
this.stop();
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
class BaseDataManager {
|
|
1577
|
+
constructor(platform, flagManager, credential, config, getPollingPaths, getStreamingPaths, baseHeaders, emitter, diagnosticsManager) {
|
|
1578
|
+
this.platform = platform;
|
|
1579
|
+
this.flagManager = flagManager;
|
|
1580
|
+
this.credential = credential;
|
|
1581
|
+
this.config = config;
|
|
1582
|
+
this.getPollingPaths = getPollingPaths;
|
|
1583
|
+
this.getStreamingPaths = getStreamingPaths;
|
|
1584
|
+
this.baseHeaders = baseHeaders;
|
|
1585
|
+
this.emitter = emitter;
|
|
1586
|
+
this.diagnosticsManager = diagnosticsManager;
|
|
1587
|
+
this.logger = config.logger;
|
|
1588
|
+
this.dataSourceStatusManager = new DataSourceStatusManager(emitter);
|
|
1589
|
+
this.dataSourceEventHandler = new DataSourceEventHandler(flagManager, this.dataSourceStatusManager, this.config.logger);
|
|
1590
|
+
}
|
|
1591
|
+
/**
|
|
1592
|
+
* Set additional connection parameters for requests polling/streaming.
|
|
1593
|
+
*/
|
|
1594
|
+
setConnectionParams(connectionParams) {
|
|
1595
|
+
this.connectionParams = connectionParams;
|
|
1596
|
+
}
|
|
1597
|
+
createPollingProcessor(context, checkedContext, identifyResolve, identifyReject) {
|
|
1598
|
+
const processor = new PollingProcessor(JSON.stringify(context), {
|
|
1599
|
+
credential: this.credential,
|
|
1600
|
+
serviceEndpoints: this.config.serviceEndpoints,
|
|
1601
|
+
paths: this.getPollingPaths(),
|
|
1602
|
+
baseHeaders: this.baseHeaders,
|
|
1603
|
+
pollInterval: this.config.pollInterval,
|
|
1604
|
+
withReasons: this.config.withReasons,
|
|
1605
|
+
useReport: this.config.useReport,
|
|
1606
|
+
queryParameters: this.connectionParams?.queryParameters,
|
|
1607
|
+
}, this.platform.requests, this.platform.encoding, async (flags) => {
|
|
1608
|
+
await this.dataSourceEventHandler.handlePut(checkedContext, flags);
|
|
1609
|
+
identifyResolve?.();
|
|
1610
|
+
}, (err) => {
|
|
1611
|
+
this.emitter.emit('error', context, err);
|
|
1612
|
+
this.dataSourceEventHandler.handlePollingError(err);
|
|
1613
|
+
identifyReject?.(err);
|
|
1614
|
+
});
|
|
1615
|
+
this.updateProcessor = this.decorateProcessorWithStatusReporting(processor, this.dataSourceStatusManager);
|
|
1616
|
+
}
|
|
1617
|
+
createStreamingProcessor(context, checkedContext, identifyResolve, identifyReject) {
|
|
1618
|
+
const processor = new StreamingProcessor(JSON.stringify(context), {
|
|
1619
|
+
credential: this.credential,
|
|
1620
|
+
serviceEndpoints: this.config.serviceEndpoints,
|
|
1621
|
+
paths: this.getStreamingPaths(),
|
|
1622
|
+
baseHeaders: this.baseHeaders,
|
|
1623
|
+
initialRetryDelayMillis: this.config.streamInitialReconnectDelay * 1000,
|
|
1624
|
+
withReasons: this.config.withReasons,
|
|
1625
|
+
useReport: this.config.useReport,
|
|
1626
|
+
queryParameters: this.connectionParams?.queryParameters,
|
|
1627
|
+
}, this.createStreamListeners(checkedContext, identifyResolve), this.platform.requests, this.platform.encoding, this.diagnosticsManager, (e) => {
|
|
1628
|
+
this.emitter.emit('error', context, e);
|
|
1629
|
+
this.dataSourceEventHandler.handleStreamingError(e);
|
|
1630
|
+
identifyReject?.(e);
|
|
1631
|
+
});
|
|
1632
|
+
this.updateProcessor = this.decorateProcessorWithStatusReporting(processor, this.dataSourceStatusManager);
|
|
1633
|
+
}
|
|
1634
|
+
createStreamListeners(context, identifyResolve) {
|
|
1635
|
+
const listeners = new Map();
|
|
1636
|
+
listeners.set('put', {
|
|
1637
|
+
deserializeData: JSON.parse,
|
|
1638
|
+
processJson: async (flags) => {
|
|
1639
|
+
await this.dataSourceEventHandler.handlePut(context, flags);
|
|
1640
|
+
identifyResolve?.();
|
|
1641
|
+
},
|
|
1642
|
+
});
|
|
1643
|
+
listeners.set('patch', {
|
|
1644
|
+
deserializeData: JSON.parse,
|
|
1645
|
+
processJson: async (patchFlag) => {
|
|
1646
|
+
this.dataSourceEventHandler.handlePatch(context, patchFlag);
|
|
1647
|
+
},
|
|
1648
|
+
});
|
|
1649
|
+
listeners.set('delete', {
|
|
1650
|
+
deserializeData: JSON.parse,
|
|
1651
|
+
processJson: async (deleteFlag) => {
|
|
1652
|
+
this.dataSourceEventHandler.handleDelete(context, deleteFlag);
|
|
1653
|
+
},
|
|
1654
|
+
});
|
|
1655
|
+
return listeners;
|
|
1656
|
+
}
|
|
1657
|
+
decorateProcessorWithStatusReporting(processor, statusManager) {
|
|
1658
|
+
return {
|
|
1659
|
+
start: () => {
|
|
1660
|
+
// update status before starting processor to ensure potential errors are reported after initializing
|
|
1661
|
+
statusManager.requestStateUpdate(exports.DataSourceState.Initializing);
|
|
1662
|
+
processor.start();
|
|
1663
|
+
},
|
|
1664
|
+
stop: () => {
|
|
1665
|
+
processor.stop();
|
|
1666
|
+
statusManager.requestStateUpdate(exports.DataSourceState.Closed);
|
|
1667
|
+
},
|
|
1668
|
+
close: () => {
|
|
1669
|
+
processor.close();
|
|
1670
|
+
statusManager.requestStateUpdate(exports.DataSourceState.Closed);
|
|
1671
|
+
},
|
|
1672
|
+
};
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
exports.platform = jsSdkCommon__namespace;
|
|
1677
|
+
exports.BaseDataManager = BaseDataManager;
|
|
1678
|
+
exports.LDClientImpl = LDClientImpl;
|
|
1679
|
+
exports.Requestor = Requestor;
|
|
1680
|
+
Object.keys(jsSdkCommon).forEach(function (k) {
|
|
1681
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
1682
|
+
enumerable: true,
|
|
1683
|
+
get: function () { return jsSdkCommon[k]; }
|
|
1684
|
+
});
|
|
1685
|
+
});
|
|
1686
|
+
//# sourceMappingURL=index.cjs.map
|