@logbrew/react-native 0.1.34 → 0.1.36

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 (33) hide show
  1. package/README.md +33 -4
  2. package/android/src/main/java/co/logbrew/reactnative/AndroidDiagnosticsRuntime.java +2 -0
  3. package/android/src/main/java/co/logbrew/reactnative/AndroidNativeDiagnostics.java +56 -2
  4. package/android/src/main/java/co/logbrew/reactnative/EventRecordStore.java +25 -0
  5. package/android/src/main/java/co/logbrew/reactnative/FatalStoreModuleImpl.java +30 -3
  6. package/android/src/newarch/java/co/logbrew/reactnative/FatalStoreModule.java +6 -0
  7. package/android/src/oldarch/java/co/logbrew/reactnative/FatalStoreModule.java +6 -0
  8. package/expo.cjs +1 -1
  9. package/fatal-replay.cjs +97 -110
  10. package/global-errors.cjs +94 -130
  11. package/global-errors.d.cts +23 -3
  12. package/global-errors.d.ts +23 -3
  13. package/index.cjs +47 -20
  14. package/index.d.cts +1 -0
  15. package/index.d.ts +1 -0
  16. package/index.native.js +4 -3
  17. package/instrumentation.cjs +80 -57
  18. package/ios/AppleDiagnostics/LBRNAppleNativeDiagnostics.swift +1 -1
  19. package/ios/GeneratedAppleDiagnostics/LogBrewCrash/NativeCrashCorrelation.swift +2 -0
  20. package/ios/GeneratedAppleDiagnostics/LogBrewCrash/NativeCrashReplay.swift +31 -7
  21. package/ios/GeneratedAppleDiagnostics/SOURCE-MANIFEST.json +2 -2
  22. package/ios/LBRNEventRecordStore.h +1 -0
  23. package/ios/LBRNEventRecordStore.m +43 -0
  24. package/ios/LBRNFatalStoreModule.mm +14 -0
  25. package/metadata.cjs +1 -1
  26. package/native-bridge.cjs +112 -8
  27. package/native-bridge.d.cts +34 -0
  28. package/native-bridge.d.ts +34 -0
  29. package/native-bridge.js +2 -1
  30. package/package.json +1 -1
  31. package/persistent-delivery.native.js +12 -1
  32. package/promise-rejections.cjs +11 -1
  33. package/src/NativeLogBrewFatalStore.ts +4 -0
package/README.md CHANGED
@@ -80,7 +80,9 @@ For mobile apps, prefer an app-scoped public key through `clientKey`. Expo
80
80
  inlines `EXPO_PUBLIC_*` values into the app, so never use a server key there.
81
81
  `apiKey` is still accepted for compatibility with lower-level SDK examples.
82
82
 
83
- Supplying `transport` enables the core SDK's bounded automatic delivery. It
83
+ Supplying `transport` enables the core SDK's bounded automatic delivery. On
84
+ mobile it sends 5 seconds after the first queued event or at 50 queued events,
85
+ which saves battery and radio wakeups; set `deliveryIntervalMs` to change it. It
84
86
  coalesces concurrent work, retries transient failures, retains rejected
85
87
  batches, and pauses repeated automatic sends after authentication, rate-limit,
86
88
  or non-retryable failures. Do not add a second app-owned flush interval.
@@ -160,7 +162,10 @@ The native queue stores one compact event per atomic record, is limited to
160
162
  when you configure them on the client. Both platforms use app-private storage.
161
163
  The client key is not written to disk; its SHA-256 digest separates queues
162
164
  after key rotation. The queue does not provide mathematically exactly-once
163
- delivery.
165
+ delivery. When the queue is full, a fatal JavaScript report removes the oldest
166
+ events that are not fatal and are not being sent, and counts each one as
167
+ `evicted_for_fatal` in `deliveryHealth().droppedByReason`. The fatal is dropped
168
+ only when the rest of the queue is fatal reports or a batch being sent.
164
169
 
165
170
  A successful `shutdown()` drains and closes the queue. A failed flush or
166
171
  shutdown leaves the exact remainder available to the same client and the next
@@ -283,7 +288,16 @@ status. The Node ESM and CommonJS entries never import React Native;
283
288
  non-React-Native callers may still inject the documented advanced `fatalStore`
284
289
  adapter.
285
290
 
286
- Automatic events exclude the original error message, raw stack, arbitrary metadata, full URLs, hosts, query strings, local absolute paths, payloads, and native error text. `onDiagnostic` receives only a fixed code. This error-handler integration does not claim mathematically exactly-once delivery, native crash capture, ANR or hang detection, or symbolication. The client-level persistent queue owns restart delivery.
291
+ Automatic events keep structured stack frames with the file basename, line,
292
+ column, function name, and the bundle Debug ID that symbolication needs,
293
+ including Hermes bytecode positions. They also keep the error type, the client
294
+ breadcrumbs and context (such as release and session), and the last route that
295
+ the screen or navigation helpers recorded, as `metadata.lastRoute`. They
296
+ exclude the original error message, raw stack text, arbitrary metadata, full
297
+ URLs, hosts, query strings, local directories, payloads, and native error text.
298
+ The exception chain marks the removed message as `redacted`. Records replayed
299
+ from an injected `fatalStore` keep only the file basename, line, and column of
300
+ each frame. `onDiagnostic` receives only a fixed code. This error-handler integration does not claim mathematically exactly-once delivery, native crash capture, ANR or hang detection, or symbolication. The client-level persistent queue owns restart delivery.
287
301
 
288
302
  ### Apple native crash and app-hang diagnostics
289
303
 
@@ -297,7 +311,9 @@ main-thread hangs.
297
311
  Android diagnostics are opt-in and require a native build. They persist Java
298
312
  crashes, one NDK fault frame, and main-thread hangs to the normal app-private
299
313
  event queue. NDK evidence enters that queue on the next launch and remains
300
- until queue admission succeeds. Native signal records retain the project,
314
+ until queue admission succeeds. A Java crash or hang report that finds the
315
+ queue full waits in a separate app-private pending store and enters the queue
316
+ when diagnostics are next installed with room. Native signal records retain the project,
301
317
  release, environment, and service active at crash time, so an app update before
302
318
  relaunch cannot reassign the crash to a newer build.
303
319
 
@@ -330,6 +346,12 @@ Capture excludes messages, thread names, console output, local variables,
330
346
  request data, authentication values, device identifiers, and raw tombstones. Java and
331
347
  hang reports keep at most 32 frames; NDK reports keep one build ID and offset.
332
348
 
349
+ React Native ends a fatal JavaScript error on Android with a
350
+ `JavascriptException` crash. When the global error handler persisted that
351
+ fatal report in the same process less than 10 seconds earlier, Android
352
+ diagnostics skip the crash once, so one JavaScript fatal produces one issue.
353
+ Otherwise the crash is recorded as usual.
354
+
333
355
  `uninstallLogBrewAndroidNativeDiagnostics()` reinstates prior
334
356
  handlers, stops the watchdog, and leaves queued events available. After closing
335
357
  the client, purge only for an explicit discard. Upload exact-release Android
@@ -425,6 +447,13 @@ clears both the JavaScript history and its native crash snapshot. Missing,
425
447
  corrupt, and captured breadcrumb state remains explicit on replay; corrupt
426
448
  optional context never discards the crash.
427
449
 
450
+ React Native ends a fatal JavaScript error on iOS with an `RCTFatalException`.
451
+ After the global error handler stores that fatal report, it adds a
452
+ `react-native.fatal` breadcrumb. When that breadcrumb is the last one in the
453
+ native crash report, replay discards the `RCTFatalException` report and counts
454
+ it as `discarded`, so one JavaScript fatal produces one issue. Otherwise the
455
+ native crash replays as usual.
456
+
428
457
  Installation creates app-private, data-protected storage that iOS excludes from
429
458
  device data archives. Pending reports are partitioned by project, so changing
430
459
  the configured project does not replay an earlier project's records with the
@@ -35,6 +35,8 @@ final class AndroidDiagnosticsRuntime implements Thread.UncaughtExceptionHandler
35
35
  new AndroidNativeDiagnostics(
36
36
  eventStore,
37
37
  signalStore,
38
+ new EventRecordStore(
39
+ new File(storageRoot, "logbrew-android-pending-v1"), new AndroidParentDirectorySync()),
38
40
  configuration,
39
41
  scheduler,
40
42
  afterPersist,
@@ -85,9 +85,15 @@ final class AndroidNativeDiagnostics {
85
85
  private static final int MAX_FRAMES = 32;
86
86
  static final long WATCHDOG_POLL_MS = 500;
87
87
  private static final AtomicLong SEQUENCE = new AtomicLong();
88
+ private static final String JAVASCRIPT_FATAL_RECORD = "{\"type\":\"issue\",\"id\":\"evt_rn_fatal_";
89
+ private static final String JAVASCRIPT_EXCEPTION = "com.facebook.react.common.JavascriptException";
90
+ private static final long JAVASCRIPT_FATAL_WINDOW_NANOS = 10_000_000_000L;
91
+ private static long javaScriptFatalPersistedAtNanos;
92
+ private static boolean javaScriptFatalUnclaimed;
88
93
 
89
94
  final EventRecordStore eventStore;
90
95
  final AndroidNativeSignalStore signalStore;
96
+ final EventRecordStore pendingStore;
91
97
  final Configuration configuration;
92
98
  private final Scheduler scheduler;
93
99
  private final Runnable afterPersist;
@@ -100,6 +106,7 @@ final class AndroidNativeDiagnostics {
100
106
  AndroidNativeDiagnostics(
101
107
  EventRecordStore eventStore,
102
108
  AndroidNativeSignalStore signalStore,
109
+ EventRecordStore pendingStore,
103
110
  Configuration configuration,
104
111
  Scheduler scheduler,
105
112
  Runnable afterPersist,
@@ -108,6 +115,7 @@ final class AndroidNativeDiagnostics {
108
115
  String applicationPackage) {
109
116
  this.eventStore = eventStore;
110
117
  this.signalStore = signalStore;
118
+ this.pendingStore = pendingStore;
111
119
  this.configuration = configuration;
112
120
  this.scheduler = scheduler;
113
121
  this.afterPersist = afterPersist;
@@ -122,6 +130,7 @@ final class AndroidNativeDiagnostics {
122
130
  return "already_installed";
123
131
  }
124
132
  replaySignalRecord();
133
+ replayPendingRecords();
125
134
  scheduler.start(configuration.anrThresholdMs, this::reportHang);
126
135
  installed = true;
127
136
  return "installed";
@@ -138,7 +147,9 @@ final class AndroidNativeDiagnostics {
138
147
 
139
148
  void handleUncaught(Thread thread, Throwable error) {
140
149
  try {
141
- append(javaCrashEvent(error));
150
+ if (!claimPersistedJavaScriptFatal(error, System.nanoTime())) {
151
+ appendOrKeepPending(javaCrashEvent(error));
152
+ }
142
153
  } finally {
143
154
  previousHandler.uncaughtException(thread, error);
144
155
  }
@@ -150,7 +161,33 @@ final class AndroidNativeDiagnostics {
150
161
  }
151
162
  long boundedDuration =
152
163
  Math.max(configuration.anrThresholdMs, Math.min(durationMs, 300_000));
153
- append(hangEvent(mainFrames.get(), boundedDuration));
164
+ appendOrKeepPending(hangEvent(mainFrames.get(), boundedDuration));
165
+ }
166
+
167
+ /** Notes a JavaScript fatal report that this process just persisted to the shared queue. */
168
+ static synchronized void notePersistedRecord(String serializedEvent, long nowNanos) {
169
+ if (serializedEvent != null
170
+ && serializedEvent.startsWith(JAVASCRIPT_FATAL_RECORD)
171
+ && !serializedEvent.contains("\"replayed\":true")) {
172
+ javaScriptFatalPersistedAtNanos = nowNanos;
173
+ javaScriptFatalUnclaimed = true;
174
+ }
175
+ }
176
+
177
+ // React Native ends a JavaScript fatal by throwing JavascriptException right after
178
+ // the global handler runs. Skip that copy once, and only shortly after a persisted
179
+ // report, so a stale report can never hide a later crash.
180
+ static synchronized boolean claimPersistedJavaScriptFatal(Throwable error, long nowNanos) {
181
+ Throwable cause = error;
182
+ for (int depth = 0; cause != null && depth < 8; depth++, cause = cause.getCause()) {
183
+ if (JAVASCRIPT_EXCEPTION.equals(cause.getClass().getName())) {
184
+ boolean claimed = javaScriptFatalUnclaimed
185
+ && nowNanos - javaScriptFatalPersistedAtNanos <= JAVASCRIPT_FATAL_WINDOW_NANOS;
186
+ javaScriptFatalUnclaimed = false;
187
+ return claimed;
188
+ }
189
+ }
190
+ return false;
154
191
  }
155
192
 
156
193
  static boolean shouldReportHang(
@@ -165,6 +202,23 @@ final class AndroidNativeDiagnostics {
165
202
  }
166
203
  }
167
204
 
205
+ // A full or failing queue keeps the report in the pending store; the next install appends it.
206
+ private void appendOrKeepPending(String serializedEvent) {
207
+ if (!append(serializedEvent)) {
208
+ pendingStore.append(serializedEvent, serializedEvent.getBytes(StandardCharsets.UTF_8).length);
209
+ }
210
+ }
211
+
212
+ private void replayPendingRecords() {
213
+ EventRecordStore.Result pending = pendingStore.load();
214
+ int appended = 0;
215
+ while (appended < pending.records.size()
216
+ && append(pending.records.get(appended).serializedEvent)) {
217
+ appended += 1;
218
+ }
219
+ pendingStore.acknowledge(appended);
220
+ }
221
+
168
222
  private boolean append(String serializedEvent) {
169
223
  int bytes = serializedEvent.getBytes(StandardCharsets.UTF_8).length;
170
224
  EventRecordStore.Result result = eventStore.append(serializedEvent, bytes);
@@ -99,6 +99,31 @@ final class EventRecordStore {
99
99
  return Result.status("acknowledged");
100
100
  }
101
101
 
102
+ /** Deletes queued records at ascending load() positions so a fatal report can take their place. */
103
+ synchronized Result remove(int[] indexes) {
104
+ Snapshot snapshot = snapshot();
105
+ int previous = -1;
106
+ for (int index : indexes) {
107
+ if (snapshot == null || index <= previous || index >= snapshot.records.size()) {
108
+ return Result.status("storage_error");
109
+ }
110
+ previous = index;
111
+ }
112
+ for (int index : indexes) {
113
+ File recordFile = new File(directory, eventFileName(snapshot.records.get(index).sequence));
114
+ if (!isSafeRegularFile(recordFile) || !recordFile.delete()) {
115
+ // Earlier deletions may have committed; fail closed instead of diverging from the client.
116
+ poisoned = true;
117
+ return Result.status("storage_error");
118
+ }
119
+ }
120
+ if (parentDirectorySync.sync(directory) == ParentDirectorySyncResult.FAILED) {
121
+ poisoned = true;
122
+ return Result.status("storage_error");
123
+ }
124
+ return Result.status("removed");
125
+ }
126
+
102
127
  synchronized Result purge() {
103
128
  if (poisoned || !prepareDirectory() || !removeStaleTemporaryFiles()) {
104
129
  return Result.status("storage_error");
@@ -2,6 +2,7 @@ package co.logbrew.reactnative;
2
2
 
3
3
  import com.facebook.react.bridge.Arguments;
4
4
  import com.facebook.react.bridge.ReactApplicationContext;
5
+ import com.facebook.react.bridge.ReadableArray;
5
6
  import com.facebook.react.bridge.ReadableMap;
6
7
  import com.facebook.react.bridge.ReadableMapKeySetIterator;
7
8
  import com.facebook.react.bridge.ReadableType;
@@ -72,9 +73,14 @@ final class FatalStoreModuleImpl {
72
73
  try {
73
74
  EventRecordStore eventStore = eventStore(queueKey);
74
75
  Integer byteCount = integer(eventBytes);
75
- return eventStore == null || byteCount == null
76
- ? status("storage_error")
77
- : eventResultMap(eventStore.append(serializedEvent, byteCount));
76
+ if (eventStore == null || byteCount == null) {
77
+ return status("storage_error");
78
+ }
79
+ EventRecordStore.Result result = eventStore.append(serializedEvent, byteCount);
80
+ if ("appended".equals(result.status)) {
81
+ AndroidNativeDiagnostics.notePersistedRecord(serializedEvent, System.nanoTime());
82
+ }
83
+ return eventResultMap(result);
78
84
  } catch (RuntimeException error) {
79
85
  return status("storage_error");
80
86
  }
@@ -92,6 +98,27 @@ final class FatalStoreModuleImpl {
92
98
  }
93
99
  }
94
100
 
101
+ WritableMap removeEventRecords(String queueKey, ReadableArray indexes) {
102
+ try {
103
+ EventRecordStore eventStore = eventStore(queueKey);
104
+ int[] recordIndexes = new int[indexes == null ? 0 : indexes.size()];
105
+ for (int position = 0; position < recordIndexes.length; position += 1) {
106
+ Integer index = indexes.getType(position) == ReadableType.Number
107
+ ? integer(indexes.getDouble(position))
108
+ : null;
109
+ if (index == null) {
110
+ return status("storage_error");
111
+ }
112
+ recordIndexes[position] = index;
113
+ }
114
+ return eventStore == null || indexes == null
115
+ ? status("storage_error")
116
+ : eventResultMap(eventStore.remove(recordIndexes));
117
+ } catch (RuntimeException error) {
118
+ return status("storage_error");
119
+ }
120
+ }
121
+
95
122
  WritableMap purgeEventRecords(String queueKey) {
96
123
  try {
97
124
  EventRecordStore eventStore = eventStore(queueKey);
@@ -1,6 +1,7 @@
1
1
  package co.logbrew.reactnative;
2
2
 
3
3
  import com.facebook.react.bridge.ReactApplicationContext;
4
+ import com.facebook.react.bridge.ReadableArray;
4
5
  import com.facebook.react.bridge.ReadableMap;
5
6
  import com.facebook.react.bridge.WritableMap;
6
7
 
@@ -38,6 +39,11 @@ final class FatalStoreModule extends NativeLogBrewFatalStoreSpec {
38
39
  return implementation.acknowledgeEventRecords(queueKey, count);
39
40
  }
40
41
 
42
+ @Override
43
+ public WritableMap removeEventRecords(String queueKey, ReadableArray indexes) {
44
+ return implementation.removeEventRecords(queueKey, indexes);
45
+ }
46
+
41
47
  @Override
42
48
  public WritableMap purgeEventRecords(String queueKey) {
43
49
  return implementation.purgeEventRecords(queueKey);
@@ -3,6 +3,7 @@ package co.logbrew.reactnative;
3
3
  import com.facebook.react.bridge.ReactApplicationContext;
4
4
  import com.facebook.react.bridge.ReactContextBaseJavaModule;
5
5
  import com.facebook.react.bridge.ReactMethod;
6
+ import com.facebook.react.bridge.ReadableArray;
6
7
  import com.facebook.react.bridge.ReadableMap;
7
8
  import com.facebook.react.bridge.WritableMap;
8
9
 
@@ -40,6 +41,11 @@ final class FatalStoreModule extends ReactContextBaseJavaModule {
40
41
  return implementation.acknowledgeEventRecords(queueKey, count);
41
42
  }
42
43
 
44
+ @ReactMethod(isBlockingSynchronousMethod = true)
45
+ public WritableMap removeEventRecords(String queueKey, ReadableArray indexes) {
46
+ return implementation.removeEventRecords(queueKey, indexes);
47
+ }
48
+
43
49
  @ReactMethod(isBlockingSynchronousMethod = true)
44
50
  public WritableMap purgeEventRecords(String queueKey) {
45
51
  return implementation.purgeEventRecords(queueKey);
package/expo.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  const PLUGIN_NAME = "@logbrew/react-native/expo";
2
2
  const START_MARKER = " # @generated by @logbrew/react-native/expo";
3
3
  const END_MARKER = " # @end @logbrew/react-native/expo";
4
- const MANAGED_BLOCK = / # @generated by @logbrew\/react-native\/expo[\s\S]*? # @end @logbrew\/react-native\/expo\n?/gu;
4
+ const MANAGED_BLOCK = / {2}# @generated by @logbrew\/react-native\/expo[\s\S]*? {2}# @end @logbrew\/react-native\/expo\n?/gu;
5
5
  const POD_BLOCK = [
6
6
  START_MARKER,
7
7
  " require 'pathname'",