@bugsee/react-native 0.0.0-stage → 7.0.0-beta1

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 (250) hide show
  1. package/BugseeReactNative.podspec +95 -0
  2. package/LICENSE +21 -0
  3. package/README.md +552 -2
  4. package/android/build.gradle +162 -0
  5. package/android/consumer-rules.pro +2 -0
  6. package/android/src/main/AndroidManifest.xml +33 -0
  7. package/android/src/main/java/com/bugsee/reactnative/AppearanceBridge.java +82 -0
  8. package/android/src/main/java/com/bugsee/reactnative/AttributeBridge.java +209 -0
  9. package/android/src/main/java/com/bugsee/reactnative/BreadcrumbFilterBridge.java +443 -0
  10. package/android/src/main/java/com/bugsee/reactnative/BridgeJson.java +168 -0
  11. package/android/src/main/java/com/bugsee/reactnative/BugseeModule.java +1869 -0
  12. package/android/src/main/java/com/bugsee/reactnative/BugseeOptionEnums.java +136 -0
  13. package/android/src/main/java/com/bugsee/reactnative/BugseePackage.java +42 -0
  14. package/android/src/main/java/com/bugsee/reactnative/BugseeReactNativeWrapper.java +200 -0
  15. package/android/src/main/java/com/bugsee/reactnative/BugseeStatusMapper.java +66 -0
  16. package/android/src/main/java/com/bugsee/reactnative/BugseeTokens.java +22 -0
  17. package/android/src/main/java/com/bugsee/reactnative/ConsoleEchoDedup.java +202 -0
  18. package/android/src/main/java/com/bugsee/reactnative/CreatedReports.java +127 -0
  19. package/android/src/main/java/com/bugsee/reactnative/DataRequestBridge.java +375 -0
  20. package/android/src/main/java/com/bugsee/reactnative/ExceptionBridge.java +88 -0
  21. package/android/src/main/java/com/bugsee/reactnative/ExceptionExecutors.java +33 -0
  22. package/android/src/main/java/com/bugsee/reactnative/LogFilterBridge.java +375 -0
  23. package/android/src/main/java/com/bugsee/reactnative/NetworkEvents.java +227 -0
  24. package/android/src/main/java/com/bugsee/reactnative/NetworkFilterBridge.java +498 -0
  25. package/android/src/main/java/com/bugsee/reactnative/PluginEndpoint.java +48 -0
  26. package/android/src/main/java/com/bugsee/reactnative/ReactNativeWebException.java +19 -0
  27. package/android/src/main/java/com/bugsee/reactnative/ReactNativeWrapperInitProvider.java +61 -0
  28. package/android/src/main/java/com/bugsee/reactnative/ReactRootOriginTracker.java +867 -0
  29. package/android/src/main/java/com/bugsee/reactnative/ReportArgs.java +58 -0
  30. package/android/src/main/java/com/bugsee/reactnative/ReportHandlerBridge.java +336 -0
  31. package/android/src/main/java/com/bugsee/reactnative/ReportHandlerDeadlines.java +57 -0
  32. package/android/src/main/java/com/bugsee/reactnative/ReportOps.java +287 -0
  33. package/android/src/main/java/com/bugsee/reactnative/SecureRectanglePulls.java +113 -0
  34. package/android/src/main/java/com/bugsee/reactnative/SecureRectangleStore.java +692 -0
  35. package/android/src/main/java/com/bugsee/reactnative/SpanHandles.java +143 -0
  36. package/android/src/main/java/com/bugsee/reactnative/WrapperChannelHolder.java +137 -0
  37. package/android/src/main/java/com/bugsee/reactnative/WrapperEventBus.java +110 -0
  38. package/android/src/main/java/com/bugsee/reactnative/WrapperRegistrar.java +54 -0
  39. package/app.plugin.js +4 -0
  40. package/ios/BGSRNConsoleCapture.h +52 -0
  41. package/ios/BGSRNConsoleCapture.mm +344 -0
  42. package/ios/BugseeModule.h +25 -0
  43. package/ios/BugseeModule.mm +2728 -0
  44. package/ios/Package.swift +71 -0
  45. package/ios/Support/Package.resolved +14 -0
  46. package/ios/Support/Package.swift +44 -0
  47. package/ios/Support/Sources/BugseeRNSupport/BGSRNAttributes.m +52 -0
  48. package/ios/Support/Sources/BugseeRNSupport/BGSRNCreatedReportOps.m +236 -0
  49. package/ios/Support/Sources/BugseeRNSupport/BGSRNCreatedReports.m +184 -0
  50. package/ios/Support/Sources/BugseeRNSupport/BGSRNDataRequestBridge.m +431 -0
  51. package/ios/Support/Sources/BugseeRNSupport/BGSRNErrorMessage.m +40 -0
  52. package/ios/Support/Sources/BugseeRNSupport/BGSRNEventBus.m +70 -0
  53. package/ios/Support/Sources/BugseeRNSupport/BGSRNExceptions.m +63 -0
  54. package/ios/Support/Sources/BugseeRNSupport/BGSRNGuardedEmit.mm +32 -0
  55. package/ios/Support/Sources/BugseeRNSupport/BGSRNJSON.m +40 -0
  56. package/ios/Support/Sources/BugseeRNSupport/BGSRNLaunchHold.m +147 -0
  57. package/ios/Support/Sources/BugseeRNSupport/BGSRNMainThread.m +23 -0
  58. package/ios/Support/Sources/BugseeRNSupport/BGSRNNetworkEvents.m +159 -0
  59. package/ios/Support/Sources/BugseeRNSupport/BGSRNNetworkFilter.m +155 -0
  60. package/ios/Support/Sources/BugseeRNSupport/BGSRNPluginEndpoint.m +21 -0
  61. package/ios/Support/Sources/BugseeRNSupport/BGSRNReactRootOriginTracker.m +63 -0
  62. package/ios/Support/Sources/BugseeRNSupport/BGSRNReactWindow.m +251 -0
  63. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportArgs.m +47 -0
  64. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportDeadlines.m +8 -0
  65. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportHandlerBridge.m +308 -0
  66. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportOps.m +345 -0
  67. package/ios/Support/Sources/BugseeRNSupport/BGSRNSecureRectangles.m +508 -0
  68. package/ios/Support/Sources/BugseeRNSupport/BGSRNSettleOnce.m +62 -0
  69. package/ios/Support/Sources/BugseeRNSupport/BGSRNSpanHandles.m +136 -0
  70. package/ios/Support/Sources/BugseeRNSupport/BGSRNStatusMapper.m +18 -0
  71. package/ios/Support/Sources/BugseeRNSupport/BGSRNTokens.m +9 -0
  72. package/ios/Support/Sources/BugseeRNSupport/BGSRNValues.m +5 -0
  73. package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapper.m +54 -0
  74. package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapperChannelHolder.m +59 -0
  75. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNAttributes.h +63 -0
  76. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReportOps.h +51 -0
  77. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReports.h +81 -0
  78. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNDataRequestBridge.h +118 -0
  79. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNErrorMessage.h +30 -0
  80. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNEventBus.h +45 -0
  81. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNExceptions.h +31 -0
  82. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNGuardedEmit.h +31 -0
  83. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNJSON.h +25 -0
  84. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNLaunchHold.h +86 -0
  85. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNMainThread.h +32 -0
  86. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNNetworkEvents.h +28 -0
  87. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNNetworkFilter.h +28 -0
  88. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNPluginEndpoint.h +17 -0
  89. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReactRootOriginTracker.h +48 -0
  90. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReactWindow.h +135 -0
  91. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportArgs.h +17 -0
  92. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportDeadlines.h +37 -0
  93. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportHandlerBridge.h +94 -0
  94. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportOps.h +86 -0
  95. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSecureRectangles.h +198 -0
  96. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSettleOnce.h +25 -0
  97. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSpanHandles.h +49 -0
  98. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNStatusMapper.h +26 -0
  99. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNTokens.h +14 -0
  100. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNValues.h +18 -0
  101. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapper.h +40 -0
  102. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapperChannelHolder.h +74 -0
  103. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNAttributesTests.m +194 -0
  104. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNCreatedReportOpsTests.m +274 -0
  105. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNCreatedReportsTests.m +240 -0
  106. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNDataRequestBridgeTests.m +685 -0
  107. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNErrorMessageTests.m +102 -0
  108. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNEventBusTests.m +136 -0
  109. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNExceptionsTests.m +68 -0
  110. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.h +41 -0
  111. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.m +166 -0
  112. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNGuardedEmitTests.mm +85 -0
  113. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNJSONTests.m +281 -0
  114. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNLaunchHoldTests.m +230 -0
  115. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNMainThreadTests.m +97 -0
  116. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNNetworkEventsTests.m +99 -0
  117. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNNetworkFilterTests.m +134 -0
  118. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNPluginEndpointTests.m +38 -0
  119. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReactRootOriginTrackerTests.m +210 -0
  120. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReactWindowTests.m +548 -0
  121. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportArgsTests.m +51 -0
  122. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportDeadlinesTests.m +22 -0
  123. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportHandlerBridgeTests.m +382 -0
  124. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportOpsTests.m +295 -0
  125. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSecureRectanglesTests.m +568 -0
  126. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSettleOnceTests.m +134 -0
  127. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSpanHandlesTests.m +153 -0
  128. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNStatusMapperTests.m +81 -0
  129. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNValuesTests.m +32 -0
  130. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperChannelHolderTests.m +205 -0
  131. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperTests.m +66 -0
  132. package/ios/react-native-spm-prefix.h +10 -0
  133. package/native-versions.json +10 -0
  134. package/package.json +62 -3
  135. package/plugin/build/bundle-phase.d.ts +41 -0
  136. package/plugin/build/bundle-phase.js +116 -0
  137. package/plugin/build/bundle-phase.js.map +1 -0
  138. package/plugin/build/dsym-script.d.ts +32 -0
  139. package/plugin/build/dsym-script.js +159 -0
  140. package/plugin/build/dsym-script.js.map +1 -0
  141. package/plugin/build/endpoint.d.ts +30 -0
  142. package/plugin/build/endpoint.js +69 -0
  143. package/plugin/build/endpoint.js.map +1 -0
  144. package/plugin/build/gradle.d.ts +65 -0
  145. package/plugin/build/gradle.js +1066 -0
  146. package/plugin/build/gradle.js.map +1 -0
  147. package/plugin/build/index.d.ts +85 -0
  148. package/plugin/build/index.js +217 -0
  149. package/plugin/build/index.js.map +1 -0
  150. package/plugin/build/manifest.d.ts +48 -0
  151. package/plugin/build/manifest.js +69 -0
  152. package/plugin/build/manifest.js.map +1 -0
  153. package/plugin/build/native-versions.baked.json +4 -0
  154. package/plugin/build/native-versions.d.ts +10 -0
  155. package/plugin/build/native-versions.js +27 -0
  156. package/plugin/build/native-versions.js.map +1 -0
  157. package/plugin/build/pbx-string.d.ts +8 -0
  158. package/plugin/build/pbx-string.js +31 -0
  159. package/plugin/build/pbx-string.js.map +1 -0
  160. package/plugin/build/podfile.d.ts +3 -0
  161. package/plugin/build/podfile.js +28 -0
  162. package/plugin/build/podfile.js.map +1 -0
  163. package/plugin/build/properties.d.ts +19 -0
  164. package/plugin/build/properties.js +41 -0
  165. package/plugin/build/properties.js.map +1 -0
  166. package/plugin/build/scheme.d.ts +20 -0
  167. package/plugin/build/scheme.js +107 -0
  168. package/plugin/build/scheme.js.map +1 -0
  169. package/react-native.config.js +26 -0
  170. package/scripts/bugsee-sourcemaps.gradle +201 -0
  171. package/scripts/bugsee-xcode.sh +83 -0
  172. package/scripts/compose-then-inject.js +113 -0
  173. package/scripts/hermes-sourcemaps.js +634 -0
  174. package/scripts/hermesc-preserve-js.cmd +6 -0
  175. package/scripts/hermesc-preserve-js.js +200 -0
  176. package/scripts/hermesc-preserve-js.sh +7 -0
  177. package/src/NativeBugsee.ts +622 -0
  178. package/src/apm/notify.ts +58 -0
  179. package/src/apm/span.ts +403 -0
  180. package/src/apm/types.ts +57 -0
  181. package/src/appearance/report.ts +171 -0
  182. package/src/architecture.ts +48 -0
  183. package/src/attributes/errors.ts +39 -0
  184. package/src/attributes/validate.ts +150 -0
  185. package/src/breadcrumbs/add.ts +58 -0
  186. package/src/breadcrumbs/data.ts +33 -0
  187. package/src/breadcrumbs/filter.ts +215 -0
  188. package/src/breadcrumbs/types.ts +64 -0
  189. package/src/bridge/json.ts +110 -0
  190. package/src/console/capture.ts +180 -0
  191. package/src/console/dedup.ts +175 -0
  192. package/src/data/validate.ts +259 -0
  193. package/src/errorName.ts +32 -0
  194. package/src/exceptions/ErrorBoundary.tsx +92 -0
  195. package/src/exceptions/RootErrorReporter.tsx +61 -0
  196. package/src/exceptions/debugIds.ts +118 -0
  197. package/src/exceptions/handlers.ts +236 -0
  198. package/src/exceptions/options.ts +85 -0
  199. package/src/exceptions/payload.ts +456 -0
  200. package/src/exceptions/report.ts +170 -0
  201. package/src/exceptions/sha1.ts +148 -0
  202. package/src/exceptions/stack.ts +355 -0
  203. package/src/index.ts +944 -0
  204. package/src/logs/filter.ts +90 -0
  205. package/src/logs/log.ts +36 -0
  206. package/src/network/add.ts +165 -0
  207. package/src/network/filter.ts +198 -0
  208. package/src/options/AndroidLaunchOptions.ts +78 -0
  209. package/src/options/BugseeLaunchOptions.ts +190 -0
  210. package/src/options/IOSLaunchOptions.ts +57 -0
  211. package/src/options/android-options-manifest.json +646 -0
  212. package/src/options/createDefaultLaunchOptions.ts +29 -0
  213. package/src/options/endpoint.ts +58 -0
  214. package/src/options/enums.ts +104 -0
  215. package/src/options/ios-enums.json +19 -0
  216. package/src/options/keys.ts +39 -0
  217. package/src/options/option-enums.json +34 -0
  218. package/src/options/option-keys.json +107 -0
  219. package/src/report/BugseeReport.ts +264 -0
  220. package/src/report/CreatedReport.ts +127 -0
  221. package/src/report/dispatcher.ts +95 -0
  222. package/src/report/errors.ts +48 -0
  223. package/src/report/fields.ts +40 -0
  224. package/src/report/types.ts +157 -0
  225. package/src/report/validate.ts +197 -0
  226. package/src/secure/BugseeSecure.tsx +202 -0
  227. package/src/secure/ancestry.ts +622 -0
  228. package/src/secure/drivers.ts +342 -0
  229. package/src/secure/followEvents.ts +57 -0
  230. package/src/secure/hold.ts +99 -0
  231. package/src/secure/measureLoop.ts +323 -0
  232. package/src/secure/nativeAnimation.ts +1005 -0
  233. package/src/secure/rectangles.ts +93 -0
  234. package/src/secure/registry.ts +237 -0
  235. package/src/secure/surface.ts +83 -0
  236. package/src/secure/tracker.ts +723 -0
  237. package/src/secure/transition.ts +488 -0
  238. package/src/secure/unit.ts +22 -0
  239. package/src/status.ts +16 -0
  240. package/src/version.ts +9 -0
  241. package/src/viewtree/anchor.tsx +105 -0
  242. package/src/viewtree/constants.ts +20 -0
  243. package/src/viewtree/fiber.ts +251 -0
  244. package/src/viewtree/requests.ts +347 -0
  245. package/src/viewtree/walk.ts +902 -0
  246. package/src/wrapper/channel.ts +33 -0
  247. package/src/wrapper/collect.ts +52 -0
  248. package/src/wrapper/events.ts +45 -0
  249. package/src/wrapper/identity.ts +54 -0
  250. package/src/wrapper/lifecycle-events.ts +89 -0
@@ -0,0 +1,58 @@
1
+ package com.bugsee.reactnative;
2
+
3
+ import android.util.Log;
4
+
5
+ import androidx.annotation.Nullable;
6
+
7
+ import com.bugsee.library.contracts.options.IssueSeverity;
8
+
9
+ import java.util.ArrayList;
10
+ import java.util.List;
11
+
12
+ /**
13
+ * Turns the numbers and lists JS sends for a manual report into the types
14
+ * the Android SDK expects.
15
+ *
16
+ * <p>Severity is the enum's internal value, never its ordinal.
17
+ * {@code IssueSeverity.fromIntValue} maps anything outside 1..5 to
18
+ * {@code VeryLow}, so the range is checked here first: {@code 0} and anything
19
+ * else become null and the SDK applies its own default. Only a value outside
20
+ * that sentinel is logged; {@code 0} is the wire's way of saying "default".
21
+ */
22
+ final class ReportArgs {
23
+
24
+ private static final String TAG = "BugseeRN";
25
+
26
+ private ReportArgs() {
27
+ }
28
+
29
+ /** 0 and anything outside 1..5 are null (the SDK default). 1..5 map by value. */
30
+ @Nullable
31
+ static IssueSeverity severity(final int wire) {
32
+ if (wire == 0) {
33
+ return null;
34
+ }
35
+ if (wire >= 1 && wire <= 5) {
36
+ return IssueSeverity.fromIntValue(wire);
37
+ }
38
+ // Never the value itself: a severity is app data, and this line reaches the
39
+ // SDK's own log capture.
40
+ Log.w(TAG, "severity is outside 1..5; the SDK default applies");
41
+ return null;
42
+ }
43
+
44
+ /** null stays null. Anything that is not a string is dropped. */
45
+ @Nullable
46
+ static ArrayList<String> labels(@Nullable final List<?> wire) {
47
+ if (wire == null) {
48
+ return null;
49
+ }
50
+ final ArrayList<String> result = new ArrayList<>();
51
+ for (final Object item : wire) {
52
+ if (item instanceof String) {
53
+ result.add((String) item);
54
+ }
55
+ }
56
+ return result;
57
+ }
58
+ }
@@ -0,0 +1,336 @@
1
+ package com.bugsee.reactnative;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import androidx.annotation.Nullable;
5
+
6
+ import android.util.Log;
7
+
8
+ import com.bugsee.library.Bugsee;
9
+ import com.bugsee.library.contracts.options.Options;
10
+ import com.bugsee.library.contracts.reporting.Report;
11
+
12
+ import java.util.Map;
13
+ import java.util.concurrent.ConcurrentHashMap;
14
+ import java.util.concurrent.ScheduledFuture;
15
+ import java.util.concurrent.ScheduledThreadPoolExecutor;
16
+ import java.util.concurrent.TimeUnit;
17
+ import java.util.concurrent.atomic.AtomicBoolean;
18
+ import java.util.concurrent.atomic.AtomicLong;
19
+ import java.util.concurrent.atomic.AtomicReference;
20
+
21
+ /**
22
+ * Routes the SDK's report callbacks to JS and back, through a table of
23
+ * opaque handles.
24
+ *
25
+ * <p>Every dispatch ends in exactly one run of the SDK's completion, whichever
26
+ * gets there first: JS completing the handle, the handle's deadline, the
27
+ * bridge detaching (a reload), or a failure to reach JS at all. The guard is
28
+ * one {@link AtomicBoolean} per handle; everything else -- the table, the
29
+ * timer -- is bookkeeping that may race freely behind it.
30
+ *
31
+ * <p>On Android only reports dispatched on the SDK's live
32
+ * {@code BugseeReportHandlerThread} reach JS; every other non-terminating
33
+ * dispatch is early-crash recovery, which the SDK runs on a spawned bounded
34
+ * thread with a no-op completion and lets proceed as soon as the handler
35
+ * returns. JS attached then has just called {@code launch} on the very
36
+ * native-modules thread its report ops queue behind, so it could never act
37
+ * before the report moves on; those complete natively at once instead.
38
+ *
39
+ * <p>Like {@link WrapperEventBus}, this outlives any one React instance: the
40
+ * wrapper that calls in is registered at process start, while the module that
41
+ * receives comes and goes with the JS runtime.
42
+ */
43
+ final class ReportHandlerBridge {
44
+
45
+ private static final String TAG = "BugseeRN";
46
+
47
+ enum Phase {
48
+ BEFORE("before"),
49
+ AFTER("after");
50
+
51
+ final String wire;
52
+
53
+ Phase(final String wire) {
54
+ this.wire = wire;
55
+ }
56
+ }
57
+
58
+ /** What the module implements to emit {@code onReportHandlerRequest}. */
59
+ interface Sink {
60
+ void onReportHandlerRequest(
61
+ String handleId,
62
+ String phase,
63
+ String reportId,
64
+ String type,
65
+ double deadlineMs);
66
+ }
67
+
68
+ interface Cancellable {
69
+ void cancel();
70
+ }
71
+
72
+ /** Arms a handle's deadline. Injectable so tests control time. */
73
+ interface Scheduler {
74
+ Cancellable schedule(Runnable task, long delayMs);
75
+ }
76
+
77
+ /** Its own interface: {@code java.util.function} needs API 24, minSdk is 21. */
78
+ interface LiveDeadlineSource {
79
+ long liveMs();
80
+ }
81
+
82
+ /**
83
+ * Where the one-line outcome of every dispatch goes. Logcat in production,
84
+ * where the device tests match these lines; injectable so the JVM tests
85
+ * can assert them too.
86
+ */
87
+ interface OutcomeLog {
88
+ void line(String message);
89
+ }
90
+
91
+ private static final class Handle {
92
+ final String id;
93
+ final Report report;
94
+ final Runnable sdkCompletion;
95
+ /** The sink this handle was emitted to; its detach completes it. */
96
+ final Sink owner;
97
+ /** The exactly-once guard. Whoever flips it runs the completion. */
98
+ final AtomicBoolean done = new AtomicBoolean();
99
+ volatile Cancellable timer;
100
+
101
+ Handle(final String id, final Report report, final Runnable sdkCompletion, final Sink owner) {
102
+ this.id = id;
103
+ this.report = report;
104
+ this.sdkCompletion = sdkCompletion;
105
+ this.owner = owner;
106
+ }
107
+ }
108
+
109
+ private static final ReportHandlerBridge SHARED =
110
+ new ReportHandlerBridge(new DaemonScheduler(), ReportHandlerBridge::liveMsFromLaunchOptions);
111
+
112
+ @NonNull
113
+ static ReportHandlerBridge shared() {
114
+ return SHARED;
115
+ }
116
+
117
+ private final Scheduler scheduler;
118
+ private final LiveDeadlineSource live;
119
+ private final OutcomeLog outcomes;
120
+ private final AtomicReference<Sink> sink = new AtomicReference<>();
121
+ private final Map<String, Handle> handles = new ConcurrentHashMap<>();
122
+ private final AtomicLong counter = new AtomicLong();
123
+ private volatile boolean beforeRegistered;
124
+ private volatile boolean afterRegistered;
125
+
126
+ ReportHandlerBridge(@NonNull final Scheduler scheduler, @NonNull final LiveDeadlineSource live) {
127
+ this(scheduler, live, message -> Log.i(TAG, message));
128
+ }
129
+
130
+ ReportHandlerBridge(
131
+ @NonNull final Scheduler scheduler,
132
+ @NonNull final LiveDeadlineSource live,
133
+ @NonNull final OutcomeLog outcomes
134
+ ) {
135
+ this.scheduler = scheduler;
136
+ this.live = live;
137
+ this.outcomes = outcomes;
138
+ }
139
+
140
+ void setPhases(final boolean before, final boolean after) {
141
+ beforeRegistered = before;
142
+ afterRegistered = after;
143
+ }
144
+
145
+ /**
146
+ * Attaches the module of a new JS runtime, which has registered no phases
147
+ * yet. Until it does, dispatches complete at once rather than emit to a
148
+ * runtime with no listener, where they would sit until the deadline.
149
+ */
150
+ void attach(@NonNull final Sink newSink) {
151
+ sink.set(newSink);
152
+ setPhases(false, false);
153
+ }
154
+
155
+ /**
156
+ * Completes every handle emitted to {@code stale}: nothing in any other JS
157
+ * runtime knows them, so waiting for their deadlines would only delay the
158
+ * reports. Clears the sink and the phases only if {@code stale} is still
159
+ * the attached one -- a fast reload attaches the new module before the old
160
+ * one is invalidated, and an unconditional clear would silence it.
161
+ */
162
+ void detach(@NonNull final Sink stale) {
163
+ if (sink.compareAndSet(stale, null)) {
164
+ setPhases(false, false);
165
+ }
166
+ for (final Handle handle : handles.values()) {
167
+ if (handle.owner == stale) {
168
+ finish(handle, "detach");
169
+ }
170
+ }
171
+ }
172
+
173
+ void dispatch(
174
+ @NonNull final Phase phase,
175
+ @NonNull final Report report,
176
+ final boolean isTerminating,
177
+ @NonNull final Runnable sdkCompletion
178
+ ) {
179
+ // The process dies when this returns; nothing asynchronous survives.
180
+ if (isTerminating) {
181
+ completeUnminted(phase, report, sdkCompletion, "terminating");
182
+ return;
183
+ }
184
+ // Early-crash recovery: the SDK will not wait for JS. See the class doc.
185
+ if (!ReportHandlerDeadlines.isLive(Thread.currentThread().getName())) {
186
+ completeUnminted(phase, report, sdkCompletion, "recovery");
187
+ return;
188
+ }
189
+ final Sink target = sink.get();
190
+ final boolean registered = phase == Phase.BEFORE ? beforeRegistered : afterRegistered;
191
+ if (target == null || !registered) {
192
+ completeUnminted(phase, report, sdkCompletion, "no-handler");
193
+ return;
194
+ }
195
+ final long deadlineMs = live.liveMs();
196
+ if (deadlineMs < ReportHandlerDeadlines.MIN_USEFUL_DEADLINE_MS) {
197
+ completeUnminted(phase, report, sdkCompletion, "no-handler");
198
+ return;
199
+ }
200
+
201
+ final Handle handle = new Handle(
202
+ "rh-" + counter.incrementAndGet(), report, sdkCompletion, target);
203
+ handles.put(handle.id, handle);
204
+ // A detach that ran after the sink read above swept the table before
205
+ // this handle was in it. detach() clears the sink before it sweeps, so
206
+ // whichever side comes second catches the handle: its sweep sees the
207
+ // put, or this sees the cleared (or replaced) sink.
208
+ if (sink.get() != target) {
209
+ finish(handle, "detach");
210
+ return;
211
+ }
212
+ try {
213
+ // The task holds the handle itself, not its id: a deadline that
214
+ // was already running when JS completed must still hit the guard.
215
+ final Cancellable timer = scheduler.schedule(() -> finish(handle, "deadline"), deadlineMs);
216
+ handle.timer = timer;
217
+ if (handle.done.get()) {
218
+ // Finished between put and here; don't leave the timer armed.
219
+ cancelQuietly(timer);
220
+ }
221
+ outcomes.line("report handler " + handle.id + " phase=" + phase.wire
222
+ + " deadline=" + deadlineMs);
223
+ target.onReportHandlerRequest(
224
+ handle.id, phase.wire, report.getId(), ReportOps.typeOf(report), deadlineMs);
225
+ } catch (final Throwable e) {
226
+ // A dead bridge throws from the emit, on the SDK's thread. JS will
227
+ // never answer, so answer for it now.
228
+ Log.w(TAG, "report handler " + handle.id + " could not reach JS: " + e.getClass().getName());
229
+ finish(handle, "no-handler");
230
+ }
231
+ }
232
+
233
+ /** @return true only for the call that ran the SDK completion. */
234
+ boolean complete(@NonNull final String handleId) {
235
+ final Handle handle = handles.get(handleId);
236
+ return handle != null && finish(handle, "js");
237
+ }
238
+
239
+ /** The live report behind {@code handleId}, or null once it is completed. */
240
+ @Nullable
241
+ Report reportFor(@NonNull final String handleId) {
242
+ final Handle handle = handles.get(handleId);
243
+ return handle == null || handle.done.get() ? null : handle.report;
244
+ }
245
+
246
+ private boolean finish(@NonNull final Handle handle, @NonNull final String by) {
247
+ if (!handle.done.compareAndSet(false, true)) {
248
+ return false;
249
+ }
250
+ handles.remove(handle.id, handle);
251
+ final Cancellable timer = handle.timer;
252
+ if (timer != null) {
253
+ cancelQuietly(timer);
254
+ }
255
+ outcomes.line("report handler " + handle.id + " completed by=" + by);
256
+ runQuietly(handle.sdkCompletion);
257
+ return true;
258
+ }
259
+
260
+ /** No handle was minted; the line still carries the report for correlation. */
261
+ private void completeUnminted(
262
+ @NonNull final Phase phase,
263
+ @NonNull final Report report,
264
+ @NonNull final Runnable sdkCompletion,
265
+ @NonNull final String by
266
+ ) {
267
+ outcomes.line("report handler - completed by=" + by + " phase=" + phase.wire
268
+ + " report=" + safeId(report));
269
+ runQuietly(sdkCompletion);
270
+ }
271
+
272
+ /** The SDK's own fault must not escape into JS's thread or the timer's. */
273
+ private static void runQuietly(@NonNull final Runnable sdkCompletion) {
274
+ try {
275
+ sdkCompletion.run();
276
+ } catch (final Throwable e) {
277
+ Log.w(TAG, "report handler completion threw: " + e.getClass().getName());
278
+ }
279
+ }
280
+
281
+ private static void cancelQuietly(@NonNull final Cancellable timer) {
282
+ try {
283
+ timer.cancel();
284
+ } catch (final Throwable e) {
285
+ Log.w(TAG, "report handler timer cancel threw: " + e.getClass().getName());
286
+ }
287
+ }
288
+
289
+ @Nullable
290
+ private static String safeId(@NonNull final Report report) {
291
+ try {
292
+ return report.getId();
293
+ } catch (final Throwable e) {
294
+ return null;
295
+ }
296
+ }
297
+
298
+ /**
299
+ * The {@code ReportHandlerCallbackTimeout} option in effect, read at each
300
+ * dispatch because a relaunch can change it. Any failure reads as
301
+ * "unknown", which is the default deadline.
302
+ */
303
+ private static long liveMsFromLaunchOptions() {
304
+ Integer seconds = null;
305
+ try {
306
+ final Object value = Bugsee.getLaunchOptions().getOption(Options.ReportHandlerCallbackTimeout);
307
+ if (value instanceof Number) {
308
+ seconds = ((Number) value).intValue();
309
+ }
310
+ } catch (final Throwable e) {
311
+ Log.w(TAG, "could not read the report handler timeout option: " + e.getClass().getName());
312
+ }
313
+ return ReportHandlerDeadlines.liveMs(seconds);
314
+ }
315
+
316
+ /** One daemon thread, started on first use, for every handle's deadline. */
317
+ private static final class DaemonScheduler implements Scheduler {
318
+ private final ScheduledThreadPoolExecutor executor;
319
+
320
+ DaemonScheduler() {
321
+ executor = new ScheduledThreadPoolExecutor(1, runnable -> {
322
+ final Thread thread = new Thread(runnable, "BugseeRN-ReportDeadline");
323
+ thread.setDaemon(true);
324
+ return thread;
325
+ });
326
+ // A handle completed by JS should not leave 25 s of dead task queued.
327
+ executor.setRemoveOnCancelPolicy(true);
328
+ }
329
+
330
+ @Override
331
+ public Cancellable schedule(final Runnable task, final long delayMs) {
332
+ final ScheduledFuture<?> future = executor.schedule(task, delayMs, TimeUnit.MILLISECONDS);
333
+ return () -> future.cancel(false);
334
+ }
335
+ }
336
+ }
@@ -0,0 +1,57 @@
1
+ package com.bugsee.reactnative;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import androidx.annotation.Nullable;
5
+
6
+ /**
7
+ * How long JS gets to handle one report callback, and whether it gets one.
8
+ *
9
+ * <p>Only the SDK's live dispatch thread reaches JS at all (see
10
+ * {@link ReportHandlerBridge}). There the deadline sits INSIDE the SDK's
11
+ * per-handler cap: past that cap the SDK proceeds without us, so a JS
12
+ * deadline that overshoots it is not generous -- it lets JS keep writing to a
13
+ * report that has already moved on.
14
+ */
15
+ final class ReportHandlerDeadlines {
16
+
17
+ /** 5 s inside the SDK's default 30 s per-handler cap. */
18
+ static final long LIVE_DEADLINE_MS = 25_000L;
19
+
20
+ /**
21
+ * Below this a round trip to JS and back cannot reliably fit, so the
22
+ * handler is completed at once instead of racing a deadline it will lose.
23
+ */
24
+ static final long MIN_USEFUL_DEADLINE_MS = 1_000L;
25
+
26
+ /**
27
+ * The SDK's dedicated dispatch thread, and the ONLY thread the healthy,
28
+ * long-cap path runs on (it keeps this name when the SDK recycles it).
29
+ * Matched by name because the SDK tells a handler nothing else about which
30
+ * path it is on.
31
+ */
32
+ static final String LIVE_HANDLER_THREAD = "BugseeReportHandlerThread";
33
+
34
+ private ReportHandlerDeadlines() {
35
+ }
36
+
37
+ /**
38
+ * The live deadline for the {@code ReportHandlerCallbackTimeout} option, in
39
+ * seconds.
40
+ *
41
+ * <p>{@code null} (unreadable) and {@code 0} (per-handler timer disabled;
42
+ * the SDK's 60 s chain cap still applies) both mean the default. The SDK
43
+ * normalises a negative value to {@code 0}, and so does this. Otherwise a
44
+ * second under the option, never more than the default.
45
+ */
46
+ static long liveMs(@Nullable final Integer optionSeconds) {
47
+ if (optionSeconds == null || optionSeconds <= 0) {
48
+ return LIVE_DEADLINE_MS;
49
+ }
50
+ return Math.min(LIVE_DEADLINE_MS, optionSeconds * 1000L - 1000L);
51
+ }
52
+
53
+ /** Whether {@code threadName} is the SDK's live dispatch thread. */
54
+ static boolean isLive(@NonNull final String threadName) {
55
+ return LIVE_HANDLER_THREAD.equals(threadName);
56
+ }
57
+ }