@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/fatal-replay.cjs CHANGED
@@ -1,331 +1,55 @@
1
1
  "use strict";
2
2
 
3
- const FATAL_RECORD_SCHEMA_VERSION = 1;
4
- const MAX_ADMITTED_FATAL_IDS = 256;
5
- const MAX_FATAL_FILENAME_BYTES = 512;
6
- const MAX_FATAL_STACK_FRAMES = 24;
7
- const MAX_FATAL_ID_LENGTH = 128;
8
- const MAX_FATAL_RECORD_BYTES = 16 * 1024;
9
- const DURABLE_QUEUE = Symbol.for("co.logbrew.react-native.durable-event-queue");
10
-
11
- const admittedFatalIdsByClient = new WeakMap();
12
-
13
- function createFatalController({
14
- client,
15
- createEvent,
16
- eventMessage,
17
- fatalStore,
18
- isErrorName,
19
- issue,
20
- onDiagnostic
21
- }) {
22
- const methods = fatalStoreMethods(fatalStore);
23
- const directAdmission = !methods && safeReadProperty(client, DURABLE_QUEUE) === true;
24
- const state = {
25
- acknowledgedRecords: 0,
26
- available: methods !== undefined || directAdmission,
27
- corruptRecords: 0,
28
- droppedRecords: 0,
29
- lastOutcome: methods || directAdmission ? "idle" : "unavailable",
30
- replayedRecords: 0,
31
- storedRecords: 0
32
- };
33
-
34
- return {
35
- available: state.available,
36
- discard() {
37
- return discardPendingFatal(fatalStore, methods, state, onDiagnostic);
38
- },
39
- health() {
40
- return fatalHealthSnapshot(state);
41
- },
42
- replay() {
43
- if (!methods) {
44
- return;
45
- }
46
- replayPendingFatal({
47
- client,
48
- eventMessage,
49
- fatalStore,
50
- isErrorName,
51
- issue,
52
- methods,
53
- onDiagnostic,
54
- state
55
- });
56
- },
57
- store(error) {
58
- if (!methods) {
59
- return directAdmission
60
- ? admitFatalError({ client, createEvent, error, issue, onDiagnostic, state })
61
- : false;
62
- }
63
- return storeFatalError({
64
- createEvent,
65
- error,
66
- fatalStore,
67
- methods,
68
- onDiagnostic,
69
- state
70
- });
71
- }
72
- };
73
- }
74
-
75
- function admitFatalError({ client, createEvent, error, issue, onDiagnostic, state }) {
76
- let admission;
77
- try {
78
- const event = createEvent(error);
79
- admission = captureIssueWithAdmission(client, issue, event.id, event.timestamp, event.attributes);
80
- } catch {
81
- state.lastOutcome = "storage_error";
82
- emitDiagnostic(onDiagnostic, "fatal_store_failed");
83
- return false;
84
- }
85
- if (admission === "unavailable") {
86
- state.lastOutcome = "admission_unavailable";
87
- emitDiagnostic(onDiagnostic, "fatal_admission_unavailable");
88
- return false;
89
- }
90
- if (admission !== "admitted") {
91
- state.lastOutcome = "storage_error";
92
- emitDiagnostic(onDiagnostic, "fatal_store_failed");
93
- return false;
94
- }
95
- state.storedRecords = incrementBounded(state.storedRecords);
96
- state.lastOutcome = "stored";
97
- return true;
98
- }
99
-
100
- function fatalStoreMethods(fatalStore) {
101
- if (!isObjectLike(fatalStore)) {
102
- return undefined;
103
- }
104
- const methods = {
105
- acknowledge: safeFunction(fatalStore, "acknowledgeFatalRecord"),
106
- discard: safeFunction(fatalStore, "discardFatalRecord"),
107
- read: safeFunction(fatalStore, "readFatalRecord"),
108
- write: safeFunction(fatalStore, "writeFatalRecord")
109
- };
110
- return Object.values(methods).every((method) => typeof method === "function")
111
- ? methods
112
- : undefined;
113
- }
114
-
115
- function discardPendingFatal(fatalStore, methods, state, onDiagnostic) {
116
- if (!methods) {
117
- return false;
118
- }
119
- let result;
120
- try {
121
- result = methods.discard.call(fatalStore);
122
- } catch {
123
- state.lastOutcome = "discard_failed";
124
- emitDiagnostic(onDiagnostic, "fatal_discard_failed");
125
- return false;
126
- }
127
- if (resultStatus(result) !== "discarded") {
128
- state.lastOutcome = resultStatus(result) === "empty" ? "empty" : "discard_failed";
129
- if (state.lastOutcome === "discard_failed") {
130
- emitDiagnostic(onDiagnostic, "fatal_discard_failed");
131
- }
132
- return false;
133
- }
134
- state.lastOutcome = "discarded";
135
- return true;
136
- }
137
-
138
- function storeFatalError({
139
- createEvent,
140
- error,
141
- fatalStore,
142
- methods,
143
- onDiagnostic,
144
- state
145
- }) {
146
- if (!methods) {
147
- return false;
148
- }
149
- let result;
150
- try {
151
- result = methods.write.call(fatalStore, createFatalRecord(createEvent(error)));
152
- } catch {
153
- result = undefined;
154
- }
155
- const status = resultStatus(result);
156
- if (status === "stored" || status === "stored_after_corruption") {
157
- state.storedRecords = incrementBounded(state.storedRecords);
158
- state.corruptRecords = boundedResultCounter(result, "corruptRecords", state.corruptRecords);
159
- state.lastOutcome = status;
160
- return true;
161
- }
162
- if (status === "dropped_pending") {
163
- state.droppedRecords = boundedResultCounter(
164
- result,
165
- "droppedRecords",
166
- incrementBounded(state.droppedRecords)
167
- );
168
- state.lastOutcome = status;
169
- emitDiagnostic(onDiagnostic, "fatal_record_dropped");
170
- return false;
171
- }
172
- state.lastOutcome = "storage_error";
173
- emitDiagnostic(onDiagnostic, "fatal_store_failed");
174
- return false;
175
- }
176
-
177
- function replayPendingFatal({
178
- client,
179
- eventMessage,
180
- fatalStore,
181
- isErrorName,
182
- issue,
183
- methods,
184
- onDiagnostic,
185
- state
186
- }) {
187
- if (!methods) {
188
- return;
189
- }
190
- let result;
191
- try {
192
- result = methods.read.call(fatalStore);
193
- } catch {
194
- result = undefined;
195
- }
196
- const status = resultStatus(result);
197
- if (status === "empty") {
198
- state.lastOutcome = "empty";
199
- return;
200
- }
201
- if (status === "corrupt_discarded") {
202
- state.corruptRecords = boundedResultCounter(
203
- result,
204
- "corruptRecords",
205
- incrementBounded(state.corruptRecords)
206
- );
207
- state.lastOutcome = status;
208
- emitDiagnostic(onDiagnostic, "fatal_corrupt_record_discarded");
209
- return;
210
- }
211
- const record = status === "pending"
212
- ? validatedFatalRecord(safeReadProperty(result, "record"), isErrorName)
213
- : undefined;
214
- if (!record) {
215
- state.lastOutcome = "replay_failed";
216
- emitDiagnostic(onDiagnostic, "fatal_replay_failed");
217
- return;
218
- }
219
-
220
- state.corruptRecords = Math.max(state.corruptRecords, record.corruptRecords);
221
- state.droppedRecords = Math.max(state.droppedRecords, record.droppedRecords);
222
- if (wasAdmittedInRuntime(client, record.id)) {
223
- acknowledgeFatalRecord(fatalStore, methods, record.id, state, onDiagnostic);
224
- return;
225
- }
226
-
227
- let admission;
228
- try {
229
- admission = captureIssueWithAdmission(
230
- client, issue, record.id, record.timestamp, fatalEventAttributes(record, eventMessage)
231
- );
232
- } catch {
233
- state.lastOutcome = "replay_failed";
234
- emitDiagnostic(onDiagnostic, "fatal_replay_failed");
235
- return;
236
- }
237
- if (admission === "unavailable") {
238
- state.lastOutcome = "admission_unavailable";
239
- emitDiagnostic(onDiagnostic, "fatal_replay_admission_unavailable");
240
- return;
241
- }
242
- if (admission !== "admitted") {
243
- state.lastOutcome = "replay_not_admitted";
244
- emitDiagnostic(onDiagnostic, "fatal_replay_not_admitted");
245
- return;
246
- }
247
-
248
- rememberAdmittedFatalId(client, record.id);
249
- state.replayedRecords = incrementBounded(state.replayedRecords);
250
- acknowledgeFatalRecord(fatalStore, methods, record.id, state, onDiagnostic);
251
- }
252
-
253
- function acknowledgeFatalRecord(fatalStore, methods, recordId, state, onDiagnostic) {
254
- let result;
255
- try {
256
- result = methods.acknowledge.call(fatalStore, recordId);
257
- } catch {
258
- result = undefined;
259
- }
260
- if (resultStatus(result) === "acknowledged"
261
- && safeReadProperty(result, "recordId") === recordId) {
262
- state.acknowledgedRecords = incrementBounded(state.acknowledgedRecords);
263
- state.lastOutcome = "acknowledged";
264
- return;
265
- }
266
- state.lastOutcome = "acknowledge_failed";
267
- emitDiagnostic(onDiagnostic, "fatal_acknowledge_failed");
268
- }
269
-
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";
275
- }
276
- const status = resultStatus(capture.call(client, id, timestamp, attributes));
277
- return status === "admitted" || status === "filtered" || status === "dropped"
278
- ? status
279
- : "unavailable";
280
- }
281
-
282
- function wasAdmittedInRuntime(client, recordId) {
283
- return isObjectLike(client) && admittedFatalIdsByClient.get(client)?.has(recordId) === true;
284
- }
285
-
286
- function rememberAdmittedFatalId(client, recordId) {
287
- if (!isObjectLike(client)) {
288
- return;
289
- }
290
- let admittedIds = admittedFatalIdsByClient.get(client);
291
- if (!admittedIds) {
292
- admittedIds = new Set();
293
- admittedFatalIdsByClient.set(client, admittedIds);
294
- }
295
- if (admittedIds.has(recordId)) {
296
- return;
297
- }
298
- admittedIds.add(recordId);
299
- if (admittedIds.size > MAX_ADMITTED_FATAL_IDS) {
300
- admittedIds.delete(admittedIds.values().next().value);
301
- }
302
- }
3
+ const { emitDiagnostic, incrementBounded, safeReadProperty } = require("./global-errors-support.cjs");
4
+ const { boundedResultCounter, createFatalRecord, resultStatus, validatedFatalRecord } = require("./fatal-record.cjs");
5
+ const {
6
+ attempt,
7
+ captureIssueWithAdmission,
8
+ fatalStoreMethods,
9
+ rememberAdmittedFatalId,
10
+ wasAdmittedInRuntime
11
+ } = require("./fatal-admission.cjs");
12
+
13
+ /**
14
+ * @import { UnknownFunction } from "./global-errors-support.cjs"
15
+ * @import { FatalEventInput, FatalRecord } from "./fatal-record.cjs"
16
+ * @import { ReactNativeFatalReplayHealth } from "./global-errors.cjs"
17
+ * @import { Admission, FatalStoreMethods } from "./fatal-admission.cjs"
18
+ * @typedef {ReactNativeFatalReplayHealth["lastOutcome"]} FatalOutcome
19
+ * @typedef {Readonly<{
20
+ * client: unknown,
21
+ * createEvent: (error: unknown) => FatalEventInput,
22
+ * eventMessage: string,
23
+ * fatalStore: unknown,
24
+ * isErrorName: (value: unknown) => boolean,
25
+ * issue: UnknownFunction,
26
+ * onDiagnostic: unknown
27
+ * }>} FatalControllerOptions
28
+ * @typedef {Readonly<{
29
+ * available: boolean,
30
+ * discard: () => boolean,
31
+ * health: () => ReactNativeFatalReplayHealth,
32
+ * replay: () => void,
33
+ * store: (error: unknown) => boolean
34
+ * }>} FatalController
35
+ */
303
36
 
304
- function createFatalRecord(event) {
305
- const record = {
306
- corruptRecords: 0,
307
- droppedRecords: 0,
308
- errorName: event.attributes.exception.type,
309
- id: event.id,
310
- schemaVersion: FATAL_RECORD_SCHEMA_VERSION,
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();
319
- }
320
- return record;
321
- }
37
+ const DURABLE_QUEUE = Symbol.for("co.logbrew.react-native.durable-event-queue");
38
+ const NO_COUNT = 0;
322
39
 
40
+ /**
41
+ * @param {FatalRecord} record
42
+ * @param {string} eventMessage
43
+ * @returns {Readonly<Record<string, unknown>>}
44
+ */
323
45
  function fatalEventAttributes(record, eventMessage) {
324
46
  return Object.freeze({
325
47
  exception: Object.freeze({
326
48
  type: record.errorName,
49
+
327
50
  mechanism: Object.freeze({
328
51
  type: "react_native_error_utils",
52
+
329
53
  handled: false
330
54
  })
331
55
  }),
@@ -341,221 +65,286 @@ function fatalEventAttributes(record, eventMessage) {
341
65
  replayed: true,
342
66
  source: "react-native.global_error"
343
67
  }),
344
- ...(record.stackFrames.length === 0
68
+ ...(record.stackFrames.length === NO_COUNT
345
69
  ? {}
346
70
  : { stackFrames: record.stackFrames.map((frame) => Object.freeze({ ...frame })) }),
347
71
  title: eventMessage
348
72
  });
349
73
  }
350
74
 
351
- function validatedFatalRecord(value, isErrorName) {
352
- try {
353
- if (!isPlainObject(value)
354
- || !hasExactKeys(value, [
355
- "corruptRecords",
356
- "droppedRecords",
357
- "errorName",
358
- "id",
359
- "schemaVersion",
360
- "stackFrames",
361
- "timestamp"
362
- ])) {
363
- return undefined;
364
- }
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) {
377
- return undefined;
378
- }
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,
392
- schemaVersion: FATAL_RECORD_SCHEMA_VERSION,
393
- stackFrames: frames,
394
- timestamp
395
- };
396
- return utf8ByteLength(JSON.stringify(record)) <= MAX_FATAL_RECORD_BYTES ? record : undefined;
397
- } catch {
398
- return undefined;
75
+ class FatalReplay {
76
+ /**
77
+ * @param {FatalControllerOptions} options
78
+ */
79
+ constructor(options) {
80
+ this.options = options;
81
+ this.methods = fatalStoreMethods(options.fatalStore);
82
+ this.directAdmission = !this.methods && safeReadProperty(options.client, DURABLE_QUEUE) === true;
83
+ this.acknowledgedRecords = NO_COUNT;
84
+ this.available = this.methods !== undefined || this.directAdmission;
85
+ this.corruptRecords = NO_COUNT;
86
+ this.droppedRecords = NO_COUNT;
87
+ /** @type {FatalOutcome} */
88
+ this.lastOutcome = this.methods || this.directAdmission ? "idle" : "unavailable";
89
+ this.replayedRecords = NO_COUNT;
90
+ this.storedRecords = NO_COUNT;
91
+ }
92
+
93
+ /**
94
+ * @returns {ReactNativeFatalReplayHealth}
95
+ */
96
+ health() {
97
+ return Object.freeze({
98
+ acknowledgedRecords: this.acknowledgedRecords,
99
+ available: this.available,
100
+ corruptRecords: this.corruptRecords,
101
+ droppedRecords: this.droppedRecords,
102
+ lastOutcome: this.lastOutcome,
103
+ replayedRecords: this.replayedRecords,
104
+ storedRecords: this.storedRecords
105
+ });
106
+ }
107
+
108
+ /**
109
+ * @param {FatalOutcome} outcome
110
+ * @param {string} code
111
+ * @returns {false}
112
+ */
113
+ fail(outcome, code) {
114
+ this.lastOutcome = outcome;
115
+ emitDiagnostic(this.options.onDiagnostic, code);
116
+ return false;
399
117
  }
400
- }
401
118
 
402
- function validatedFatalFrame(value) {
403
- try {
404
- if (!isPlainObject(value)
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) {
119
+ /**
120
+ * @param {unknown} error
121
+ * @returns {Admission | undefined}
122
+ */
123
+ admission(error) {
124
+ const { client, createEvent, issue } = this.options;
125
+ try {
126
+ const event = createEvent(error);
127
+ return captureIssueWithAdmission(client, issue, event.id, event.timestamp, event.attributes);
128
+ } catch {
412
129
  return undefined;
413
130
  }
414
- return {
415
- column,
416
- filename,
417
- line
418
- };
419
- } catch {
420
- return undefined;
421
131
  }
422
- }
423
-
424
- function validFatalFilename(value) {
425
- return typeof value === "string"
426
- && value.length > 0
427
- && utf8ByteLength(value) <= MAX_FATAL_FILENAME_BYTES
428
- && !hasControlCharacter(value)
429
- && !value.startsWith("/")
430
- && !/^[A-Za-z]:/u.test(value)
431
- && !value.includes("\\")
432
- && !value.includes("://")
433
- && !value.includes("?")
434
- && !value.includes("#")
435
- && !value.split("/").includes("..");
436
- }
437
-
438
- function normalizedStoredFatalFilename(value) {
439
- return typeof value === "string"
440
- && value.startsWith("/")
441
- && value.length > 1
442
- && value.indexOf("/", 1) === -1
443
- ? value.slice(1)
444
- : value;
445
- }
446
132
 
447
- function validFatalTimestamp(value) {
448
- return typeof value === "string"
449
- && value.length >= 20
450
- && value.length <= 35
451
- && /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]{1,9})?Z$/u.test(value);
452
- }
453
-
454
- function resultStatus(result) {
455
- try {
456
- const status = isPlainObject(result) ? result.status : undefined;
457
- return typeof status === "string" ? status : undefined;
458
- } catch {
459
- return undefined;
133
+ /**
134
+ * @param {unknown} error
135
+ * @returns {boolean}
136
+ */
137
+ admit(error) {
138
+ const admission = this.admission(error);
139
+ if (admission === "unavailable") {
140
+ return this.fail("admission_unavailable", "fatal_admission_unavailable");
141
+ }
142
+ if (admission !== "admitted") {
143
+ return this.fail("storage_error", "fatal_store_failed");
144
+ }
145
+ this.storedRecords = incrementBounded(this.storedRecords);
146
+ this.lastOutcome = "stored";
147
+ return true;
460
148
  }
461
- }
462
149
 
463
- function boundedResultCounter(result, property, fallback) {
464
- const value = isPlainObject(result) ? safeReadProperty(result, property) : undefined;
465
- return isBoundedCounter(value) ? value : fallback;
466
- }
467
-
468
- function hasExactKeys(value, expected) {
469
- try {
470
- const keys = Object.keys(value).sort();
471
- return keys.length === expected.length
472
- && keys.every((key, index) => key === expected[index]);
473
- } catch {
474
- return false;
150
+ /**
151
+ * @returns {boolean}
152
+ */
153
+ discard() {
154
+ const { methods } = this;
155
+ if (!methods) {
156
+ return false;
157
+ }
158
+ return this.recordDiscardResult(attempt(() => methods.discard.call(this.options.fatalStore)));
475
159
  }
476
- }
477
160
 
478
- function isBoundedCounter(value) {
479
- return Number.isSafeInteger(value) && value >= 0;
480
- }
481
-
482
- function isPlainObject(value) {
483
- if (value === null || typeof value !== "object") {
484
- return false;
485
- }
486
- try {
487
- const prototype = Object.getPrototypeOf(value);
488
- return prototype === Object.prototype || prototype === null;
489
- } catch {
490
- return false;
161
+ /**
162
+ * @param {unknown} result
163
+ * @returns {boolean}
164
+ */
165
+ recordDiscardResult(result) {
166
+ if (resultStatus(result) === "discarded") {
167
+ this.lastOutcome = "discarded";
168
+ return true;
169
+ }
170
+ if (resultStatus(result) === "empty") {
171
+ this.lastOutcome = "empty";
172
+ return false;
173
+ }
174
+ return this.fail("discard_failed", "fatal_discard_failed");
491
175
  }
492
- }
493
176
 
494
- function safeReadProperty(value, property) {
495
- if (!isObjectLike(value)) {
496
- return undefined;
497
- }
498
- try {
499
- return value[property];
500
- } catch {
501
- return undefined;
177
+ /**
178
+ * @param {unknown} error
179
+ * @returns {boolean}
180
+ */
181
+ store(error) {
182
+ const { methods } = this;
183
+ if (!methods) {
184
+ return this.directAdmission ? this.admit(error) : false;
185
+ }
186
+ const { createEvent, fatalStore } = this.options;
187
+ return this.recordStoreResult(attempt(() => methods.write.call(fatalStore, createFatalRecord(createEvent(error)))));
188
+ }
189
+
190
+ /**
191
+ * @param {unknown} result
192
+ * @returns {boolean}
193
+ */
194
+ recordStoreResult(result) {
195
+ const status = resultStatus(result);
196
+ if (status === "stored" || status === "stored_after_corruption") {
197
+ this.storedRecords = incrementBounded(this.storedRecords);
198
+ this.corruptRecords = boundedResultCounter(result, "corruptRecords", this.corruptRecords);
199
+ this.lastOutcome = status;
200
+ return true;
201
+ }
202
+ if (status === "dropped_pending") {
203
+ this.droppedRecords = boundedResultCounter(result, "droppedRecords", incrementBounded(this.droppedRecords));
204
+ return this.fail(status, "fatal_record_dropped");
205
+ }
206
+ return this.fail("storage_error", "fatal_store_failed");
207
+ }
208
+
209
+ /**
210
+ * @param {FatalStoreMethods} methods
211
+ * @param {string} recordId
212
+ * @returns {void}
213
+ */
214
+ acknowledge(methods, recordId) {
215
+ const result = attempt(() => methods.acknowledge.call(this.options.fatalStore, recordId));
216
+ if (resultStatus(result) === "acknowledged"
217
+ && safeReadProperty(result, "recordId") === recordId) {
218
+ this.acknowledgedRecords = incrementBounded(this.acknowledgedRecords);
219
+ this.lastOutcome = "acknowledged";
220
+ return;
221
+ }
222
+ this.fail("acknowledge_failed", "fatal_acknowledge_failed");
223
+ }
224
+
225
+ /**
226
+ * @param {FatalRecord} record
227
+ * @returns {Admission | undefined}
228
+ */
229
+ replayAdmission(record) {
230
+ const { client, eventMessage, issue } = this.options;
231
+ try {
232
+ return captureIssueWithAdmission(client, issue, record.id, record.timestamp, fatalEventAttributes(record, eventMessage));
233
+ } catch {
234
+ return undefined;
235
+ }
502
236
  }
503
- }
504
-
505
- function safeFunction(value, property) {
506
- const candidate = safeReadProperty(value, property);
507
- return typeof candidate === "function" ? candidate : undefined;
508
- }
509
-
510
- function hasControlCharacter(value) {
511
- return Array.from(value).some((character) => {
512
- const code = character.codePointAt(0);
513
- return code !== undefined && (code <= 31 || code === 127);
514
- });
515
- }
516
-
517
- function positiveInteger(value) {
518
- return Number.isSafeInteger(value) && value > 0 && value <= 2147483647 ? value : undefined;
519
- }
520
237
 
521
- function incrementBounded(value) {
522
- return value >= Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER : value + 1;
523
- }
524
-
525
- function emitDiagnostic(onDiagnostic, code) {
526
- if (typeof onDiagnostic !== "function") {
527
- return;
528
- }
529
- try {
530
- onDiagnostic(Object.freeze({ code }));
531
- } catch {
532
- // Diagnostics must never interfere with application error handling.
238
+ /**
239
+ * @param {FatalStoreMethods} methods
240
+ * @param {FatalRecord} record
241
+ * @returns {void}
242
+ */
243
+ replayRecord(methods, record) {
244
+ this.corruptRecords = Math.max(this.corruptRecords, record.corruptRecords);
245
+ this.droppedRecords = Math.max(this.droppedRecords, record.droppedRecords);
246
+ if (wasAdmittedInRuntime(this.options.client, record.id)) {
247
+ this.acknowledge(methods, record.id);
248
+ return;
249
+ }
250
+ this.recordReplayAdmission(methods, record, this.replayAdmission(record));
251
+ }
252
+
253
+ /**
254
+ * @param {FatalStoreMethods} methods
255
+ * @param {FatalRecord} record
256
+ * @param {Admission | undefined} admission
257
+ * @returns {void}
258
+ */
259
+ recordReplayAdmission(methods, record, admission) {
260
+ if (admission === undefined) {
261
+ this.fail("replay_failed", "fatal_replay_failed");
262
+ } else if (admission === "unavailable") {
263
+ this.fail("admission_unavailable", "fatal_replay_admission_unavailable");
264
+ } else if (admission === "admitted") {
265
+ rememberAdmittedFatalId(this.options.client, record.id);
266
+ this.replayedRecords = incrementBounded(this.replayedRecords);
267
+ this.acknowledge(methods, record.id);
268
+ } else {
269
+ this.fail("replay_not_admitted", "fatal_replay_not_admitted");
270
+ }
533
271
  }
534
- }
535
272
 
536
- function utf8ByteLength(value) {
537
- let bytes = 0;
538
- for (const character of value) {
539
- const code = character.codePointAt(0);
540
- bytes += code <= 0x7f ? 1 : code <= 0x7ff ? 2 : code <= 0xffff ? 3 : 4;
273
+ /**
274
+ * @returns {void}
275
+ */
276
+ replay() {
277
+ const { methods } = this;
278
+ if (!methods) {
279
+ return;
280
+ }
281
+ this.recordReadResult(methods, attempt(() => methods.read.call(this.options.fatalStore)));
282
+ }
283
+
284
+ /**
285
+ * @param {FatalStoreMethods} methods
286
+ * @param {unknown} result
287
+ * @returns {void}
288
+ */
289
+ recordReadResult(methods, result) {
290
+ const status = resultStatus(result);
291
+ if (status === "empty") {
292
+ this.lastOutcome = "empty";
293
+ return;
294
+ }
295
+ if (status === "corrupt_discarded") {
296
+ this.corruptRecords = boundedResultCounter(result, "corruptRecords", incrementBounded(this.corruptRecords));
297
+ this.fail(status, "fatal_corrupt_record_discarded");
298
+ return;
299
+ }
300
+ this.replayResult(methods, status === "pending"
301
+ ? validatedFatalRecord(safeReadProperty(result, "record"), this.options.isErrorName)
302
+ : undefined);
303
+ }
304
+
305
+ /**
306
+ * @param {FatalStoreMethods} methods
307
+ * @param {FatalRecord | undefined} record
308
+ * @returns {void}
309
+ */
310
+ replayResult(methods, record) {
311
+ if (record) {
312
+ this.replayRecord(methods, record);
313
+ } else {
314
+ this.fail("replay_failed", "fatal_replay_failed");
315
+ }
541
316
  }
542
- return bytes;
543
317
  }
544
318
 
545
- function fatalHealthSnapshot(state) {
546
- return Object.freeze({
547
- acknowledgedRecords: state.acknowledgedRecords,
548
- available: state.available,
549
- corruptRecords: state.corruptRecords,
550
- droppedRecords: state.droppedRecords,
551
- lastOutcome: state.lastOutcome,
552
- replayedRecords: state.replayedRecords,
553
- storedRecords: state.storedRecords
554
- });
555
- }
556
-
557
- function isObjectLike(value) {
558
- return value !== null && (typeof value === "object" || typeof value === "function");
319
+ /**
320
+ * @param {FatalControllerOptions} options
321
+ * @returns {FatalController}
322
+ */
323
+ function createFatalController({
324
+ client,
325
+ createEvent,
326
+ eventMessage,
327
+ fatalStore,
328
+ isErrorName,
329
+ issue,
330
+ onDiagnostic
331
+ }) {
332
+ const fatal = new FatalReplay({ client, createEvent, eventMessage, fatalStore, isErrorName, issue, onDiagnostic });
333
+ return {
334
+ available: fatal.available,
335
+ discard() {
336
+ return fatal.discard();
337
+ },
338
+ health() {
339
+ return fatal.health();
340
+ },
341
+ replay() {
342
+ fatal.replay();
343
+ },
344
+ store(error) {
345
+ return fatal.store(error);
346
+ }
347
+ };
559
348
  }
560
349
 
561
350
  module.exports = { captureIssueWithAdmission, createFatalController };