@logbrew/react-native 0.1.39 → 0.1.40

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 (96) hide show
  1. package/android-native-diagnostics.d.ts +15 -5
  2. package/android-native-diagnostics.js +170 -94
  3. package/apollo.cjs +435 -226
  4. package/apollo.d.cts +2 -70
  5. package/apollo.d.ts +24 -12
  6. package/apollo.js +438 -221
  7. package/apple-native-diagnostics-config.js +176 -0
  8. package/apple-native-diagnostics-context.js +232 -0
  9. package/apple-native-diagnostics-module.js +229 -0
  10. package/apple-native-diagnostics.d.ts +42 -11
  11. package/apple-native-diagnostics.js +60 -389
  12. package/expo.cjs +29 -16
  13. package/expo.d.ts +10 -9
  14. package/expo.js +4 -3
  15. package/fatal-admission.cjs +113 -0
  16. package/fatal-record.cjs +317 -0
  17. package/fatal-replay.cjs +297 -508
  18. package/global-errors-capture.cjs +369 -0
  19. package/global-errors-event.cjs +174 -0
  20. package/global-errors-support.cjs +128 -0
  21. package/global-errors.cjs +75 -351
  22. package/global-errors.d.cts +29 -278
  23. package/global-errors.d.ts +57 -27
  24. package/global-errors.js +7 -2
  25. package/global-errors.native.js +83 -48
  26. package/index-breadcrumbs.cjs +139 -0
  27. package/index-errors.cjs +167 -0
  28. package/index-events.cjs +365 -0
  29. package/index-navigation.cjs +327 -0
  30. package/index-random.cjs +120 -0
  31. package/index-react.cjs +189 -0
  32. package/index-spans.cjs +190 -0
  33. package/index-trace-fetch.cjs +322 -0
  34. package/index-trace.cjs +335 -0
  35. package/index-transport.cjs +201 -0
  36. package/index-values.cjs +232 -0
  37. package/index.cjs +95 -1243
  38. package/index.d.cts +2 -505
  39. package/index.d.ts +130 -469
  40. package/index.native.d.ts +146 -22
  41. package/index.native.js +184 -159
  42. package/instrumentation-fetch.cjs +113 -0
  43. package/instrumentation-install.cjs +241 -0
  44. package/instrumentation-runtime.cjs +65 -0
  45. package/instrumentation-teardown.cjs +93 -0
  46. package/instrumentation-values.cjs +282 -0
  47. package/instrumentation-xhr-request.cjs +298 -0
  48. package/instrumentation-xhr.cjs +233 -0
  49. package/instrumentation.cjs +110 -599
  50. package/instrumentation.d.cts +2 -84
  51. package/instrumentation.d.ts +22 -8
  52. package/ios/AppleDiagnostics/LBRNAppleNativeDiagnostics.swift +1 -1
  53. package/lifecycle.cjs +181 -78
  54. package/lifecycle.d.cts +1 -50
  55. package/lifecycle.d.ts +17 -6
  56. package/metadata.cjs +279 -114
  57. package/metro-debug-id.cjs +296 -0
  58. package/metro-serializer.cjs +219 -0
  59. package/metro.cjs +153 -297
  60. package/metro.d.cts +2 -57
  61. package/metro.d.ts +27 -13
  62. package/metro.js +2 -3
  63. package/native-bridge-async.cjs +165 -0
  64. package/native-bridge-scope.cjs +148 -0
  65. package/native-bridge.cjs +83 -186
  66. package/native-bridge.d.cts +2 -94
  67. package/native-bridge.d.ts +28 -15
  68. package/native-client-bindings.js +230 -0
  69. package/native-diagnostics-values.js +120 -0
  70. package/native-module-lookup.js +31 -0
  71. package/native-null.js +3 -0
  72. package/null-value.cjs +4 -0
  73. package/package.json +43 -1
  74. package/persistent-delivery-store.js +247 -0
  75. package/persistent-delivery.native.js +183 -261
  76. package/promise-rejection-handlers.cjs +368 -0
  77. package/promise-rejections.cjs +169 -361
  78. package/react-native.config.js +3 -1
  79. package/release-artifacts-files.cjs +94 -0
  80. package/release-artifacts-options.cjs +182 -0
  81. package/release-artifacts.cjs +278 -207
  82. package/release-artifacts.d.cts +2 -55
  83. package/release-artifacts.d.ts +18 -7
  84. package/release-artifacts.js +2 -4
  85. package/resource-fetch-graphql.cjs +265 -0
  86. package/resource-fetch-size.cjs +156 -0
  87. package/resource-fetch.cjs +227 -428
  88. package/resource-fetch.d.cts +1 -60
  89. package/resource-fetch.d.ts +17 -6
  90. package/scripts/sync-apple-native-sources.mjs +101 -53
  91. package/types/capture.d.ts +95 -0
  92. package/types/config.d.ts +85 -0
  93. package/types/defaults.d.ts +55 -0
  94. package/types/events.d.ts +203 -0
  95. package/types/provider.d.ts +81 -0
  96. package/types/trace.d.ts +104 -0
package/global-errors.cjs CHANGED
@@ -1,380 +1,104 @@
1
1
  "use strict";
2
2
 
3
- const { createReactNativeErrorEvent } = require("./index.cjs");
4
- const { captureIssueWithAdmission, createFatalController } = require("./fatal-replay.cjs");
3
+ const { createFatalController } = require("./fatal-replay.cjs");
4
+ const { GlobalErrorCapture, activeInstallation, inactiveInstallation } = require("./global-errors-capture.cjs");
5
+ const { createAutomaticErrorEvent, isSafeErrorName } = require("./global-errors-event.cjs");
6
+ const { DiagnosticGate, emitDiagnostic, isFunction, isObjectLike, safeFunction } = require("./global-errors-support.cjs");
5
7
  const {
6
8
  createLogBrewReactNativePromiseRejectionHandlers,
7
9
  installLogBrewReactNativePromiseRejectionTracker
8
10
  } = require("./promise-rejections.cjs");
9
11
 
10
- const AUTOMATIC_ERROR_MESSAGE = "React Native global JavaScript report";
11
- const MAX_STACK_BYTES = 16 * 1024;
12
- const SAFE_ERROR_NAMES = new Set([
13
- "Error",
14
- "EvalError",
15
- "RangeError",
16
- "ReferenceError",
17
- "SyntaxError",
18
- "TypeError",
19
- "URIError"
20
- ]);
21
- const LAST_ROUTE = Symbol.for("co.logbrew.react-native.last-route");
22
- const installations = new WeakMap();
23
- let nextEventSequence = 0;
24
-
25
- function installLogBrewReactNativeGlobalErrorHandler({
26
- client,
27
- errorUtils = globalThis?.ErrorUtils,
28
- fatalStore,
29
- onDiagnostic
30
- } = {}) {
31
- const issue = safeFunction(client, "issue");
32
- const getGlobalHandler = safeFunction(errorUtils, "getGlobalHandler");
33
- const setGlobalHandler = safeFunction(errorUtils, "setGlobalHandler");
34
- if (!issue || !getGlobalHandler || !setGlobalHandler || !isObjectLike(errorUtils)) {
35
- emitDiagnostic(onDiagnostic, "handler_unavailable");
36
- return inactiveInstallation();
37
- }
12
+ /**
13
+ * @import { UnknownFunction } from "./global-errors-support.cjs"
14
+ * @import { LogBrewReactNativeGlobalErrorHandlerInstallation } from "./global-errors.cjs"
15
+ * @typedef {Readonly<{
16
+ * client: object,
17
+ * errorUtils: object,
18
+ * fatalStore: unknown,
19
+ * getGlobalHandler: UnknownFunction,
20
+ * issue: UnknownFunction,
21
+ * onDiagnostic: unknown,
22
+ * previousHandler: UnknownFunction,
23
+ * setGlobalHandler: UnknownFunction
24
+ * }>} InstallationInput
25
+ * @typedef {Readonly<{ client?: unknown, errorUtils?: unknown, fatalStore?: unknown, onDiagnostic?: unknown }>} InstallOptions
26
+ */
38
27
 
39
- const existing = installations.get(errorUtils);
40
- if (existing?.health().active) {
41
- return existing;
42
- }
28
+ const AUTOMATIC_ERROR_MESSAGE = "React Native global JavaScript report";
43
29
 
44
- let previousHandler;
30
+ /**
31
+ * @param {UnknownFunction} getGlobalHandler
32
+ * @param {object} errorUtils
33
+ * @returns {UnknownFunction | undefined}
34
+ */
35
+ function readPreviousHandler(getGlobalHandler, errorUtils) {
45
36
  try {
46
- previousHandler = getGlobalHandler.call(errorUtils);
37
+ const previousHandler = getGlobalHandler.call(errorUtils);
38
+ return isFunction(previousHandler) ? previousHandler : undefined;
47
39
  } catch {
48
- emitDiagnostic(onDiagnostic, "handler_unavailable");
49
- return inactiveInstallation();
50
- }
51
- if (typeof previousHandler !== "function") {
52
- emitDiagnostic(onDiagnostic, "handler_unavailable");
53
- return inactiveInstallation();
40
+ return undefined;
54
41
  }
42
+ }
55
43
 
56
- const state = {
57
- active: true,
58
- capturedEvents: 0,
59
- emittingDiagnostic: false,
60
- handling: false,
61
- lastOutcome: "idle",
62
- suppressedEvents: 0
63
- };
44
+ /**
45
+ * @param {InstallationInput} input
46
+ * @returns {LogBrewReactNativeGlobalErrorHandlerInstallation}
47
+ */
48
+ function startCapture(input) {
49
+ const { client, fatalStore, issue, onDiagnostic } = input;
50
+ const diagnostics = new DiagnosticGate(onDiagnostic);
51
+ /**
52
+ * @param {Readonly<{ code: string }>} diagnostic
53
+ * @returns {void}
54
+ */
55
+ function forwardDiagnostic(diagnostic) {
56
+ diagnostics.emit(diagnostic.code);
57
+ }
64
58
  const fatal = createFatalController({
65
59
  client,
66
60
  createEvent: (error) => createAutomaticErrorEvent(error, client, true),
67
61
  eventMessage: AUTOMATIC_ERROR_MESSAGE,
68
62
  fatalStore,
69
- isErrorName: (value) => SAFE_ERROR_NAMES.has(value),
63
+ isErrorName: isSafeErrorName,
70
64
  issue,
71
- onDiagnostic: (diagnostic) => emitStateDiagnostic(state, onDiagnostic, diagnostic.code)
72
- });
73
- const capturedErrors = new WeakSet();
74
- const handler = (error, isFatal) => {
75
- if (!state.active) {
76
- previousHandler(error, isFatal);
77
- return;
78
- }
79
- if (state.handling) {
80
- recordSuppression(state, onDiagnostic, "recursive_capture_suppressed");
81
- return;
82
- }
83
- state.handling = true;
84
- try {
85
- if (isFatal === true) {
86
- if (fatal.available) {
87
- if (fatal.store(error)) {
88
- state.lastOutcome = "fatal_stored";
89
- markPersistedFatal(client);
90
- } else {
91
- state.lastOutcome = fatal.health().lastOutcome === "admission_unavailable"
92
- ? "admission_unavailable"
93
- : "fatal_store_failed";
94
- }
95
- } else {
96
- state.lastOutcome = "fatal_unsupported";
97
- emitStateDiagnostic(state, onDiagnostic, "fatal_capture_requires_sync_store");
98
- }
99
- } else if (isObjectLike(error) && capturedErrors.has(error)) {
100
- recordSuppression(state, onDiagnostic, "duplicate_capture_suppressed");
101
- } else {
102
- captureNonfatalError(client, issue, error, capturedErrors, state, onDiagnostic);
103
- }
104
- } finally {
105
- try {
106
- if (typeof previousHandler === "function") {
107
- previousHandler(error, isFatal);
108
- }
109
- } finally {
110
- state.handling = false;
111
- }
112
- }
113
- };
114
-
115
- const installation = Object.freeze({
116
- discardPendingFatalRecord() {
117
- return fatal.discard();
118
- },
119
- fatalHealth() {
120
- return fatal.health();
121
- },
122
- health() {
123
- return healthSnapshot(state);
124
- },
125
- remove() {
126
- if (!state.active) {
127
- return false;
128
- }
129
- let ownsHandler;
130
- try {
131
- ownsHandler = getGlobalHandler.call(errorUtils) === handler;
132
- } catch {
133
- return false;
134
- }
135
- if (!ownsHandler) {
136
- state.active = false;
137
- state.lastOutcome = "handler_replaced";
138
- installations.delete(errorUtils);
139
- return false;
140
- }
141
- try {
142
- setGlobalHandler.call(errorUtils, previousHandler);
143
- } catch {
144
- state.lastOutcome = "remove_failed";
145
- return false;
146
- }
147
- state.active = false;
148
- state.lastOutcome = "removed";
149
- installations.delete(errorUtils);
150
- return true;
151
- }
152
- });
153
-
154
- try {
155
- setGlobalHandler.call(errorUtils, handler);
156
- } catch {
157
- state.active = false;
158
- try {
159
- if (getGlobalHandler.call(errorUtils) === handler) {
160
- setGlobalHandler.call(errorUtils, previousHandler);
161
- }
162
- } catch {
163
- // An inactive wrapper only delegates if a platform setter failed after mutation.
164
- }
165
- emitDiagnostic(onDiagnostic, "handler_unavailable");
166
- return inactiveInstallation();
167
- }
168
- installations.set(errorUtils, installation);
169
- fatal.replay();
170
- return installation;
171
- }
172
-
173
- // The last breadcrumb tells Apple crash capture that the JavaScript report already
174
- // holds this crash, so the RCTFatalException that React Native raises next is not
175
- // reported twice.
176
- function markPersistedFatal(client) {
177
- try {
178
- client.addBreadcrumb({ category: "react-native.fatal", level: "fatal", type: "error" });
179
- } catch {
180
- // The report is stored; without the marker the native copy is only a duplicate.
181
- }
182
- }
183
-
184
- function captureNonfatalError(client, issue, error, capturedErrors, state, onDiagnostic) {
185
- const tracksIdentity = isObjectLike(error);
186
- if (tracksIdentity) {
187
- capturedErrors.add(error);
188
- }
189
- try {
190
- const event = createAutomaticErrorEvent(error, client);
191
- const admission = captureIssueWithAdmission(client, issue, event.id, event.timestamp, event.attributes);
192
- if (admission !== "admitted") {
193
- if (tracksIdentity) {
194
- capturedErrors.delete(error);
195
- }
196
- state.lastOutcome = admission === "unavailable"
197
- ? "admission_unavailable"
198
- : admission === "filtered" ? "filtered" : "capture_failed";
199
- emitStateDiagnostic(state, onDiagnostic, admission === "unavailable"
200
- ? "capture_admission_unavailable"
201
- : admission === "filtered" ? "capture_filtered" : "capture_failed");
202
- return;
203
- }
204
- state.capturedEvents = incrementBounded(state.capturedEvents);
205
- state.lastOutcome = "captured";
206
- } catch {
207
- if (tracksIdentity) {
208
- capturedErrors.delete(error);
209
- }
210
- state.lastOutcome = "capture_failed";
211
- emitStateDiagnostic(state, onDiagnostic, "capture_failed");
212
- }
213
- }
214
-
215
- function createAutomaticErrorEvent(error, client, fatal = false) {
216
- const sanitizedError = new Error(AUTOMATIC_ERROR_MESSAGE);
217
- sanitizedError.name = safeErrorName(error);
218
- sanitizedError.stack = automaticStack(error);
219
- const lastRoute = safeReadProperty(client, LAST_ROUTE);
220
- const event = createReactNativeErrorEvent(sanitizedError, {
221
- id: nextEventId(fatal ? "fatal" : "global"),
222
- handled: false,
223
- includeStack: false,
224
- level: fatal ? "fatal" : "error",
225
- mechanism: "react_native_error_utils"
65
+ onDiagnostic: forwardDiagnostic
226
66
  });
227
- const { exceptionChain, metadata, stackFrames } = event.attributes;
228
- // Frames keep file basenames only; Debug IDs were already matched on the full path.
229
- return {
230
- ...event,
231
- attributes: {
232
- ...event.attributes,
233
- exceptionChain: {
234
- ...exceptionChain,
235
- entries: exceptionChain.entries.map((entry) => ({
236
- ...entry,
237
- ...(entry.stackFrames ? { stackFrames: basenameFrames(entry.stackFrames) } : {})
238
- }))
239
- },
240
- ...(stackFrames ? { stackFrames: basenameFrames(stackFrames) } : {}),
241
- title: AUTOMATIC_ERROR_MESSAGE,
242
- message: AUTOMATIC_ERROR_MESSAGE,
243
- metadata: {
244
- ...metadata,
245
- ...(metadata.errorFrameFile ? { errorFrameFile: basename(metadata.errorFrameFile) } : {}),
246
- ...(metadata.releaseArtifactCodeFile
247
- ? { releaseArtifactCodeFile: basename(metadata.releaseArtifactCodeFile) }
248
- : {}),
249
- ...(typeof metadata.issueGroupingKey === "string"
250
- ? { issueGroupingKey: metadata.issueGroupingKey.replace(/^([^:]+:[^:]+:).*[\\/]/u, "$1") }
251
- : {}),
252
- automatic: true,
253
- fatal,
254
- handled: !fatal,
255
- ...(typeof lastRoute === "string" ? { lastRoute } : {}),
256
- mechanism: "react_native_error_utils",
257
- source: "react-native.global_error"
258
- }
259
- }
260
- };
261
- }
262
-
263
- // Hermes and V8 put the message before the frames. Keep only frame lines so
264
- // message text cannot become a frame, even when the message changed later.
265
- function automaticStack(error) {
266
- const stack = safeReadProperty(error, "stack");
267
- if (typeof stack !== "string") {
268
- return "";
269
- }
270
- const lines = stack.slice(0, MAX_STACK_BYTES).split(/\r?\n/u);
271
- const frames = lines.filter((line) => line.trimStart().startsWith("at "));
272
- return (frames.length > 0 ? frames : lines).join("\n");
67
+ return new GlobalErrorCapture({ ...input, diagnostics, fatal }).install();
273
68
  }
274
69
 
275
- function basenameFrames(frames) {
276
- return frames?.map((frame) => ({ ...frame, filename: basename(frame.filename) }));
277
- }
278
-
279
- function basename(value) {
280
- return typeof value === "string" ? value.replace(/^.*[\\/]/u, "") : value;
281
- }
282
-
283
- function safeErrorName(error) {
284
- const name = safeReadProperty(error, "name");
285
- const candidate = typeof name === "string" ? name : "Error";
286
- return SAFE_ERROR_NAMES.has(candidate) ? candidate : "Error";
287
- }
288
-
289
- function safeReadProperty(value, property) {
290
- if (!isObjectLike(value)) {
291
- return undefined;
292
- }
293
- try {
294
- return value[property];
295
- } catch {
296
- return undefined;
297
- }
298
- }
299
-
300
- function safeFunction(value, property) {
301
- const candidate = safeReadProperty(value, property);
302
- return typeof candidate === "function" ? candidate : undefined;
303
- }
304
-
305
- function nextEventId(kind) {
306
- nextEventSequence = incrementBounded(nextEventSequence);
307
- return `evt_rn_${kind}_${Date.now().toString(36)}_${nextEventSequence.toString(36)}`;
308
- }
309
-
310
- function incrementBounded(value) {
311
- return value >= Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER : value + 1;
312
- }
313
-
314
- function recordSuppression(state, onDiagnostic, code) {
315
- state.suppressedEvents = incrementBounded(state.suppressedEvents);
316
- state.lastOutcome = code === "recursive_capture_suppressed" ? "recursive_suppressed" : "duplicate_suppressed";
317
- emitStateDiagnostic(state, onDiagnostic, code);
318
- }
319
-
320
- function emitStateDiagnostic(state, onDiagnostic, code) {
321
- if (state.emittingDiagnostic) {
322
- return;
323
- }
324
- state.emittingDiagnostic = true;
325
- try {
326
- emitDiagnostic(onDiagnostic, code);
327
- } finally {
328
- state.emittingDiagnostic = false;
70
+ /**
71
+ * @param {Omit<InstallationInput, "previousHandler">} input
72
+ * @returns {LogBrewReactNativeGlobalErrorHandlerInstallation}
73
+ */
74
+ function installHandler(input) {
75
+ const previousHandler = readPreviousHandler(input.getGlobalHandler, input.errorUtils);
76
+ if (!previousHandler) {
77
+ emitDiagnostic(input.onDiagnostic, "handler_unavailable");
78
+ return inactiveInstallation();
329
79
  }
80
+ return startCapture({ ...input, previousHandler });
330
81
  }
331
82
 
332
- function emitDiagnostic(onDiagnostic, code) {
333
- if (typeof onDiagnostic !== "function") {
334
- return;
335
- }
336
- try {
337
- onDiagnostic(Object.freeze({ code }));
338
- } catch {
339
- // Diagnostics must never interfere with application error handling.
83
+ /**
84
+ * @param {InstallOptions} [options]
85
+ * @returns {LogBrewReactNativeGlobalErrorHandlerInstallation}
86
+ */
87
+ function installLogBrewReactNativeGlobalErrorHandler({
88
+ client,
89
+ errorUtils = Reflect.get(globalThis, "ErrorUtils"),
90
+ fatalStore,
91
+ onDiagnostic
92
+ } = {}) {
93
+ const issue = safeFunction(client, "issue");
94
+ const getGlobalHandler = safeFunction(errorUtils, "getGlobalHandler");
95
+ const setGlobalHandler = safeFunction(errorUtils, "setGlobalHandler");
96
+ if (!issue || !getGlobalHandler || !setGlobalHandler || !isObjectLike(errorUtils) || !isObjectLike(client)) {
97
+ emitDiagnostic(onDiagnostic, "handler_unavailable");
98
+ return inactiveInstallation();
340
99
  }
341
- }
342
-
343
- function inactiveInstallation() {
344
- const snapshot = Object.freeze({
345
- active: false,
346
- capturedEvents: 0,
347
- lastOutcome: "unavailable",
348
- suppressedEvents: 0
349
- });
350
- const fatalSnapshot = Object.freeze({
351
- acknowledgedRecords: 0,
352
- available: false,
353
- corruptRecords: 0,
354
- droppedRecords: 0,
355
- lastOutcome: "unavailable",
356
- replayedRecords: 0,
357
- storedRecords: 0
358
- });
359
- return Object.freeze({
360
- discardPendingFatalRecord: () => false,
361
- fatalHealth: () => fatalSnapshot,
362
- health: () => snapshot,
363
- remove: () => false
364
- });
365
- }
366
-
367
- function healthSnapshot(state) {
368
- return Object.freeze({
369
- active: state.active,
370
- capturedEvents: state.capturedEvents,
371
- lastOutcome: state.lastOutcome,
372
- suppressedEvents: state.suppressedEvents
373
- });
374
- }
375
-
376
- function isObjectLike(value) {
377
- return value !== null && (typeof value === "object" || typeof value === "function");
100
+ return activeInstallation(errorUtils)
101
+ ?? installHandler({ client, errorUtils, fatalStore, getGlobalHandler, issue, onDiagnostic, setGlobalHandler });
378
102
  }
379
103
 
380
104
  const defaultExport = {