@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,93 @@
1
+ /**
2
+ * A region the SDK must not record, in React Native's own coordinate shape —
3
+ * what `measureInWindow` hands back, so a caller can pass it straight through.
4
+ */
5
+ export interface SecureRectangle {
6
+ x: number;
7
+ y: number;
8
+ width: number;
9
+ height: number;
10
+ }
11
+
12
+ /** The wire format is int32, and a coordinate past it wraps to a negative. */
13
+ const INT32_MIN = -2_147_483_648;
14
+ const INT32_MAX = 2_147_483_647;
15
+
16
+ /**
17
+ * Flattens rectangles into the `[left, top, right, bottom, ...]` list both
18
+ * SDKs pack into their pulled buffer.
19
+ *
20
+ * Rounding goes OUTWARDS rather than to nearest. React Native measurements are
21
+ * floats on any device with a non-integer pixel ratio, the buffer is int32, and
22
+ * a redaction that falls a fraction short leaves a strip along an edge visible
23
+ * — which is where text sits. Covering at most a pixel too much is the safe
24
+ * direction to be wrong in.
25
+ *
26
+ * Throws rather than skipping a malformed rectangle: every silent reading of
27
+ * one is a region the caller believes is redacted and is not.
28
+ *
29
+ * `scale` converts React Native's density-independent points into the unit the
30
+ * target SDK takes. The two differ, and the iOS header says so: "Coordinates
31
+ * are in points in the screen's coordinate space ... (Android uses pixels)".
32
+ * So iOS passes 1 and Android passes the device pixel ratio. Publishing
33
+ * unconverted points on a 3x Android device redacts a third of the intended
34
+ * area -- a leak, in the direction that still looks like it is working.
35
+ */
36
+ export function flattenSecureRectangles(
37
+ rectangles: readonly SecureRectangle[],
38
+ scale: number = 1,
39
+ ): number[] {
40
+ if (!Number.isFinite(scale) || scale <= 0) {
41
+ throw new RangeError(
42
+ 'secure rectangle scale must be a positive, finite number; ' +
43
+ 'a wrong scale redacts the wrong region while looking like it works',
44
+ );
45
+ }
46
+
47
+ const flat: number[] = [];
48
+
49
+ rectangles.forEach((rectangle, index) => {
50
+ const { x, y, width, height } = rectangle;
51
+
52
+ for (const [name, value] of Object.entries({ x, y, width, height })) {
53
+ if (!Number.isFinite(value)) {
54
+ throw new RangeError(`secure rectangle [${index}] has a non-finite ${name}`);
55
+ }
56
+ }
57
+
58
+ if (width < 0 || height < 0) {
59
+ throw new RangeError(
60
+ `secure rectangle [${index}] has a negative extent; ` +
61
+ 'a mirrored region would redact the wrong pixels',
62
+ );
63
+ }
64
+
65
+ // Scale BEFORE rounding. Rounding in points and scaling afterwards
66
+ // reintroduces fractional pixel edges, which is the same sub-pixel gap the
67
+ // outward rounding exists to close.
68
+ const left = Math.floor(x * scale);
69
+ const top = Math.floor(y * scale);
70
+ const right = Math.ceil((x + width) * scale);
71
+ const bottom = Math.ceil((y + height) * scale);
72
+
73
+ // After rounding, not before: a sub-pixel sliver rounds out to a real one
74
+ // and should still be redacted, while a genuinely empty region should not
75
+ // churn the version on every frame it is laid out.
76
+ if (right <= left || bottom <= top) {
77
+ return;
78
+ }
79
+
80
+ for (const [name, value] of Object.entries({ left, top, right, bottom })) {
81
+ if (value < INT32_MIN || value > INT32_MAX) {
82
+ throw new RangeError(
83
+ `secure rectangle [${index}] has a ${name} that does not fit int32 ` +
84
+ 'and would wrap to a region elsewhere',
85
+ );
86
+ }
87
+ }
88
+
89
+ flat.push(left, top, right, bottom);
90
+ });
91
+
92
+ return flat;
93
+ }
@@ -0,0 +1,237 @@
1
+ import NativeBugsee from '../NativeBugsee';
2
+ import { errorName } from '../errorName';
3
+ import { flattenSecureRectangles } from './rectangles';
4
+ import type { SecureRectangle } from './rectangles';
5
+ import { MAIN_SURFACE } from './surface';
6
+ import { secureRectangleScale } from './unit';
7
+
8
+ /**
9
+ * Who published a set of secure rectangles.
10
+ *
11
+ * `Bugsee.setSecureRectangles` owns `manual:<display>`, one per display, and
12
+ * each mounted `<BugseeSecure>` owns a token object of its own. Keeping them
13
+ * apart is the point: a component unmounting must never clear what the app set
14
+ * by hand, and a manual clear must never uncover a mounted component.
15
+ */
16
+ export type SecureOwner = object | `manual:${number}`;
17
+
18
+ export { MAIN_SURFACE };
19
+
20
+ interface OwnerEntry {
21
+ readonly display: number;
22
+ readonly surface: number;
23
+ readonly rectangles: readonly SecureRectangle[];
24
+ }
25
+
26
+ /** Insertion-ordered, so the published union is stable across publishes. */
27
+ const owners = new Map<SecureOwner, OwnerEntry>();
28
+
29
+ /**
30
+ * What last reached the bridge, per display and surface. A missing entry has
31
+ * never been published by this JS runtime, so its first publish always
32
+ * crosses, even an empty one: the native store outlives a JS reload and may
33
+ * still hold a set this runtime knows nothing about.
34
+ */
35
+ const published = new Map<string, readonly number[]>();
36
+
37
+ function publishKey(display: number, surface: number): string {
38
+ return `${display}:${surface}`;
39
+ }
40
+
41
+ /**
42
+ * The scale each lane's union was last flattened at, for as long as native
43
+ * holds that union. Absent while a lane's last publish has not reached the
44
+ * bridge, so nothing on it is skipped until one does.
45
+ */
46
+ const laneScale = new Map<string, number>();
47
+
48
+ function sameList(a: readonly number[], b: readonly number[]): boolean {
49
+ return a.length === b.length && a.every((value, index) => value === b[index]);
50
+ }
51
+
52
+ function sameRectangles(a: readonly SecureRectangle[], b: readonly SecureRectangle[]): boolean {
53
+ if (a.length !== b.length) {
54
+ return false;
55
+ }
56
+ for (let index = 0; index < a.length; index += 1) {
57
+ const [p, q] = [a[index]!, b[index]!];
58
+ if (p.x !== q.x || p.y !== q.y || p.width !== q.width || p.height !== q.height) {
59
+ return false;
60
+ }
61
+ }
62
+ return true;
63
+ }
64
+
65
+ /**
66
+ * Publishes the union of every owner's rectangles on `display` for each
67
+ * surface that has (or had) owners there.
68
+ *
69
+ * Re-flattened every time rather than cached per owner: the pixel ratio is
70
+ * read per publish (see `secureRectangleScale`). A ratio change reaches each
71
+ * lane the next time any owner on it publishes: an owner that has not moved
72
+ * skips only while its lane was flattened at the current ratio.
73
+ *
74
+ * Skipping an unchanged union costs nothing observable: both native stores
75
+ * already keep their version still for a no-op write. It saves a bridge hop
76
+ * on every 100 ms re-measure that found nothing moved.
77
+ */
78
+ function publish(display: number, surfaces: ReadonlySet<number>): void {
79
+ const scale = secureRectangleScale();
80
+ for (const surface of surfaces) {
81
+ const union: SecureRectangle[] = [];
82
+ for (const entry of owners.values()) {
83
+ if (entry.display === display && entry.surface === surface) {
84
+ union.push(...entry.rectangles);
85
+ }
86
+ }
87
+
88
+ const flat = flattenSecureRectangles(union, scale);
89
+ const key = publishKey(display, surface);
90
+ const last = published.get(key);
91
+ if (last !== undefined && sameList(last, flat)) {
92
+ laneScale.set(key, scale);
93
+ continue;
94
+ }
95
+
96
+ laneScale.delete(key);
97
+ if (surface === MAIN_SURFACE) {
98
+ NativeBugsee.setSecureRectangles(display, flat);
99
+ } else {
100
+ NativeBugsee.setSecureRectanglesOnSurface(display, surface, flat);
101
+ }
102
+ // Only after the call returned: a publish that threw was not published, and
103
+ // recording it would skip the retry as "unchanged".
104
+ published.set(key, flat);
105
+ laneScale.set(key, scale);
106
+ }
107
+ }
108
+
109
+ /**
110
+ * Replaces `owner`'s rectangles with `rectangles` on `display`/`surface` and
111
+ * publishes that lane's union.
112
+ *
113
+ * Throws, changing nothing, if any rectangle is malformed. The owner's
114
+ * previous rectangles stay published: a bad call never uncovers a region.
115
+ *
116
+ * An owner republishing exactly what it holds, on the same lane, costs a
117
+ * comparison and nothing else (every mounted `<BugseeSecure>` does so every
118
+ * re-measure it has not moved), unless the lane's last publish failed or the
119
+ * pixel ratio changed since.
120
+ */
121
+ export function setOwnerRectangles(
122
+ owner: SecureOwner,
123
+ display: number,
124
+ rectangles: readonly SecureRectangle[],
125
+ surface: number = MAIN_SURFACE,
126
+ ): void {
127
+ const previous = owners.get(owner);
128
+ if (
129
+ previous !== undefined &&
130
+ previous.display === display &&
131
+ previous.surface === surface &&
132
+ laneScale.get(publishKey(display, surface)) === secureRectangleScale() &&
133
+ sameRectangles(previous.rectangles, rectangles)
134
+ ) {
135
+ return;
136
+ }
137
+
138
+ // Validation first, before the entry changes.
139
+ flattenSecureRectangles(rectangles, secureRectangleScale());
140
+
141
+ // A copy: the caller keeps its array and objects, and an edit through them
142
+ // must not change what a later publish for some other owner sends.
143
+ owners.set(owner, {
144
+ display,
145
+ surface,
146
+ rectangles: rectangles.map(({ x, y, width, height }) => ({ x, y, width, height })),
147
+ });
148
+
149
+ const surfaces = new Set<number>([surface]);
150
+ if (previous !== undefined) {
151
+ surfaces.add(previous.surface);
152
+ }
153
+ // The new display is covered before the old one is uncovered.
154
+ publishOrDefer(display, surfaces);
155
+ if (previous !== undefined && previous.display !== display) {
156
+ publishOrDefer(previous.display, new Set([previous.surface]));
157
+ }
158
+ }
159
+
160
+ /** The lanes changed inside {@link publishTogether}, by display, in the order they changed. */
161
+ let deferred: Map<number, Set<number>> | undefined;
162
+
163
+ /**
164
+ * Lanes whose last publish inside {@link publishTogether} threw: the next
165
+ * call that retries publishes them again, whether or not anything on them
166
+ * changed (an owner that has not moved never asks again).
167
+ */
168
+ let failed = new Map<number, Set<number>>();
169
+
170
+ /** Lanes warned about since they last published: one warning per lane per outage. */
171
+ const warned = new Set<string>();
172
+
173
+ function addLanes(into: Map<number, Set<number>>, display: number, surfaces: ReadonlySet<number>): void {
174
+ const lanes = into.get(display) ?? new Set<number>();
175
+ surfaces.forEach((surface) => lanes.add(surface));
176
+ into.set(display, lanes);
177
+ }
178
+
179
+ function publishOrDefer(display: number, surfaces: ReadonlySet<number>): void {
180
+ if (deferred === undefined) {
181
+ publish(display, surfaces);
182
+ return;
183
+ }
184
+ addLanes(deferred, display, surfaces);
185
+ }
186
+
187
+ /**
188
+ * Runs `changes`, publishing every lane they changed once, at the end,
189
+ * rather than once per change: every instance re-measured in one tick or
190
+ * frame changes its lane, and each publish re-flattens the lane's whole
191
+ * union. A lane whose publish throws is logged once until it publishes
192
+ * again, and published again by the next call that `retryFailed` (the tick,
193
+ * so at most every 100 ms), changed or not; a call that changes it publishes
194
+ * it anyway. Inside another call, it simply runs `changes`.
195
+ */
196
+ export function publishTogether(changes: () => void, retryFailed = false): void {
197
+ if (deferred !== undefined) {
198
+ changes();
199
+ return;
200
+ }
201
+ deferred = new Map();
202
+ if (retryFailed) {
203
+ failed.forEach((surfaces, display) => addLanes(deferred as Map<number, Set<number>>, display, surfaces));
204
+ failed = new Map();
205
+ }
206
+ try {
207
+ changes();
208
+ } finally {
209
+ const lanes = deferred;
210
+ deferred = undefined;
211
+ for (const [display, surfaces] of lanes) {
212
+ const keys = [...surfaces].map((surface) => publishKey(display, surface));
213
+ try {
214
+ publish(display, surfaces);
215
+ // Still listed as failed, a lane that went through here republishes
216
+ // the same list on the next retry, which the registry skips.
217
+ keys.forEach((key) => warned.delete(key));
218
+ } catch (error) {
219
+ addLanes(failed, display, surfaces);
220
+ if (keys.some((key) => !warned.has(key))) {
221
+ keys.forEach((key) => warned.add(key));
222
+ console.warn('[Bugsee] could not publish secure rectangles', errorName(error));
223
+ }
224
+ }
225
+ }
226
+ }
227
+ }
228
+
229
+ /** Removes `owner`'s rectangles and republishes what the other owners hold. */
230
+ export function clearOwner(owner: SecureOwner): void {
231
+ const previous = owners.get(owner);
232
+ if (previous === undefined) {
233
+ return;
234
+ }
235
+ owners.delete(owner);
236
+ publish(previous.display, new Set([previous.surface]));
237
+ }
@@ -0,0 +1,83 @@
1
+ import { FiberTag } from '../viewtree/fiber';
2
+ import type { FiberLike } from '../viewtree/fiber';
3
+
4
+ /**
5
+ * Which React surface a view is measured in, read off the fiber tree with
6
+ * no native call.
7
+ *
8
+ * Fabric `measureInWindow` stops at the nearest `RootNodeKind` ancestor. A
9
+ * `<Modal>` is one (`ModalHostViewShadowNode`), and its host fiber is the
10
+ * `RCTModalHostView` component, so a view inside a Modal is measured from
11
+ * that Modal's content and everything else from the app's own root. A
12
+ * surface is named by the Modal host's React tag (Android also gives the
13
+ * Modal's dialog root that id); the app's root is {@link MAIN_SURFACE}.
14
+ * Native translates each surface's rectangles by that surface's own origin.
15
+ */
16
+
17
+ /** The app's own React root; matches native `MAIN_SURFACE`. */
18
+ export const MAIN_SURFACE = 0;
19
+
20
+ /**
21
+ * A view known to be inside a `<Modal>` whose host's tag cannot be read.
22
+ * Never the main surface: native serves a surface whose origin it cannot
23
+ * find as the whole display.
24
+ */
25
+ export const UNKNOWN_MODAL_SURFACE = -1;
26
+
27
+ /** The host component type of a `<Modal>` (`RCTModalHostViewNativeComponent`). */
28
+ export const MODAL_HOST_TYPE = 'RCTModalHostView';
29
+
30
+ /** A cycle in a malformed tree must not spin forever. */
31
+ const MAX_ANCESTORS = 10_000;
32
+
33
+ /**
34
+ * The native tag of a host fiber, via its public instance (Fabric's carry
35
+ * `__nativeTag`), or `null` when it has none.
36
+ */
37
+ export function nativeTagOfFiber(fiber: FiberLike): number | null {
38
+ try {
39
+ // eslint-disable-next-line @typescript-eslint/no-require-imports -- lazy, like the view tree's measure: only a real renderer has it.
40
+ const rendererProxy = require('react-native/Libraries/ReactNative/RendererProxy') as {
41
+ getPublicInstanceFromInternalInstanceHandle: (fiber: unknown) => unknown;
42
+ };
43
+ const publicInstance = rendererProxy.getPublicInstanceFromInternalInstanceHandle(fiber);
44
+ const tag = (publicInstance as { __nativeTag?: unknown } | null)?.__nativeTag;
45
+ // Number.isFinite never coerces: a string or a missing tag is not finite.
46
+ return Number.isFinite(tag) ? (tag as number) : null;
47
+ } catch {
48
+ return null;
49
+ }
50
+ }
51
+
52
+ /**
53
+ * The surface `fiber` is measured in: the tag of the nearest `<Modal>` host
54
+ * above it, {@link UNKNOWN_MODAL_SURFACE} when that host has no tag, or
55
+ * {@link MAIN_SURFACE} when there is no Modal above it. `fiber` itself is
56
+ * not considered: a Modal host is measured in its parent's surface.
57
+ */
58
+ export function surfaceOfFiber(
59
+ fiber: FiberLike,
60
+ tagOf: (fiber: FiberLike) => number | null = nativeTagOfFiber,
61
+ ): number {
62
+ let current = fiber.return;
63
+ for (let steps = 0; current != null && steps < MAX_ANCESTORS; steps += 1) {
64
+ if (current.tag === FiberTag.HostComponent && current.type === MODAL_HOST_TYPE) {
65
+ return tagOf(current) ?? UNKNOWN_MODAL_SURFACE;
66
+ }
67
+ current = current.return;
68
+ }
69
+ return MAIN_SURFACE;
70
+ }
71
+
72
+ /**
73
+ * The surface of a mounted host's public instance (Fabric's carries its
74
+ * fiber as `__internalInstanceHandle`). {@link MAIN_SURFACE} when the
75
+ * instance has no fiber, which only a non-Fabric renderer produces.
76
+ */
77
+ export function surfaceOfInstance(instance: object): number {
78
+ const fiber = (instance as { __internalInstanceHandle?: unknown }).__internalInstanceHandle;
79
+ if (fiber === null || typeof fiber !== 'object') {
80
+ return MAIN_SURFACE;
81
+ }
82
+ return surfaceOfFiber(fiber as FiberLike);
83
+ }