@datadog/mobile-react-native-openfeature 3.9.0 → 3.11.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 (71) hide show
  1. package/README.md +118 -10
  2. package/lib/commonjs/datadogCoreProvider.js +129 -0
  3. package/lib/commonjs/datadogCoreProvider.js.map +1 -0
  4. package/lib/commonjs/offlineEvaluation.js +198 -0
  5. package/lib/commonjs/offlineEvaluation.js.map +1 -0
  6. package/lib/commonjs/offlineProvider.js +44 -8
  7. package/lib/commonjs/offlineProvider.js.map +1 -1
  8. package/lib/commonjs/rules-based.js +62 -0
  9. package/lib/commonjs/rules-based.js.map +1 -0
  10. package/lib/module/datadogCoreProvider.js +122 -0
  11. package/lib/module/datadogCoreProvider.js.map +1 -0
  12. package/lib/module/offlineEvaluation.js +192 -0
  13. package/lib/module/offlineEvaluation.js.map +1 -0
  14. package/lib/module/offlineProvider.js +45 -9
  15. package/lib/module/offlineProvider.js.map +1 -1
  16. package/lib/module/rules-based.js +50 -0
  17. package/lib/module/rules-based.js.map +1 -0
  18. package/lib/typescript/core/src/config/FileBasedConfiguration.d.ts.map +1 -1
  19. package/lib/typescript/core/src/config/FileBasedConfiguration.type.d.ts +2 -0
  20. package/lib/typescript/core/src/config/FileBasedConfiguration.type.d.ts.map +1 -1
  21. package/lib/typescript/core/src/config/async/AutoInstrumentationConfiguration.d.ts +4 -0
  22. package/lib/typescript/core/src/config/async/AutoInstrumentationConfiguration.d.ts.map +1 -1
  23. package/lib/typescript/core/src/config/async/PartialInitializationConfiguration.d.ts +2 -0
  24. package/lib/typescript/core/src/config/async/PartialInitializationConfiguration.d.ts.map +1 -1
  25. package/lib/typescript/core/src/config/features/RumConfiguration.d.ts +4 -0
  26. package/lib/typescript/core/src/config/features/RumConfiguration.d.ts.map +1 -1
  27. package/lib/typescript/core/src/config/features/RumConfiguration.type.d.ts +21 -0
  28. package/lib/typescript/core/src/config/features/RumConfiguration.type.d.ts.map +1 -1
  29. package/lib/typescript/core/src/config/features/RumConfigurationNative.d.ts +2 -0
  30. package/lib/typescript/core/src/config/features/RumConfigurationNative.d.ts.map +1 -1
  31. package/lib/typescript/core/src/flags/FlagsClient.d.ts +38 -1
  32. package/lib/typescript/core/src/flags/FlagsClient.d.ts.map +1 -1
  33. package/lib/typescript/core/src/flags/internal.d.ts +1 -0
  34. package/lib/typescript/core/src/flags/internal.d.ts.map +1 -1
  35. package/lib/typescript/core/src/flags/types.d.ts +1 -1
  36. package/lib/typescript/core/src/flags/types.d.ts.map +1 -1
  37. package/lib/typescript/core/src/rum/DdRum.d.ts +1 -0
  38. package/lib/typescript/core/src/rum/DdRum.d.ts.map +1 -1
  39. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/RequestContext.d.ts +2 -0
  40. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/RequestContext.d.ts.map +1 -1
  41. package/lib/typescript/core/src/rum/types.d.ts +6 -0
  42. package/lib/typescript/core/src/rum/types.d.ts.map +1 -1
  43. package/lib/typescript/core/src/sdk/AccountInfoSingleton/AccountInfoSingleton.d.ts.map +1 -1
  44. package/lib/typescript/core/src/sdk/UserInfoSingleton/UserInfoSingleton.d.ts.map +1 -1
  45. package/lib/typescript/core/src/specs/NativeDdRum.d.ts +6 -0
  46. package/lib/typescript/core/src/specs/NativeDdRum.d.ts.map +1 -1
  47. package/lib/typescript/core/src/version.d.ts +1 -1
  48. package/lib/typescript/core/src/version.d.ts.map +1 -1
  49. package/lib/typescript/react-native-openfeature/src/datadogCoreProvider.d.ts +31 -0
  50. package/lib/typescript/react-native-openfeature/src/datadogCoreProvider.d.ts.map +1 -0
  51. package/lib/typescript/react-native-openfeature/src/index.d.ts +1 -0
  52. package/lib/typescript/react-native-openfeature/src/index.d.ts.map +1 -1
  53. package/lib/typescript/react-native-openfeature/src/offlineEvaluation.d.ts +33 -0
  54. package/lib/typescript/react-native-openfeature/src/offlineEvaluation.d.ts.map +1 -0
  55. package/lib/typescript/react-native-openfeature/src/offlineProvider.d.ts +14 -8
  56. package/lib/typescript/react-native-openfeature/src/offlineProvider.d.ts.map +1 -1
  57. package/lib/typescript/react-native-openfeature/src/rules-based.d.ts +17 -0
  58. package/lib/typescript/react-native-openfeature/src/rules-based.d.ts.map +1 -0
  59. package/package.json +8 -4
  60. package/rules-based/package.json +7 -0
  61. package/src/__tests__/__utils__/coreConfiguration.ts +84 -0
  62. package/src/__tests__/datadogCoreProvider.integration.test.ts +230 -0
  63. package/src/__tests__/datadogCoreProvider.test.ts +587 -0
  64. package/src/__tests__/entrypoints.test.ts +35 -0
  65. package/src/__tests__/offlineProvider.delegation.test.ts +795 -0
  66. package/src/__tests__/offlineProvider.test.ts +74 -10
  67. package/src/datadogCoreProvider.ts +223 -0
  68. package/src/index.ts +1 -0
  69. package/src/offlineEvaluation.ts +295 -0
  70. package/src/offlineProvider.ts +64 -13
  71. package/src/rules-based.ts +58 -0
@@ -4,6 +4,8 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
+ import type { FlagsConfiguration } from '@datadog/flagging-core';
8
+ import { InternalLog, SdkVerbosity } from '@datadog/mobile-react-native';
7
9
  import type {
8
10
  FlagsClient,
9
11
  ParsedFlagsConfiguration
@@ -12,6 +14,7 @@ import {
12
14
  ErrorCode,
13
15
  GeneralError,
14
16
  InvalidContextError,
17
+ ParseError,
15
18
  ProviderEvents,
16
19
  ProviderNotReadyError
17
20
  } from '@openfeature/web-sdk';
@@ -23,6 +26,7 @@ import type {
23
26
 
24
27
  import { DatadogCoreOpenFeatureProvider } from './coreProvider';
25
28
  import { isEmptyContext, toDdContext } from './mappers';
29
+ import { OfflineEvaluation } from './offlineEvaluation';
26
30
 
27
31
  // The outcome of a `FlagsClient` reconcile. Derived from the client so the provider maps it to
28
32
  // OpenFeature transitions; not part of the package's public API.
@@ -40,21 +44,26 @@ type ProviderErrorEvent = { message: string; errorCode: ErrorCode };
40
44
  const OF_ERROR_CODE: Record<ConfigurationErrorCode, ErrorCode> = {
41
45
  INVALID_CONTEXT: ErrorCode.INVALID_CONTEXT,
42
46
  PROVIDER_NOT_READY: ErrorCode.PROVIDER_NOT_READY,
47
+ PARSE_ERROR: ErrorCode.PARSE_ERROR,
43
48
  GENERAL: ErrorCode.GENERAL
44
49
  };
45
50
 
46
51
  /**
47
52
  * An offline Datadog OpenFeature provider.
48
53
  *
49
- * It behaves like the online `DatadogOpenFeatureProvider` — same flag evaluation and
50
- * exposure/RUM tracking — **except it never fetches configuration from the network**.
54
+ * Delegates precomputed and rules-based evaluation to `DatadogCoreProvider`, while preserving
55
+ * named native clients and exposure/RUM tracking. It never fetches configuration from the network.
51
56
  * Instead of fetching on `initialize`/`onContextChange`, it evaluates against a configuration
52
57
  * supplied via {@link DatadogOfflineOpenFeatureProvider.setConfiguration}. A precomputed
53
58
  * configuration carries the evaluation context it was computed for, so you should **not** call
54
59
  * `OpenFeature.setContext` for the offline precomputed flow — see the class remarks.
55
60
  *
56
- * A runtime context that does not match the configuration's embedded context (compared after
57
- * normalization) cannot be served (offline never fetches), so it puts the provider into the
61
+ * Rules are evaluated locally for the supplied context. A missing/null targeting key is not an
62
+ * anonymous subject: an evaluation that reaches a shard on the targeting key returns
63
+ * TARGETING_KEY_MISSING, while an explicit empty string remains a valid targeting key.
64
+ * Precomputed context normalization/adoption is unchanged.
65
+ * Without usable rules, a runtime context that does not match the precomputed configuration's embedded context (after normalization)
66
+ * cannot be served (offline never fetches), so it puts the provider into the
58
67
  * OpenFeature `ERROR` state and evaluations fall back to your coded defaults (`INVALID_CONTEXT`).
59
68
  * An empty *effective* context re-adopts the embedded context and recovers — but note that
60
69
  * `clearContext(domain)` falls back to the global context, which may itself be non-empty and
@@ -118,11 +127,12 @@ export class DatadogOfflineOpenFeatureProvider extends DatadogCoreOpenFeaturePro
118
127
  /**
119
128
  * Load a configuration into the provider for offline evaluation.
120
129
  *
121
- * @param configuration A configuration parsed from a `ConfigurationWire` string via
122
- * `configurationFromString`.
130
+ * @param configuration A precomputed or rules-based configuration parsed with
131
+ * `coreConfigurationFromString` from the `/rules-based` entry point. The existing precomputed-only `configurationFromString`
132
+ * output remains supported.
123
133
  */
124
- setConfiguration(configuration: ParsedFlagsConfiguration): void {
125
- const result = this.flagsClient.setConfiguration(configuration);
134
+ setConfiguration(configuration: FlagsConfiguration): void {
135
+ const result = this.installConfiguration(configuration);
126
136
 
127
137
  if (result.status === 'ready') {
128
138
  if (this.configurationInError) {
@@ -144,16 +154,53 @@ export class DatadogOfflineOpenFeatureProvider extends DatadogCoreOpenFeaturePro
144
154
  }
145
155
  }
146
156
 
157
+ private installConfiguration(
158
+ configuration: FlagsConfiguration
159
+ ): ConfigurationResult {
160
+ if (typeof this.flagsClient.__ddSetOfflineEvaluator === 'function') {
161
+ return this.flagsClient.__ddSetOfflineEvaluator(
162
+ helpers => new OfflineEvaluation(configuration, helpers)
163
+ );
164
+ }
165
+
166
+ // Older core SDKs predate the evaluator bridge. Keep their released precomputed behavior,
167
+ // but never silently ignore usable rules or keep serving a stale configuration when they
168
+ // are supplied. A rules parse error has no rules to ignore, so precomputed data still loads.
169
+ if (configuration?.rules) {
170
+ InternalLog.log(
171
+ 'DatadogOfflineOpenFeatureProvider rules-based evaluation requires an updated @datadog/mobile-react-native SDK. Update both Datadog packages together.',
172
+ SdkVerbosity.WARN
173
+ );
174
+ this.flagsClient.setConfiguration({});
175
+ return { status: 'error', errorCode: 'GENERAL' };
176
+ }
177
+ return this.flagsClient.setConfiguration(
178
+ configuration as ParsedFlagsConfiguration
179
+ );
180
+ }
181
+
147
182
  private applyContext(context: OFEvaluationContext): ConfigurationResult {
148
183
  // An empty context means "no external override": clear it so a loaded precomputed
149
184
  // configuration is served against its embedded context. Order-independent — the synthetic
150
185
  // `initialize({})`, `setContext({})`, and `clearContext()` all re-adopt the embedded
151
186
  // context rather than being treated as a mismatch.
152
- const result = isEmptyContext(context)
153
- ? this.flagsClient.resetEvaluationContextWithoutFetching()
154
- : this.flagsClient.setEvaluationContextWithoutFetching(
155
- toDdContext(context)
156
- );
187
+ let result: ConfigurationResult;
188
+ if (isEmptyContext(context)) {
189
+ result = this.flagsClient.resetEvaluationContextWithoutFetching();
190
+ } else if (
191
+ typeof this.flagsClient.__ddSetOfflineEvaluationContext ===
192
+ 'function'
193
+ ) {
194
+ result = this.flagsClient.__ddSetOfflineEvaluationContext(
195
+ toDdContext(context),
196
+ typeof context.targetingKey === 'string'
197
+ );
198
+ } else {
199
+ // Older core SDKs retain their precomputed-only context behavior.
200
+ result = this.flagsClient.setEvaluationContextWithoutFetching(
201
+ toDdContext(context)
202
+ );
203
+ }
157
204
 
158
205
  this.configurationInError = result.status === 'error';
159
206
 
@@ -176,6 +223,10 @@ export class DatadogOfflineOpenFeatureProvider extends DatadogCoreOpenFeaturePro
176
223
  return new ProviderNotReadyError(
177
224
  'The Datadog offline provider has no configuration loaded. Provide one via setConfiguration.'
178
225
  );
226
+ case 'PARSE_ERROR':
227
+ return new ParseError(
228
+ 'The Datadog offline provider cannot parse the loaded configuration.'
229
+ );
179
230
  default:
180
231
  return new GeneralError(
181
232
  'The Datadog offline provider cannot serve the loaded configuration.'
@@ -0,0 +1,58 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import { configurationToString } from '@datadog/flagging-core/rules-based';
8
+ import { getMD5Hash } from '@datadog/flagging-core';
9
+ import type { FlagsConfiguration } from '@datadog/flagging-core';
10
+
11
+ import { DatadogCoreEvaluationProvider } from './datadogCoreProvider';
12
+
13
+ // Rules parsing loads Protobuf-ES. Keep it behind this entry, as the browser SDK does.
14
+ export { configurationFromString as coreConfigurationFromString } from '@datadog/flagging-core/rules-based';
15
+ export type { FlagsConfiguration } from '@datadog/flagging-core';
16
+
17
+ /**
18
+ * Evaluates manually supplied precomputed or rules-based configurations in JavaScript using
19
+ * @datadog/flagging-core. Ported from openfeature-js-client's browser DatadogCoreProvider.
20
+ *
21
+ * Unlike the native-backed providers, this provider does not use DdFlags, fetch configuration,
22
+ * or send exposure/RUM events. Load a configuration before registering it with OpenFeature.
23
+ * Precomputed configurations require a matching OpenFeature context, including when it is empty.
24
+ */
25
+ export class DatadogCoreProvider extends DatadogCoreEvaluationProvider {
26
+ private fallbackConfigurationSequence = 0;
27
+
28
+ protected computeConfigurationId(
29
+ configuration: FlagsConfiguration
30
+ ): string {
31
+ try {
32
+ // Retrieval metadata and the UFC build timestamp can change without changing rules.
33
+ // The backend's semantic Fingerprint() also excludes the rules' CreatedAt.
34
+ return getMD5Hash(
35
+ configurationToString({
36
+ ...configuration,
37
+ precomputed: configuration.precomputed && {
38
+ ...configuration.precomputed,
39
+ fetchedAt: undefined,
40
+ etag: undefined
41
+ },
42
+ rules: configuration.rules && {
43
+ ...configuration.rules,
44
+ response: {
45
+ ...configuration.rules.response,
46
+ createdAt: undefined
47
+ },
48
+ fetchedAt: undefined,
49
+ etag: undefined
50
+ }
51
+ })
52
+ );
53
+ } catch {
54
+ this.fallbackConfigurationSequence += 1;
55
+ return `core-configuration-${this.fallbackConfigurationSequence}`;
56
+ }
57
+ }
58
+ }