@datadog/mobile-react-native 3.0.3 → 3.1.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 (69) hide show
  1. package/DatadogSDKReactNative.podspec +10 -9
  2. package/android/build.gradle +10 -8
  3. package/android/src/main/kotlin/com/datadog/reactnative/DdFlagsImplementation.kt +237 -0
  4. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkBridgeExt.kt +262 -78
  5. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkReactNativePackage.kt +3 -1
  6. package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
  7. package/android/src/newarch/kotlin/com/datadog/reactnative/DdFlags.kt +66 -0
  8. package/android/src/oldarch/kotlin/com/datadog/reactnative/DdFlags.kt +63 -0
  9. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkBridgeExtTest.kt +242 -0
  10. package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +38 -0
  11. package/android/src/test/kotlin/com/datadog/tools/unit/MockRumMonitor.kt +3 -0
  12. package/ios/Sources/DatadogSDKReactNative.h +2 -0
  13. package/ios/Sources/DdFlags.h +24 -0
  14. package/ios/Sources/DdFlags.mm +83 -0
  15. package/ios/Sources/DdFlagsImplementation.swift +263 -0
  16. package/ios/Sources/RNDdSdkConfiguration.swift +2 -1
  17. package/ios/Sources/SdkVersion.swift +1 -1
  18. package/lib/commonjs/flags/DdFlags.js +51 -0
  19. package/lib/commonjs/flags/DdFlags.js.map +1 -0
  20. package/lib/commonjs/flags/FlagsClient.js +218 -0
  21. package/lib/commonjs/flags/FlagsClient.js.map +1 -0
  22. package/lib/commonjs/flags/internal.js +40 -0
  23. package/lib/commonjs/flags/internal.js.map +1 -0
  24. package/lib/commonjs/flags/types.js +6 -0
  25. package/lib/commonjs/flags/types.js.map +1 -0
  26. package/lib/commonjs/index.js +7 -0
  27. package/lib/commonjs/index.js.map +1 -1
  28. package/lib/commonjs/specs/NativeDdFlags.js +19 -0
  29. package/lib/commonjs/specs/NativeDdFlags.js.map +1 -0
  30. package/lib/commonjs/version.js +1 -1
  31. package/lib/module/flags/DdFlags.js +45 -0
  32. package/lib/module/flags/DdFlags.js.map +1 -0
  33. package/lib/module/flags/FlagsClient.js +211 -0
  34. package/lib/module/flags/FlagsClient.js.map +1 -0
  35. package/lib/module/flags/internal.js +33 -0
  36. package/lib/module/flags/internal.js.map +1 -0
  37. package/lib/module/flags/types.js +2 -0
  38. package/lib/module/flags/types.js.map +1 -0
  39. package/lib/module/index.js +2 -1
  40. package/lib/module/index.js.map +1 -1
  41. package/lib/module/specs/NativeDdFlags.js +17 -0
  42. package/lib/module/specs/NativeDdFlags.js.map +1 -0
  43. package/lib/module/version.js +1 -1
  44. package/lib/typescript/flags/DdFlags.d.ts +3 -0
  45. package/lib/typescript/flags/DdFlags.d.ts.map +1 -0
  46. package/lib/typescript/flags/FlagsClient.d.ts +114 -0
  47. package/lib/typescript/flags/FlagsClient.d.ts.map +1 -0
  48. package/lib/typescript/flags/internal.d.ts +14 -0
  49. package/lib/typescript/flags/internal.d.ts.map +1 -0
  50. package/lib/typescript/flags/types.d.ts +202 -0
  51. package/lib/typescript/flags/types.d.ts.map +1 -0
  52. package/lib/typescript/index.d.ts +5 -2
  53. package/lib/typescript/index.d.ts.map +1 -1
  54. package/lib/typescript/nativeModulesTypes.d.ts +5 -0
  55. package/lib/typescript/nativeModulesTypes.d.ts.map +1 -1
  56. package/lib/typescript/specs/NativeDdFlags.d.ts +15 -0
  57. package/lib/typescript/specs/NativeDdFlags.d.ts.map +1 -0
  58. package/lib/typescript/version.d.ts +1 -1
  59. package/package.json +2 -2
  60. package/src/flags/DdFlags.ts +59 -0
  61. package/src/flags/FlagsClient.ts +266 -0
  62. package/src/flags/__tests__/DdFlags.test.ts +65 -0
  63. package/src/flags/__tests__/FlagsClient.test.ts +320 -0
  64. package/src/flags/internal.ts +63 -0
  65. package/src/flags/types.ts +215 -0
  66. package/src/index.tsx +15 -1
  67. package/src/nativeModulesTypes.ts +6 -0
  68. package/src/specs/NativeDdFlags.ts +35 -0
  69. package/src/version.ts +1 -1
@@ -13,21 +13,22 @@ Pod::Spec.new do |s|
13
13
  s.platforms = { :ios => "12.0", :tvos => "12.0" }
14
14
  s.source = { :git => "https://github.com/DataDog/dd-sdk-reactnative.git", :tag => "#{s.version}" }
15
15
 
16
-
16
+
17
17
  s.source_files = "ios/Sources/*.{h,m,mm,swift}"
18
-
18
+
19
19
  s.dependency "React-Core"
20
20
 
21
21
  # /!\ Remember to keep the versions in sync with DatadogSDKReactNativeSessionReplay.podspec
22
- s.dependency 'DatadogCore', '3.4.0'
23
- s.dependency 'DatadogLogs', '3.4.0'
24
- s.dependency 'DatadogTrace', '3.4.0'
25
- s.dependency 'DatadogRUM', '3.4.0'
26
- s.dependency 'DatadogCrashReporting', '3.4.0'
22
+ s.dependency 'DatadogCore', '3.5.0'
23
+ s.dependency 'DatadogLogs', '3.5.0'
24
+ s.dependency 'DatadogTrace', '3.5.0'
25
+ s.dependency 'DatadogRUM', '3.5.0'
26
+ s.dependency 'DatadogCrashReporting', '3.5.0'
27
+ s.dependency 'DatadogFlags', '3.5.0'
27
28
 
28
29
  # DatadogWebViewTracking is not available for tvOS
29
- s.ios.dependency 'DatadogWebViewTracking', '3.4.0'
30
-
30
+ s.ios.dependency 'DatadogWebViewTracking', '3.5.0'
31
+
31
32
  s.test_spec 'Tests' do |test_spec|
32
33
  test_spec.source_files = 'ios/Tests/**/*.{swift,json}'
33
34
  test_spec.resources = 'ios/Tests/Fixtures'
@@ -195,22 +195,23 @@ dependencies {
195
195
  }
196
196
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
197
197
  compileOnly "com.squareup.okhttp3:okhttp:3.12.13"
198
- // dd-sdk-android-rum requires androidx.metrics:metrics-performance.
198
+ // dd-sdk-android-rum requires androidx.metrics:metrics-performance.
199
199
  // From 2.21.0, it uses 1.0.0-beta02, which requires Gradle 8.6.0.
200
- // This breaks builds if the React Native target is below 0.76.0. as it relies on Gradle 8.5.0.
200
+ // This breaks builds if the React Native target is below 0.76.0. as it relies on Gradle 8.5.0.
201
201
  // To avoid this, we enforce 1.0.0-beta01 on RN < 0.76.0
202
202
  if (reactNativeMinorVersion < 76) {
203
- implementation("com.datadoghq:dd-sdk-android-rum:3.4.0") {
203
+ implementation("com.datadoghq:dd-sdk-android-rum:3.5.0") {
204
204
  exclude group: "androidx.metrics", module: "metrics-performance"
205
205
  }
206
206
  implementation "androidx.metrics:metrics-performance:1.0.0-beta01"
207
207
  } else {
208
- implementation "com.datadoghq:dd-sdk-android-rum:3.4.0"
208
+ implementation "com.datadoghq:dd-sdk-android-rum:3.5.0"
209
209
  }
210
- implementation "com.datadoghq:dd-sdk-android-logs:3.4.0"
211
- implementation "com.datadoghq:dd-sdk-android-trace:3.4.0"
212
- implementation "com.datadoghq:dd-sdk-android-webview:3.4.0"
213
- implementation "com.datadoghq:dd-sdk-android-ndk:3.4.0"
210
+ implementation "com.datadoghq:dd-sdk-android-logs:3.5.0"
211
+ implementation "com.datadoghq:dd-sdk-android-trace:3.5.0"
212
+ implementation "com.datadoghq:dd-sdk-android-webview:3.5.0"
213
+ implementation "com.datadoghq:dd-sdk-android-ndk:3.5.0"
214
+ implementation "com.datadoghq:dd-sdk-android-flags:3.5.0"
214
215
  implementation "com.google.code.gson:gson:2.10.0"
215
216
  testImplementation "org.junit.platform:junit-platform-launcher:1.6.2"
216
217
  testImplementation "org.junit.jupiter:junit-jupiter-api:5.6.2"
@@ -237,6 +238,7 @@ unMock {
237
238
  keep("android.os.SystemProperties")
238
239
  keep("android.view.Choreographer")
239
240
  keep("android.view.DisplayEventReceiver")
241
+ keepStartingWith("org.json.")
240
242
  }
241
243
 
242
244
  tasks.withType(Test) {
@@ -0,0 +1,237 @@
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
+ package com.datadog.reactnative
8
+
9
+ import com.datadog.android.Datadog
10
+ import com.datadog.android.api.InternalLogger
11
+ import com.datadog.android.api.SdkCore
12
+ import com.datadog.android.flags.EvaluationContextCallback
13
+ import com.datadog.android.flags.Flags
14
+ import com.datadog.android.flags.FlagsClient
15
+ import com.datadog.android.flags.FlagsConfiguration
16
+ import com.datadog.android.flags._FlagsInternalProxy
17
+ import com.datadog.android.flags.model.EvaluationContext
18
+ import com.datadog.android.flags.model.FlagsClientState
19
+ import com.datadog.android.flags.model.UnparsedFlag
20
+ import com.facebook.react.bridge.Promise
21
+ import com.facebook.react.bridge.ReadableMap
22
+ import org.json.JSONObject
23
+ import java.util.Locale
24
+
25
+ /**
26
+ * The entry point to use Datadog's Flags feature.
27
+ */
28
+ class DdFlagsImplementation(
29
+ private val sdkCore: SdkCore = Datadog.getInstance(),
30
+ ) {
31
+ private val clients: MutableMap<String, FlagsClient> = mutableMapOf()
32
+
33
+ /**
34
+ * Enable the Flags feature with the provided configuration.
35
+ * @param configuration The configuration for Flags.
36
+ */
37
+ fun enable(
38
+ configuration: ReadableMap,
39
+ promise: Promise,
40
+ ) {
41
+ val flagsConfig = buildFlagsConfiguration(configuration.toMap())
42
+ if (flagsConfig != null) {
43
+ Flags.enable(flagsConfig, sdkCore)
44
+ } else {
45
+ InternalLogger.UNBOUND.log(
46
+ InternalLogger.Level.ERROR,
47
+ InternalLogger.Target.USER,
48
+ { "Invalid configuration provided for Flags. Feature initialization skipped." },
49
+ )
50
+ }
51
+ promise.resolve(null)
52
+ }
53
+
54
+ private fun getClient(name: String): FlagsClient = clients.getOrPut(name) { FlagsClient.Builder(name, sdkCore).build() }
55
+
56
+ /**
57
+ * Set the evaluation context for a specific client.
58
+ * @param clientName The name of the client.
59
+ * @param targetingKey The targeting key.
60
+ * @param attributes The attributes for the evaluation context (will be converted to strings).
61
+ */
62
+ fun setEvaluationContext(
63
+ clientName: String,
64
+ targetingKey: String,
65
+ attributes: ReadableMap,
66
+ promise: Promise,
67
+ ) {
68
+ val client = getClient(clientName)
69
+ val internalClient = _FlagsInternalProxy(client)
70
+
71
+ // Set the evaluation context.
72
+ val evaluationContext = buildEvaluationContext(targetingKey, attributes)
73
+ client.setEvaluationContext(
74
+ evaluationContext,
75
+ object : EvaluationContextCallback {
76
+ override fun onSuccess() {
77
+ val flagsSnapshot = internalClient.getFlagAssignmentsSnapshot()
78
+ val serializedFlagsSnapshot =
79
+ flagsSnapshot.mapValues { (key, flag) ->
80
+ convertUnparsedFlagToMap(key, flag)
81
+ }.toWritableMap()
82
+ promise.resolve(serializedFlagsSnapshot)
83
+ }
84
+
85
+ override fun onFailure(error: Throwable) {
86
+ // If network request fails and there are cached flags, return them.
87
+ if (client.state.getCurrentState() == FlagsClientState.Stale) {
88
+ val flagsSnapshot = internalClient.getFlagAssignmentsSnapshot()
89
+ val serializedFlagsSnapshot =
90
+ flagsSnapshot.mapValues { (key, flag) ->
91
+ convertUnparsedFlagToMap(key, flag)
92
+ }.toWritableMap()
93
+ promise.resolve(serializedFlagsSnapshot)
94
+ } else {
95
+ promise.reject("CLIENT_NOT_INITIALIZED", error.message, error)
96
+ }
97
+ }
98
+ },
99
+ )
100
+ }
101
+
102
+ /**
103
+ * A bridge for tracking feature flag evaluations in React Native.
104
+ * @param clientName The name of the client.
105
+ * @param key The key of the flag.
106
+ * @param rawFlag The raw flag from the JavaScript cache.
107
+ * @param targetingKey The targeting key.
108
+ * @param attributes The attributes for the evaluation context.
109
+ * @param promise The promise to resolve.
110
+ */
111
+ @Suppress("LongParameterList")
112
+ fun trackEvaluation(
113
+ clientName: String,
114
+ key: String,
115
+ rawFlag: ReadableMap,
116
+ targetingKey: String,
117
+ attributes: ReadableMap,
118
+ promise: Promise,
119
+ ) {
120
+ val client = getClient(clientName)
121
+ val internalClient = _FlagsInternalProxy(client)
122
+
123
+ val flag = convertMapToUnparsedFlag(rawFlag.toMap())
124
+ val evaluationContext = buildEvaluationContext(targetingKey, attributes)
125
+ internalClient.trackFlagSnapshotEvaluation(key, flag, evaluationContext)
126
+
127
+ promise.resolve(null)
128
+ }
129
+
130
+ internal companion object {
131
+ internal const val NAME = "DdFlags"
132
+ }
133
+ }
134
+
135
+ @Suppress("UNCHECKED_CAST")
136
+ private fun buildFlagsConfiguration(configuration: Map<String, Any?>): FlagsConfiguration? {
137
+ val enabled = configuration["enabled"] as? Boolean ?: false
138
+
139
+ if (!enabled) {
140
+ return null
141
+ }
142
+
143
+ // Hard set `gracefulModeEnabled` to `true` because SDK misconfigurations are handled on JS
144
+ // side.
145
+ // This prevents crashes on hot reload when clients are recreated.
146
+ val gracefulModeEnabled = true
147
+
148
+ val trackExposures = configuration["trackExposures"] as? Boolean ?: true
149
+ val rumIntegrationEnabled = configuration["rumIntegrationEnabled"] as? Boolean ?: true
150
+
151
+ return FlagsConfiguration
152
+ .Builder()
153
+ .apply {
154
+ gracefulModeEnabled(gracefulModeEnabled)
155
+ trackExposures(trackExposures)
156
+ rumIntegrationEnabled(rumIntegrationEnabled)
157
+
158
+ // The SDK automatically appends endpoint names to the custom endpoints.
159
+ // The input config expects a base URL rather than a full URL.
160
+ (configuration["customFlagsEndpoint"] as? String)?.let {
161
+ useCustomFlagEndpoint("$it/precompute-assignments")
162
+ }
163
+ (configuration["customExposureEndpoint"] as? String)?.let {
164
+ useCustomExposureEndpoint("$it/api/v2/exposures")
165
+ }
166
+ }.build()
167
+ }
168
+
169
+ private fun buildEvaluationContext(
170
+ targetingKey: String,
171
+ attributes: ReadableMap,
172
+ ): EvaluationContext {
173
+ val parsed = mutableMapOf<String, String>()
174
+
175
+ for ((key, value) in attributes.entryIterator) {
176
+ parsed[key] = value.toString()
177
+ }
178
+
179
+ return EvaluationContext(targetingKey, parsed)
180
+ }
181
+
182
+ private fun convertUnparsedFlagToMap(
183
+ flagKey: String,
184
+ flag: UnparsedFlag,
185
+ ): Map<String, Any?> {
186
+ // Parse the value based on variationType
187
+ val parsedValue: Any? =
188
+ when (flag.variationType) {
189
+ "boolean" -> flag.variationValue.lowercase(Locale.US).toBooleanStrictOrNull()
190
+ "string" -> flag.variationValue
191
+ "integer" -> flag.variationValue.toIntOrNull()
192
+ "number", "float" -> flag.variationValue.toDoubleOrNull()
193
+ "object" -> try {
194
+ JSONObject(flag.variationValue).toMap()
195
+ } catch (_: Exception) {
196
+ null
197
+ }
198
+ else -> {
199
+ null
200
+ }
201
+ }
202
+
203
+ if (parsedValue == null) {
204
+ InternalLogger.UNBOUND.log(
205
+ InternalLogger.Level.ERROR,
206
+ InternalLogger.Target.USER,
207
+ { "Flag '$flagKey': Failed to parse value '${flag.variationValue}' as '${flag.variationType}'" },
208
+ )
209
+ }
210
+
211
+ // Return a [Map] as an intermediate because it is easier to use; we can convert it to WritableMap right before sending to React Native.
212
+ return mapOf(
213
+ "key" to flagKey,
214
+ "value" to (parsedValue ?: flag.variationValue),
215
+ "allocationKey" to flag.allocationKey,
216
+ "variationKey" to flag.variationKey,
217
+ "variationType" to flag.variationType,
218
+ "variationValue" to flag.variationValue,
219
+ "reason" to flag.reason,
220
+ "doLog" to flag.doLog,
221
+ "extraLogging" to flag.extraLogging.toMap(),
222
+ )
223
+ }
224
+
225
+ @Suppress("UNCHECKED_CAST")
226
+ private fun convertMapToUnparsedFlag(map: Map<String, Any>): UnparsedFlag =
227
+ object : UnparsedFlag {
228
+ override val variationType: String = map["variationType"] as? String ?: ""
229
+ override val variationValue: String = map["variationValue"] as? String ?: ""
230
+ override val doLog: Boolean = map["doLog"] as? Boolean ?: false
231
+ override val allocationKey: String = map["allocationKey"] as? String ?: ""
232
+ override val variationKey: String = map["variationKey"] as? String ?: ""
233
+ override val extraLogging: JSONObject =
234
+ (map["extraLogging"] as? Map<String, Any>)?.toJSONObject()
235
+ ?: JSONObject()
236
+ override val reason: String = map["reason"] as? String ?: ""
237
+ }