@logbrew/react-native 0.1.39 → 0.1.41

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 (99) hide show
  1. package/README.md +3 -3
  2. package/android-native-diagnostics.d.ts +15 -5
  3. package/android-native-diagnostics.js +170 -94
  4. package/apollo.cjs +435 -226
  5. package/apollo.d.cts +2 -70
  6. package/apollo.d.ts +24 -12
  7. package/apollo.js +438 -221
  8. package/apple-native-diagnostics-config.js +176 -0
  9. package/apple-native-diagnostics-context.js +232 -0
  10. package/apple-native-diagnostics-module.js +229 -0
  11. package/apple-native-diagnostics.d.ts +42 -11
  12. package/apple-native-diagnostics.js +60 -389
  13. package/expo.cjs +29 -16
  14. package/expo.d.ts +10 -9
  15. package/expo.js +4 -3
  16. package/fatal-admission.cjs +113 -0
  17. package/fatal-record.cjs +317 -0
  18. package/fatal-replay.cjs +297 -508
  19. package/global-errors-capture.cjs +369 -0
  20. package/global-errors-event.cjs +174 -0
  21. package/global-errors-support.cjs +128 -0
  22. package/global-errors.cjs +75 -351
  23. package/global-errors.d.cts +29 -278
  24. package/global-errors.d.ts +57 -27
  25. package/global-errors.js +7 -2
  26. package/global-errors.native.js +83 -48
  27. package/index-breadcrumbs.cjs +139 -0
  28. package/index-errors.cjs +167 -0
  29. package/index-events.cjs +365 -0
  30. package/index-navigation.cjs +327 -0
  31. package/index-random.cjs +120 -0
  32. package/index-react.cjs +189 -0
  33. package/index-spans.cjs +190 -0
  34. package/index-trace-fetch.cjs +322 -0
  35. package/index-trace.cjs +335 -0
  36. package/index-transport.cjs +201 -0
  37. package/index-values.cjs +232 -0
  38. package/index.cjs +97 -1245
  39. package/index.d.cts +2 -505
  40. package/index.d.ts +130 -469
  41. package/index.native.d.ts +146 -22
  42. package/index.native.js +184 -159
  43. package/instrumentation-fetch.cjs +113 -0
  44. package/instrumentation-install.cjs +241 -0
  45. package/instrumentation-runtime.cjs +65 -0
  46. package/instrumentation-teardown.cjs +93 -0
  47. package/instrumentation-values.cjs +282 -0
  48. package/instrumentation-xhr-request.cjs +298 -0
  49. package/instrumentation-xhr.cjs +233 -0
  50. package/instrumentation.cjs +110 -599
  51. package/instrumentation.d.cts +2 -84
  52. package/instrumentation.d.ts +22 -8
  53. package/ios/AppleDiagnostics/LBRNAppleNativeDiagnostics.swift +1 -1
  54. package/ios/GeneratedAppleDiagnostics/LogBrew/DeliveryLifecycle.swift +1 -1
  55. package/ios/GeneratedAppleDiagnostics/SOURCE-MANIFEST.json +1 -1
  56. package/lifecycle.cjs +181 -78
  57. package/lifecycle.d.cts +1 -50
  58. package/lifecycle.d.ts +17 -6
  59. package/metadata.cjs +279 -114
  60. package/metro-debug-id.cjs +296 -0
  61. package/metro-serializer.cjs +219 -0
  62. package/metro.cjs +153 -297
  63. package/metro.d.cts +2 -57
  64. package/metro.d.ts +27 -13
  65. package/metro.js +2 -3
  66. package/native-bridge-async.cjs +165 -0
  67. package/native-bridge-scope.cjs +148 -0
  68. package/native-bridge.cjs +83 -186
  69. package/native-bridge.d.cts +2 -94
  70. package/native-bridge.d.ts +28 -15
  71. package/native-client-bindings.js +230 -0
  72. package/native-diagnostics-values.js +120 -0
  73. package/native-module-lookup.js +31 -0
  74. package/native-null.js +3 -0
  75. package/null-value.cjs +4 -0
  76. package/package.json +43 -1
  77. package/persistent-delivery-store.js +247 -0
  78. package/persistent-delivery.native.js +183 -261
  79. package/promise-rejection-handlers.cjs +368 -0
  80. package/promise-rejections.cjs +169 -361
  81. package/react-native.config.js +3 -1
  82. package/release-artifacts-files.cjs +94 -0
  83. package/release-artifacts-options.cjs +182 -0
  84. package/release-artifacts.cjs +278 -207
  85. package/release-artifacts.d.cts +2 -55
  86. package/release-artifacts.d.ts +18 -7
  87. package/release-artifacts.js +2 -4
  88. package/resource-fetch-graphql.cjs +265 -0
  89. package/resource-fetch-size.cjs +156 -0
  90. package/resource-fetch.cjs +227 -428
  91. package/resource-fetch.d.cts +1 -60
  92. package/resource-fetch.d.ts +17 -6
  93. package/scripts/sync-apple-native-sources.mjs +101 -53
  94. package/types/capture.d.ts +95 -0
  95. package/types/config.d.ts +85 -0
  96. package/types/defaults.d.ts +55 -0
  97. package/types/events.d.ts +203 -0
  98. package/types/provider.d.ts +81 -0
  99. package/types/trace.d.ts +104 -0
@@ -0,0 +1,369 @@
1
+ "use strict";
2
+
3
+ const { captureIssueWithAdmission } = require("./fatal-replay.cjs");
4
+ const { createAutomaticErrorEvent } = require("./global-errors-event.cjs");
5
+ const { emitDiagnostic, incrementBounded, isFunction, isObjectLike } = require("./global-errors-support.cjs");
6
+
7
+ /**
8
+ * @import { DiagnosticEmitter, UnknownFunction } from "./global-errors-support.cjs"
9
+ * @import { Admission } from "./fatal-admission.cjs"
10
+ * @import { FatalController } from "./fatal-replay.cjs"
11
+ * @import {
12
+ * LogBrewReactNativeGlobalErrorHandlerInstallation,
13
+ * ReactNativeFatalReplayHealth,
14
+ * ReactNativeGlobalErrorHealth
15
+ * } from "./global-errors.cjs"
16
+ * @typedef {ReactNativeGlobalErrorHealth["lastOutcome"]} GlobalErrorOutcome
17
+ * @typedef {Readonly<{
18
+ * client: object,
19
+ * diagnostics: DiagnosticEmitter,
20
+ * errorUtils: object,
21
+ * fatal: FatalController,
22
+ * getGlobalHandler: UnknownFunction,
23
+ * issue: UnknownFunction,
24
+ * onDiagnostic: unknown,
25
+ * previousHandler: UnknownFunction,
26
+ * setGlobalHandler: UnknownFunction
27
+ * }>} GlobalErrorCaptureOptions
28
+ */
29
+
30
+ const NO_COUNT = 0;
31
+ /** @type {Readonly<Record<"dropped" | "failed" | "filtered" | "unavailable", readonly [GlobalErrorOutcome, string]>>} */
32
+ const NONFATAL_FAILURES = {
33
+ dropped: ["capture_failed", "capture_failed"],
34
+ failed: ["capture_failed", "capture_failed"],
35
+ filtered: ["filtered", "capture_filtered"],
36
+ unavailable: ["admission_unavailable", "capture_admission_unavailable"]
37
+ };
38
+
39
+ /** @type {WeakMap<object, LogBrewReactNativeGlobalErrorHandlerInstallation>} */
40
+ const installations = new WeakMap();
41
+
42
+ /**
43
+ * @returns {LogBrewReactNativeGlobalErrorHandlerInstallation}
44
+ */
45
+ function inactiveInstallation() {
46
+ const snapshot = Object.freeze({
47
+ active: false,
48
+ capturedEvents: NO_COUNT,
49
+ lastOutcome: "unavailable",
50
+ suppressedEvents: NO_COUNT
51
+ });
52
+ /** @type {ReactNativeFatalReplayHealth} */
53
+ const fatalSnapshot = Object.freeze({
54
+ acknowledgedRecords: NO_COUNT,
55
+ available: false,
56
+ corruptRecords: NO_COUNT,
57
+ droppedRecords: NO_COUNT,
58
+ lastOutcome: "unavailable",
59
+ replayedRecords: NO_COUNT,
60
+ storedRecords: NO_COUNT
61
+ });
62
+ return Object.freeze({
63
+ discardPendingFatalRecord: () => false,
64
+ fatalHealth: () => fatalSnapshot,
65
+ health: () => snapshot,
66
+ remove: () => false
67
+ });
68
+ }
69
+
70
+ /**
71
+ * @param {object} client
72
+ * @returns {boolean}
73
+ */
74
+ function markPersistedFatal(client) {
75
+ try {
76
+ /** @type {unknown} */
77
+ const addBreadcrumb = Reflect.get(client, "addBreadcrumb");
78
+ const breadcrumb = { category: "react-native.fatal", level: "fatal", type: "error" };
79
+ if (!isFunction(addBreadcrumb)) {
80
+ return false;
81
+ }
82
+ Reflect.apply(addBreadcrumb, client, [breadcrumb]);
83
+ return true;
84
+ } catch {
85
+ return false;
86
+ }
87
+ }
88
+
89
+ class GlobalErrorCapture {
90
+ active = true;
91
+ capturedEvents = NO_COUNT;
92
+ /** @type {WeakSet<object>} */
93
+ capturedErrors = new WeakSet();
94
+ handling = false;
95
+ /** @type {GlobalErrorOutcome} */
96
+ lastOutcome = "idle";
97
+ suppressedEvents = NO_COUNT;
98
+
99
+ /**
100
+ * @param {GlobalErrorCaptureOptions} options
101
+ */
102
+ constructor(options) {
103
+ this.options = options;
104
+ /**
105
+ * @param {unknown} error
106
+ * @param {unknown} isFatal
107
+ * @returns {void}
108
+ */
109
+ const handler = (error, isFatal) => {
110
+ this.handle(error, isFatal);
111
+ };
112
+ this.handler = handler;
113
+ }
114
+
115
+ /**
116
+ * @returns {ReactNativeGlobalErrorHealth}
117
+ */
118
+ health() {
119
+ return Object.freeze({
120
+ active: this.active,
121
+ capturedEvents: this.capturedEvents,
122
+ lastOutcome: this.lastOutcome,
123
+ suppressedEvents: this.suppressedEvents
124
+ });
125
+ }
126
+
127
+ /**
128
+ * @param {"duplicate_capture_suppressed" | "recursive_capture_suppressed"} code
129
+ * @returns {void}
130
+ */
131
+ recordSuppression(code) {
132
+ this.suppressedEvents = incrementBounded(this.suppressedEvents);
133
+ this.lastOutcome = code === "recursive_capture_suppressed" ? "recursive_suppressed" : "duplicate_suppressed";
134
+ this.options.diagnostics.emit(code);
135
+ }
136
+
137
+ /**
138
+ * @param {unknown} error
139
+ * @returns {void}
140
+ */
141
+ captureFatal(error) {
142
+ const { client, diagnostics, fatal } = this.options;
143
+ if (!fatal.available) {
144
+ this.lastOutcome = "fatal_unsupported";
145
+ diagnostics.emit("fatal_capture_requires_sync_store");
146
+ } else if (fatal.store(error)) {
147
+ this.lastOutcome = "fatal_stored";
148
+ markPersistedFatal(client);
149
+ } else {
150
+ this.lastOutcome = fatal.health().lastOutcome === "admission_unavailable"
151
+ ? "admission_unavailable"
152
+ : "fatal_store_failed";
153
+ }
154
+ }
155
+
156
+ /**
157
+ * @param {unknown} error
158
+ * @returns {Admission | "failed"}
159
+ */
160
+ admitNonfatal(error) {
161
+ const { client, issue } = this.options;
162
+ try {
163
+ const event = createAutomaticErrorEvent(error, client);
164
+ return captureIssueWithAdmission(client, issue, event.id, event.timestamp, event.attributes);
165
+ } catch {
166
+ return "failed";
167
+ }
168
+ }
169
+
170
+ /**
171
+ * @param {unknown} error
172
+ * @returns {void}
173
+ */
174
+ captureNonfatal(error) {
175
+ if (isObjectLike(error)) {
176
+ this.capturedErrors.add(error);
177
+ }
178
+ const admission = this.admitNonfatal(error);
179
+ if (admission === "admitted") {
180
+ this.capturedEvents = incrementBounded(this.capturedEvents);
181
+ this.lastOutcome = "captured";
182
+ } else {
183
+ this.recordNonfatalFailure(error, admission);
184
+ }
185
+ }
186
+
187
+ /**
188
+ * @param {unknown} error
189
+ * @param {Exclude<Admission, "admitted"> | "failed"} admission
190
+ * @returns {void}
191
+ */
192
+ recordNonfatalFailure(error, admission) {
193
+ if (isObjectLike(error)) {
194
+ this.capturedErrors.delete(error);
195
+ }
196
+ const [outcome, code] = NONFATAL_FAILURES[admission];
197
+ this.lastOutcome = outcome;
198
+ this.options.diagnostics.emit(code);
199
+ }
200
+
201
+ /**
202
+ * @param {unknown} error
203
+ * @param {unknown} isFatal
204
+ * @returns {void}
205
+ */
206
+ capture(error, isFatal) {
207
+ if (isFatal === true) {
208
+ this.captureFatal(error);
209
+ } else if (isObjectLike(error) && this.capturedErrors.has(error)) {
210
+ this.recordSuppression("duplicate_capture_suppressed");
211
+ } else {
212
+ this.captureNonfatal(error);
213
+ }
214
+ }
215
+
216
+ /**
217
+ * @param {unknown} error
218
+ * @param {unknown} isFatal
219
+ * @returns {void}
220
+ */
221
+ handle(error, isFatal) {
222
+ const { previousHandler } = this.options;
223
+ if (!this.active) {
224
+ previousHandler(error, isFatal);
225
+ return;
226
+ }
227
+ if (this.handling) {
228
+ this.recordSuppression("recursive_capture_suppressed");
229
+ return;
230
+ }
231
+ this.handleOnce(error, isFatal);
232
+ }
233
+
234
+ /**
235
+ * @param {unknown} error
236
+ * @param {unknown} isFatal
237
+ * @returns {void}
238
+ */
239
+ handleOnce(error, isFatal) {
240
+ const { previousHandler } = this.options;
241
+ this.handling = true;
242
+ try {
243
+ this.capture(error, isFatal);
244
+ } finally {
245
+ try {
246
+ previousHandler(error, isFatal);
247
+ } finally {
248
+ this.handling = false;
249
+ }
250
+ }
251
+ }
252
+
253
+ /**
254
+ * @returns {boolean | undefined}
255
+ */
256
+ ownsHandler() {
257
+ const { errorUtils, getGlobalHandler } = this.options;
258
+ try {
259
+ return getGlobalHandler.call(errorUtils) === this.handler;
260
+ } catch {
261
+ return undefined;
262
+ }
263
+ }
264
+
265
+ /**
266
+ * @param {GlobalErrorOutcome} outcome
267
+ * @returns {void}
268
+ */
269
+ deactivate(outcome) {
270
+ this.active = false;
271
+ this.lastOutcome = outcome;
272
+ installations.delete(this.options.errorUtils);
273
+ }
274
+
275
+ /**
276
+ * @returns {boolean}
277
+ */
278
+ remove() {
279
+ if (!this.active) {
280
+ return false;
281
+ }
282
+ const owns = this.ownsHandler();
283
+ if (owns === false) {
284
+ this.deactivate("handler_replaced");
285
+ }
286
+ return owns === true && this.restoreForRemoval();
287
+ }
288
+
289
+ /**
290
+ * @returns {boolean}
291
+ */
292
+ restoreForRemoval() {
293
+ const { errorUtils, previousHandler, setGlobalHandler } = this.options;
294
+ try {
295
+ setGlobalHandler.call(errorUtils, previousHandler);
296
+ } catch {
297
+ this.lastOutcome = "remove_failed";
298
+ return false;
299
+ }
300
+ this.deactivate("removed");
301
+ return true;
302
+ }
303
+
304
+ /**
305
+ * @returns {boolean}
306
+ */
307
+ restorePreviousHandler() {
308
+ const { errorUtils, previousHandler, setGlobalHandler } = this.options;
309
+ try {
310
+ if (this.ownsHandler() === true) {
311
+ setGlobalHandler.call(errorUtils, previousHandler);
312
+ }
313
+ return true;
314
+ } catch {
315
+ return false;
316
+ }
317
+ }
318
+
319
+ /**
320
+ * @returns {LogBrewReactNativeGlobalErrorHandlerInstallation}
321
+ */
322
+ installationHandle() {
323
+ const { fatal } = this.options;
324
+ return Object.freeze({
325
+ discardPendingFatalRecord: () => fatal.discard(),
326
+ fatalHealth: () => fatal.health(),
327
+ health: () => this.health(),
328
+ remove: () => this.remove()
329
+ });
330
+ }
331
+
332
+ /**
333
+ * @param {unknown} onDiagnostic
334
+ * @returns {LogBrewReactNativeGlobalErrorHandlerInstallation}
335
+ */
336
+ failInstallation(onDiagnostic) {
337
+ this.active = false;
338
+ this.restorePreviousHandler();
339
+ emitDiagnostic(onDiagnostic, "handler_unavailable");
340
+ return inactiveInstallation();
341
+ }
342
+
343
+ /**
344
+ * @returns {LogBrewReactNativeGlobalErrorHandlerInstallation}
345
+ */
346
+ install() {
347
+ const { errorUtils, fatal, onDiagnostic, setGlobalHandler } = this.options;
348
+ const installation = this.installationHandle();
349
+ try {
350
+ setGlobalHandler.call(errorUtils, this.handler);
351
+ } catch {
352
+ return this.failInstallation(onDiagnostic);
353
+ }
354
+ installations.set(errorUtils, installation);
355
+ fatal.replay();
356
+ return installation;
357
+ }
358
+ }
359
+
360
+ /**
361
+ * @param {unknown} value
362
+ * @returns {LogBrewReactNativeGlobalErrorHandlerInstallation | undefined}
363
+ */
364
+ function activeInstallation(value) {
365
+ const existing = isObjectLike(value) ? installations.get(value) : undefined;
366
+ return existing?.health().active === true ? existing : undefined;
367
+ }
368
+
369
+ module.exports = { GlobalErrorCapture, activeInstallation, inactiveInstallation };
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+
3
+ const { createReactNativeErrorEvent } = require("./index.cjs");
4
+ const { incrementBounded, safeReadProperty } = require("./global-errors-support.cjs");
5
+
6
+ /**
7
+ * @import { IssueExceptionChain, IssueStackFrame, Metadata } from "@logbrew/sdk"
8
+ * @import { ReactNativeErrorEvent } from "./index.cjs"
9
+ */
10
+
11
+ /**
12
+ * @template Value
13
+ * @typedef {Value extends readonly (infer Item)[]
14
+ * ? readonly DeepReadonly<Item>[]
15
+ * : Value extends object ? { readonly [Key in keyof Value]: DeepReadonly<Value[Key]> } : Value} DeepReadonly
16
+ */
17
+
18
+ const AUTOMATIC_ERROR_MESSAGE = "React Native global JavaScript report";
19
+ const MAX_STACK_BYTES = 16_384;
20
+ const FIRST_INDEX = 0;
21
+ const NO_FRAMES = 0;
22
+ const ID_RADIX = 36;
23
+ const SAFE_ERROR_NAMES = new Set([
24
+ "Error",
25
+ "EvalError",
26
+ "RangeError",
27
+ "ReferenceError",
28
+ "SyntaxError",
29
+ "TypeError",
30
+ "URIError"
31
+ ]);
32
+ const LAST_ROUTE = Symbol.for("co.logbrew.react-native.last-route");
33
+ const LINE_BREAK_PATTERN = /\r?\n/u;
34
+ const DIRECTORY_PREFIX_PATTERN = /^.*[\\/]/u;
35
+ const GROUPING_PATH_PATTERN = /^(?<prefix>[^:]+:[^:]+:).*[\\/]/u;
36
+ const sequence = { next: FIRST_INDEX };
37
+
38
+ /** @type {(value: unknown) => boolean} */
39
+ const isTruthy = Boolean;
40
+
41
+ /**
42
+ * @param {unknown} value
43
+ * @returns {boolean}
44
+ */
45
+ function isSafeErrorName(value) {
46
+ return typeof value === "string" && SAFE_ERROR_NAMES.has(value);
47
+ }
48
+
49
+ /**
50
+ * @param {string} kind
51
+ * @returns {string}
52
+ */
53
+ function nextEventId(kind) {
54
+ sequence.next = incrementBounded(sequence.next);
55
+ return `evt_rn_${kind}_${Date.now().toString(ID_RADIX)}_${sequence.next.toString(ID_RADIX)}`;
56
+ }
57
+
58
+ /**
59
+ * @param {unknown} error
60
+ * @returns {string}
61
+ */
62
+ function automaticStack(error) {
63
+ const stack = safeReadProperty(error, "stack");
64
+ if (typeof stack !== "string") {
65
+ return "";
66
+ }
67
+ const lines = stack.slice(FIRST_INDEX, MAX_STACK_BYTES).split(LINE_BREAK_PATTERN);
68
+ const frames = lines.filter((line) => line.trimStart().startsWith("at "));
69
+ return (frames.length > NO_FRAMES ? frames : lines).join("\n");
70
+ }
71
+
72
+ /**
73
+ * @template Value
74
+ * @param {Value} value
75
+ * @returns {Value | string}
76
+ */
77
+ function basename(value) {
78
+ return typeof value === "string" ? value.replace(DIRECTORY_PREFIX_PATTERN, "") : value;
79
+ }
80
+
81
+ /**
82
+ * @param {DeepReadonly<IssueStackFrame[]>} frames
83
+ * @returns {DeepReadonly<IssueStackFrame[]>}
84
+ */
85
+ function basenameFrames(frames) {
86
+ return frames.map((frame) => ({ ...frame, filename: basename(frame.filename) }));
87
+ }
88
+
89
+ /**
90
+ * @param {unknown} error
91
+ * @returns {string}
92
+ */
93
+ function safeErrorName(error) {
94
+ const name = safeReadProperty(error, "name");
95
+ const candidate = typeof name === "string" ? name : "Error";
96
+ return SAFE_ERROR_NAMES.has(candidate) ? candidate : "Error";
97
+ }
98
+
99
+ /**
100
+ * @param {DeepReadonly<IssueExceptionChain>} exceptionChain
101
+ * @returns {DeepReadonly<IssueExceptionChain>}
102
+ */
103
+ function basenameExceptionChain(exceptionChain) {
104
+ return {
105
+ ...exceptionChain,
106
+ entries: exceptionChain.entries.map((entry) => ({
107
+ ...entry,
108
+ ...(entry.stackFrames ? { stackFrames: basenameFrames(entry.stackFrames) } : {})
109
+ }))
110
+ };
111
+ }
112
+
113
+ /**
114
+ * @param {Readonly<Metadata>} metadata
115
+ * @param {unknown} lastRoute
116
+ * @param {boolean} fatal
117
+ * @returns {Metadata}
118
+ */
119
+ function automaticMetadata(metadata, lastRoute, fatal) {
120
+ const { errorFrameFile, issueGroupingKey, releaseArtifactCodeFile } = metadata;
121
+ return {
122
+ ...metadata,
123
+ ...(isTruthy(errorFrameFile) ? { errorFrameFile: basename(errorFrameFile) } : {}),
124
+ ...(isTruthy(releaseArtifactCodeFile)
125
+ ? { releaseArtifactCodeFile: basename(releaseArtifactCodeFile) }
126
+ : {}),
127
+ ...(typeof issueGroupingKey === "string"
128
+ ? { issueGroupingKey: issueGroupingKey.replace(GROUPING_PATH_PATTERN, "$<prefix>") }
129
+ : {}),
130
+ automatic: true,
131
+ fatal,
132
+ handled: !fatal,
133
+ ...(typeof lastRoute === "string" ? { lastRoute } : {}),
134
+ mechanism: "react_native_error_utils",
135
+ source: "react-native.global_error"
136
+ };
137
+ }
138
+
139
+ /**
140
+ * @param {unknown} error
141
+ * @param {unknown} client
142
+ * @param {boolean} [fatal]
143
+ * @returns {DeepReadonly<ReactNativeErrorEvent>}
144
+ */
145
+ function createAutomaticErrorEvent(error, client, fatal = false) {
146
+ const sanitizedError = new Error(AUTOMATIC_ERROR_MESSAGE);
147
+ sanitizedError.name = safeErrorName(error);
148
+ sanitizedError.stack = automaticStack(error);
149
+ const lastRoute = safeReadProperty(client, LAST_ROUTE);
150
+ const event = createReactNativeErrorEvent(sanitizedError, {
151
+ handled: false,
152
+ id: nextEventId(fatal ? "fatal" : "global"),
153
+ includeStack: false,
154
+ level: fatal ? "fatal" : "error",
155
+ mechanism: "react_native_error_utils"
156
+ });
157
+ const { exceptionChain, metadata, stackFrames } = event.attributes;
158
+ if (exceptionChain === undefined || metadata === undefined) {
159
+ throw new TypeError("React Native error event has no exception chain or metadata");
160
+ }
161
+ return {
162
+ ...event,
163
+ attributes: {
164
+ ...event.attributes,
165
+ exceptionChain: basenameExceptionChain(exceptionChain),
166
+ ...(stackFrames ? { stackFrames: basenameFrames(stackFrames) } : {}),
167
+ message: AUTOMATIC_ERROR_MESSAGE,
168
+ metadata: automaticMetadata(metadata, lastRoute, fatal),
169
+ title: AUTOMATIC_ERROR_MESSAGE
170
+ }
171
+ };
172
+ }
173
+
174
+ module.exports = { createAutomaticErrorEvent, isSafeErrorName };
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * @typedef {(this: unknown, ...args: readonly unknown[]) => unknown} UnknownFunction
5
+ * @typedef {(diagnostic: Readonly<{ code: string }>) => unknown} DiagnosticListener
6
+ * @typedef {Readonly<{ emit: (code: string) => void }>} DiagnosticEmitter
7
+ */
8
+
9
+ const STEP = 1;
10
+ const LAST_C0_CONTROL = 31;
11
+ const DELETE_CONTROL = 127;
12
+ const FIRST_INDEX = 0;
13
+
14
+ /**
15
+ * @param {unknown} value
16
+ * @returns {value is object}
17
+ */
18
+ function isObjectLike(value) {
19
+ return value !== null && (typeof value === "object" || typeof value === "function");
20
+ }
21
+
22
+ /**
23
+ * @param {unknown} value
24
+ * @returns {value is UnknownFunction}
25
+ */
26
+ function isFunction(value) {
27
+ return typeof value === "function";
28
+ }
29
+
30
+ /**
31
+ * @param {unknown} value
32
+ * @param {PropertyKey} property
33
+ * @returns {unknown}
34
+ */
35
+ function safeReadProperty(value, property) {
36
+ if (!isObjectLike(value)) {
37
+ return undefined;
38
+ }
39
+ try {
40
+ return Reflect.get(value, property);
41
+ } catch {
42
+ return undefined;
43
+ }
44
+ }
45
+
46
+ /**
47
+ * @param {unknown} value
48
+ * @param {PropertyKey} property
49
+ * @returns {UnknownFunction | undefined}
50
+ */
51
+ function safeFunction(value, property) {
52
+ const candidate = safeReadProperty(value, property);
53
+ return isFunction(candidate) ? candidate : undefined;
54
+ }
55
+
56
+ /**
57
+ * @param {number} value
58
+ * @returns {number}
59
+ */
60
+ function incrementBounded(value) {
61
+ return value >= Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER : value + STEP;
62
+ }
63
+
64
+ /**
65
+ * @param {string} value
66
+ * @returns {boolean}
67
+ */
68
+ function hasControlCharacter(value) {
69
+ return [...value].some((character) => {
70
+ const code = character.codePointAt(FIRST_INDEX);
71
+ return code !== undefined && (code <= LAST_C0_CONTROL || code === DELETE_CONTROL);
72
+ });
73
+ }
74
+
75
+ /**
76
+ * @param {unknown} onDiagnostic
77
+ * @param {string} code
78
+ * @returns {boolean}
79
+ */
80
+ function emitDiagnostic(onDiagnostic, code) {
81
+ if (!isFunction(onDiagnostic)) {
82
+ return false;
83
+ }
84
+ try {
85
+ onDiagnostic(Object.freeze({ code }));
86
+ return true;
87
+ } catch {
88
+ return false;
89
+ }
90
+ }
91
+
92
+ class DiagnosticGate {
93
+ emitting = false;
94
+
95
+ /**
96
+ * @param {unknown} onDiagnostic
97
+ */
98
+ constructor(onDiagnostic) {
99
+ this.onDiagnostic = onDiagnostic;
100
+ }
101
+
102
+ /**
103
+ * @param {string} code
104
+ * @returns {void}
105
+ */
106
+ emit(code) {
107
+ if (this.emitting) {
108
+ return;
109
+ }
110
+ this.emitting = true;
111
+ try {
112
+ emitDiagnostic(this.onDiagnostic, code);
113
+ } finally {
114
+ this.emitting = false;
115
+ }
116
+ }
117
+ }
118
+
119
+ module.exports = {
120
+ DiagnosticGate,
121
+ emitDiagnostic,
122
+ hasControlCharacter,
123
+ incrementBounded,
124
+ isFunction,
125
+ isObjectLike,
126
+ safeFunction,
127
+ safeReadProperty
128
+ };