@datadog/mobile-react-native 1.0.0-rc8 → 1.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 (192) hide show
  1. package/DatadogSDKReactNative.podspec +2 -1
  2. package/android/build.gradle +73 -3
  3. package/android/gradle.properties +1 -0
  4. package/android/src/main/kotlin/com/datadog/reactnative/DatadogSDKWrapper.kt +54 -0
  5. package/android/src/main/kotlin/com/datadog/reactnative/DatadogWrapper.kt +86 -0
  6. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdLogs.kt +27 -11
  7. package/android/src/main/kotlin/com/datadog/reactnative/DdRum.kt +305 -0
  8. package/android/src/main/kotlin/com/datadog/reactnative/DdSdk.kt +280 -0
  9. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdSdkBridgeExt.kt +2 -2
  10. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfiguration.kt +29 -0
  11. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdSdkConfigurationExt.kt +2 -4
  12. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdSdkReactNativePackage.kt +3 -0
  13. package/android/src/main/kotlin/com/datadog/reactnative/DdTrace.kt +86 -0
  14. package/android/src/main/kotlin/com/datadog/reactnative/GlobalState.kt +32 -0
  15. package/android/src/main/kotlin/com/datadog/reactnative/NoOpViewTrackingStrategy.kt +23 -0
  16. package/android/src/main/kotlin/com/datadog/reactnative/ProxyAuthenticator.kt +60 -0
  17. package/android/src/test/kotlin/com/datadog/reactnative/DdLogsTest.kt +170 -0
  18. package/android/src/test/kotlin/com/datadog/reactnative/DdRumTest.kt +408 -0
  19. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +1547 -0
  20. package/android/src/test/kotlin/com/datadog/reactnative/DdTraceTest.kt +306 -0
  21. package/android/src/test/kotlin/com/datadog/reactnative/GlobalStateTest.kt +118 -0
  22. package/android/src/test/kotlin/com/datadog/reactnative/ProxyAuthenticatorTest.kt +123 -0
  23. package/android/src/test/kotlin/com/datadog/tools/unit/DdSdkConfigurationExt.kt +33 -0
  24. package/android/src/test/kotlin/com/datadog/tools/unit/GenericAssert.kt +43 -0
  25. package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +22 -0
  26. package/android/src/test/kotlin/com/datadog/tools/unit/ReflectUtils.kt +267 -0
  27. package/android/src/test/kotlin/com/datadog/tools/unit/forge/BaseConfigurator.kt +26 -0
  28. package/android/src/test/kotlin/com/datadog/tools/unit/forge/DdSdkConfigurationForgeryFactory.kt +33 -0
  29. package/android/src/test/kotlin/com/datadog/tools/unit/forge/PackageInfoForgeryFactory.kt +20 -0
  30. package/android/src/test/kotlin/com/datadog/tools/unit/forge/Throwable.kt +31 -0
  31. package/android/src/test/kotlin/com/datadog/tools/unit/forge/ThrowableForgeryFactory.kt +21 -0
  32. package/android/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker +1 -0
  33. package/datadog-sourcemaps.gradle +137 -0
  34. package/ios/Sources/AnyEncodable.swift +138 -0
  35. package/ios/Sources/Attributes.swift +101 -0
  36. package/ios/Sources/DdLogs.swift +35 -11
  37. package/ios/Sources/DdRum.m +3 -1
  38. package/ios/Sources/DdRum.swift +184 -22
  39. package/ios/Sources/DdSdk.swift +190 -10
  40. package/ios/Sources/DdSdkConfiguration.swift +51 -0
  41. package/ios/Sources/DdTrace.swift +41 -6
  42. package/ios/Sources/GlobalState.swift +28 -0
  43. package/ios/Sources/QueueHolder.swift +10 -0
  44. package/ios/Sources/RNDdSdkConfiguration.swift +5 -6
  45. package/lib/commonjs/DdSdkReactNative.js +16 -2
  46. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  47. package/lib/commonjs/DdSdkReactNativeConfiguration.js +24 -0
  48. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  49. package/lib/commonjs/foundation.js +36 -2
  50. package/lib/commonjs/foundation.js.map +1 -1
  51. package/lib/commonjs/nativeModulesTypes.js +36 -0
  52. package/lib/commonjs/nativeModulesTypes.js.map +1 -0
  53. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +4 -17
  54. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  55. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +4 -4
  56. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  57. package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +73 -0
  58. package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -0
  59. package/lib/commonjs/rum/instrumentation/{TraceIdentifier.js → resourceTracking/domain/distributedTracing.js} +40 -1
  60. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -0
  61. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +51 -0
  62. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -0
  63. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js +25 -0
  64. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -0
  65. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.js +2 -0
  66. package/lib/commonjs/rum/instrumentation/{DdRumXhr.js.map → resourceTracking/domain/interfaces/RumResource.js.map} +0 -0
  67. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +57 -0
  68. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -0
  69. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js +49 -0
  70. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js.map +1 -0
  71. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js +45 -0
  72. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js.map +1 -0
  73. package/lib/commonjs/rum/instrumentation/{URLHostParser.js → resourceTracking/implementation/URLHostParser.js} +16 -7
  74. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/URLHostParser.js.map +1 -0
  75. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +167 -0
  76. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -0
  77. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/responseSize.js +97 -0
  78. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/responseSize.js.map +1 -0
  79. package/lib/commonjs/types.js +26 -1
  80. package/lib/commonjs/types.js.map +1 -1
  81. package/lib/commonjs/version.js +1 -1
  82. package/lib/commonjs/version.js.map +1 -1
  83. package/lib/module/DdSdkReactNative.js +15 -1
  84. package/lib/module/DdSdkReactNative.js.map +1 -1
  85. package/lib/module/DdSdkReactNativeConfiguration.js +24 -0
  86. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  87. package/lib/module/foundation.js +36 -2
  88. package/lib/module/foundation.js.map +1 -1
  89. package/lib/module/nativeModulesTypes.js +26 -0
  90. package/lib/module/nativeModulesTypes.js.map +1 -0
  91. package/lib/module/rum/instrumentation/DdEventsInterceptor.js +2 -14
  92. package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  93. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +3 -4
  94. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  95. package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +61 -0
  96. package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -0
  97. package/lib/module/rum/instrumentation/{TraceIdentifier.js → resourceTracking/domain/distributedTracing.js} +35 -1
  98. package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -0
  99. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +34 -0
  100. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -0
  101. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js +16 -0
  102. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -0
  103. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.js +2 -0
  104. package/lib/module/rum/instrumentation/{DdRumXhr.js.map → resourceTracking/domain/interfaces/RumResource.js.map} +0 -0
  105. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +50 -0
  106. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -0
  107. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js +40 -0
  108. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js.map +1 -0
  109. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js +36 -0
  110. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js.map +1 -0
  111. package/{src/rum/instrumentation/URLHostParser.ts → lib/module/rum/instrumentation/resourceTracking/implementation/URLHostParser.js} +15 -8
  112. package/lib/module/rum/instrumentation/resourceTracking/implementation/URLHostParser.js.map +1 -0
  113. package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +151 -0
  114. package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -0
  115. package/lib/module/rum/instrumentation/resourceTracking/implementation/responseSize.js +86 -0
  116. package/lib/module/rum/instrumentation/resourceTracking/implementation/responseSize.js.map +1 -0
  117. package/lib/module/types.js +23 -0
  118. package/lib/module/types.js.map +1 -1
  119. package/lib/module/version.js +1 -1
  120. package/lib/module/version.js.map +1 -1
  121. package/lib/typescript/DdSdkReactNative.d.ts +2 -0
  122. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +20 -0
  123. package/lib/typescript/foundation.d.ts +3 -2
  124. package/lib/typescript/nativeModulesTypes.d.ts +168 -0
  125. package/lib/typescript/rum/instrumentation/DdEventsInterceptor.d.ts +0 -15
  126. package/lib/typescript/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +15 -0
  127. package/lib/typescript/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +18 -0
  128. package/lib/typescript/rum/instrumentation/resourceTracking/domain/firstPartyHosts.d.ts +6 -0
  129. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.d.ts +8 -0
  130. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +20 -0
  131. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.d.ts +8 -0
  132. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.d.ts +6 -0
  133. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.d.ts +17 -0
  134. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/URLHostParser.d.ts +6 -0
  135. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/XHRProxy.d.ts +25 -0
  136. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/responseSize.d.ts +3 -0
  137. package/lib/typescript/types.d.ts +42 -7
  138. package/lib/typescript/version.d.ts +1 -1
  139. package/package.json +7 -5
  140. package/src/DdSdkReactNative.tsx +21 -1
  141. package/src/DdSdkReactNativeConfiguration.tsx +20 -0
  142. package/src/__tests__/DdSdkReactNative.test.tsx +87 -8
  143. package/src/__tests__/foundation.test.ts +79 -0
  144. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +9 -9
  145. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +1 -1
  146. package/src/foundation.tsx +40 -11
  147. package/src/nativeModulesTypes.ts +258 -0
  148. package/src/rum/instrumentation/DdEventsInterceptor.tsx +2 -17
  149. package/src/rum/instrumentation/DdRumErrorTracking.tsx +3 -4
  150. package/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.tsx +67 -0
  151. package/src/rum/instrumentation/resourceTracking/__tests__/DdRumResourceTracking.test.ts +79 -0
  152. package/src/{__tests__/rum/instrumentation → rum/instrumentation/resourceTracking/__tests__}/__utils__/XMLHttpRequestMock.ts +0 -0
  153. package/src/{__tests__/rum/instrumentation/TraceIdentifier.test.tsx → rum/instrumentation/resourceTracking/domain/__tests__/distributedTracing.test.ts} +1 -1
  154. package/src/rum/instrumentation/resourceTracking/domain/__tests__/firstPartyHosts.test.ts +22 -0
  155. package/src/rum/instrumentation/resourceTracking/domain/distributedTracing.tsx +87 -0
  156. package/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.ts +52 -0
  157. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.ts +15 -0
  158. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.ts +27 -0
  159. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.ts +78 -0
  160. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/ResourceReporter.test.ts +83 -0
  161. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/__utils__/ResourceMockFactory.ts +45 -0
  162. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/internalDevResourceBlocklist.test.ts +69 -0
  163. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.ts +50 -0
  164. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.ts +70 -0
  165. package/src/rum/instrumentation/resourceTracking/implementation/URLHostParser.ts +30 -0
  166. package/src/rum/instrumentation/resourceTracking/implementation/XHRProxy.ts +197 -0
  167. package/src/{__tests__/rum/instrumentation → rum/instrumentation/resourceTracking/implementation/__tests__}/URLHostParser.test.ts +3 -3
  168. package/src/{__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx → rum/instrumentation/resourceTracking/implementation/__tests__/XHRProxy.test.ts} +83 -49
  169. package/src/rum/instrumentation/resourceTracking/implementation/responseSize.ts +83 -0
  170. package/src/types.tsx +62 -7
  171. package/src/version.ts +1 -1
  172. package/android/src/main/java/com/datadog/reactnative/DdRum.kt +0 -142
  173. package/android/src/main/java/com/datadog/reactnative/DdSdk.kt +0 -67
  174. package/android/src/main/java/com/datadog/reactnative/DdTrace.kt +0 -51
  175. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +0 -356
  176. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +0 -1
  177. package/lib/commonjs/rum/instrumentation/DdRumXhr.js +0 -2
  178. package/lib/commonjs/rum/instrumentation/TraceIdentifier.js.map +0 -1
  179. package/lib/commonjs/rum/instrumentation/URLHostParser.js.map +0 -1
  180. package/lib/module/rum/instrumentation/DdRumResourceTracking.js +0 -331
  181. package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +0 -1
  182. package/lib/module/rum/instrumentation/DdRumXhr.js +0 -2
  183. package/lib/module/rum/instrumentation/TraceIdentifier.js.map +0 -1
  184. package/lib/module/rum/instrumentation/URLHostParser.js +0 -24
  185. package/lib/module/rum/instrumentation/URLHostParser.js.map +0 -1
  186. package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +0 -40
  187. package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +0 -23
  188. package/lib/typescript/rum/instrumentation/TraceIdentifier.d.ts +0 -1
  189. package/lib/typescript/rum/instrumentation/URLHostParser.d.ts +0 -6
  190. package/src/rum/instrumentation/DdRumResourceTracking.tsx +0 -426
  191. package/src/rum/instrumentation/DdRumXhr.tsx +0 -33
  192. package/src/rum/instrumentation/TraceIdentifier.tsx +0 -34
@@ -0,0 +1,267 @@
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.tools.unit
8
+
9
+ import java.lang.reflect.Field
10
+ import java.lang.reflect.InvocationTargetException
11
+ import java.lang.reflect.Method
12
+ import java.lang.reflect.Modifier
13
+ import java.util.LinkedList
14
+ import kotlin.reflect.jvm.isAccessible
15
+
16
+ /**
17
+ * Creates an instance of the given class name.
18
+ * @param className the full name of the class to instantiate
19
+ * @param params the parameters to provide the constructor
20
+ * @return the created instance
21
+ */
22
+ @Suppress("SpreadOperator")
23
+ fun createInstance(
24
+ className: String,
25
+ vararg params: Any?
26
+ ): Any {
27
+ return Class.forName(className)
28
+ .kotlin
29
+ .constructors.first()
30
+ .apply { isAccessible = true }
31
+ .call(*params)
32
+ }
33
+
34
+ /**
35
+ * Sets a static value on the target class.
36
+ * @param fieldName the name of the field
37
+ * @param fieldValue the value to set
38
+ */
39
+ @Suppress("SwallowedException")
40
+ inline fun <reified T, R> Class<T>.setStaticValue(
41
+ fieldName: String,
42
+ fieldValue: R
43
+ ) {
44
+ val field = getDeclaredField(fieldName)
45
+
46
+ // make it accessible
47
+ field.isAccessible = true
48
+
49
+ // Make it non final
50
+ try {
51
+ val modifiersField = Field::class.java.getDeclaredField("modifiers")
52
+ modifiersField.isAccessible = true
53
+ modifiersField.setInt(field, field.modifiers and Modifier.FINAL.inv())
54
+ } catch (e: NoSuchFieldException) {
55
+ // do nothing
56
+ @Suppress("PrintStackTrace")
57
+ e.printStackTrace()
58
+ }
59
+ field.set(null, fieldValue)
60
+ }
61
+
62
+ /**
63
+ * Gets the static value from the target class.
64
+ * @param className the full name of the class
65
+ * @param fieldName the name of the field
66
+ */
67
+ inline fun <reified R> getStaticValue(
68
+ className: String,
69
+ fieldName: String
70
+ ): R {
71
+ val clazz = Class.forName(className)
72
+ val field = clazz.getDeclaredField(fieldName)
73
+ // make it accessible
74
+ field.isAccessible = true
75
+
76
+ return field.get(null) as R
77
+ }
78
+
79
+ /**
80
+ * Gets the static value from the target class.
81
+ * @param fieldName the name of the field
82
+ */
83
+ inline fun <reified T, reified R> Class<T>.getStaticValue(fieldName: String): R {
84
+
85
+ val field = getDeclaredField(fieldName)
86
+
87
+ // make it accessible
88
+ field.isAccessible = true
89
+
90
+ return field.get(null) as R
91
+ }
92
+
93
+ /**
94
+ * Sets the field value on the target instance.
95
+ * @param fieldName the name of the field
96
+ * @param fieldValue the value of the field
97
+ */
98
+ @Suppress("SwallowedException")
99
+ inline fun <reified T> Any.setFieldValue(
100
+ fieldName: String,
101
+ fieldValue: T
102
+ ): Boolean {
103
+ var field: Field? = null
104
+ val classesToSearch = LinkedList<Class<*>>()
105
+ classesToSearch.add(this.javaClass)
106
+ val classesSearched = mutableSetOf<Class<*>>()
107
+
108
+ while (field == null && classesToSearch.isNotEmpty()) {
109
+ val toSearchIn = classesToSearch.remove()
110
+ try {
111
+ field = toSearchIn.getDeclaredField(fieldName)
112
+ } catch (e: NoSuchFieldException) {
113
+ // do nothing
114
+ }
115
+ classesSearched.add(toSearchIn)
116
+ toSearchIn.superclass?.let {
117
+ if (!classesSearched.contains(it)) {
118
+ classesToSearch.add(it)
119
+ }
120
+ }
121
+ }
122
+
123
+ // make it accessible
124
+ if (field != null) {
125
+ field.isAccessible = true
126
+
127
+ // Make it non final
128
+ val modifiersField = Field::class.java.getDeclaredField("modifiers")
129
+ modifiersField.isAccessible = true
130
+ modifiersField.setInt(field, field.modifiers and Modifier.FINAL.inv())
131
+
132
+ field.set(this, fieldValue)
133
+ return true
134
+ } else {
135
+ return false
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Gets the field value from the target instance.
141
+ * @param fieldName the name of the field
142
+ */
143
+ inline fun <reified T, R : Any> R.getFieldValue(
144
+ fieldName: String,
145
+ enclosingClass: Class<R> = this.javaClass
146
+ ): T {
147
+ val field = enclosingClass.getDeclaredField(fieldName)
148
+ field.isAccessible = true
149
+ return field.get(this) as T
150
+ }
151
+
152
+ /**
153
+ * Invokes a method on the target instance.
154
+ * @param methodName the name of the method
155
+ * @param params the parameters to provide the method
156
+ * @return the result from the invoked method
157
+ */
158
+ @Suppress("SpreadOperator", "UNCHECKED_CAST", "TooGenericExceptionCaught")
159
+ fun <T : Any> T.invokeMethod(
160
+ methodName: String,
161
+ vararg params: Any?
162
+ ): Any? {
163
+ val declarationParams = Array<Class<*>?>(params.size) {
164
+ params[it]?.javaClass
165
+ }
166
+
167
+ val method = getDeclaredMethodRecursively(methodName, true, declarationParams)
168
+ val wasAccessible = method.isAccessible
169
+
170
+ val output: Any?
171
+ method.isAccessible = true
172
+ try {
173
+ output = if (params.isEmpty()) {
174
+ method.invoke(this)
175
+ } else {
176
+ method.invoke(this, *params)
177
+ }
178
+ } catch (e: InvocationTargetException) {
179
+ throw e.cause ?: e
180
+ } finally {
181
+ method.isAccessible = wasAccessible
182
+ }
183
+
184
+ return output
185
+ }
186
+
187
+ /**
188
+ * Invokes a method on the target instance, where one or more of the parameters
189
+ * are generics.
190
+ * @param methodName the name of the method
191
+ * @param params the parameters to provide the method
192
+ * @return the result from the invoked method
193
+ */
194
+ @Suppress("SpreadOperator", "UNCHECKED_CAST")
195
+ fun <T : Any> T.invokeGenericMethod(
196
+ methodName: String,
197
+ vararg params: Any
198
+ ): Any? {
199
+ val declarationParams = Array<Class<*>?>(params.size) {
200
+ params[it].javaClass
201
+ }
202
+
203
+ val method = getDeclaredMethodRecursively(methodName, false, declarationParams)
204
+ val wasAccessible = method.isAccessible
205
+
206
+ val output: Any?
207
+ method.isAccessible = true
208
+ try {
209
+ output = if (params.isEmpty()) {
210
+ method.invoke(this)
211
+ } else {
212
+ method.invoke(this, *params)
213
+ }
214
+ } catch (e: InvocationTargetException) {
215
+ throw e.cause ?: e
216
+ } finally {
217
+ method.isAccessible = wasAccessible
218
+ }
219
+
220
+ return output
221
+ }
222
+
223
+ @Suppress("TooGenericExceptionCaught", "SwallowedException", "SpreadOperator")
224
+ private fun <T : Any> T.getDeclaredMethodRecursively(
225
+ methodName: String,
226
+ matchingParams: Boolean,
227
+ declarationParams: Array<Class<*>?>
228
+ ): Method {
229
+ val classesToSearch = mutableListOf<Class<*>>(this.javaClass)
230
+ val classesSearched = mutableListOf<Class<*>>()
231
+ var method: Method?
232
+ do {
233
+ val lookingInClass = classesToSearch.removeAt(0)
234
+ classesSearched.add(lookingInClass)
235
+ method = try {
236
+ if (matchingParams) {
237
+ lookingInClass.getDeclaredMethod(methodName, *declarationParams)
238
+ } else {
239
+ lookingInClass.declaredMethods.firstOrNull {
240
+ it.name == methodName &&
241
+ it.parameterTypes.size == declarationParams.size
242
+ }
243
+ }
244
+ } catch (e: Throwable) {
245
+ null
246
+ }
247
+
248
+ val superclass = lookingInClass.superclass
249
+ if (superclass != null &&
250
+ !classesToSearch.contains(superclass) &&
251
+ !classesSearched.contains(superclass)
252
+ ) {
253
+ classesToSearch.add(superclass)
254
+ }
255
+ lookingInClass.interfaces.forEach {
256
+ if (!classesToSearch.contains(it) && !classesSearched.contains(it)) {
257
+ classesToSearch.add(it)
258
+ }
259
+ }
260
+ } while (method == null && classesToSearch.isNotEmpty())
261
+
262
+ checkNotNull(method) {
263
+ "Unable to access method $methodName on ${javaClass.canonicalName}"
264
+ }
265
+
266
+ return method
267
+ }
@@ -0,0 +1,26 @@
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.tools.unit.forge
8
+
9
+ import fr.xgouchet.elmyr.Forge
10
+ import fr.xgouchet.elmyr.ForgeConfigurator
11
+ import fr.xgouchet.elmyr.jvm.useJvmFactories
12
+
13
+ /**
14
+ * Base implementation of a [ForgeConfigurator], adding the JVM Forgery Factories (Date, …) and
15
+ * a [ThrowableForgeryFactory].
16
+ */
17
+ open class BaseConfigurator :
18
+ ForgeConfigurator {
19
+ /** @inheritDoc */
20
+ override fun configure(forge: Forge) {
21
+ forge.addFactory(ThrowableForgeryFactory())
22
+ forge.addFactory(DdSdkConfigurationForgeryFactory())
23
+ forge.addFactory(PackageInfoForgeryFactory())
24
+ forge.useJvmFactories()
25
+ }
26
+ }
@@ -0,0 +1,33 @@
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.tools.unit.forge
8
+
9
+ import com.datadog.reactnative.DdSdkConfiguration
10
+ import fr.xgouchet.elmyr.Forge
11
+ import fr.xgouchet.elmyr.ForgeryFactory
12
+ import java.util.UUID
13
+
14
+ class DdSdkConfigurationForgeryFactory : ForgeryFactory<DdSdkConfiguration> {
15
+ override fun getForgery(forge: Forge): DdSdkConfiguration {
16
+ return DdSdkConfiguration(
17
+ clientToken = forge.aStringMatching("pub[a-f0-9]{32}"),
18
+ env = forge.anAlphabeticalString(),
19
+ applicationId = forge.aNullable { getForgery<UUID>().toString() },
20
+ nativeCrashReportEnabled = forge.aNullable { aBool() },
21
+ sampleRate = forge.aNullable { aDouble(0.0, 100.0) },
22
+ site = forge.aNullable { anElementFrom("US", "EU", "GOV") },
23
+ additionalConfig = forge.aMap {
24
+ forge.anAsciiString() to forge.anElementFrom(
25
+ forge.aMap { forge.anAsciiString() to forge.aString() },
26
+ forge.aString(),
27
+ null
28
+ )
29
+ },
30
+ trackingConsent = forge.aNullable { anElementFrom("pending", "granted", "not_granted") }
31
+ )
32
+ }
33
+ }
@@ -0,0 +1,20 @@
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.tools.unit.forge
8
+
9
+ import android.content.pm.PackageInfo
10
+ import fr.xgouchet.elmyr.Forge
11
+ import fr.xgouchet.elmyr.ForgeryFactory
12
+ import java.util.UUID
13
+
14
+ class PackageInfoForgeryFactory : ForgeryFactory<PackageInfo> {
15
+ override fun getForgery(forge: Forge): PackageInfo {
16
+ val packageInfo = PackageInfo();
17
+ packageInfo.versionName = forge.aStringMatching("[0-9]\\.[0-9]\\.[0-9]")
18
+ return packageInfo
19
+ }
20
+ }
@@ -0,0 +1,31 @@
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.tools.unit.forge
8
+
9
+ import fr.xgouchet.elmyr.Forge
10
+ import fr.xgouchet.elmyr.ForgeryException
11
+ import java.io.FileNotFoundException
12
+ import java.io.IOException
13
+ import java.io.InvalidObjectException
14
+
15
+ /**
16
+ * @return a random [Throwable] instance with a forged message.
17
+ */
18
+ fun Forge.aThrowable(): Throwable {
19
+ val errorMessage = anAlphabeticalString()
20
+ return anElementFrom(
21
+ IOException(errorMessage),
22
+ IllegalStateException(errorMessage),
23
+ UnknownError(errorMessage),
24
+ ArrayIndexOutOfBoundsException(errorMessage),
25
+ NullPointerException(errorMessage),
26
+ ForgeryException(errorMessage),
27
+ InvalidObjectException(errorMessage),
28
+ UnsupportedOperationException(errorMessage),
29
+ FileNotFoundException(errorMessage)
30
+ )
31
+ }
@@ -0,0 +1,21 @@
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.tools.unit.forge
8
+
9
+ import fr.xgouchet.elmyr.Forge
10
+ import fr.xgouchet.elmyr.ForgeryFactory
11
+
12
+ /**
13
+ * A [ForgeryFactory] generating a random [Throwable] instance with a forged message.
14
+ */
15
+ class ThrowableForgeryFactory :
16
+ ForgeryFactory<Throwable> {
17
+ /** @inheritDoc */
18
+ override fun getForgery(forge: Forge): Throwable {
19
+ return forge.aThrowable()
20
+ }
21
+ }
@@ -0,0 +1,137 @@
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 org.apache.tools.ant.taskdefs.condition.Os
8
+
9
+ def reactConfig = project.hasProperty("react") ? project.react : [:]
10
+ def reactRoot = file(reactConfig.root ?: "../../")
11
+
12
+ afterEvaluate {
13
+
14
+ def androidExtension = extensions.findByName("android")
15
+
16
+ if (androidExtension == null || android.applicationVariants == null) {
17
+ throw new GradleException("Android extension is missing or running not in the application" +
18
+ " module. Make sure you've applied datadog-sourcemaps.gradle in the" +
19
+ " application module (usually it is located in the android/app/build.gradle file).")
20
+ }
21
+
22
+ androidExtension.applicationVariants.all { variant ->
23
+
24
+ def releaseVersion = variant.versionName
25
+ def buildVersion = variant.versionCode
26
+
27
+ if (releaseVersion == null) {
28
+ throw new GradleException("Cannot determine application version. Make sure to" +
29
+ " define versionName property in the application/variant config.")
30
+ }
31
+
32
+ // locations and names are defined in "../../node_modules/react-native/react.gradle"
33
+ def targetName = variant.name.capitalize()
34
+ def targetPath = variant.dirName
35
+
36
+ def bundleTask = tasks.findByName("bundle${targetName}JsAndAssets")
37
+
38
+ if (bundleTask == null) {
39
+ logger.info("Cannot find JS bundle task for variant=${targetName}.")
40
+ return
41
+ }
42
+ if (!bundleTask.enabled) {
43
+ logger.info("JS bundle task for variant=${targetName} is not enabled.")
44
+ return
45
+ }
46
+
47
+ def serviceName = getServiceName(variant)
48
+ logger.info("Service name used for the upload of variant=${targetName} is ${serviceName}.")
49
+
50
+ def bundleAssetName = reactConfig.bundleAssetName ?: "index.android.bundle"
51
+
52
+ def jsBundleDir = file("$buildDir/generated/assets/react/${targetPath}")
53
+ def jsBundleFile = file("$jsBundleDir/$bundleAssetName")
54
+
55
+ def jsSourceMapsDir = file("$buildDir/generated/sourcemaps/react/${targetPath}")
56
+ def jsOutputSourceMapFile = file("$jsSourceMapsDir/${bundleAssetName}.map")
57
+
58
+ def uploadTask = tasks.create("upload${targetName}Sourcemaps") {
59
+ group = "datadog"
60
+ description = "Uploads sourcemaps to Datadog."
61
+
62
+ def execCommand = [
63
+ "yarn",
64
+ "datadog-ci",
65
+ "react-native",
66
+ "upload",
67
+ "--platform",
68
+ "android",
69
+ "--service",
70
+ serviceName,
71
+ "--bundle",
72
+ jsBundleFile.absolutePath,
73
+ "--sourcemap",
74
+ jsOutputSourceMapFile.absolutePath,
75
+ "--release-version",
76
+ releaseVersion,
77
+ "--build-version",
78
+ buildVersion
79
+ ]
80
+
81
+ doFirst {
82
+ if (!jsBundleFile.exists()) {
83
+ throw new GradleException("JS bundle file doesn't exist, aborting upload.")
84
+ }
85
+
86
+ if (!jsOutputSourceMapFile.exists()) {
87
+ throw new GradleException("JS sourcemap file doesn't exist, aborting upload.")
88
+ }
89
+
90
+ runShellCommand(execCommand, reactRoot)
91
+
92
+ }
93
+ }
94
+
95
+ uploadTask.dependsOn bundleTask
96
+ bundleTask.finalizedBy uploadTask
97
+
98
+ }
99
+ }
100
+
101
+ private def runShellCommand(
102
+ List<String> command,
103
+ File workingDirectory
104
+ ) {
105
+ def outputStream = new ByteArrayOutputStream()
106
+ def errorStream = new ByteArrayOutputStream()
107
+
108
+ try {
109
+ def result = exec {
110
+ workingDir = workingDirectory
111
+ standardOutput = outputStream
112
+ errorOutput = errorStream
113
+ if (Os.isFamily(Os.FAMILY_WINDOWS)) {
114
+ commandLine("cmd", "/c", *command)
115
+ } else {
116
+ commandLine(*command)
117
+ }
118
+ }
119
+
120
+ if (result.exitValue != 0) {
121
+ logger.error(errorStream.toString("UTF-8"))
122
+ result.rethrowFailure()
123
+ } else {
124
+ logger.lifecycle(outputStream.toString("UTF-8"))
125
+ }
126
+ } catch (Exception e) {
127
+ def errorStreamContent = errorStream.toString("UTF-8")
128
+ def standardStreamContent = outputStream.toString("UTF-8")
129
+ logger.error("Exception raised during command execution," +
130
+ " stderr=${errorStreamContent}, stdout=${standardStreamContent}")
131
+ throw e
132
+ }
133
+ }
134
+
135
+ private static def getServiceName(variant) {
136
+ return variant.applicationId
137
+ }
@@ -0,0 +1,138 @@
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 2019-2020 Datadog, Inc.
5
+ */
6
+
7
+ import Foundation
8
+
9
+ internal func castAttributesToSwift(_ attributes: NSDictionary) -> [String: Encodable] {
10
+ return castAttributesToSwift(attributes as? [String: Any] ?? [:])
11
+ }
12
+
13
+ internal func castAttributesToSwift(_ attributes: [String: Any]) -> [String: Encodable] {
14
+ var casted: [String: Encodable] = [:]
15
+
16
+ attributes.forEach { key, value in
17
+ if let castedValue = castByPreservingTypeInformation(attributeValue: value) {
18
+ // If possible, cast attribute by preserving its type information
19
+ casted[key] = castedValue
20
+ } else {
21
+ // Otherwise, cast by preserving its encoded value (and loosing type information)
22
+ casted[key] = castByPreservingEncodedValue(attributeValue: value)
23
+ }
24
+ }
25
+
26
+ return casted
27
+ }
28
+
29
+ /// Casts `Any` value to `Encodable` by preserving its type information.
30
+ private func castByPreservingTypeInformation(attributeValue: Any) -> Encodable? {
31
+ switch attributeValue {
32
+ case let string as String: // unpacking `NSTaggedPointerString`
33
+ return string // cast to String
34
+ case let number as NSNumber: // unpacking `__NSCFNumber`
35
+ switch CFNumberGetType(number) {
36
+ case .charType:
37
+ return number.boolValue // cast to Bool
38
+ case .sInt8Type:
39
+ return number.int8Value // cast to Int8
40
+ case .sInt16Type:
41
+ return number.int16Value // cast to Int16
42
+ case .sInt32Type:
43
+ return number.int32Value // cast to Int32
44
+ case .sInt64Type:
45
+ return number.int64Value // cast to Int64
46
+ case .shortType:
47
+ return number.uint16Value // cast to UInt 16
48
+ case .longType:
49
+ return number.uint32Value // cast to UInt32
50
+ case .longLongType:
51
+ return number.uint64Value // cast to UInt64
52
+ case .intType, .nsIntegerType, .cfIndexType:
53
+ return number.intValue // cast to Int
54
+ case .floatType, .float32Type:
55
+ return number.floatValue // cast to Float
56
+ case .doubleType, .float64Type, .cgFloatType:
57
+ return number.doubleValue // cast to Double
58
+ @unknown default:
59
+ return nil
60
+ }
61
+ default:
62
+ return nil
63
+ }
64
+ }
65
+
66
+ /// Casts `Any` value to `Encodable` ereasing its type information, but preserving data representation
67
+ /// when value gets encoded.
68
+ private func castByPreservingEncodedValue(attributeValue: Any) -> Encodable {
69
+ return AnyEncodable(attributeValue)
70
+ }
71
+
72
+ /// Type erasing `Encodable` wrapper to bridge Objective-C's `Any` to Swift `Encodable`.
73
+ ///
74
+ /// Inspired by `AnyCodable` by Flight-School (MIT):
75
+ /// https://github.com/Flight-School/AnyCodable/blob/master/Sources/AnyCodable/AnyEncodable.swift
76
+ internal class AnyEncodable: Encodable {
77
+ internal let value: Any
78
+
79
+ init(_ value: Any) {
80
+ self.value = value
81
+ }
82
+
83
+ func encode(to encoder: Encoder) throws {
84
+ var container = encoder.singleValueContainer()
85
+
86
+ switch value {
87
+ case let number as NSNumber:
88
+ try encodeNSNumber(number, into: &container)
89
+ case is NSNull, is Void:
90
+ try container.encodeNil()
91
+ case let string as String:
92
+ try container.encode(string)
93
+ case let date as Date:
94
+ try container.encode(date)
95
+ case let url as URL:
96
+ try container.encode(url)
97
+ case let array as [Any]:
98
+ try container.encode(array.map { AnyEncodable($0) })
99
+ case let dictionary as [String: Any]:
100
+ try container.encode(dictionary.mapValues { AnyEncodable($0) })
101
+ default:
102
+ let context = EncodingError.Context(
103
+ codingPath: container.codingPath,
104
+ debugDescription: "Value \(value) cannot be encoded - \(type(of: value)) is not supported by `AnyEncodable`."
105
+ )
106
+ throw EncodingError.invalidValue(value, context)
107
+ }
108
+ }
109
+ }
110
+
111
+ private func encodeNSNumber(_ nsnumber: NSNumber, into container: inout SingleValueEncodingContainer) throws {
112
+ switch CFNumberGetType(nsnumber) {
113
+ case .charType:
114
+ try container.encode(nsnumber.boolValue)
115
+ case .sInt8Type:
116
+ try container.encode(nsnumber.int8Value)
117
+ case .sInt16Type:
118
+ try container.encode(nsnumber.int16Value)
119
+ case .sInt32Type:
120
+ try container.encode(nsnumber.int32Value)
121
+ case .sInt64Type:
122
+ try container.encode(nsnumber.int64Value)
123
+ case .shortType:
124
+ try container.encode(nsnumber.uint16Value)
125
+ case .longType:
126
+ try container.encode(nsnumber.uint32Value)
127
+ case .longLongType:
128
+ try container.encode(nsnumber.uint64Value)
129
+ case .intType, .nsIntegerType, .cfIndexType:
130
+ try container.encode(nsnumber.intValue)
131
+ case .floatType, .float32Type:
132
+ try container.encode(nsnumber.floatValue)
133
+ case .doubleType, .float64Type, .cgFloatType:
134
+ try container.encode(nsnumber.doubleValue)
135
+ @unknown default:
136
+ return
137
+ }
138
+ }