@launchdarkly/js-client-sdk-common 1.8.0 → 1.9.1-beta.1

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