@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,282 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * @import { LogBrewClient, Metadata, MetadataValue, SpanAttributes } from "@logbrew/sdk"
5
+ * @import {
6
+ * ReactNativeSpanEvent,
7
+ * ReactNativeTraceContext,
8
+ * TracePropagationTarget
9
+ * } from "./index.cjs"
10
+ */
11
+
12
+ /**
13
+ * @typedef {(...args: readonly unknown[]) => unknown} Callable
14
+ * @typedef {{ readonly [key: string]: MetadataValue | undefined }} LooseMetadata
15
+ * @typedef {{ readonly [key: string]: unknown }} UnknownRecord
16
+ * @typedef {{
17
+ * readonly OS?: string | undefined,
18
+ * readonly Version?: string | number | undefined,
19
+ * readonly isPad?: boolean | undefined,
20
+ * readonly constants?: { readonly isTesting?: boolean | undefined, readonly [key: string]: unknown } | undefined
21
+ * }} PlatformInput
22
+ * @typedef {{
23
+ * readonly currentState?: string | null | undefined,
24
+ * readonly addEventListener?: ((type: "change", listener: (state: string) => void) => unknown) | undefined
25
+ * }} AppStateInput
26
+ * @typedef {Readonly<ReactNativeTraceContext> | string} TraceInput
27
+ * @typedef {(length: number) => ArrayLike<number>} RandomValues
28
+ * @typedef {{
29
+ * readonly appState?: AppStateInput | undefined,
30
+ * readonly durationMs?: number | undefined,
31
+ * readonly metadata?: LooseMetadata | undefined,
32
+ * readonly method?: string | undefined,
33
+ * readonly platform?: PlatformInput | undefined,
34
+ * readonly responseSizeBytes?: number | undefined,
35
+ * readonly routeTemplate?: unknown,
36
+ * readonly screen?: string | undefined,
37
+ * readonly sessionId?: string | undefined,
38
+ * readonly status?: SpanAttributes["status"] | undefined,
39
+ * readonly statusCode?: number | undefined,
40
+ * readonly timestamp?: string | undefined,
41
+ * readonly trace?: TraceInput | undefined
42
+ * }} ResourceSpanInput
43
+ * @typedef {{
44
+ * readonly durationMs?: number | undefined,
45
+ * readonly metadata?: LooseMetadata | undefined,
46
+ * readonly name?: string | undefined,
47
+ * readonly status?: SpanAttributes["status"] | undefined,
48
+ * readonly trace?: TraceInput | undefined
49
+ * }} SpanAttributesInput
50
+ * @typedef {{
51
+ * readonly parentSpanId?: string | undefined,
52
+ * readonly randomValues?: RandomValues | undefined,
53
+ * readonly traceFlags?: string | undefined,
54
+ * readonly traceparent?: string | undefined
55
+ * }} TraceContextInput
56
+ * @typedef {{
57
+ * readonly fetchImpl?: unknown,
58
+ * readonly trace?: TraceInput | undefined,
59
+ * readonly tracePropagationTargets?: readonly TracePropagationTarget[] | undefined
60
+ * }} TraceparentFetchInput
61
+ * @typedef {{
62
+ * readonly appState?: AppStateInput | undefined,
63
+ * readonly captureInitialRoute?: boolean | undefined,
64
+ * readonly includeRouteKey?: boolean | undefined,
65
+ * readonly metadata?: Readonly<Metadata> | undefined,
66
+ * readonly now?: (() => string) | undefined,
67
+ * readonly nowMs?: (() => number) | undefined,
68
+ * readonly onError?: ((error: unknown) => void) | undefined,
69
+ * readonly platform?: PlatformInput | undefined,
70
+ * readonly trace?: TraceInput | undefined
71
+ * }} NavigationListenerInput
72
+ * @typedef {{
73
+ * captureReactNativeResourceSpan(client: LogBrewClient, input?: ResourceSpanInput): ReactNativeSpanEvent,
74
+ * createReactNativeSpanAttributes(input?: SpanAttributesInput): SpanAttributes,
75
+ * createReactNativeTraceContext(config?: TraceContextInput): ReactNativeTraceContext,
76
+ * createReactNativeTraceHeaders(trace?: TraceInput): { traceparent: string },
77
+ * createReactNavigationSpanListener(client: LogBrewClient, container: unknown, options?: NavigationListenerInput): () => void,
78
+ * createTraceparentFetch(config?: TraceparentFetchInput): (input: unknown, init?: unknown) => unknown,
79
+ * getActiveLogBrewTrace(): ReactNativeTraceContext | undefined,
80
+ * getReactNativeContext(options?: { readonly appState?: AppStateInput | undefined, readonly platform?: PlatformInput | undefined }): Metadata,
81
+ * getReactNativeTraceMetadata(trace?: TraceInput | null): Metadata,
82
+ * shouldPropagateTraceparent(url: unknown, tracePropagationTargets?: readonly TracePropagationTarget[]): boolean
83
+ * }} IndexRuntime
84
+ * @typedef {{
85
+ * createSafeReactNativeMetadata(metadata: Readonly<Metadata> | undefined, metadataFactory: unknown, context: unknown): Readonly<Metadata> | undefined,
86
+ * safeReactNativeMetadataFactoryResult(candidate: unknown): Metadata
87
+ * }} MetadataRuntime
88
+ * @typedef {{ readonly origin: string, readonly pathname: string, toString(): string }} ParsedUrl
89
+ * @typedef {new (input: unknown, base: string) => ParsedUrl} UrlConstructor
90
+ */
91
+
92
+ /** @type {IndexRuntime} */
93
+ const indexRuntime = require("./index.cjs");
94
+ /** @type {MetadataRuntime} */
95
+ const metadataRuntime = require("./metadata.cjs");
96
+
97
+ const ONE_BYTE_LIMIT = 0x80;
98
+ const TWO_BYTE_LIMIT = 0x8_00;
99
+ const THREE_BYTE_LIMIT = 0x1_00_00;
100
+ const ONE_BYTE = 1;
101
+ const TWO_BYTES = 2;
102
+ const THREE_BYTES = 3;
103
+ const FOUR_BYTES = 4;
104
+ const NO_BYTES = 0;
105
+ const MIN_DURATION_MS = 0;
106
+ const QUERY_OR_HASH = /[?#]/u;
107
+ const PATH_PART = 1;
108
+ const URL_BASE = "https://logbrew.local";
109
+
110
+ /**
111
+ * @param {unknown} value
112
+ * @returns {value is UnknownRecord}
113
+ */
114
+ function isObjectLike(value) {
115
+ return (typeof value === "object" && value !== null) || typeof value === "function";
116
+ }
117
+
118
+ /**
119
+ * @param {unknown} value
120
+ * @returns {value is Record<PropertyKey, unknown>}
121
+ */
122
+ function isWritableRecord(value) {
123
+ return (typeof value === "object" && value !== null) || typeof value === "function";
124
+ }
125
+
126
+ /**
127
+ * @param {unknown} value
128
+ * @returns {value is Callable}
129
+ */
130
+ function isCallable(value) {
131
+ return typeof value === "function";
132
+ }
133
+
134
+ /**
135
+ * @param {unknown} value
136
+ * @param {PropertyKey} key
137
+ * @returns {unknown}
138
+ */
139
+ function readField(value, key) {
140
+ return isObjectLike(value) ? Reflect.get(value, key) : undefined;
141
+ }
142
+
143
+ /**
144
+ * @param {string} character
145
+ * @returns {number}
146
+ */
147
+ function characterBytes(character) {
148
+ const codePoint = character.codePointAt(NO_BYTES) ?? NO_BYTES;
149
+ if (codePoint < ONE_BYTE_LIMIT) {
150
+ return ONE_BYTE;
151
+ }
152
+ if (codePoint < TWO_BYTE_LIMIT) {
153
+ return TWO_BYTES;
154
+ }
155
+ return codePoint < THREE_BYTE_LIMIT ? THREE_BYTES : FOUR_BYTES;
156
+ }
157
+
158
+ /**
159
+ * @param {string} value
160
+ * @returns {number}
161
+ */
162
+ function utf8ByteLength(value) {
163
+ let bytes = NO_BYTES;
164
+ for (const character of value) {
165
+ bytes += characterBytes(character);
166
+ }
167
+ return bytes;
168
+ }
169
+
170
+ /**
171
+ * @param {unknown} error
172
+ * @returns {string}
173
+ */
174
+ function errorName(error) {
175
+ const name = readField(error, "name");
176
+ return typeof name === "string" && name.trim() !== "" ? name : "Error";
177
+ }
178
+
179
+ /**
180
+ * @param {number} startedAtMs
181
+ * @param {() => number} nowMs
182
+ * @returns {number | undefined}
183
+ */
184
+ function elapsedMs(startedAtMs, nowMs) {
185
+ const durationMs = nowMs() - startedAtMs;
186
+ return Number.isFinite(durationMs) ? Math.max(MIN_DURATION_MS, durationMs) : undefined;
187
+ }
188
+
189
+ /**
190
+ * @param {unknown} value
191
+ * @returns {value is { toString(): string }}
192
+ */
193
+ function isStringObject(value) {
194
+ return isObjectLike(value) && Object.prototype.isPrototypeOf.call(String.prototype, value);
195
+ }
196
+
197
+ /**
198
+ * @param {unknown} value
199
+ * @returns {string | undefined}
200
+ */
201
+ function stringValue(value) {
202
+ if (typeof value === "string") {
203
+ return value;
204
+ }
205
+ return isStringObject(value) ? value.toString() : undefined;
206
+ }
207
+
208
+ /**
209
+ * @param {unknown} value
210
+ * @returns {value is UrlConstructor}
211
+ */
212
+ function isUrlConstructor(value) {
213
+ return typeof value === "function";
214
+ }
215
+
216
+ /**
217
+ * @returns {UrlConstructor | undefined}
218
+ */
219
+ function urlConstructor() {
220
+ const constructor = readField(globalThis, "URL");
221
+ return isUrlConstructor(constructor) ? constructor : undefined;
222
+ }
223
+
224
+ /**
225
+ * @param {unknown} value
226
+ * @returns {ParsedUrl | undefined}
227
+ */
228
+ function parseUrl(value) {
229
+ const URLConstructor = urlConstructor();
230
+ if (URLConstructor === undefined) {
231
+ return undefined;
232
+ }
233
+ try {
234
+ return new URLConstructor(value, URL_BASE);
235
+ } catch {
236
+ return undefined;
237
+ }
238
+ }
239
+
240
+ /**
241
+ * @param {string} value
242
+ * @returns {string}
243
+ */
244
+ function stripQueryAndHash(value) {
245
+ return value.split(QUERY_OR_HASH, PATH_PART).join("");
246
+ }
247
+
248
+ /**
249
+ * @param {unknown} value
250
+ * @returns {string}
251
+ */
252
+ function toText(value) {
253
+ return typeof value === "string" ? value : String(value);
254
+ }
255
+
256
+ /**
257
+ * @param {{ readonly url: unknown }} context
258
+ * @returns {string}
259
+ */
260
+ function defaultRouteTemplateFactory({ url }) {
261
+ const parsedUrl = parseUrl(url);
262
+ return parsedUrl === undefined ? stripQueryAndHash(toText(url)) : parsedUrl.pathname;
263
+ }
264
+
265
+ module.exports = {
266
+ defaultRouteTemplateFactory,
267
+ elapsedMs,
268
+ errorName,
269
+ indexRuntime,
270
+ isCallable,
271
+ isObjectLike,
272
+ isStringObject,
273
+ isWritableRecord,
274
+ metadataRuntime,
275
+ parseUrl,
276
+ readField,
277
+ stringValue,
278
+ stripQueryAndHash,
279
+ toText,
280
+ urlConstructor,
281
+ utf8ByteLength
282
+ };
@@ -0,0 +1,298 @@
1
+ "use strict";
2
+
3
+ const {
4
+ elapsedMs,
5
+ errorName,
6
+ indexRuntime,
7
+ isCallable,
8
+ isWritableRecord,
9
+ metadataRuntime,
10
+ readField,
11
+ stringValue,
12
+ toText,
13
+ utf8ByteLength
14
+ } = require("./instrumentation-values.cjs");
15
+
16
+ /**
17
+ * @import { LogBrewClient, Metadata, SpanAttributes } from "@logbrew/sdk"
18
+ * @import { TracePropagationTarget } from "./index.cjs"
19
+ * @import { AppStateInput, Callable, PlatformInput, TraceInput } from "./instrumentation-values.cjs"
20
+ */
21
+
22
+ /**
23
+ * @typedef {{
24
+ * readonly appState: AppStateInput | undefined,
25
+ * readonly client: LogBrewClient,
26
+ * readonly doneState: unknown,
27
+ * readonly headersReceivedState: unknown,
28
+ * readonly measureXhrResponseBodySize: boolean,
29
+ * readonly metadata: Readonly<Metadata>,
30
+ * readonly metadataFactory: unknown,
31
+ * readonly nowMs: () => number,
32
+ * readonly platform: PlatformInput | undefined,
33
+ * readonly routeTemplate: string | undefined,
34
+ * readonly routeTemplateFactory: (context: { readonly url: unknown }) => unknown,
35
+ * readonly screen: string | undefined,
36
+ * readonly sessionId: string | undefined,
37
+ * readonly status: SpanAttributes["status"] | undefined,
38
+ * readonly trace: TraceInput
39
+ * }} XhrCaptureSettings
40
+ * @typedef {{ readonly open: Callable, readonly send: Callable, readonly setRequestHeader: Callable }} XhrMethods
41
+ * @typedef {{
42
+ * readonly capture: XhrCaptureSettings,
43
+ * readonly now: () => string,
44
+ * readonly tracePropagationTargets: readonly TracePropagationTarget[]
45
+ * }} XhrWrapperSettings
46
+ * @typedef {{ readonly args: readonly unknown[], readonly methods: XhrMethods, readonly settings: XhrWrapperSettings }} XhrSend
47
+ */
48
+
49
+ const MIN_BYTES = 0;
50
+ const DECIMAL = 10;
51
+
52
+ /**
53
+ * @param {unknown} xhr
54
+ * @param {string} property
55
+ * @returns {unknown}
56
+ */
57
+ function readXhrResponseValue(xhr, property) {
58
+ try {
59
+ return readField(xhr, property);
60
+ } catch {
61
+ return undefined;
62
+ }
63
+ }
64
+
65
+ /**
66
+ * @param {unknown} value
67
+ * @returns {value is number}
68
+ */
69
+ function isByteCount(value) {
70
+ return typeof value === "number" && Number.isFinite(value) && value >= MIN_BYTES;
71
+ }
72
+
73
+ /**
74
+ * @param {unknown} response
75
+ * @returns {number | undefined}
76
+ */
77
+ function binaryResponseBytes(response) {
78
+ if (response instanceof ArrayBuffer) {
79
+ return response.byteLength;
80
+ }
81
+ if (ArrayBuffer.isView(response)) {
82
+ return response.byteLength;
83
+ }
84
+ const size = readField(response, "size");
85
+ return isByteCount(size) ? size : undefined;
86
+ }
87
+
88
+ /**
89
+ * @param {unknown} xhr
90
+ * @returns {number | undefined}
91
+ */
92
+ function xhrResponseBodySizeBytes(xhr) {
93
+ const responseType = readXhrResponseValue(xhr, "responseType");
94
+ if (responseType === undefined || responseType === "" || responseType === "text") {
95
+ const responseText = stringValue(readXhrResponseValue(xhr, "responseText"));
96
+ if (responseText !== undefined) {
97
+ return utf8ByteLength(responseText);
98
+ }
99
+ }
100
+ const response = readXhrResponseValue(xhr, "response");
101
+ const responseString = stringValue(response);
102
+ return responseString === undefined ? binaryResponseBytes(response) : utf8ByteLength(responseString);
103
+ }
104
+
105
+ /**
106
+ * @param {unknown} xhr
107
+ * @param {boolean} measureXhrResponseBodySize
108
+ * @returns {number | undefined}
109
+ */
110
+ function xhrResponseSizeBytes(xhr, measureXhrResponseBodySize) {
111
+ const getResponseHeader = readField(xhr, "getResponseHeader");
112
+ if (!isCallable(getResponseHeader)) {
113
+ return measureXhrResponseBodySize ? xhrResponseBodySizeBytes(xhr) : undefined;
114
+ }
115
+ const contentLength = Number.parseInt(toText(Reflect.apply(getResponseHeader, xhr, ["Content-Length"]) ?? ""), DECIMAL);
116
+ if (Number.isFinite(contentLength) && contentLength >= MIN_BYTES) {
117
+ return contentLength;
118
+ }
119
+ return measureXhrResponseBodySize ? xhrResponseBodySizeBytes(xhr) : undefined;
120
+ }
121
+
122
+ /**
123
+ * @param {unknown} xhr
124
+ * @returns {number | undefined}
125
+ */
126
+ function xhrStatusCode(xhr) {
127
+ const status = readField(xhr, "status");
128
+ return typeof status === "number" && Number.isFinite(status) ? status : undefined;
129
+ }
130
+
131
+ /**
132
+ * @param {XhrCaptureSettings} capture
133
+ * @param {unknown} error
134
+ * @returns {XhrCaptureSettings}
135
+ */
136
+ function xhrErrorSettings(capture, error) {
137
+ return {
138
+ ...capture,
139
+ measureXhrResponseBodySize: false,
140
+ metadata: {
141
+ ...capture.metadata,
142
+ xhrErrorName: errorName(error),
143
+ xhrErrorValueType: typeof error
144
+ },
145
+ status: "error"
146
+ };
147
+ }
148
+
149
+ /**
150
+ * @param {unknown} xhr
151
+ * @param {unknown} url
152
+ * @param {XhrSend} request
153
+ * @returns {void}
154
+ */
155
+ function propagateTraceparent(xhr, url, { methods, settings }) {
156
+ if (indexRuntime.shouldPropagateTraceparent(url, settings.tracePropagationTargets)) {
157
+ Reflect.apply(methods.setRequestHeader, xhr, ["traceparent", indexRuntime.createReactNativeTraceHeaders(settings.capture.trace).traceparent]);
158
+ }
159
+ }
160
+
161
+ class XhrRequestState {
162
+ /**
163
+ * @param {string} method
164
+ * @param {unknown} url
165
+ */
166
+ constructor(method, url) {
167
+ this.method = method;
168
+ this.reported = false;
169
+ this.url = url;
170
+ /** @type {string | undefined} */
171
+ this.body = undefined;
172
+ this.startedAtMs = Number.NaN;
173
+ this.timestamp = "";
174
+ /** @type {number | undefined} */
175
+ this.responseStartAtMs = undefined;
176
+ }
177
+
178
+ /**
179
+ * @param {unknown} xhr
180
+ * @param {XhrSend} request
181
+ * @returns {unknown}
182
+ */
183
+ send(xhr, { args, methods, settings }) {
184
+ const [body] = args;
185
+ this.body = stringValue(body);
186
+ this.startedAtMs = settings.capture.nowMs();
187
+ this.timestamp = settings.now();
188
+ this.trackReadyState(xhr, settings.capture);
189
+ propagateTraceparent(xhr, this.url, { args, methods, settings });
190
+ try {
191
+ return Reflect.apply(methods.send, xhr, args);
192
+ } catch (error) {
193
+ this.capture(xhr, xhrErrorSettings(settings.capture, error));
194
+ throw error;
195
+ }
196
+ }
197
+
198
+ /**
199
+ * @returns {number | undefined}
200
+ */
201
+ responseStartDurationMs() {
202
+ const { responseStartAtMs } = this;
203
+ return responseStartAtMs === undefined ? undefined : elapsedMs(this.startedAtMs, () => responseStartAtMs);
204
+ }
205
+
206
+ /**
207
+ * @param {unknown} xhr
208
+ * @param {XhrCaptureSettings} settings
209
+ * @returns {void}
210
+ */
211
+ capture(xhr, settings) {
212
+ if (this.reported) {
213
+ return;
214
+ }
215
+ this.reported = true;
216
+ const durationMs = elapsedMs(this.startedAtMs, settings.nowMs);
217
+ const responseStartDurationMs = this.responseStartDurationMs();
218
+ const responseSizeBytes = xhrResponseSizeBytes(xhr, settings.measureXhrResponseBodySize);
219
+ const routeTemplate = settings.routeTemplate ?? settings.routeTemplateFactory({ url: this.url });
220
+ const statusCode = xhrStatusCode(xhr);
221
+ indexRuntime.captureReactNativeResourceSpan(settings.client, {
222
+ appState: settings.appState,
223
+ durationMs,
224
+ metadata: {
225
+ ...metadataRuntime.createSafeReactNativeMetadata(settings.metadata, settings.metadataFactory, {
226
+ durationMs,
227
+ init: this.body === undefined ? undefined : { body: this.body },
228
+ input: this.url,
229
+ method: this.method,
230
+ routeTemplate,
231
+ status: settings.status ?? undefined,
232
+ statusCode,
233
+ url: this.url
234
+ }),
235
+ responseStartDurationMs,
236
+ transport: "xhr"
237
+ },
238
+ method: this.method,
239
+ platform: settings.platform,
240
+ responseSizeBytes,
241
+ routeTemplate,
242
+ screen: settings.screen,
243
+ sessionId: settings.sessionId,
244
+ status: settings.status ?? undefined,
245
+ statusCode,
246
+ timestamp: this.timestamp,
247
+ trace: settings.trace
248
+ });
249
+ }
250
+
251
+ /**
252
+ * @param {unknown} xhr
253
+ * @param {XhrCaptureSettings} settings
254
+ * @returns {void}
255
+ */
256
+ readyStateChanged(xhr, settings) {
257
+ const readyState = readField(xhr, "readyState");
258
+ if (readyState === settings.headersReceivedState && this.responseStartAtMs === undefined) {
259
+ this.responseStartAtMs = settings.nowMs();
260
+ return;
261
+ }
262
+ if (readField(xhr, "readyState") === settings.doneState && !this.reported) {
263
+ this.capture(xhr, settings);
264
+ }
265
+ }
266
+
267
+ /**
268
+ * @param {unknown} xhr
269
+ * @param {XhrCaptureSettings} settings
270
+ * @returns {void}
271
+ */
272
+ trackReadyState(xhr, settings) {
273
+ const { onReadyStateChange } = { onReadyStateChange: () => { this.readyStateChanged(xhr, settings); } };
274
+ const addEventListener = readField(xhr, "addEventListener");
275
+ if (isCallable(addEventListener)) {
276
+ Reflect.apply(addEventListener, xhr, ["readystatechange", onReadyStateChange]);
277
+ return;
278
+ }
279
+ const originalOnReadyStateChange = readField(xhr, "onreadystatechange");
280
+ /**
281
+ * @this {unknown}
282
+ * @param {readonly unknown[]} args
283
+ * @returns {unknown}
284
+ */
285
+ function logBrewOnReadyStateChange(...args) {
286
+ onReadyStateChange();
287
+ if (isCallable(originalOnReadyStateChange)) {
288
+ return Reflect.apply(originalOnReadyStateChange, this, args);
289
+ }
290
+ return undefined;
291
+ }
292
+ if (isWritableRecord(xhr)) {
293
+ xhr["onreadystatechange"] = logBrewOnReadyStateChange;
294
+ }
295
+ }
296
+ }
297
+
298
+ module.exports = { XhrRequestState };