@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,622 @@
1
+ import { FiberTag } from '../viewtree/fiber';
2
+ import type { FiberLike, WindowRect } from '../viewtree/fiber';
3
+
4
+ /**
5
+ * What lies between a `<BugseeSecure>` and its surface's root, read off the
6
+ * fiber tree once per mount, and what that says each tick about how much of
7
+ * the view is really on screen.
8
+ *
9
+ * Fabric `measureInWindow` is shadow-tree layout: it ignores every ancestor's
10
+ * clipping. So a region scrolled out of its ScrollView would still be
11
+ * published. This module finds the ancestors that clip the view, so the
12
+ * component can intersect its rectangle with them, and the one kind of
13
+ * ancestor that hides a view without touching its layout: an inactive
14
+ * react-native-screens screen, which stays mounted with its layout intact.
15
+ *
16
+ * Everything else that hides a view already shows in its layout, so it is
17
+ * left to the shadow tree: a `display: 'none'` subtree, and the content of a
18
+ * hidden Offscreen (`<Activity>`, Suspense), which Fabric hides by cloning
19
+ * its hosts with `display: 'none'` (`cloneHiddenInstance`), both measure
20
+ * 0x0, which publishes nothing. A frozen react-native-screens screen is
21
+ * hidden Offscreen content that stays drawn (`RNSScreen` nulls `display` in
22
+ * its view config for exactly that), and it keeps its layout, so it stays
23
+ * masked.
24
+ *
25
+ * Every rule leans towards masking: a view is hidden only on positive
26
+ * committed evidence, and an ancestor that cannot be read or measured clips
27
+ * nothing.
28
+ *
29
+ * Committed props come from the CURRENT fiber of each ancestor. A fiber and
30
+ * its alternate swap roles on every update, a stored `.return` chain can run
31
+ * through either, and both a work-in-progress fiber's `memoizedProps` and a
32
+ * Fabric host's `canonical.currentProps` are written during render, before
33
+ * (and whether or not) that render commits. The fiber root's `current` is
34
+ * the committed tree, and its `.child`/`.sibling` links are the committed
35
+ * links, so each tick resolves the stored path top-down from it, taking at
36
+ * every level whichever of the stored fiber and its alternate is a child of
37
+ * the current parent. A path that does not resolve is unknown: nothing is
38
+ * hidden and nothing clipped.
39
+ */
40
+
41
+ /**
42
+ * Host types whose own bounds clip their content, unless styled
43
+ * `overflow: 'visible'` (iOS: `clipsToBounds = getClipsContentToBounds()`): a
44
+ * ScrollView's viewport (`ScrollViewNativeComponent`, vertical on both
45
+ * platforms and horizontal on iOS, and
46
+ * `AndroidHorizontalScrollViewNativeComponent`). Their content containers
47
+ * scroll with the content and clip nothing.
48
+ */
49
+ export const SCROLL_HOST_TYPES: ReadonlySet<unknown> = new Set(['RCTScrollView', 'AndroidHorizontalScrollView']);
50
+
51
+ /**
52
+ * Host types whose shadow node is `RootNodeKind`: `measureInWindow` stops
53
+ * there (`LayoutableShadowNode::computeRelativeLayoutMetrics`) and natively
54
+ * their content is drawn outside the hosts above, so nothing above one
55
+ * clips or hides what is inside, and the host itself clips nothing (only
56
+ * {@link MODAL_SCREEN_HOST_TYPE} can hide it). React Native's `<Modal>` and
57
+ * `<InputAccessoryView>` (the JS names are their `paperComponentName`s),
58
+ * and react-native-screens' `FullWindowOverlay`, modal screen and split
59
+ * screen, plus its gamma form sheet host. A `HostPortal` fiber ends the walk
60
+ * the same way: its children are drawn in another container.
61
+ */
62
+ export const ROOT_KIND_HOST_TYPES: ReadonlySet<unknown> = new Set([
63
+ 'RCTModalHostView',
64
+ 'RCTInputAccessoryView',
65
+ 'RNSFullWindowOverlay',
66
+ 'RNSModalScreen',
67
+ 'RNSSplitScreen',
68
+ // Not RootNodeKind, but its content is presented in a sheet of its own.
69
+ 'RNSFormSheetHost',
70
+ ]);
71
+
72
+ /** react-native-screens' screen host (`ScreenNativeComponent`). */
73
+ export const SCREEN_HOST_TYPE = 'RNSScreen';
74
+
75
+ /**
76
+ * react-native-screens' iOS modal screen host (`ModalScreenNativeComponent`),
77
+ * which takes `activityState` like a screen. It is also root-kind, so it ends
78
+ * the walk: it can hide what is inside it, but never clips it.
79
+ */
80
+ export const MODAL_SCREEN_HOST_TYPE = 'RNSModalScreen';
81
+
82
+ /**
83
+ * The react-native-screens containers whose screens `activityState` 0 really
84
+ * hides (detaches): a screen hides the view only when the nearest host above
85
+ * it is one of these. Anywhere else the prop proves nothing.
86
+ */
87
+ export const SCREEN_CONTAINER_HOST_TYPES: ReadonlySet<unknown> = new Set([
88
+ 'RNSScreenContainer',
89
+ 'RNSScreenNavigationContainer',
90
+ 'RNSScreenStack',
91
+ ]);
92
+
93
+ /** `activityState` of an inactive react-native-screens screen. */
94
+ const INACTIVE_SCREEN = 0;
95
+
96
+ /** What a host's `props` holds before its first read: equal to no props object. */
97
+ const UNREAD: unique symbol = Symbol('unread');
98
+
99
+ /** A cycle in a malformed tree must not spin forever. */
100
+ const MAX_ANCESTORS = 10_000;
101
+
102
+ type MeasureInWindow = (callback: (x: number, y: number, width: number, height: number) => void) => void;
103
+
104
+ interface HostAncestor {
105
+ /** Where the host is in `Ancestry.path`. */
106
+ readonly index: number;
107
+ /** Where the next host above it is in `Ancestry.path` (its length when there is none). */
108
+ above: number;
109
+ readonly scrolls: boolean;
110
+ readonly screen: boolean;
111
+ /** A root-kind host: it ends the walk and clips nothing. */
112
+ readonly boundary: boolean;
113
+ /** The committed props object `clips` was last read from. */
114
+ props: unknown;
115
+ clips: boolean;
116
+ /** The host's public instance, looked up the first time it clips. */
117
+ instance: { measureInWindow: MeasureInWindow } | undefined;
118
+ }
119
+
120
+ export interface Ancestry {
121
+ /** The view's own host fiber, as stored at mount; `null` when there is none. */
122
+ readonly self: FiberLike | null;
123
+ /** Every fiber above the view, nearest first, up to and including the tree's root. */
124
+ readonly path: readonly FiberLike[];
125
+ /** The fiber root (`HostRoot.stateNode`), or `null` when the path has none. */
126
+ readonly root: { current?: unknown } | null;
127
+ /** The hosts below the nearest root-kind host, nearest first. */
128
+ readonly hosts: readonly HostAncestor[];
129
+ /** The screens among `hosts`. */
130
+ readonly screens: readonly HostAncestor[];
131
+ /**
132
+ * Where in `path` the `Animated` components below the nearest root-kind
133
+ * host are (`Animated.View` and the like, nearest first): only their props
134
+ * can hold a native-driven transform, which the shadow tree never sees.
135
+ */
136
+ readonly animated: readonly number[];
137
+ /** Whether the last {@link isHidden} read everything; `false` clips nothing. */
138
+ readable: boolean;
139
+ /** The current fibers of `path` as the last {@link isHidden} resolved them, if it did. */
140
+ resolved: readonly FiberLike[] | undefined;
141
+ }
142
+
143
+ /** Nothing above the view can be read: it is never hidden and never clipped. */
144
+ export function noAncestry(): Ancestry {
145
+ return {
146
+ self: null,
147
+ path: [],
148
+ root: null,
149
+ hosts: [],
150
+ screens: [],
151
+ animated: [],
152
+ readable: false,
153
+ resolved: undefined,
154
+ };
155
+ }
156
+
157
+ function displayNameOf(fiber: FiberLike): unknown {
158
+ const type = fiber.type as { displayName?: unknown; render?: { displayName?: unknown } } | null | undefined;
159
+ if (type === null || (typeof type !== 'function' && typeof type !== 'object')) {
160
+ return undefined;
161
+ }
162
+ return type.displayName ?? type.render?.displayName;
163
+ }
164
+
165
+ /**
166
+ * Whether `fiber` is a Reanimated component (`createAnimatedComponent` of
167
+ * react-native-reanimated names every one `AnimatedComponent(<name>)`).
168
+ */
169
+ export function isReanimatedComponent(fiber: FiberLike): boolean {
170
+ const name = displayNameOf(fiber);
171
+ return typeof name === 'string' && name.startsWith('AnimatedComponent(');
172
+ }
173
+
174
+ /**
175
+ * Whether `fiber` is an `Animated` component (`Animated.createAnimatedComponent`
176
+ * names every one `Animated(<name>)`), as a plain function or a `forwardRef`,
177
+ * or a Reanimated one.
178
+ */
179
+ export function isAnimatedComponent(fiber: FiberLike): boolean {
180
+ const name = displayNameOf(fiber);
181
+ return (typeof name === 'string' && name.startsWith('Animated(')) || isReanimatedComponent(fiber);
182
+ }
183
+
184
+ /**
185
+ * The ancestors of the host `fiber`, up to the tree's root. Only the hosts
186
+ * below the nearest root-kind host can clip or hide it. `fiber` itself is
187
+ * not included.
188
+ */
189
+ export function ancestryOfFiber(fiber: FiberLike): Ancestry {
190
+ const path: FiberLike[] = [];
191
+ const hosts: HostAncestor[] = [];
192
+ const animated: number[] = [];
193
+ let bounded = false;
194
+ let current = fiber.return;
195
+ for (let steps = 0; current != null && steps < MAX_ANCESTORS; steps += 1) {
196
+ bounded ||= current.tag === FiberTag.HostPortal;
197
+ if (current.tag === FiberTag.HostComponent) {
198
+ const below = hosts[hosts.length - 1];
199
+ if (below !== undefined && below.above === MAX_ANCESTORS) {
200
+ below.above = path.length;
201
+ }
202
+ const boundary = ROOT_KIND_HOST_TYPES.has(current.type);
203
+ if (!bounded && (!boundary || current.type === MODAL_SCREEN_HOST_TYPE)) {
204
+ hosts.push({
205
+ index: path.length,
206
+ above: MAX_ANCESTORS,
207
+ scrolls: SCROLL_HOST_TYPES.has(current.type),
208
+ screen: current.type === SCREEN_HOST_TYPE || current.type === MODAL_SCREEN_HOST_TYPE,
209
+ boundary,
210
+ props: UNREAD,
211
+ clips: false,
212
+ instance: undefined,
213
+ });
214
+ }
215
+ bounded ||= boundary;
216
+ } else if (!bounded && isAnimatedComponent(current)) {
217
+ animated.push(path.length);
218
+ }
219
+ path.push(current);
220
+ current = current.return;
221
+ }
222
+ const last = hosts[hosts.length - 1];
223
+ if (last !== undefined && last.above === MAX_ANCESTORS) {
224
+ last.above = path.length;
225
+ }
226
+ const top = path[path.length - 1];
227
+ const root = top?.tag === FiberTag.HostRoot ? (top.stateNode as { current?: unknown } | null) : null;
228
+ return {
229
+ self: fiber,
230
+ path,
231
+ root,
232
+ hosts,
233
+ screens: hosts.filter((host) => host.screen),
234
+ animated,
235
+ readable: false,
236
+ resolved: undefined,
237
+ };
238
+ }
239
+
240
+ /**
241
+ * The ancestry of a mounted host's public instance (Fabric's carries its
242
+ * fiber as `__internalInstanceHandle`). {@link noAncestry} when there is no
243
+ * fiber or the tree cannot be read.
244
+ */
245
+ export function ancestryOfInstance(instance: object): Ancestry {
246
+ try {
247
+ // No fiber (or not an object) has no `.return`; `null` throws on reading it.
248
+ const fiber = (instance as { __internalInstanceHandle?: unknown }).__internalInstanceHandle;
249
+ if (typeof fiber !== 'object') {
250
+ return noAncestry();
251
+ }
252
+ return ancestryOfFiber(fiber as FiberLike);
253
+ } catch {
254
+ return noAncestry();
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Each current parent's children, collected in the running pass the first
260
+ * time any view looks among them, so views that are siblings in a long list
261
+ * scan that list once a pass rather than once each.
262
+ */
263
+ let passChildren: Map<FiberLike, Set<FiberLike>> | undefined;
264
+
265
+ /** The child of `parent` (a current fiber) that is `fiber` or its alternate, if any. */
266
+ function currentChild(parent: FiberLike, fiber: FiberLike): FiberLike | undefined {
267
+ let children = passChildren?.get(parent);
268
+ if (children === undefined) {
269
+ children = new Set();
270
+ for (let child = parent.child; child != null; child = child.sibling) {
271
+ children.add(child);
272
+ }
273
+ passChildren?.set(parent, children);
274
+ }
275
+ // No child is null or undefined, so a missing alternate is never found.
276
+ const alternate = fiber.alternate as FiberLike;
277
+ return children.has(fiber) ? fiber : children.has(alternate) ? alternate : undefined;
278
+ }
279
+
280
+ /**
281
+ * The current fiber of every fiber in `ancestry.path` from `downTo` up, in
282
+ * the same order (entries below `downTo` are left as the root), or
283
+ * `undefined` when the path does not resolve against the committed tree.
284
+ * Reads fibers only, which React never makes throw.
285
+ *
286
+ * Nothing is kept from one pass to the next: React keeps no commit count, and
287
+ * the root's `current` alternates between the same two fibers, so a cached
288
+ * level cannot be told apart from one two commits stale.
289
+ */
290
+ function resolve(ancestry: Ancestry, downTo = 0): FiberLike[] | undefined {
291
+ const { path, root } = ancestry;
292
+ // A root exists only on a path that ends at one.
293
+ const top = path[path.length - 1] as FiberLike;
294
+ const rootCurrent = root?.current as FiberLike | null | undefined;
295
+ if (rootCurrent == null || (rootCurrent !== top && rootCurrent !== top.alternate)) {
296
+ return undefined;
297
+ }
298
+ // The root's entry stays; every other is replaced below, or the path does
299
+ // not resolve.
300
+ const resolved = path.map(() => rootCurrent);
301
+ for (let index = path.length - 2; index >= downTo; index -= 1) {
302
+ const current = currentChild(resolved[index + 1] as FiberLike, path[index] as FiberLike);
303
+ if (current === undefined) {
304
+ return undefined;
305
+ }
306
+ resolved[index] = current;
307
+ }
308
+ return resolved;
309
+ }
310
+
311
+ /**
312
+ * `key` of a style as React Native flattens it: the last value set wins,
313
+ * through nested arrays; falsy entries are skipped.
314
+ */
315
+ export function styleValue(style: unknown, key: string): unknown {
316
+ if (Array.isArray(style)) {
317
+ return style.reduceRight<unknown>(
318
+ (found, entry) => (found !== undefined ? found : styleValue(entry, key)),
319
+ undefined,
320
+ );
321
+ }
322
+ if (style !== null && typeof style === 'object') {
323
+ return (style as Record<string, unknown>)[key];
324
+ }
325
+ return undefined;
326
+ }
327
+
328
+ /**
329
+ * An `activityState` set on a fiber between a screen host and the host
330
+ * above it (the `Animated.createAnimatedComponent` wrapper react-native-screens
331
+ * renders the host through) that is not a plain number: an Animated node,
332
+ * whose value native drives without React committing it.
333
+ */
334
+ function animatedActivity(resolved: readonly FiberLike[], from: number, to: number): boolean {
335
+ for (let index = from; index < to; index += 1) {
336
+ const value = ((resolved[index] as FiberLike).memoizedProps as { activityState?: unknown } | null)
337
+ ?.activityState;
338
+ if (value !== null && typeof value === 'object') {
339
+ return true;
340
+ }
341
+ }
342
+ return false;
343
+ }
344
+
345
+ /** Re-reads `host`'s clip from its current fiber, if its committed props changed. */
346
+ function refresh(host: HostAncestor, resolved: readonly FiberLike[]): void {
347
+ const props = (resolved[host.index] as FiberLike).memoizedProps;
348
+ if (props !== host.props) {
349
+ const overflow = styleValue((props as { style?: unknown } | null)?.style, 'overflow');
350
+ host.clips = !host.boundary && (host.scrolls ? overflow !== 'visible' : overflow === 'hidden');
351
+ // Only once read: a read that threw is tried again next time.
352
+ host.props = props;
353
+ }
354
+ }
355
+
356
+ /**
357
+ * Whether `screen` hides the view in the committed tree: `activityState` 0
358
+ * on its current fiber, not driven by an Animated node on the wrapper above
359
+ * it, under a react-native-screens container that detaches inactive screens.
360
+ */
361
+ function screenHides(screen: HostAncestor, ancestry: Ancestry, resolved: readonly FiberLike[]): boolean {
362
+ const activityState = ((resolved[screen.index] as FiberLike).memoizedProps as { activityState?: unknown } | null)
363
+ ?.activityState;
364
+ return (
365
+ activityState === INACTIVE_SCREEN &&
366
+ SCREEN_CONTAINER_HOST_TYPES.has(ancestry.path[screen.above]?.type) &&
367
+ !animatedActivity(resolved, screen.index + 1, screen.above)
368
+ );
369
+ }
370
+
371
+ /**
372
+ * Whether a react-native-screens screen above the view is inactive in the
373
+ * committed tree (see `screenHides`). Also reads which hosts clip, for
374
+ * {@link clipToAncestors}. When anything cannot be read (the path does not
375
+ * resolve, or a props read throws) the answer is `false` and nothing is
376
+ * clipped either.
377
+ */
378
+ export function isHidden(ancestry: Ancestry): boolean {
379
+ ancestry.readable = false;
380
+ const resolved = resolve(ancestry);
381
+ ancestry.resolved = resolved;
382
+ if (resolved === undefined) {
383
+ return false;
384
+ }
385
+ // Props are the app's: a getter on one can throw.
386
+ try {
387
+ for (const host of ancestry.hosts) {
388
+ refresh(host, resolved);
389
+ }
390
+ const hidden = ancestry.screens.some((screen) => screenHides(screen, ancestry, resolved));
391
+ ancestry.readable = true;
392
+ return hidden;
393
+ } catch {
394
+ return false;
395
+ }
396
+ }
397
+
398
+ /**
399
+ * The screens alone of {@link isHidden}, for a view withdrawn as hidden to
400
+ * check every frame: it resolves the path only down to the nearest screen,
401
+ * and only once a pass for every view under the same screen.
402
+ */
403
+ export function screensHide(ancestry: Ancestry): boolean {
404
+ const nearest = ancestry.screens[0];
405
+ if (nearest === undefined) {
406
+ return false;
407
+ }
408
+ const key = ancestry.path[nearest.index] as FiberLike;
409
+ const known = passScreens?.get(key);
410
+ if (known !== undefined) {
411
+ return known;
412
+ }
413
+ const resolved = resolve(ancestry, nearest.index);
414
+ let hidden: boolean;
415
+ // An unresolved path (`undefined`) throws here as a props getter can.
416
+ try {
417
+ hidden = ancestry.screens.some((screen) => screenHides(screen, ancestry, resolved as FiberLike[]));
418
+ } catch {
419
+ hidden = false;
420
+ }
421
+ passScreens?.set(key, hidden).set(key.alternate, hidden);
422
+ return hidden;
423
+ }
424
+
425
+ /** {@link screensHide}'s answers in the running pass, by both fibers of the nearest screen. */
426
+ let passScreens: Map<FiberLike | null | undefined, boolean> | undefined;
427
+
428
+ /**
429
+ * How many component fibers can stand between a ScrollView's host and its
430
+ * content container's host: on Android a vertical ScrollView renders its
431
+ * content through the `View` component (`VScrollContentViewNativeComponent`).
432
+ */
433
+ export const CONTENT_DEPTH = 4;
434
+
435
+ /**
436
+ * The nodes whose movement can change how `ancestry`'s clipping ancestors
437
+ * (as the last {@link isHidden} read them) cut the view: every clipping
438
+ * host itself, whose bounds are the clip (a viewport that grows uncovers
439
+ * more of the view), and a ScrollView's content container, which moves as
440
+ * it scrolls: the nearest host below it on the way to the view.
441
+ */
442
+ export function clipWatchNodes(ancestry: Ancestry): FiberLike[] {
443
+ if (!ancestry.readable) {
444
+ return [];
445
+ }
446
+ return ancestry.hosts
447
+ .filter((host) => host.clips)
448
+ .flatMap((host) => {
449
+ const clipper = ancestry.path[host.index] as FiberLike;
450
+ const below = ancestry.path
451
+ .slice(Math.max(0, host.index - CONTENT_DEPTH), host.index)
452
+ .reverse()
453
+ .find((fiber) => fiber.tag === FiberTag.HostComponent);
454
+ return host.scrolls && below !== undefined ? [below, clipper] : [clipper];
455
+ });
456
+ }
457
+
458
+ /**
459
+ * A ScrollView host's content container: the first host down its first
460
+ * children, through at most {@link CONTENT_DEPTH} components; `undefined`
461
+ * when there is none.
462
+ */
463
+ export function contentOf(scroller: FiberLike): FiberLike | undefined {
464
+ let fiber = scroller.child;
465
+ for (let depth = 0; fiber != null && depth < CONTENT_DEPTH; depth += 1) {
466
+ if (fiber.tag === FiberTag.HostComponent) {
467
+ return fiber;
468
+ }
469
+ fiber = fiber.child;
470
+ }
471
+ return undefined;
472
+ }
473
+
474
+ /** Public instances of watched nodes, looked up once each. */
475
+ const nodeInstances = new WeakMap<FiberLike, { measureInWindow: MeasureInWindow }>();
476
+
477
+ /** `fiber`'s window rectangle, as {@link measureHost} answers, measured at most once a pass. */
478
+ export function measureFiber(fiber: FiberLike): WindowRect | null | undefined {
479
+ let instance = nodeInstances.get(fiber);
480
+ if (instance === undefined) {
481
+ instance = publicInstanceOf(fiber);
482
+ nodeInstances.set(fiber, instance);
483
+ }
484
+ return measureInstance(instance);
485
+ }
486
+
487
+ function publicInstanceOf(fiber: FiberLike): { measureInWindow: MeasureInWindow } {
488
+ // eslint-disable-next-line @typescript-eslint/no-require-imports -- lazy, like surface.ts: only a renderer has it.
489
+ const rendererProxy = require('react-native/Libraries/ReactNative/RendererProxy') as {
490
+ getPublicInstanceFromInternalInstanceHandle: (fiber: unknown) => { measureInWindow: MeasureInWindow };
491
+ };
492
+ return rendererProxy.getPublicInstanceFromInternalInstanceHandle(fiber);
493
+ }
494
+
495
+ /**
496
+ * The clipping ancestors measured in the running {@link measurePass}, by
497
+ * public instance: views under one ScrollView all clip to it, and measure it
498
+ * once a pass. `undefined` outside a pass, which measures every time.
499
+ */
500
+ let passMeasurements: Map<object, WindowRect | null | undefined> | undefined;
501
+
502
+ /**
503
+ * Runs `pass` with each clipping ancestor measured, and each ancestor's
504
+ * current fiber resolved, at most once. Only for a synchronous stretch of JS
505
+ * (the re-measure loop's tick): no commit can move anything while it runs.
506
+ */
507
+ export function measurePass(pass: () => void): void {
508
+ passMeasurements = new Map();
509
+ passChildren = new Map();
510
+ passScreens = new Map();
511
+ passMemos = new Map();
512
+ try {
513
+ pass();
514
+ } finally {
515
+ passMeasurements = undefined;
516
+ passChildren = undefined;
517
+ passScreens = undefined;
518
+ passMemos = undefined;
519
+ }
520
+ }
521
+
522
+ /** What {@link passMemo} computed in the running pass, by key. */
523
+ let passMemos: Map<object, unknown> | undefined;
524
+
525
+ /**
526
+ * `compute()`, at most once per `key` in the running {@link measurePass}
527
+ * (many views under one `Animated` component read its nodes once a pass);
528
+ * every time outside a pass.
529
+ */
530
+ export function passMemo<T>(key: object, compute: () => T): T {
531
+ if (passMemos?.has(key)) {
532
+ return passMemos.get(key) as T;
533
+ }
534
+ const value = compute();
535
+ passMemos?.set(key, value);
536
+ return value;
537
+ }
538
+
539
+ /**
540
+ * `host`'s window rectangle; `null` for an exact 0/0/0/0, which Fabric
541
+ * answers for a node or layout it cannot find; `undefined` unless it answers
542
+ * at once with finite numbers.
543
+ */
544
+ function measureHost(host: HostAncestor, fiber: FiberLike): WindowRect | null | undefined {
545
+ return measureInstance((host.instance ??= publicInstanceOf(fiber)));
546
+ }
547
+
548
+ function measureInstance(instance: { measureInWindow: MeasureInWindow }): WindowRect | null | undefined {
549
+ if (passMeasurements?.has(instance)) {
550
+ return passMeasurements.get(instance);
551
+ }
552
+ let measured: WindowRect | null | undefined;
553
+ instance.measureInWindow((x, y, width, height) => {
554
+ const answer = { x, y, width, height };
555
+ if (finite(answer)) {
556
+ measured = x === 0 && y === 0 && width === 0 && height === 0 ? null : answer;
557
+ }
558
+ });
559
+ passMeasurements?.set(instance, measured);
560
+ return measured;
561
+ }
562
+
563
+ function finite({ x, y, width, height }: WindowRect): boolean {
564
+ return Number.isFinite(x) && Number.isFinite(y) && Number.isFinite(width) && Number.isFinite(height);
565
+ }
566
+
567
+ /**
568
+ * The part of `rect` inside every ancestor that clips now (as the last
569
+ * {@link isHidden} read them), or `null` when none of it is. Only the
570
+ * ancestors between `path` indices `above` and `below` cut it: a native-driven
571
+ * transform between two clippers moves what the lower one left, and the upper
572
+ * one cuts where it was moved to.
573
+ *
574
+ * Fails closed: `rect` itself, unclipped, when it is not finite, the last
575
+ * read was not complete, or any clipping ancestor throws, answers late or
576
+ * answers with a non-finite rectangle. An ancestor answering 0/0/0/0 clips
577
+ * nothing. Measures nothing, and returns `rect` as it is, when nothing
578
+ * clips.
579
+ */
580
+ export function clipToAncestors(
581
+ rect: WindowRect,
582
+ ancestry: Ancestry,
583
+ above = -1,
584
+ below = Number.POSITIVE_INFINITY,
585
+ ): WindowRect | null {
586
+ if (!finite(rect) || !ancestry.readable) {
587
+ return rect;
588
+ }
589
+ const clips: Array<WindowRect | null | undefined> = [];
590
+ try {
591
+ for (const host of ancestry.hosts) {
592
+ // `above` and `below` are Animated components' indices, never a host's.
593
+ // Stryker disable next-line EqualityOperator -- so `>=` and `<=` pick the same hosts
594
+ if (host.clips && host.index > above && host.index < below) {
595
+ clips.push(measureHost(host, ancestry.path[host.index] as FiberLike));
596
+ }
597
+ }
598
+ } catch {
599
+ return rect;
600
+ }
601
+ if (clips.includes(undefined)) {
602
+ return rect;
603
+ }
604
+ const known = clips.filter((clip): clip is WindowRect => clip !== null);
605
+ if (known.length === 0) {
606
+ return rect;
607
+ }
608
+ let left = rect.x;
609
+ let top = rect.y;
610
+ let right = rect.x + rect.width;
611
+ let bottom = rect.y + rect.height;
612
+ for (const clip of known) {
613
+ left = Math.max(left, clip.x);
614
+ top = Math.max(top, clip.y);
615
+ right = Math.min(right, clip.x + clip.width);
616
+ bottom = Math.min(bottom, clip.y + clip.height);
617
+ }
618
+ if (right <= left || bottom <= top) {
619
+ return null;
620
+ }
621
+ return { x: left, y: top, width: right - left, height: bottom - top };
622
+ }