@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
@@ -1,24 +1,42 @@
1
1
  "use strict";
2
2
 
3
3
  const { SdkError } = require("@logbrew/sdk");
4
- const {
5
- captureReactNativeResourceSpan,
6
- createReactNavigationSpanListener,
7
- createReactNativeTraceContext,
8
- createReactNativeTraceHeaders,
9
- getActiveLogBrewTrace,
10
- shouldPropagateTraceparent
11
- } = require("./index.cjs");
12
- const { createAppStateLifecycleSpanListener } = require("./lifecycle.cjs");
13
- const { createSafeReactNativeMetadata } = require("./metadata.cjs");
14
- const {
15
- clearLogBrewNativeBridgeScope,
16
- syncLogBrewNativeBridgeScope,
17
- withLogBrewNativeBridgeScope
18
- } = require("./native-bridge.cjs");
19
- const { createReactNativeResourceFetch } = require("./resource-fetch.cjs");
20
-
21
- function createLogBrewReactNativeInstrumentation(client, {
4
+ const { createInstrumentationTeardown } = require("./instrumentation-teardown.cjs");
5
+ const { installInstrumentation, nativeScopeOptions } = require("./instrumentation-install.cjs");
6
+ const { nativeBridgeRuntime } = require("./instrumentation-runtime.cjs");
7
+ const { indexRuntime, isCallable, isObjectLike } = require("./instrumentation-values.cjs");
8
+
9
+ /**
10
+ * @import { LogBrewClient } from "@logbrew/sdk"
11
+ * @import { InstrumentationTeardown } from "./instrumentation-teardown.cjs"
12
+ * @import {
13
+ * ActiveSettings,
14
+ * InstalledHooks,
15
+ * InstrumentationOptions,
16
+ * InstrumentationSettings
17
+ * } from "./instrumentation-install.cjs"
18
+ * @import { ResourceFetch } from "./instrumentation-runtime.cjs"
19
+ * @import { TraceInput } from "./instrumentation-values.cjs"
20
+ */
21
+
22
+ /**
23
+ * @typedef {{
24
+ * readonly globalFetch: InstalledHooks["globalFetch"],
25
+ * readonly globalXMLHttpRequest: InstalledHooks["globalXMLHttpRequest"],
26
+ * readonly remove: () => void,
27
+ * readonly resourceFetch: ResourceFetch,
28
+ * readonly stop: () => void,
29
+ * syncNativeBridgeScope(options?: unknown): unknown,
30
+ * readonly trace: TraceInput,
31
+ * withNativeBridgeScope(callbackOrOptions: unknown, maybeCallback?: unknown): unknown
32
+ * }} InstrumentationHandle
33
+ */
34
+
35
+ /**
36
+ * @param {InstrumentationOptions} [options]
37
+ * @returns {InstrumentationSettings}
38
+ */
39
+ function instrumentationSettings({
22
40
  appState,
23
41
  captureInitialLifecycleState = false,
24
42
  captureInitialNavigationRoute = false,
@@ -48,612 +66,105 @@ function createLogBrewReactNativeInstrumentation(client, {
48
66
  traceFlags = "01",
49
67
  tracePropagationTargets = []
50
68
  } = {}) {
51
- requireClient(client);
52
- const activeTrace = resolveInstrumentationTrace({ randomValues, trace, traceFlags });
53
- const removers = [];
54
- const resolvedNavigation = navigationContainer ?? navigation;
55
- let nativeScopeApplied = false;
56
- let resourceFetch;
57
- let globalFetch;
58
- let globalXMLHttpRequest;
59
-
60
- try {
61
- if (appState?.addEventListener) {
62
- removers.push(createAppStateLifecycleSpanListener(client, appState, {
63
- captureInitialState: captureInitialLifecycleState,
64
- metadata,
65
- now,
66
- nowMs,
67
- onError,
68
- platform,
69
- screen,
70
- sessionId,
71
- trace: activeTrace
72
- }));
73
- }
74
-
75
- if (resolvedNavigation) {
76
- removers.push(createReactNavigationSpanListener(client, resolvedNavigation, {
77
- appState,
78
- captureInitialRoute: captureInitialNavigationRoute,
79
- includeRouteKey,
80
- metadata,
81
- now,
82
- nowMs,
83
- onError,
84
- platform,
85
- trace: activeTrace
86
- }));
87
- }
88
-
89
- if (nativeBridge) {
90
- syncLogBrewNativeBridgeScope(nativeBridge, {
91
- logger,
92
- metadata,
93
- screen,
94
- sessionId,
95
- source: "react-native.instrumentation",
96
- trace: activeTrace
97
- });
98
- nativeScopeApplied = true;
99
- }
100
-
101
- resourceFetch = createReactNativeResourceFetch(client, {
102
- appState,
103
- fetchImpl,
104
- measureResponseBodySize: measureFetchResponseBodySize,
105
- metadata,
106
- metadataFactory,
107
- now,
108
- nowMs,
109
- platform,
110
- randomValues,
111
- routeTemplate,
112
- routeTemplateFactory,
113
- screen,
114
- sessionId,
115
- trace: activeTrace,
116
- traceFlags,
117
- tracePropagationTargets
118
- });
119
- globalFetch = instrumentGlobalFetch ? installGlobalFetchInstrumentation(client, {
120
- appState,
121
- globalObject,
122
- measureFetchResponseBodySize,
123
- metadata,
124
- metadataFactory,
125
- now,
126
- nowMs,
127
- platform,
128
- randomValues,
129
- routeTemplate,
130
- routeTemplateFactory,
131
- screen,
132
- sessionId,
133
- trace: activeTrace,
134
- traceFlags,
135
- tracePropagationTargets
136
- }) : undefined;
137
- if (globalFetch) {
138
- removers.push(globalFetch.remove);
139
- }
140
- globalXMLHttpRequest = instrumentGlobalXMLHttpRequest ? installGlobalXMLHttpRequestInstrumentation(client, {
141
- appState,
142
- globalObject,
143
- measureXhrResponseBodySize,
144
- metadata,
145
- metadataFactory,
146
- now,
147
- nowMs,
148
- platform,
149
- routeTemplate,
150
- routeTemplateFactory,
151
- screen,
152
- sessionId,
153
- trace: activeTrace,
154
- tracePropagationTargets
155
- }) : undefined;
156
- if (globalXMLHttpRequest) {
157
- removers.push(globalXMLHttpRequest.remove);
158
- }
159
- } catch (error) {
160
- try {
161
- removeConfiguredInstrumentation({
162
- nativeBridge: nativeScopeApplied ? nativeBridge : undefined,
163
- removers
164
- });
165
- } catch {
166
- // Preserve the setup error after attempting every removal.
167
- }
168
- throw error;
169
- }
170
-
171
- let removed = false;
172
- const remove = () => {
173
- if (removed) {
174
- return;
175
- }
176
- removeConfiguredInstrumentation({ nativeBridge, removers });
177
- removed = true;
178
- };
179
-
180
- const handle = {
181
- globalFetch,
182
- globalXMLHttpRequest,
183
- remove,
184
- resourceFetch,
185
- stop: remove,
186
- syncNativeBridgeScope(options = {}) {
187
- if (!nativeBridge) {
188
- return undefined;
189
- }
190
- return syncLogBrewNativeBridgeScope(nativeBridge, {
191
- logger,
192
- metadata,
193
- screen,
194
- sessionId,
195
- source: "react-native.instrumentation",
196
- trace: activeTrace,
197
- ...options
198
- });
199
- },
200
- trace: activeTrace,
201
- withNativeBridgeScope(callbackOrOptions, maybeCallback) {
202
- if (!nativeBridge) {
203
- throw new SdkError("configuration_error", "withNativeBridgeScope requires nativeBridge");
204
- }
205
- if (typeof callbackOrOptions === "function") {
206
- return withLogBrewNativeBridgeScope(nativeBridge, {
207
- logger,
208
- metadata,
209
- screen,
210
- sessionId,
211
- source: "react-native.instrumentation",
212
- trace: activeTrace
213
- }, callbackOrOptions);
214
- }
215
- return withLogBrewNativeBridgeScope(nativeBridge, {
216
- logger,
217
- metadata,
218
- screen,
219
- sessionId,
220
- source: "react-native.instrumentation",
221
- trace: activeTrace,
222
- ...(callbackOrOptions ?? {})
223
- }, maybeCallback);
224
- }
69
+ return {
70
+ appState, captureInitialLifecycleState, captureInitialNavigationRoute, fetchImpl, globalObject, includeRouteKey,
71
+ instrumentGlobalFetch, instrumentGlobalXMLHttpRequest, logger, measureFetchResponseBodySize, measureXhrResponseBodySize,
72
+ metadata, metadataFactory, nativeBridge, navigation, navigationContainer, now, nowMs, onError, platform, randomValues,
73
+ routeTemplate, routeTemplateFactory, screen, sessionId, trace, traceFlags, tracePropagationTargets
225
74
  };
226
- return Object.freeze(handle);
227
75
  }
228
76
 
77
+ /**
78
+ * @param {InstrumentationSettings} settings
79
+ * @returns {TraceInput}
80
+ */
229
81
  function resolveInstrumentationTrace({ randomValues, trace, traceFlags }) {
230
82
  if (typeof trace === "string") {
231
- return createReactNativeTraceContext({ randomValues, traceFlags, traceparent: trace });
83
+ return indexRuntime.createReactNativeTraceContext({ randomValues, traceFlags, traceparent: trace });
232
84
  }
233
- return trace ?? getActiveLogBrewTrace() ?? createReactNativeTraceContext({ randomValues, traceFlags });
85
+ return trace ?? indexRuntime.getActiveLogBrewTrace() ?? indexRuntime.createReactNativeTraceContext({ randomValues, traceFlags });
234
86
  }
235
87
 
88
+ /**
89
+ * @param {LogBrewClient | null | undefined} client
90
+ * @returns {asserts client is LogBrewClient}
91
+ */
236
92
  function requireClient(client) {
237
93
  if (!client) {
238
94
  throw new SdkError("configuration_error", "createLogBrewReactNativeInstrumentation requires a client");
239
95
  }
240
96
  }
241
97
 
242
- function removeConfiguredInstrumentation({ nativeBridge, removers }) {
243
- const failures = [];
244
- const failedRemovers = [];
245
- if (nativeBridge) {
246
- try {
247
- clearLogBrewNativeBridgeScope(nativeBridge);
248
- } catch (error) {
249
- failures.push(error);
250
- }
251
- }
252
- for (const removeListener of removers.splice(0).reverse()) {
253
- try {
254
- removeListener();
255
- } catch (error) {
256
- failedRemovers.unshift(removeListener);
257
- failures.push(error);
258
- }
259
- }
260
- removers.push(...failedRemovers);
261
- if (failures.length > 0) {
262
- throw failures[0];
263
- }
98
+ /**
99
+ * @param {unknown} options
100
+ * @returns {{ readonly [key: string]: unknown }}
101
+ */
102
+ function scopeOverrides(options) {
103
+ return isObjectLike(options) ? options : {};
264
104
  }
265
105
 
266
- function installGlobalFetchInstrumentation(client, {
267
- appState,
268
- globalObject,
269
- measureFetchResponseBodySize,
270
- metadata,
271
- metadataFactory,
272
- now,
273
- nowMs,
274
- platform,
275
- randomValues,
276
- routeTemplate,
277
- routeTemplateFactory,
278
- screen,
279
- sessionId,
280
- trace,
281
- traceFlags,
282
- tracePropagationTargets
283
- }) {
284
- if ((typeof globalObject !== "object" && typeof globalObject !== "function") || globalObject === null) {
285
- throw new SdkError("configuration_error", "instrumentGlobalFetch requires a globalObject");
286
- }
287
- const originalFetch = globalObject.fetch;
288
- if (typeof originalFetch !== "function") {
289
- throw new SdkError("configuration_error", "instrumentGlobalFetch requires globalObject.fetch");
290
- }
291
- const globalResourceFetch = createReactNativeResourceFetch(client, {
292
- appState,
293
- fetchImpl: (input, init) => originalFetch.call(globalObject, input, init),
294
- measureResponseBodySize: measureFetchResponseBodySize,
295
- metadata,
296
- metadataFactory,
297
- now,
298
- nowMs,
299
- platform,
300
- randomValues,
301
- routeTemplate,
302
- routeTemplateFactory,
303
- screen,
304
- sessionId,
305
- trace,
306
- traceFlags,
307
- tracePropagationTargets
308
- });
309
- let removed = false;
310
- const wrappedFetch = (input, init) => globalResourceFetch(input, init);
311
- try {
312
- globalObject.fetch = wrappedFetch;
313
- } catch {
314
- throw new SdkError("configuration_error", "instrumentGlobalFetch could not patch globalObject.fetch");
315
- }
316
- if (globalObject.fetch !== wrappedFetch) {
317
- throw new SdkError("configuration_error", "instrumentGlobalFetch could not patch globalObject.fetch");
318
- }
319
- const remove = () => {
320
- if (removed) {
321
- return;
322
- }
323
- removed = true;
324
- if (globalObject.fetch === wrappedFetch) {
325
- globalObject.fetch = originalFetch;
326
- }
327
- };
328
- return Object.freeze({
329
- fetch: wrappedFetch,
330
- remove,
331
- stop: remove
332
- });
333
- }
334
-
335
- function installGlobalXMLHttpRequestInstrumentation(client, {
336
- appState,
337
- globalObject,
338
- measureXhrResponseBodySize,
339
- metadata,
340
- metadataFactory,
341
- now,
342
- nowMs,
343
- platform,
344
- routeTemplate,
345
- routeTemplateFactory,
346
- screen,
347
- sessionId,
348
- trace,
349
- tracePropagationTargets
350
- }) {
351
- if ((typeof globalObject !== "object" && typeof globalObject !== "function") || globalObject === null) {
352
- throw new SdkError("configuration_error", "instrumentGlobalXMLHttpRequest requires a globalObject");
353
- }
354
- const xhrType = globalObject.XMLHttpRequest;
355
- if (typeof xhrType !== "function" || !xhrType.prototype) {
356
- throw new SdkError("configuration_error", "instrumentGlobalXMLHttpRequest requires globalObject.XMLHttpRequest");
357
- }
358
- const prototype = xhrType.prototype;
359
- const originalOpen = prototype.open;
360
- const originalSend = prototype.send;
361
- const originalSetRequestHeader = prototype.setRequestHeader;
362
- if (typeof originalOpen !== "function" || typeof originalSend !== "function" || typeof originalSetRequestHeader !== "function") {
363
- throw new SdkError("configuration_error", "instrumentGlobalXMLHttpRequest requires open, send, and setRequestHeader");
364
- }
365
-
366
- const contextKey = Symbol("logbrew.xhr");
367
- const headersReceivedState = typeof xhrType.HEADERS_RECEIVED === "number" ? xhrType.HEADERS_RECEIVED : 2;
368
- const doneState = typeof xhrType.DONE === "number" ? xhrType.DONE : 4;
369
- const safeRouteTemplateFactory = routeTemplateFactory ?? defaultXhrRouteTemplateFactory;
370
-
371
- /** @this {XMLHttpRequest} */
372
- function wrappedOpen(method, url) {
373
- this[contextKey] = {
374
- method: normalizeXhrMethod(method),
375
- reported: false,
376
- url: xhrUrl(url)
377
- };
378
- return originalOpen.apply(this, arguments);
379
- }
380
-
381
- /** @this {XMLHttpRequest} */
382
- function wrappedSend(body) {
383
- const context = this[contextKey];
384
- if (!context) {
385
- return originalSend.apply(this, arguments);
386
- }
387
- context.body = xhrRequestBody(body);
388
- context.startedAtMs = nowMs();
389
- context.timestamp = now();
390
- installXhrReadyStateTracker(this, context, {
391
- appState,
392
- client,
393
- doneState,
394
- headersReceivedState,
395
- measureXhrResponseBodySize,
396
- metadata,
397
- metadataFactory,
398
- nowMs,
399
- platform,
400
- routeTemplate,
401
- routeTemplateFactory: safeRouteTemplateFactory,
402
- screen,
403
- sessionId,
404
- trace
405
- });
406
- if (shouldPropagateTraceparent(context.url, tracePropagationTargets)) {
407
- originalSetRequestHeader.call(this, "traceparent", createReactNativeTraceHeaders(trace).traceparent);
408
- }
409
- try {
410
- return originalSend.apply(this, arguments);
411
- } catch (error) {
412
- captureXhrResourceSpan(this, context, {
413
- appState,
414
- client,
415
- metadata: {
416
- ...metadata,
417
- xhrErrorName: errorName(error),
418
- xhrErrorValueType: typeof error
419
- },
420
- metadataFactory,
421
- nowMs,
422
- platform,
423
- routeTemplate,
424
- routeTemplateFactory: safeRouteTemplateFactory,
425
- screen,
426
- sessionId,
427
- status: "error",
428
- trace
429
- });
430
- throw error;
431
- }
432
- }
433
-
434
- prototype.open = wrappedOpen;
435
- prototype.send = wrappedSend;
436
- if (prototype.open !== wrappedOpen || prototype.send !== wrappedSend) {
437
- throw new SdkError("configuration_error", "instrumentGlobalXMLHttpRequest could not patch XMLHttpRequest");
438
- }
439
-
106
+ /**
107
+ * @param {ActiveSettings} settings
108
+ * @param {InstrumentationTeardown} teardown
109
+ * @param {InstalledHooks} installed
110
+ * @returns {Readonly<InstrumentationHandle>}
111
+ */
112
+ function instrumentationHandle(settings, teardown, installed) {
113
+ const { nativeBridge } = settings;
440
114
  let removed = false;
441
- const remove = () => {
442
- if (removed) {
443
- return;
444
- }
445
- removed = true;
446
- if (prototype.open === wrappedOpen) {
447
- prototype.open = originalOpen;
448
- }
449
- if (prototype.send === wrappedSend) {
450
- prototype.send = originalSend;
115
+ const { remove } = {
116
+ remove: () => {
117
+ if (removed) {
118
+ return;
119
+ }
120
+ teardown.removeAll(nativeBridge);
121
+ removed = true;
451
122
  }
452
123
  };
453
-
454
- return Object.freeze({
124
+ /** @type {InstrumentationHandle} */
125
+ const handle = {
126
+ globalFetch: installed.globalFetch,
127
+ globalXMLHttpRequest: installed.globalXMLHttpRequest,
455
128
  remove,
456
- stop: remove
457
- });
458
- }
459
-
460
- function installXhrReadyStateTracker(xhr, context, options) {
461
- const onReadyStateChange = () => {
462
- if (xhr.readyState === options.headersReceivedState && context.responseStartAtMs === undefined) {
463
- context.responseStartAtMs = options.nowMs();
464
- return;
465
- }
466
- if (xhr.readyState === options.doneState && !context.reported) {
467
- captureXhrResourceSpan(xhr, context, options);
468
- }
469
- };
470
- if (typeof xhr.addEventListener === "function") {
471
- xhr.addEventListener("readystatechange", onReadyStateChange);
472
- return;
473
- }
474
- const originalOnReadyStateChange = xhr.onreadystatechange;
475
- xhr.onreadystatechange = function logBrewOnReadyStateChange() {
476
- onReadyStateChange();
477
- if (typeof originalOnReadyStateChange === "function") {
478
- return originalOnReadyStateChange.apply(this, arguments);
479
- }
480
- return undefined;
481
- };
482
- }
483
-
484
- function captureXhrResourceSpan(xhr, context, {
485
- appState,
486
- client,
487
- measureXhrResponseBodySize,
488
- metadata,
489
- metadataFactory,
490
- nowMs,
491
- platform,
492
- routeTemplate,
493
- routeTemplateFactory,
494
- screen,
495
- sessionId,
496
- status,
497
- trace
498
- }) {
499
- if (context.reported) {
500
- return;
501
- }
502
- context.reported = true;
503
- const durationMs = elapsedMs(context.startedAtMs, nowMs);
504
- const responseStartDurationMs = context.responseStartAtMs === undefined
505
- ? undefined
506
- : elapsedMs(context.startedAtMs, () => context.responseStartAtMs);
507
- const responseSizeBytes = xhrResponseSizeBytes(xhr, { measureXhrResponseBodySize });
508
- const safeRouteTemplate = routeTemplate ?? routeTemplateFactory({ url: context.url });
509
- const statusCode = xhrStatusCode(xhr);
510
- captureReactNativeResourceSpan(client, {
511
- appState,
512
- durationMs,
513
- metadata: {
514
- ...createSafeReactNativeMetadata(metadata, metadataFactory, {
515
- durationMs,
516
- init: context.body === undefined ? undefined : { body: context.body },
517
- input: context.url,
518
- method: context.method,
519
- routeTemplate: safeRouteTemplate,
520
- status: status ?? undefined,
521
- statusCode,
522
- url: context.url
523
- }),
524
- responseStartDurationMs,
525
- transport: "xhr"
129
+ resourceFetch: installed.resourceFetch,
130
+ stop: remove,
131
+ syncNativeBridgeScope(options = {}) {
132
+ const hasNativeBridge = Boolean(nativeBridge);
133
+ if (!hasNativeBridge) {
134
+ return undefined;
135
+ }
136
+ return nativeBridgeRuntime.syncLogBrewNativeBridgeScope(nativeBridge, { ...nativeScopeOptions(settings), ...scopeOverrides(options) });
526
137
  },
527
- method: context.method,
528
- platform,
529
- routeTemplate: safeRouteTemplate,
530
- screen,
531
- responseSizeBytes,
532
- sessionId,
533
- status: status ?? undefined,
534
- statusCode,
535
- timestamp: context.timestamp,
536
- trace
537
- });
538
- }
539
-
540
- function normalizeXhrMethod(method) {
541
- return String(method ?? "GET").toUpperCase();
542
- }
543
-
544
- function xhrUrl(url) {
545
- if (typeof url === "string") {
546
- return url;
547
- }
548
- try {
549
- return url.toString();
550
- } catch {
551
- return String(url);
552
- }
553
- }
554
-
555
- function xhrRequestBody(body) {
556
- if (typeof body === "string") {
557
- return body;
558
- }
559
- if (body instanceof String) {
560
- return body.toString();
561
- }
562
- return undefined;
563
- }
564
-
565
- function defaultXhrRouteTemplateFactory({ url }) {
566
- const URLConstructor = globalThis.URL;
567
- if (typeof URLConstructor === "function") {
568
- try {
569
- const parsedUrl = new URLConstructor(url, "https://logbrew.local");
570
- return parsedUrl.pathname;
571
- } catch {
572
- // Fall back to query/hash stripping below for non-standard request keys.
573
- }
574
- }
575
- return String(url).split(/[?#]/u, 1)[0];
576
- }
577
-
578
- function elapsedMs(startedAtMs, nowMs) {
579
- const durationMs = nowMs() - startedAtMs;
580
- return Number.isFinite(durationMs) ? Math.max(0, durationMs) : undefined;
581
- }
582
-
583
- function xhrStatusCode(xhr) {
584
- return typeof xhr?.status === "number" && Number.isFinite(xhr.status) ? xhr.status : undefined;
585
- }
586
-
587
- function xhrResponseSizeBytes(xhr, { measureXhrResponseBodySize = false } = {}) {
588
- if (typeof xhr?.getResponseHeader !== "function") {
589
- return measureXhrResponseBodySize ? xhrResponseBodySizeBytes(xhr) : undefined;
590
- }
591
- const contentLength = Number.parseInt(String(xhr.getResponseHeader("Content-Length") ?? ""), 10);
592
- if (Number.isFinite(contentLength) && contentLength >= 0) {
593
- return contentLength;
594
- }
595
- return measureXhrResponseBodySize ? xhrResponseBodySizeBytes(xhr) : undefined;
596
- }
597
-
598
- function xhrResponseBodySizeBytes(xhr) {
599
- const responseType = readXhrResponseValue(xhr, "responseType");
600
- if (responseType === undefined || responseType === "" || responseType === "text") {
601
- const responseText = readXhrResponseValue(xhr, "responseText");
602
- if (typeof responseText === "string" || responseText instanceof String) {
603
- return utf8ByteLength(responseText.toString());
604
- }
605
- }
606
- const response = readXhrResponseValue(xhr, "response");
607
- if (typeof response === "string" || response instanceof String) {
608
- return utf8ByteLength(response.toString());
609
- }
610
- if (typeof ArrayBuffer === "function") {
611
- if (response instanceof ArrayBuffer) {
612
- return response.byteLength;
613
- }
614
- if (typeof ArrayBuffer.isView === "function" && ArrayBuffer.isView(response)) {
615
- return response.byteLength;
616
- }
617
- }
618
- if (typeof response?.size === "number" && Number.isFinite(response.size) && response.size >= 0) {
619
- return response.size;
620
- }
621
- return undefined;
622
- }
623
-
624
- function readXhrResponseValue(xhr, property) {
625
- try {
626
- return xhr?.[property];
627
- } catch {
628
- return undefined;
629
- }
630
- }
631
-
632
- function utf8ByteLength(value) {
633
- let bytes = 0;
634
- for (let index = 0; index < value.length; index += 1) {
635
- const code = value.charCodeAt(index);
636
- if (code <= 0x7f) {
637
- bytes += 1;
638
- } else if (code <= 0x7ff) {
639
- bytes += 2;
640
- } else if (code >= 0xd800 && code <= 0xdbff && index + 1 < value.length) {
641
- const next = value.charCodeAt(index + 1);
642
- if (next >= 0xdc00 && next <= 0xdfff) {
643
- bytes += 4;
644
- index += 1;
645
- } else {
646
- bytes += 3;
138
+ trace: settings.trace,
139
+ withNativeBridgeScope(callbackOrOptions, maybeCallback) {
140
+ const hasNativeBridge = Boolean(nativeBridge);
141
+ if (!hasNativeBridge) {
142
+ throw new SdkError("configuration_error", "withNativeBridgeScope requires nativeBridge");
647
143
  }
648
- } else {
649
- bytes += 3;
144
+ if (isCallable(callbackOrOptions)) {
145
+ return nativeBridgeRuntime.withLogBrewNativeBridgeScope(nativeBridge, nativeScopeOptions(settings), callbackOrOptions);
146
+ }
147
+ return nativeBridgeRuntime.withLogBrewNativeBridgeScope(nativeBridge, {
148
+ ...nativeScopeOptions(settings),
149
+ ...scopeOverrides(callbackOrOptions)
150
+ }, maybeCallback);
650
151
  }
651
- }
652
- return bytes;
152
+ };
153
+ return Object.freeze(handle);
653
154
  }
654
155
 
655
- function errorName(error) {
656
- return typeof error?.name === "string" && error.name.trim() !== "" ? error.name : "Error";
156
+ /**
157
+ * @param {LogBrewClient | null | undefined} client
158
+ * @param {InstrumentationOptions} [options]
159
+ * @returns {Readonly<InstrumentationHandle>}
160
+ */
161
+ function createLogBrewReactNativeInstrumentation(client, options = {}) {
162
+ const settings = instrumentationSettings(options);
163
+ requireClient(client);
164
+ const context = { client, settings: { ...settings, trace: resolveInstrumentationTrace(settings) } };
165
+ const teardown = createInstrumentationTeardown();
166
+ const installed = installInstrumentation(context, teardown);
167
+ return instrumentationHandle(context.settings, teardown, installed);
657
168
  }
658
169
 
659
170
  module.exports = {