@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,431 @@
1
+ #import "BGSRNDataRequestBridge.h"
2
+
3
+ #import <UIKit/UIKit.h>
4
+ #import <os/lock.h>
5
+ #import <time.h>
6
+
7
+ NSString *const BGSRNDataRequestTypeViewHierarchy = @"vh";
8
+ const int64_t BGSRNDataRequestDeadlineMs = 450;
9
+
10
+ /// One request that reached the table. `done`, `timer` and `reply` are
11
+ /// guarded by the bridge's lock; everything else is immutable.
12
+ @interface BGSRNDataRequest : NSObject
13
+ @property (nonatomic, copy, readonly) NSString *requestId;
14
+ /// Nilled by `finish:`: the deadline task captures the request, and a
15
+ /// cancelled `dispatch_block` keeps its captures until its fire time.
16
+ @property (nonatomic, copy, nullable) BGSRNDataRequestReply reply;
17
+ /// The sink this request was emitted to; its detach answers it. Weak, like the
18
+ /// sink itself: the bridge must not keep a torn-down module alive.
19
+ @property (nonatomic, weak, readonly) id owner;
20
+ /// Taken on entry to `requestType:reply:`, before any check.
21
+ @property (nonatomic, readonly) int64_t startMs;
22
+ /// The exactly-once guard. Whoever flips it runs the reply.
23
+ @property (nonatomic) BOOL done;
24
+ @property (nonatomic, strong, nullable) id timer;
25
+ @end
26
+
27
+ @implementation BGSRNDataRequest
28
+
29
+ - (instancetype)initWithId:(NSString *)requestId
30
+ reply:(BGSRNDataRequestReply)reply
31
+ owner:(id)owner
32
+ startMs:(int64_t)startMs {
33
+ self = [super init];
34
+ if (self) {
35
+ _requestId = [requestId copy];
36
+ _reply = [reply copy];
37
+ _owner = owner;
38
+ _startMs = startMs;
39
+ }
40
+ return self;
41
+ }
42
+
43
+ @end
44
+
45
+ /// The SDK's own fault must not escape into JS's queue, the timer's, or back
46
+ /// into the SDK's main-thread pass.
47
+ static void RunQuietly(BGSRNDataRequestReply _Nullable reply, NSString *_Nullable payload) {
48
+ if (reply == nil) {
49
+ return;
50
+ }
51
+ @try {
52
+ reply(payload);
53
+ } @catch (NSException *exception) {
54
+ NSLog(@"BugseeRN data request reply threw: %@", NSStringFromClass(exception.class));
55
+ }
56
+ }
57
+
58
+ /// `bytes=` in the completed line: the payload's UTF-8 byte count, as on
59
+ /// Android -- never its UTF-16 length, so both platforms' lines compare.
60
+ static NSString *BytesOf(NSString *_Nullable payload) {
61
+ if (payload == nil) {
62
+ return @"null";
63
+ }
64
+ return [NSString stringWithFormat:@"%lu",
65
+ (unsigned long)[payload lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
66
+ }
67
+
68
+ /// The shortest decimal text that parses back to exactly `value` (`%g` keeps
69
+ /// only 6 significant digits), so the `type=` line's origin can be used to
70
+ /// recompute bounds. `+ 0.0` turns a -0 into 0.
71
+ static NSString *FormatCoordinate(CGFloat value) {
72
+ const double v = (double)value + 0.0;
73
+ char buffer[64];
74
+ // Fixed notation, fewest decimals first: points never need an exponent,
75
+ // and `%g` would print 40 as `4e+01` at low precision.
76
+ for (int decimals = 0; decimals <= 20; decimals++) {
77
+ snprintf(buffer, sizeof buffer, "%.*f", decimals, v);
78
+ if (strtod(buffer, NULL) == v) {
79
+ return @(buffer);
80
+ }
81
+ }
82
+ snprintf(buffer, sizeof buffer, "%.17g", v);
83
+ return @(buffer);
84
+ }
85
+
86
+ /// A point, if `value` is an `NSValue` holding a finite `CGPoint`. The block
87
+ /// is the module's, but a bad origin would put every node in the wrong place,
88
+ /// and there is no better answer than none.
89
+ static BOOL PointFrom(id _Nullable value, CGPoint *point) {
90
+ if (![value isKindOfClass:NSValue.class] || strcmp([(NSValue *)value objCType], @encode(CGPoint)) != 0) {
91
+ return NO;
92
+ }
93
+ const CGPoint p = [(NSValue *)value CGPointValue];
94
+ if (!isfinite(p.x) || !isfinite(p.y)) {
95
+ return NO;
96
+ }
97
+ // `+ 0.0` turns a -0 into 0, so the line never reads `origin=-0,0`.
98
+ *point = CGPointMake(p.x + 0.0, p.y + 0.0);
99
+ return YES;
100
+ }
101
+
102
+ /// The origin block's value; `*threw` says the block raised, which is the
103
+ /// bridge failing rather than there being no origin.
104
+ static id _Nullable CallOrigin(BGSRNDataRequestOriginBlock _Nullable block, BOOL *threw) {
105
+ *threw = NO;
106
+ if (block == nil) {
107
+ return nil;
108
+ }
109
+ @try {
110
+ return block();
111
+ } @catch (NSException *exception) {
112
+ NSLog(@"BugseeRN data request origin threw: %@", NSStringFromClass(exception.class));
113
+ *threw = YES;
114
+ return nil;
115
+ }
116
+ }
117
+
118
+ @implementation BGSRNDataRequestBridge {
119
+ BGSRNDataRequestScheduler _schedule;
120
+ BGSRNDataRequestSchedulerCancel _cancel;
121
+ BGSRNDataRequestClock _clock;
122
+ BGSRNDataRequestOutcomeLog _log;
123
+
124
+ /// Guards everything below. The SDK requests on main, JS completes on the
125
+ /// module's method queue, deadlines fire on the scheduler's, and a reload
126
+ /// attaches and detaches wherever React Native tears down: all of them meet
127
+ /// here. Never held while calling out -- not the reply, not the sink, not
128
+ /// the origin, not the scheduler, not the log.
129
+ os_unfair_lock _lock;
130
+ __weak id _sink;
131
+ BGSRNDataRequestSinkBlock _block;
132
+ BGSRNDataRequestOriginBlock _origin;
133
+ BOOL _viewTreeEnabled;
134
+ NSMutableDictionary<NSString *, BGSRNDataRequest *> *_requests;
135
+ uint64_t _counter;
136
+ }
137
+
138
+ + (BGSRNDataRequestBridge *)shared {
139
+ static BGSRNDataRequestBridge *shared = nil;
140
+ static dispatch_once_t once;
141
+ dispatch_once(&once, ^{
142
+ // One serial queue for every request's deadline, off main: the SDK's own
143
+ // timeout runs off main too, so a busy main thread cannot hold back the
144
+ // answer. A cancelled block that has not started never runs; one that has
145
+ // already started still reaches the request's guard, which is what makes
146
+ // cancelling safe to race.
147
+ //
148
+ // USER_INITIATED, the QoS of the SDK's own 500 ms timer
149
+ // (BGSCaptureDataProviderViewHierarchy.m:342-343): with only 50 ms between
150
+ // the two, a lower-QoS queue could fire after the SDK's timer under load,
151
+ // costing a counted timeout instead of our answer.
152
+ dispatch_queue_attr_t attributes =
153
+ dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, 0);
154
+ dispatch_queue_t queue = dispatch_queue_create("com.bugsee.reactnative.data-request-deadline", attributes);
155
+ shared = [[BGSRNDataRequestBridge alloc]
156
+ initWithScheduler:^id(dispatch_block_t task, int64_t delayMs) {
157
+ dispatch_block_t block = dispatch_block_create(0, task);
158
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delayMs * (int64_t)NSEC_PER_MSEC), queue, block);
159
+ return block;
160
+ }
161
+ cancel:^(id token) {
162
+ dispatch_block_cancel((dispatch_block_t)token);
163
+ }
164
+ clock:^int64_t {
165
+ // Monotonic, and unaffected by the wall clock being set.
166
+ return (int64_t)(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / NSEC_PER_MSEC);
167
+ }];
168
+ });
169
+ return shared;
170
+ }
171
+
172
+ - (instancetype)initWithScheduler:(BGSRNDataRequestScheduler)schedule
173
+ cancel:(BGSRNDataRequestSchedulerCancel)cancel
174
+ clock:(BGSRNDataRequestClock)nowMs {
175
+ return [self initWithScheduler:schedule
176
+ cancel:cancel
177
+ clock:nowMs
178
+ log:^(NSString *line) {
179
+ // NSLog, not os_log: it reaches the console
180
+ // stream the device tests match.
181
+ NSLog(@"%@", line);
182
+ }];
183
+ }
184
+
185
+ - (instancetype)initWithScheduler:(BGSRNDataRequestScheduler)schedule
186
+ cancel:(BGSRNDataRequestSchedulerCancel)cancel
187
+ clock:(BGSRNDataRequestClock)nowMs
188
+ log:(BGSRNDataRequestOutcomeLog)log {
189
+ self = [super init];
190
+ if (self) {
191
+ _schedule = [schedule copy];
192
+ _cancel = [cancel copy];
193
+ _clock = [nowMs copy];
194
+ _log = [log copy];
195
+ _lock = OS_UNFAIR_LOCK_INIT;
196
+ _requests = [NSMutableDictionary dictionary];
197
+ }
198
+ return self;
199
+ }
200
+
201
+ - (BOOL)viewTreeEnabled {
202
+ os_unfair_lock_lock(&_lock);
203
+ const BOOL enabled = _viewTreeEnabled;
204
+ os_unfair_lock_unlock(&_lock);
205
+ return enabled;
206
+ }
207
+
208
+ - (void)setViewTreeEnabled:(BOOL)viewTreeEnabled {
209
+ os_unfair_lock_lock(&_lock);
210
+ _viewTreeEnabled = viewTreeEnabled;
211
+ os_unfair_lock_unlock(&_lock);
212
+ }
213
+
214
+ - (void)setViewTreeEnabled:(BOOL)enabled forSink:(id)sink {
215
+ os_unfair_lock_lock(&_lock);
216
+ if (_sink == sink) {
217
+ _viewTreeEnabled = enabled;
218
+ }
219
+ os_unfair_lock_unlock(&_lock);
220
+ }
221
+
222
+ - (NSUInteger)outstanding {
223
+ os_unfair_lock_lock(&_lock);
224
+ const NSUInteger count = _requests.count;
225
+ os_unfair_lock_unlock(&_lock);
226
+ return count;
227
+ }
228
+
229
+ - (void)attach:(id)sink block:(BGSRNDataRequestSinkBlock)block origin:(BGSRNDataRequestOriginBlock)origin {
230
+ os_unfair_lock_lock(&_lock);
231
+ _sink = sink;
232
+ _block = [block copy];
233
+ _origin = [origin copy];
234
+ _viewTreeEnabled = NO;
235
+ os_unfair_lock_unlock(&_lock);
236
+ }
237
+
238
+ - (void)detach:(id)sink {
239
+ NSMutableArray<BGSRNDataRequest *> *stale = [NSMutableArray array];
240
+ os_unfair_lock_lock(&_lock);
241
+ if (_sink == sink) {
242
+ _sink = nil;
243
+ _block = nil;
244
+ _origin = nil;
245
+ _viewTreeEnabled = NO;
246
+ }
247
+ for (BGSRNDataRequest *request in _requests.allValues) {
248
+ if (request.owner == sink) {
249
+ [stale addObject:request];
250
+ }
251
+ }
252
+ os_unfair_lock_unlock(&_lock);
253
+
254
+ for (BGSRNDataRequest *request in stale) {
255
+ [self finish:request payload:nil by:@"detach"];
256
+ }
257
+ }
258
+
259
+ - (void)requestType:(NSString *)type reply:(BGSRNDataRequestReply)reply {
260
+ // First: the plan's `ms` is measured from the moment requestData was
261
+ // entered, not from whenever a request happens to be minted.
262
+ const int64_t start = _clock();
263
+
264
+ os_unfair_lock_lock(&_lock);
265
+ // Minted for every call, answered or not, so an id is never reused and
266
+ // every outcome line can be told apart.
267
+ NSString *requestId = [NSString stringWithFormat:@"dr-%llu", ++_counter];
268
+ id target = _sink;
269
+ BGSRNDataRequestSinkBlock block = _block;
270
+ BGSRNDataRequestOriginBlock originBlock = _origin;
271
+ const BOOL enabled = _viewTreeEnabled;
272
+ os_unfair_lock_unlock(&_lock);
273
+
274
+ if (![type isKindOfClass:NSString.class] || ![type isEqualToString:BGSRNDataRequestTypeViewHierarchy]) {
275
+ [self answerUnminted:requestId reply:reply startMs:start by:@"unknown-type"];
276
+ return;
277
+ }
278
+ if (target == nil || block == nil || !enabled) {
279
+ [self answerUnminted:requestId reply:reply startMs:start by:@"no-js"];
280
+ return;
281
+ }
282
+ // Outside the lock: the module's block reads UIKit, on this (main) thread.
283
+ BOOL originThrew = NO;
284
+ id originValue = CallOrigin(originBlock, &originThrew);
285
+ if (originThrew) {
286
+ [self answerUnminted:requestId reply:reply startMs:start by:@"failed"];
287
+ return;
288
+ }
289
+ CGPoint origin = CGPointZero;
290
+ if (!PointFrom(originValue, &origin)) {
291
+ [self answerUnminted:requestId reply:reply startMs:start by:@"no-origin"];
292
+ return;
293
+ }
294
+
295
+ BGSRNDataRequest *request = [[BGSRNDataRequest alloc] initWithId:requestId
296
+ reply:reply
297
+ owner:target
298
+ startMs:start];
299
+ os_unfair_lock_lock(&_lock);
300
+ // Checked and inserted under one hold: a detach that ran after the sink was
301
+ // read above has either already cleared it (and this answers now) or will
302
+ // find this request in the table and answer it.
303
+ const BOOL stillAttached = _sink == target;
304
+ if (stillAttached) {
305
+ _requests[requestId] = request;
306
+ }
307
+ os_unfair_lock_unlock(&_lock);
308
+ if (!stillAttached) {
309
+ [self answerUnminted:requestId reply:reply startMs:start by:@"detach"];
310
+ return;
311
+ }
312
+
313
+ // The task holds the request itself, not its id: a deadline that was
314
+ // already running when JS completed must still hit the guard.
315
+ id timer = [self scheduleQuietly:^{
316
+ [self finish:request payload:nil by:@"deadline"];
317
+ }];
318
+ if (timer == nil) {
319
+ // Registered, but nothing will ever time it out: answer now rather than
320
+ // leave the SDK to its own timeout, and leave nothing in the table for a
321
+ // later detach to answer a second time.
322
+ [self finish:request payload:nil by:@"failed"];
323
+ return;
324
+ }
325
+ os_unfair_lock_lock(&_lock);
326
+ const BOOL alreadyDone = request.done;
327
+ if (!alreadyDone) {
328
+ request.timer = timer;
329
+ }
330
+ os_unfair_lock_unlock(&_lock);
331
+ if (alreadyDone) {
332
+ // A detach finished it between registering and here. Don't leave the
333
+ // timer armed, and stop: its `completed` line is already out, so a
334
+ // `type=` line now would read backwards, and its sink is gone.
335
+ [self cancelQuietly:timer];
336
+ return;
337
+ }
338
+
339
+ _log([NSString stringWithFormat:@"BugseeRN data request %@ type=%@ origin=%@,%@", requestId, type,
340
+ FormatCoordinate(origin.x), FormatCoordinate(origin.y)]);
341
+ // Branch on the result, never @try: the sink catches inside Objective-C++
342
+ // (BGSRNGuardedEmit), and nothing may unwind through this ARC file, which
343
+ // is not built exception-safe.
344
+ const BOOL delivered = block(@{
345
+ @"requestId" : requestId,
346
+ @"type" : type,
347
+ @"originX" : @((double)origin.x),
348
+ @"originY" : @((double)origin.y),
349
+ });
350
+ if (!delivered) {
351
+ // A dead bridge, on the SDK's thread. JS will never answer, so answer for
352
+ // it now rather than let the SDK wait out the deadline.
353
+ [self finish:request payload:nil by:@"sink-threw"];
354
+ }
355
+ }
356
+
357
+ - (BOOL)complete:(NSString *)requestId payload:(NSString *)payload {
358
+ if (![requestId isKindOfClass:NSString.class]) {
359
+ return NO;
360
+ }
361
+ os_unfair_lock_lock(&_lock);
362
+ BGSRNDataRequest *request = _requests[requestId];
363
+ os_unfair_lock_unlock(&_lock);
364
+ NSString *data = [payload isKindOfClass:NSString.class] ? payload : nil;
365
+ return request != nil && [self finish:request payload:data by:@"js"];
366
+ }
367
+
368
+ #pragma mark - Private
369
+
370
+ /// @return YES only for the call that ran the reply.
371
+ - (BOOL)finish:(BGSRNDataRequest *)request payload:(NSString *_Nullable)payload by:(NSString *)by {
372
+ os_unfair_lock_lock(&_lock);
373
+ if (request.done) {
374
+ os_unfair_lock_unlock(&_lock);
375
+ return NO;
376
+ }
377
+ request.done = YES;
378
+ if (_requests[request.requestId] == request) {
379
+ [_requests removeObjectForKey:request.requestId];
380
+ }
381
+ id timer = request.timer;
382
+ request.timer = nil;
383
+ BGSRNDataRequestReply reply = request.reply;
384
+ request.reply = nil;
385
+ os_unfair_lock_unlock(&_lock);
386
+
387
+ // Outside the lock from here: the reply re-enters the SDK, and a cancel or
388
+ // a log line has no business holding up every other request.
389
+ if (timer != nil) {
390
+ [self cancelQuietly:timer];
391
+ }
392
+ RunQuietly(reply, payload);
393
+ [self logCompleted:request.requestId by:by payload:payload startMs:request.startMs];
394
+ return YES;
395
+ }
396
+
397
+ /// No request was minted into the table; the line still names the outcome.
398
+ - (void)answerUnminted:(NSString *)requestId
399
+ reply:(BGSRNDataRequestReply)reply
400
+ startMs:(int64_t)startMs
401
+ by:(NSString *)by {
402
+ RunQuietly(reply, nil);
403
+ [self logCompleted:requestId by:by payload:nil startMs:startMs];
404
+ }
405
+
406
+ /// `data request <id> completed by=<...> bytes=<n|null> ms=<elapsed>`,
407
+ /// verbatim from the plan's Phase 6 "Log lines".
408
+ - (void)logCompleted:(NSString *)requestId by:(NSString *)by payload:(NSString *_Nullable)payload startMs:(int64_t)startMs {
409
+ _log([NSString stringWithFormat:@"BugseeRN data request %@ completed by=%@ bytes=%@ ms=%lld", requestId, by,
410
+ BytesOf(payload), _clock() - startMs]);
411
+ }
412
+
413
+ /// The deadline's token, or nil if the scheduler threw or armed nothing.
414
+ - (nullable id)scheduleQuietly:(dispatch_block_t)task {
415
+ @try {
416
+ return _schedule(task, BGSRNDataRequestDeadlineMs);
417
+ } @catch (NSException *exception) {
418
+ NSLog(@"BugseeRN data request deadline could not be armed: %@", NSStringFromClass(exception.class));
419
+ return nil;
420
+ }
421
+ }
422
+
423
+ - (void)cancelQuietly:(id)timer {
424
+ @try {
425
+ _cancel(timer);
426
+ } @catch (NSException *exception) {
427
+ NSLog(@"BugseeRN data request timer cancel threw: %@", NSStringFromClass(exception.class));
428
+ }
429
+ }
430
+
431
+ @end
@@ -0,0 +1,40 @@
1
+ #import "BGSRNErrorMessage.h"
2
+
3
+ #import "BGSRNExceptions.h"
4
+ #import "BGSRNJSON.h"
5
+ #import "BGSRNReportOps.h"
6
+
7
+ NSString *const BGSRNForeignErrorMessage = @"an unexpected native error";
8
+ NSErrorUserInfoKey const BGSRNErrorIdentifierKey = @"BGSRNErrorIdentifier";
9
+
10
+ static BOOL IsOwn(id error) {
11
+ static NSSet<NSErrorDomain> *ownDomains;
12
+ static dispatch_once_t once;
13
+ dispatch_once(&once, ^{
14
+ ownDomains = [NSSet setWithArray:@[ BGSRNJSONErrorDomain, BGSRNReportErrorDomain, BGSRNExceptionsErrorDomain ]];
15
+ });
16
+ return [error isKindOfClass:NSError.class] && [ownDomains containsObject:((NSError *)error).domain];
17
+ }
18
+
19
+ NSString *BGSRNErrorMessage(NSError *error) {
20
+ if (!IsOwn(error)) {
21
+ return BGSRNForeignErrorMessage;
22
+ }
23
+ // The keys themselves, not `localizedDescription`: without one, Foundation
24
+ // generates text from the domain and code rather than returning nil.
25
+ NSDictionary *info = error.userInfo;
26
+ id description = info[NSLocalizedDescriptionKey];
27
+ if (![description isKindOfClass:NSString.class]) {
28
+ return BGSRNForeignErrorMessage;
29
+ }
30
+ NSString *message = description;
31
+ id identifier = info[BGSRNErrorIdentifierKey];
32
+ if ([identifier isKindOfClass:NSString.class]) {
33
+ message = [message stringByReplacingOccurrencesOfString:@"{identifier}" withString:identifier];
34
+ }
35
+ id underlying = info[NSUnderlyingErrorKey];
36
+ if (IsOwn(underlying)) {
37
+ message = [NSString stringWithFormat:@"%@: %@", message, BGSRNErrorMessage(underlying)];
38
+ }
39
+ return message;
40
+ }
@@ -0,0 +1,70 @@
1
+ #import "BGSRNEventBus.h"
2
+
3
+ static NSString *const kLifecyclePrefix = @"com.bugsee.lifecycle.";
4
+
5
+ @implementation BGSRNEventBus {
6
+ /// Weak: the bridge owns the module, and holding it here would keep a torn
7
+ /// down instance alive for the life of the process.
8
+ __weak id _sink;
9
+ BGSRNLifecycleBlock _block;
10
+ /// The SDK dispatches from its own threads while the bridge attaches and
11
+ /// detaches from the React thread.
12
+ NSLock *_lock;
13
+ }
14
+
15
+ + (BGSRNEventBus *)shared {
16
+ static BGSRNEventBus *shared = nil;
17
+ static dispatch_once_t once;
18
+ dispatch_once(&once, ^{ shared = [[BGSRNEventBus alloc] init]; });
19
+ return shared;
20
+ }
21
+
22
+ - (instancetype)init {
23
+ self = [super init];
24
+ if (self) {
25
+ _lock = [[NSLock alloc] init];
26
+ }
27
+ return self;
28
+ }
29
+
30
+ - (void)attach:(id)sink block:(BGSRNLifecycleBlock)block {
31
+ [_lock lock];
32
+ _sink = sink;
33
+ _block = [block copy];
34
+ [_lock unlock];
35
+ }
36
+
37
+ - (void)detach:(id)sink {
38
+ [_lock lock];
39
+ if (_sink == sink) {
40
+ _sink = nil;
41
+ _block = nil;
42
+ }
43
+ [_lock unlock];
44
+ }
45
+
46
+ - (void)emitLifecycle:(NSString *)rawName reportId:(NSString *)reportId {
47
+ [_lock lock];
48
+ // Copied under the lock and invoked outside it: a handler that calls back
49
+ // into the bus would otherwise deadlock on a non-recursive lock.
50
+ BGSRNLifecycleBlock block = _block;
51
+ const BOOL alive = _sink != nil;
52
+ [_lock unlock];
53
+
54
+ if (block == nil || !alive) {
55
+ return;
56
+ }
57
+
58
+ NSString *name = [rawName hasPrefix:kLifecyclePrefix]
59
+ ? [rawName substringFromIndex:kLifecyclePrefix.length]
60
+ : rawName;
61
+
62
+ // Branch on the result, never @try: the block catches inside Objective-C++
63
+ // (BGSRNGuardedEmit), and nothing may unwind through this ARC file, which
64
+ // is not built exception-safe. This runs on the SDK's dispatch thread.
65
+ if (!block(name, reportId)) {
66
+ NSLog(@"[Bugsee] failed to deliver lifecycle event %@", name);
67
+ }
68
+ }
69
+
70
+ @end
@@ -0,0 +1,63 @@
1
+ #import "BGSRNExceptions.h"
2
+
3
+ #import "BGSRNErrorMessage.h"
4
+ #import "BGSRNJSON.h"
5
+
6
+ NSString *const BGSRNReactNativeExceptionName = @"ReactNativeWebException";
7
+ NSErrorDomain const BGSRNExceptionsErrorDomain = @"BGSRNExceptionsErrorDomain";
8
+
9
+ /// Hands `made` to the caller's out-parameter. Every error this file makes is
10
+ /// built inline, with a string-literal description, so the scanner
11
+ /// (`scripts/raw-messages.ts`) can see that no value goes into it.
12
+ static id Fail(NSError **out, NSError *made) {
13
+ if (out != NULL) {
14
+ *out = made;
15
+ }
16
+ return nil;
17
+ }
18
+
19
+ @implementation BGSRNExceptionOptions
20
+ @end
21
+
22
+ @implementation BGSRNExceptions
23
+
24
+ + (BGSRNExceptionOptions *)loggingOptionsFromJSON:(NSString *)json error:(NSError **)error {
25
+ if (json == nil) {
26
+ if (error != NULL) {
27
+ *error = nil;
28
+ }
29
+ return nil;
30
+ }
31
+
32
+ NSError *parseError = nil;
33
+ NSDictionary<NSString *, id> *object = BGSRNJSONObject(json, &parseError);
34
+ if (object == nil) {
35
+ return Fail(error, [NSError errorWithDomain:BGSRNExceptionsErrorDomain
36
+ code:1
37
+ userInfo:@{
38
+ NSLocalizedDescriptionKey : @"exception options are not a JSON object",
39
+ NSUnderlyingErrorKey : parseError ?: NSNull.null,
40
+ }]);
41
+ }
42
+
43
+ BGSRNExceptionOptions *opts = [BGSRNExceptionOptions new];
44
+
45
+ id domain = object[@"domain"];
46
+ if (domain != nil) {
47
+ if (![domain isKindOfClass:NSString.class]) {
48
+ return Fail(error, [NSError errorWithDomain:BGSRNExceptionsErrorDomain
49
+ code:1
50
+ userInfo:@{NSLocalizedDescriptionKey : @"domain must be a string"}]);
51
+ }
52
+ opts.domain = (NSString *)domain;
53
+ }
54
+
55
+ // Unknown keys (labels, includeVideo, skipFrames, …) are ignored: JS already
56
+ // rejects them, and a future key must not refuse a report the facade sent.
57
+ if (error != NULL) {
58
+ *error = nil;
59
+ }
60
+ return opts;
61
+ }
62
+
63
+ @end
@@ -0,0 +1,32 @@
1
+ #import "BGSRNGuardedEmit.h"
2
+
3
+ #include <exception>
4
+ #include <typeinfo>
5
+
6
+ BOOL BGSRNGuardedEmit(dispatch_block_t emit, NSString *what) {
7
+ if (emit == nil) {
8
+ return NO;
9
+ }
10
+ // Everything is caught HERE, in Objective-C++, and never rethrown. The
11
+ // callers are plain Objective-C compiled without -fobjc-arc-exceptions, so
12
+ // an exception unwinding through one of their frames leaks every strong
13
+ // local in it; a BOOL crosses back instead.
14
+ try {
15
+ @try {
16
+ emit();
17
+ return YES;
18
+ } @catch (NSException *exception) {
19
+ NSLog(@"[Bugsee] %@ could not be emitted: %@", what, NSStringFromClass(exception.class));
20
+ return NO;
21
+ }
22
+ } catch (const std::exception &e) {
23
+ // The type only: what() is the exception's own message.
24
+ NSLog(@"[Bugsee] %@ could not be emitted: %s", what, typeid(e).name());
25
+ return NO;
26
+ } catch (...) {
27
+ // Not every C++ throw is a std::exception, and whatever this misses
28
+ // unwinds to std::terminate.
29
+ NSLog(@"[Bugsee] %@ could not be emitted: a non-std C++ exception", what);
30
+ return NO;
31
+ }
32
+ }
@@ -0,0 +1,40 @@
1
+ #import "BGSRNJSON.h"
2
+
3
+ NSErrorDomain const BGSRNJSONErrorDomain = @"BGSRNJSONErrorDomain";
4
+
5
+ /// Hands `made` to the caller's out-parameter. Every error this file makes is
6
+ /// built inline, with a string-literal description, so the scanner
7
+ /// (`scripts/raw-messages.ts`) can see that no value goes into it.
8
+ static id Fail(NSError **out, NSError *made) {
9
+ if (out != NULL) {
10
+ *out = made;
11
+ }
12
+ return nil;
13
+ }
14
+
15
+ NSDictionary<NSString *, id> *BGSRNJSONObject(NSString *json, NSError **error) {
16
+ if (![json isKindOfClass:NSString.class]) {
17
+ return Fail(error, [NSError errorWithDomain:BGSRNJSONErrorDomain
18
+ code:1
19
+ userInfo:@{NSLocalizedDescriptionKey : @"expected a JSON object, got nil"}]);
20
+ }
21
+ NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
22
+ NSError *parseError = nil;
23
+ // No reading options: containers come back immutable, NSNull stays in them,
24
+ // and a top-level fragment (a bare string or number) is an error. Trailing
25
+ // text after the object is one too.
26
+ id value = data == nil ? nil : [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
27
+ if (value == nil) {
28
+ // Fixed, never the parser's own diagnostic: that can repeat fragments of
29
+ // the malformed text, which a rejection or log line must not carry.
30
+ return Fail(error, [NSError errorWithDomain:BGSRNJSONErrorDomain
31
+ code:1
32
+ userInfo:@{NSLocalizedDescriptionKey : @"malformed JSON"}]);
33
+ }
34
+ if (![value isKindOfClass:NSDictionary.class]) {
35
+ return Fail(error, [NSError errorWithDomain:BGSRNJSONErrorDomain
36
+ code:1
37
+ userInfo:@{NSLocalizedDescriptionKey : @"expected a JSON object"}]);
38
+ }
39
+ return value;
40
+ }