@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/fatal-replay.cjs CHANGED
@@ -3,20 +3,21 @@
3
3
  const FATAL_RECORD_SCHEMA_VERSION = 1;
4
4
  const MAX_ADMITTED_FATAL_IDS = 256;
5
5
  const MAX_FATAL_FILENAME_BYTES = 512;
6
- const MAX_FATAL_STACK_BYTES = 16 * 1024;
7
6
  const MAX_FATAL_STACK_FRAMES = 24;
7
+ const MAX_FATAL_ID_LENGTH = 128;
8
+ const MAX_FATAL_RECORD_BYTES = 16 * 1024;
8
9
  const DURABLE_QUEUE = Symbol.for("co.logbrew.react-native.durable-event-queue");
9
10
 
10
11
  const admittedFatalIdsByClient = new WeakMap();
11
- let nextFatalSequence = 0;
12
12
 
13
13
  function createFatalController({
14
14
  client,
15
+ createEvent,
15
16
  eventMessage,
16
17
  fatalStore,
18
+ isErrorName,
17
19
  issue,
18
- onDiagnostic,
19
- sanitizers
20
+ onDiagnostic
20
21
  }) {
21
22
  const methods = fatalStoreMethods(fatalStore);
22
23
  const directAdmission = !methods && safeReadProperty(client, DURABLE_QUEUE) === true;
@@ -46,42 +47,47 @@ function createFatalController({
46
47
  client,
47
48
  eventMessage,
48
49
  fatalStore,
50
+ isErrorName,
49
51
  issue,
50
52
  methods,
51
53
  onDiagnostic,
52
- sanitizers,
53
54
  state
54
55
  });
55
56
  },
56
57
  store(error) {
57
58
  if (!methods) {
58
59
  return directAdmission
59
- ? admitFatalError({ client, error, eventMessage, issue, onDiagnostic, sanitizers, state })
60
+ ? admitFatalError({ client, createEvent, error, issue, onDiagnostic, state })
60
61
  : false;
61
62
  }
62
63
  return storeFatalError({
64
+ createEvent,
63
65
  error,
64
66
  fatalStore,
65
67
  methods,
66
68
  onDiagnostic,
67
- sanitizers,
68
69
  state
69
70
  });
70
71
  }
71
72
  };
72
73
  }
73
74
 
74
- function admitFatalError({ client, error, eventMessage, issue, onDiagnostic, sanitizers, state }) {
75
- const record = createFatalRecord(error, sanitizers);
76
- const before = admissionSnapshot(client);
75
+ function admitFatalError({ client, createEvent, error, issue, onDiagnostic, state }) {
76
+ let admission;
77
77
  try {
78
- issue.call(client, record.id, record.timestamp, fatalEventAttributes(record, eventMessage));
78
+ const event = createEvent(error);
79
+ admission = captureIssueWithAdmission(client, issue, event.id, event.timestamp, event.attributes);
79
80
  } catch {
80
81
  state.lastOutcome = "storage_error";
81
82
  emitDiagnostic(onDiagnostic, "fatal_store_failed");
82
83
  return false;
83
84
  }
84
- if (!wasAdmitted(before, admissionSnapshot(client))) {
85
+ if (admission === "unavailable") {
86
+ state.lastOutcome = "admission_unavailable";
87
+ emitDiagnostic(onDiagnostic, "fatal_admission_unavailable");
88
+ return false;
89
+ }
90
+ if (admission !== "admitted") {
85
91
  state.lastOutcome = "storage_error";
86
92
  emitDiagnostic(onDiagnostic, "fatal_store_failed");
87
93
  return false;
@@ -130,11 +136,11 @@ function discardPendingFatal(fatalStore, methods, state, onDiagnostic) {
130
136
  }
131
137
 
132
138
  function storeFatalError({
139
+ createEvent,
133
140
  error,
134
141
  fatalStore,
135
142
  methods,
136
143
  onDiagnostic,
137
- sanitizers,
138
144
  state
139
145
  }) {
140
146
  if (!methods) {
@@ -142,7 +148,7 @@ function storeFatalError({
142
148
  }
143
149
  let result;
144
150
  try {
145
- result = methods.write.call(fatalStore, createFatalRecord(error, sanitizers));
151
+ result = methods.write.call(fatalStore, createFatalRecord(createEvent(error)));
146
152
  } catch {
147
153
  result = undefined;
148
154
  }
@@ -172,10 +178,10 @@ function replayPendingFatal({
172
178
  client,
173
179
  eventMessage,
174
180
  fatalStore,
181
+ isErrorName,
175
182
  issue,
176
183
  methods,
177
184
  onDiagnostic,
178
- sanitizers,
179
185
  state
180
186
  }) {
181
187
  if (!methods) {
@@ -203,7 +209,7 @@ function replayPendingFatal({
203
209
  return;
204
210
  }
205
211
  const record = status === "pending"
206
- ? validatedFatalRecord(safeReadProperty(result, "record"), sanitizers)
212
+ ? validatedFatalRecord(safeReadProperty(result, "record"), isErrorName)
207
213
  : undefined;
208
214
  if (!record) {
209
215
  state.lastOutcome = "replay_failed";
@@ -218,16 +224,22 @@ function replayPendingFatal({
218
224
  return;
219
225
  }
220
226
 
221
- const before = admissionSnapshot(client);
227
+ let admission;
222
228
  try {
223
- issue.call(client, record.id, record.timestamp, fatalEventAttributes(record, eventMessage));
229
+ admission = captureIssueWithAdmission(
230
+ client, issue, record.id, record.timestamp, fatalEventAttributes(record, eventMessage)
231
+ );
224
232
  } catch {
225
233
  state.lastOutcome = "replay_failed";
226
234
  emitDiagnostic(onDiagnostic, "fatal_replay_failed");
227
235
  return;
228
236
  }
229
- const after = admissionSnapshot(client);
230
- if (!wasAdmitted(before, after)) {
237
+ if (admission === "unavailable") {
238
+ state.lastOutcome = "admission_unavailable";
239
+ emitDiagnostic(onDiagnostic, "fatal_replay_admission_unavailable");
240
+ return;
241
+ }
242
+ if (admission !== "admitted") {
231
243
  state.lastOutcome = "replay_not_admitted";
232
244
  emitDiagnostic(onDiagnostic, "fatal_replay_not_admitted");
233
245
  return;
@@ -255,28 +267,16 @@ function acknowledgeFatalRecord(fatalStore, methods, recordId, state, onDiagnost
255
267
  emitDiagnostic(onDiagnostic, "fatal_acknowledge_failed");
256
268
  }
257
269
 
258
- function admissionSnapshot(client) {
259
- const pendingEvents = safeFunction(client, "pendingEvents");
260
- const droppedEvents = safeFunction(client, "droppedEvents");
261
- if (!pendingEvents || !droppedEvents) {
262
- return undefined;
263
- }
264
- try {
265
- const pending = pendingEvents.call(client);
266
- const dropped = droppedEvents.call(client);
267
- return isBoundedCounter(pending) && isBoundedCounter(dropped)
268
- ? { dropped, pending }
269
- : undefined;
270
- } catch {
271
- return undefined;
270
+ function captureIssueWithAdmission(client, issue, id, timestamp, attributes) {
271
+ const capture = safeFunction(client, "issueWithAdmission");
272
+ if (!capture) {
273
+ issue.call(client, id, timestamp, attributes);
274
+ return "unavailable";
272
275
  }
273
- }
274
-
275
- function wasAdmitted(before, after) {
276
- return before !== undefined
277
- && after !== undefined
278
- && after.pending === before.pending + 1
279
- && after.dropped === before.dropped;
276
+ const status = resultStatus(capture.call(client, id, timestamp, attributes));
277
+ return status === "admitted" || status === "filtered" || status === "dropped"
278
+ ? status
279
+ : "unavailable";
280
280
  }
281
281
 
282
282
  function wasAdmittedInRuntime(client, recordId) {
@@ -301,39 +301,23 @@ function rememberAdmittedFatalId(client, recordId) {
301
301
  }
302
302
  }
303
303
 
304
- function createFatalRecord(error, sanitizers) {
305
- return Object.freeze({
304
+ function createFatalRecord(event) {
305
+ const record = {
306
306
  corruptRecords: 0,
307
307
  droppedRecords: 0,
308
- errorName: sanitizers.errorName(error),
309
- id: nextFatalEventId(),
308
+ errorName: event.attributes.exception.type,
309
+ id: event.id,
310
310
  schemaVersion: FATAL_RECORD_SCHEMA_VERSION,
311
- stackFrames: fatalStackFrames(error, sanitizers.stackFrame),
312
- timestamp: new Date().toISOString()
313
- });
314
- }
315
-
316
- function fatalStackFrames(error, stackFrame) {
317
- const stack = safeReadProperty(error, "stack");
318
- const candidate = typeof stack === "string"
319
- ? stack.slice(0, MAX_FATAL_STACK_BYTES)
320
- : "";
321
- const frames = [];
322
- for (const line of candidate.split(/\r?\n/u)) {
323
- const frame = stackFrame(line);
324
- const filename = frame ? normalizedStoredFatalFilename(frame.filename) : undefined;
325
- if (filename && validFatalFilename(filename)) {
326
- frames.push(Object.freeze({
327
- column: frame.column,
328
- filename,
329
- line: frame.line
330
- }));
331
- if (frames.length === MAX_FATAL_STACK_FRAMES) {
332
- break;
333
- }
334
- }
311
+ stackFrames: (event.attributes.stackFrames ?? []).flatMap(({ column, filename, line }) => {
312
+ const stored = normalizedStoredFatalFilename(filename);
313
+ return validFatalFilename(stored) ? [{ column, filename: stored, line }] : [];
314
+ }).slice(0, MAX_FATAL_STACK_FRAMES),
315
+ timestamp: event.timestamp
316
+ };
317
+ while (record.stackFrames.length > 0 && utf8ByteLength(JSON.stringify(record)) > MAX_FATAL_RECORD_BYTES) {
318
+ record.stackFrames.pop();
335
319
  }
336
- return Object.freeze(frames);
320
+ return record;
337
321
  }
338
322
 
339
323
  function fatalEventAttributes(record, eventMessage) {
@@ -364,7 +348,7 @@ function fatalEventAttributes(record, eventMessage) {
364
348
  });
365
349
  }
366
350
 
367
- function validatedFatalRecord(value, sanitizers) {
351
+ function validatedFatalRecord(value, isErrorName) {
368
352
  try {
369
353
  if (!isPlainObject(value)
370
354
  || !hasExactKeys(value, [
@@ -375,31 +359,41 @@ function validatedFatalRecord(value, sanitizers) {
375
359
  "schemaVersion",
376
360
  "stackFrames",
377
361
  "timestamp"
378
- ])
379
- || value.schemaVersion !== FATAL_RECORD_SCHEMA_VERSION
380
- || typeof value.id !== "string"
381
- || !/^evt_rn_fatal_[a-z0-9]+(?:_[a-z0-9]+)*$/u.test(value.id)
382
- || !sanitizers.isErrorName(value.errorName)
383
- || !isBoundedCounter(value.corruptRecords)
384
- || !isBoundedCounter(value.droppedRecords)
385
- || !validFatalTimestamp(value.timestamp)
386
- || !Array.isArray(value.stackFrames)
387
- || value.stackFrames.length > MAX_FATAL_STACK_FRAMES) {
362
+ ])) {
388
363
  return undefined;
389
364
  }
390
- const frames = value.stackFrames.map(validatedFatalFrame);
391
- if (frames.some((frame) => frame === undefined)) {
365
+ const { corruptRecords, droppedRecords, errorName, id, schemaVersion, stackFrames, timestamp } = value;
366
+ const frameCount = Array.isArray(stackFrames) ? stackFrames.length : undefined;
367
+ if (schemaVersion !== FATAL_RECORD_SCHEMA_VERSION
368
+ || typeof id !== "string"
369
+ || id.length > MAX_FATAL_ID_LENGTH
370
+ || !/^evt_rn_fatal_[a-z0-9]+(?:_[a-z0-9]+)*$/u.test(id)
371
+ || !isErrorName(errorName)
372
+ || !isBoundedCounter(corruptRecords)
373
+ || !isBoundedCounter(droppedRecords)
374
+ || !validFatalTimestamp(timestamp)
375
+ || !isBoundedCounter(frameCount)
376
+ || frameCount > MAX_FATAL_STACK_FRAMES) {
392
377
  return undefined;
393
378
  }
394
- return {
395
- corruptRecords: value.corruptRecords,
396
- droppedRecords: value.droppedRecords,
397
- errorName: value.errorName,
398
- id: value.id,
379
+ const frames = [];
380
+ for (let index = 0; index < frameCount; index += 1) {
381
+ const frame = validatedFatalFrame(stackFrames[index]);
382
+ if (!frame) {
383
+ return undefined;
384
+ }
385
+ frames.push(frame);
386
+ }
387
+ const record = {
388
+ corruptRecords,
389
+ droppedRecords,
390
+ errorName,
391
+ id,
399
392
  schemaVersion: FATAL_RECORD_SCHEMA_VERSION,
400
393
  stackFrames: frames,
401
- timestamp: value.timestamp
394
+ timestamp
402
395
  };
396
+ return utf8ByteLength(JSON.stringify(record)) <= MAX_FATAL_RECORD_BYTES ? record : undefined;
403
397
  } catch {
404
398
  return undefined;
405
399
  }
@@ -408,16 +402,19 @@ function validatedFatalRecord(value, sanitizers) {
408
402
  function validatedFatalFrame(value) {
409
403
  try {
410
404
  if (!isPlainObject(value)
411
- || !hasExactKeys(value, ["column", "filename", "line"])
412
- || !validFatalFilename(value.filename)
413
- || positiveInteger(value.line) === undefined
414
- || positiveInteger(value.column) === undefined) {
405
+ || !hasExactKeys(value, ["column", "filename", "line"])) {
406
+ return undefined;
407
+ }
408
+ const { column, filename, line } = value;
409
+ if (!validFatalFilename(filename)
410
+ || positiveInteger(line) === undefined
411
+ || positiveInteger(column) === undefined) {
415
412
  return undefined;
416
413
  }
417
414
  return {
418
- column: value.column,
419
- filename: value.filename,
420
- line: value.line
415
+ column,
416
+ filename,
417
+ line
421
418
  };
422
419
  } catch {
423
420
  return undefined;
@@ -456,9 +453,8 @@ function validFatalTimestamp(value) {
456
453
 
457
454
  function resultStatus(result) {
458
455
  try {
459
- return isPlainObject(result) && typeof result.status === "string"
460
- ? result.status
461
- : undefined;
456
+ const status = isPlainObject(result) ? result.status : undefined;
457
+ return typeof status === "string" ? status : undefined;
462
458
  } catch {
463
459
  return undefined;
464
460
  }
@@ -519,16 +515,7 @@ function hasControlCharacter(value) {
519
515
  }
520
516
 
521
517
  function positiveInteger(value) {
522
- if (!/^[1-9][0-9]*$/u.test(String(value))) {
523
- return undefined;
524
- }
525
- const number = Number(value);
526
- return Number.isSafeInteger(number) && number <= 2147483647 ? number : undefined;
527
- }
528
-
529
- function nextFatalEventId() {
530
- nextFatalSequence = incrementBounded(nextFatalSequence);
531
- return `evt_rn_fatal_${Date.now().toString(36)}_${nextFatalSequence.toString(36)}`;
518
+ return Number.isSafeInteger(value) && value > 0 && value <= 2147483647 ? value : undefined;
532
519
  }
533
520
 
534
521
  function incrementBounded(value) {
@@ -571,4 +558,4 @@ function isObjectLike(value) {
571
558
  return value !== null && (typeof value === "object" || typeof value === "function");
572
559
  }
573
560
 
574
- module.exports = { createFatalController };
561
+ module.exports = { captureIssueWithAdmission, createFatalController };