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