@logbrew/react-native 0.1.38 → 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.
- package/LogBrewReactNative.podspec +1 -1
- package/README.md +17 -15
- package/android-native-diagnostics.d.ts +15 -5
- package/android-native-diagnostics.js +170 -94
- package/apollo.cjs +435 -226
- package/apollo.d.cts +2 -70
- package/apollo.d.ts +24 -12
- package/apollo.js +438 -221
- package/apple-native-diagnostics-config.js +176 -0
- package/apple-native-diagnostics-context.js +232 -0
- package/apple-native-diagnostics-module.js +229 -0
- package/apple-native-diagnostics.d.ts +42 -11
- package/apple-native-diagnostics.js +60 -389
- package/expo.cjs +29 -16
- package/expo.d.ts +10 -9
- package/expo.js +4 -3
- package/fatal-admission.cjs +113 -0
- package/fatal-record.cjs +317 -0
- package/fatal-replay.cjs +297 -508
- package/global-errors-capture.cjs +369 -0
- package/global-errors-event.cjs +174 -0
- package/global-errors-support.cjs +128 -0
- package/global-errors.cjs +75 -351
- package/global-errors.d.cts +29 -278
- package/global-errors.d.ts +57 -27
- package/global-errors.js +7 -2
- package/global-errors.native.js +83 -48
- package/index-breadcrumbs.cjs +139 -0
- package/index-errors.cjs +167 -0
- package/index-events.cjs +365 -0
- package/index-navigation.cjs +327 -0
- package/index-random.cjs +120 -0
- package/index-react.cjs +189 -0
- package/index-spans.cjs +190 -0
- package/index-trace-fetch.cjs +322 -0
- package/index-trace.cjs +335 -0
- package/index-transport.cjs +201 -0
- package/index-values.cjs +232 -0
- package/index.cjs +95 -1220
- package/index.d.cts +2 -505
- package/index.d.ts +130 -469
- package/index.native.d.ts +146 -22
- package/index.native.js +184 -159
- package/instrumentation-fetch.cjs +113 -0
- package/instrumentation-install.cjs +241 -0
- package/instrumentation-runtime.cjs +65 -0
- package/instrumentation-teardown.cjs +93 -0
- package/instrumentation-values.cjs +282 -0
- package/instrumentation-xhr-request.cjs +298 -0
- package/instrumentation-xhr.cjs +233 -0
- package/instrumentation.cjs +110 -599
- package/instrumentation.d.cts +2 -84
- package/instrumentation.d.ts +22 -8
- package/ios/AppleDiagnostics/LBRNAppleNativeDiagnostics.swift +1 -1
- package/ios/GeneratedAppleDiagnostics/LogBrew/DeliveryEngine.swift +7 -1
- package/ios/GeneratedAppleDiagnostics/LogBrew/DeliveryEngineAutomatic.swift +36 -4
- package/ios/GeneratedAppleDiagnostics/LogBrew/DeliveryEngineQueue.swift +17 -0
- package/ios/GeneratedAppleDiagnostics/LogBrew/DeliveryLifecycle.swift +2 -0
- package/ios/GeneratedAppleDiagnostics/LogBrew/Transport.swift +41 -6
- package/ios/GeneratedAppleDiagnostics/SOURCE-MANIFEST.json +5 -5
- package/lifecycle.cjs +181 -78
- package/lifecycle.d.cts +1 -50
- package/lifecycle.d.ts +17 -6
- package/metadata.cjs +279 -114
- package/metro-debug-id.cjs +296 -0
- package/metro-serializer.cjs +219 -0
- package/metro.cjs +153 -297
- package/metro.d.cts +2 -57
- package/metro.d.ts +27 -13
- package/metro.js +2 -3
- package/native-bridge-async.cjs +165 -0
- package/native-bridge-scope.cjs +148 -0
- package/native-bridge.cjs +83 -186
- package/native-bridge.d.cts +2 -94
- package/native-bridge.d.ts +28 -15
- package/native-client-bindings.js +230 -0
- package/native-diagnostics-values.js +120 -0
- package/native-module-lookup.js +31 -0
- package/native-null.js +3 -0
- package/null-value.cjs +4 -0
- package/package.json +44 -2
- package/persistent-delivery-store.js +247 -0
- package/persistent-delivery.native.js +183 -261
- package/promise-rejection-handlers.cjs +368 -0
- package/promise-rejections.cjs +169 -361
- package/react-native.config.js +3 -1
- package/release-artifacts-files.cjs +94 -0
- package/release-artifacts-options.cjs +182 -0
- package/release-artifacts.cjs +278 -207
- package/release-artifacts.d.cts +2 -55
- package/release-artifacts.d.ts +18 -7
- package/release-artifacts.js +2 -4
- package/resource-fetch-graphql.cjs +265 -0
- package/resource-fetch-size.cjs +156 -0
- package/resource-fetch.cjs +227 -428
- package/resource-fetch.d.cts +1 -60
- package/resource-fetch.d.ts +17 -6
- package/scripts/sync-apple-native-sources.mjs +101 -53
- package/types/capture.d.ts +95 -0
- package/types/config.d.ts +85 -0
- package/types/defaults.d.ts +55 -0
- package/types/events.d.ts +203 -0
- package/types/provider.d.ts +81 -0
- package/types/trace.d.ts +104 -0
package/index-events.cjs
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
const { SdkError } = require("@logbrew/sdk");
|
|
2
|
+
const { productAnalyticsMetadata, recordIssueBreadcrumb, screenActionName } = require("./index-breadcrumbs.cjs");
|
|
3
|
+
const { defaultEventId } = require("./index-random.cjs");
|
|
4
|
+
const { getActiveLogBrewTrace, getReactNativeTraceMetadata } = require("./index-trace.cjs");
|
|
5
|
+
const {
|
|
6
|
+
compactMetadata,
|
|
7
|
+
getReactNativeContext,
|
|
8
|
+
readField,
|
|
9
|
+
requireClient,
|
|
10
|
+
requireNonEmpty,
|
|
11
|
+
statusFromStatusCode,
|
|
12
|
+
stripQueryAndHash,
|
|
13
|
+
subscriptionRemover,
|
|
14
|
+
textOf
|
|
15
|
+
} = require("./index-values.cjs");
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @import { AppStateLike, ClientLike, ContextOptions, LooseRecord } from "./index-values.cjs"
|
|
19
|
+
* @typedef {ContextOptions & { readonly trace?: unknown }} TracedContextOptions
|
|
20
|
+
* @typedef {TracedContextOptions & {
|
|
21
|
+
* readonly id?: string | undefined,
|
|
22
|
+
* readonly timestamp?: string | undefined,
|
|
23
|
+
* readonly status?: unknown
|
|
24
|
+
* }} CaptureScreenViewOptions
|
|
25
|
+
* @typedef {TracedContextOptions & {
|
|
26
|
+
* readonly id?: string | undefined,
|
|
27
|
+
* readonly timestamp?: string | undefined
|
|
28
|
+
* }} CaptureAppStateChangeOptions
|
|
29
|
+
* @typedef {CaptureAppStateChangeOptions & {
|
|
30
|
+
* readonly flushOnBackground?: unknown,
|
|
31
|
+
* readonly onFlushError?: unknown
|
|
32
|
+
* }} AppStateListenerOptions
|
|
33
|
+
* @typedef {TracedContextOptions & {
|
|
34
|
+
* readonly id?: string | undefined,
|
|
35
|
+
* readonly idFactory?: (context: { readonly name: unknown, readonly screen: unknown }) => string,
|
|
36
|
+
* readonly name?: unknown,
|
|
37
|
+
* readonly now?: () => string,
|
|
38
|
+
* readonly screen?: unknown,
|
|
39
|
+
* readonly sessionId?: unknown,
|
|
40
|
+
* readonly status?: unknown,
|
|
41
|
+
* readonly timestamp?: string | undefined,
|
|
42
|
+
* readonly traceId?: unknown
|
|
43
|
+
* }} ActionInput
|
|
44
|
+
* @typedef {TracedContextOptions & {
|
|
45
|
+
* readonly durationMs?: unknown,
|
|
46
|
+
* readonly id?: string | undefined,
|
|
47
|
+
* readonly idFactory?: (context: { readonly method: string | undefined, readonly routeTemplate: string | undefined, readonly screen: unknown }) => string,
|
|
48
|
+
* readonly method?: unknown,
|
|
49
|
+
* readonly name?: string | undefined,
|
|
50
|
+
* readonly now?: () => string,
|
|
51
|
+
* readonly routeTemplate?: unknown,
|
|
52
|
+
* readonly screen?: unknown,
|
|
53
|
+
* readonly sessionId?: unknown,
|
|
54
|
+
* readonly status?: unknown,
|
|
55
|
+
* readonly statusCode?: unknown,
|
|
56
|
+
* readonly timestamp?: string | undefined,
|
|
57
|
+
* readonly traceId?: unknown
|
|
58
|
+
* }} NetworkInput
|
|
59
|
+
* @typedef {{ readonly id: string, readonly timestamp: string, readonly attributes: LooseRecord }} ActionEvent
|
|
60
|
+
* @typedef {{ readonly addEventListener: (type: string, listener: (state: string) => void) => unknown }} AppStateSource
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
const BACKGROUND_APP_STATES = new Set(["background", "inactive"]);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @returns {string}
|
|
67
|
+
*/
|
|
68
|
+
function currentTimestamp() {
|
|
69
|
+
return new Date().toISOString();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @returns {string}
|
|
74
|
+
*/
|
|
75
|
+
function defaultActionEventId() {
|
|
76
|
+
return defaultEventId("action");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @returns {string}
|
|
81
|
+
*/
|
|
82
|
+
function defaultNetworkEventId() {
|
|
83
|
+
return defaultEventId("network");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {ClientLike} client
|
|
88
|
+
* @param {unknown} screenName
|
|
89
|
+
* @param {CaptureScreenViewOptions} [options]
|
|
90
|
+
* @returns {void}
|
|
91
|
+
*/
|
|
92
|
+
function captureScreenView(client, screenName, {
|
|
93
|
+
id,
|
|
94
|
+
timestamp = new Date().toISOString(),
|
|
95
|
+
status = "success",
|
|
96
|
+
platform,
|
|
97
|
+
appState,
|
|
98
|
+
trace,
|
|
99
|
+
metadata = {}
|
|
100
|
+
} = {}) {
|
|
101
|
+
requireClient(client);
|
|
102
|
+
requireNonEmpty("screen name", screenName);
|
|
103
|
+
client.action(id ?? defaultEventId("screen"), timestamp, {
|
|
104
|
+
name: screenActionName(textOf(screenName)),
|
|
105
|
+
status,
|
|
106
|
+
|
|
107
|
+
metadata: {
|
|
108
|
+
...getReactNativeContext({ appState, platform }),
|
|
109
|
+
screen: screenName,
|
|
110
|
+
...metadata,
|
|
111
|
+
...getReactNativeTraceMetadata(trace ?? getActiveLogBrewTrace()),
|
|
112
|
+
...productAnalyticsMetadata("screen_view", screenName)
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
recordIssueBreadcrumb(client, {
|
|
116
|
+
timestamp,
|
|
117
|
+
|
|
118
|
+
attributes: { name: screenActionName(textOf(screenName)), status }
|
|
119
|
+
}, { category: "navigation", type: "navigation" });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @param {ClientLike} client
|
|
124
|
+
* @param {unknown} state
|
|
125
|
+
* @param {CaptureAppStateChangeOptions} [options]
|
|
126
|
+
* @returns {void}
|
|
127
|
+
*/
|
|
128
|
+
function captureAppStateChange(client, state, {
|
|
129
|
+
id,
|
|
130
|
+
timestamp = new Date().toISOString(),
|
|
131
|
+
platform,
|
|
132
|
+
appState,
|
|
133
|
+
trace,
|
|
134
|
+
metadata = {}
|
|
135
|
+
} = {}) {
|
|
136
|
+
requireClient(client);
|
|
137
|
+
requireNonEmpty("app state", state);
|
|
138
|
+
client.action(id ?? defaultEventId("app_state"), timestamp, {
|
|
139
|
+
name: "app_state_change",
|
|
140
|
+
status: "success",
|
|
141
|
+
|
|
142
|
+
metadata: {
|
|
143
|
+
...getReactNativeContext({ appState, platform }),
|
|
144
|
+
appState: state,
|
|
145
|
+
...metadata,
|
|
146
|
+
...getReactNativeTraceMetadata(trace ?? getActiveLogBrewTrace())
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
recordIssueBreadcrumb(client, {
|
|
150
|
+
timestamp,
|
|
151
|
+
|
|
152
|
+
attributes: { name: "app_state_change", status: "success" }
|
|
153
|
+
}, { category: "app.state", type: "state" });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @param {ActionInput} [input]
|
|
158
|
+
* @returns {ActionEvent}
|
|
159
|
+
*/
|
|
160
|
+
function createReactNativeActionEvent({
|
|
161
|
+
id,
|
|
162
|
+
idFactory = defaultActionEventId,
|
|
163
|
+
metadata = {},
|
|
164
|
+
name,
|
|
165
|
+
now = currentTimestamp,
|
|
166
|
+
platform,
|
|
167
|
+
appState,
|
|
168
|
+
screen,
|
|
169
|
+
sessionId,
|
|
170
|
+
status = "success",
|
|
171
|
+
timestamp,
|
|
172
|
+
trace,
|
|
173
|
+
traceId
|
|
174
|
+
} = {}) {
|
|
175
|
+
return {
|
|
176
|
+
id: id ?? idFactory({ name, screen }),
|
|
177
|
+
timestamp: timestamp ?? now(),
|
|
178
|
+
|
|
179
|
+
attributes: {
|
|
180
|
+
name,
|
|
181
|
+
status,
|
|
182
|
+
|
|
183
|
+
metadata: compactMetadata({
|
|
184
|
+
...getReactNativeContext({ appState, platform }),
|
|
185
|
+
source: "react-native.action",
|
|
186
|
+
|
|
187
|
+
screen,
|
|
188
|
+
sessionId,
|
|
189
|
+
traceId,
|
|
190
|
+
...metadata,
|
|
191
|
+
...getReactNativeTraceMetadata(trace ?? getActiveLogBrewTrace()),
|
|
192
|
+
...productAnalyticsMetadata("interaction", screen)
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* @param {ClientLike} client
|
|
200
|
+
* @param {ActionInput} [input]
|
|
201
|
+
* @returns {ActionEvent}
|
|
202
|
+
*/
|
|
203
|
+
function captureReactNativeAction(client, input = {}) {
|
|
204
|
+
requireClient(client);
|
|
205
|
+
const event = createReactNativeActionEvent(input);
|
|
206
|
+
client.action(event.id, event.timestamp, event.attributes);
|
|
207
|
+
recordIssueBreadcrumb(client, event, { category: "ui.action", type: "user" });
|
|
208
|
+
return event;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* @param {NetworkInput} [input]
|
|
213
|
+
* @returns {ActionEvent}
|
|
214
|
+
*/
|
|
215
|
+
function createReactNativeNetworkEvent({
|
|
216
|
+
durationMs,
|
|
217
|
+
id,
|
|
218
|
+
idFactory = defaultNetworkEventId,
|
|
219
|
+
metadata = {},
|
|
220
|
+
method,
|
|
221
|
+
name,
|
|
222
|
+
now = currentTimestamp,
|
|
223
|
+
platform,
|
|
224
|
+
appState,
|
|
225
|
+
routeTemplate,
|
|
226
|
+
screen,
|
|
227
|
+
sessionId,
|
|
228
|
+
status,
|
|
229
|
+
statusCode,
|
|
230
|
+
timestamp,
|
|
231
|
+
trace,
|
|
232
|
+
traceId
|
|
233
|
+
} = {}) {
|
|
234
|
+
const safeRouteTemplate = stripQueryAndHash(routeTemplate);
|
|
235
|
+
const safeMethod = method === undefined ? undefined : textOf(method).toUpperCase();
|
|
236
|
+
const actionName = name ?? [safeMethod, safeRouteTemplate].filter(Boolean).join(" ");
|
|
237
|
+
return {
|
|
238
|
+
id: id ?? idFactory({ method: safeMethod, routeTemplate: safeRouteTemplate, screen }),
|
|
239
|
+
timestamp: timestamp ?? now(),
|
|
240
|
+
|
|
241
|
+
attributes: {
|
|
242
|
+
name: actionName,
|
|
243
|
+
status: status ?? statusFromStatusCode(statusCode),
|
|
244
|
+
|
|
245
|
+
metadata: compactMetadata({
|
|
246
|
+
...getReactNativeContext({ appState, platform }),
|
|
247
|
+
source: "react-native.network",
|
|
248
|
+
|
|
249
|
+
durationMs,
|
|
250
|
+
method: safeMethod,
|
|
251
|
+
routeTemplate: safeRouteTemplate,
|
|
252
|
+
screen,
|
|
253
|
+
sessionId,
|
|
254
|
+
statusCode,
|
|
255
|
+
traceId,
|
|
256
|
+
...metadata,
|
|
257
|
+
...getReactNativeTraceMetadata(trace ?? getActiveLogBrewTrace())
|
|
258
|
+
})
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* @param {ClientLike} client
|
|
265
|
+
* @param {NetworkInput} [input]
|
|
266
|
+
* @returns {ActionEvent}
|
|
267
|
+
*/
|
|
268
|
+
function captureReactNativeNetwork(client, input = {}) {
|
|
269
|
+
requireClient(client);
|
|
270
|
+
const event = createReactNativeNetworkEvent(input);
|
|
271
|
+
client.action(event.id, event.timestamp, event.attributes);
|
|
272
|
+
recordIssueBreadcrumb(client, event, { category: "http", type: "http" });
|
|
273
|
+
return event;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* @param {unknown} value
|
|
278
|
+
* @returns {value is AppStateSource}
|
|
279
|
+
*/
|
|
280
|
+
function isAppStateSource(value) {
|
|
281
|
+
return typeof readField(value, "addEventListener") === "function";
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* @param {unknown} value
|
|
286
|
+
* @returns {value is (error: unknown) => unknown}
|
|
287
|
+
*/
|
|
288
|
+
function isErrorObserver(value) {
|
|
289
|
+
return typeof value === "function";
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* @param {(error: unknown) => unknown} onFlushError
|
|
294
|
+
* @param {unknown} error
|
|
295
|
+
* @returns {boolean}
|
|
296
|
+
*/
|
|
297
|
+
function notifyFlushError(onFlushError, error) {
|
|
298
|
+
try {
|
|
299
|
+
onFlushError(error);
|
|
300
|
+
return true;
|
|
301
|
+
} catch {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* @param {unknown} pending
|
|
308
|
+
* @param {unknown} onFlushError
|
|
309
|
+
* @returns {Promise<void>}
|
|
310
|
+
*/
|
|
311
|
+
async function observeBackgroundFlush(pending, onFlushError) {
|
|
312
|
+
try {
|
|
313
|
+
await pending;
|
|
314
|
+
} catch (error) {
|
|
315
|
+
if (isErrorObserver(onFlushError)) {
|
|
316
|
+
notifyFlushError(onFlushError, error);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* @param {ClientLike} client
|
|
323
|
+
* @param {AppStateLike | null | undefined} appState
|
|
324
|
+
* @param {AppStateListenerOptions} [options]
|
|
325
|
+
* @returns {() => unknown}
|
|
326
|
+
*/
|
|
327
|
+
function createAppStateListener(client, appState, options = {}) {
|
|
328
|
+
requireClient(client);
|
|
329
|
+
if (!isAppStateSource(appState)) {
|
|
330
|
+
throw new SdkError("configuration_error", "createAppStateListener requires AppState.addEventListener");
|
|
331
|
+
}
|
|
332
|
+
const {
|
|
333
|
+
flushOnBackground = false,
|
|
334
|
+
onFlushError,
|
|
335
|
+
...captureOptions
|
|
336
|
+
} = options;
|
|
337
|
+
if (typeof flushOnBackground !== "boolean") {
|
|
338
|
+
throw new SdkError("configuration_error", "createAppStateListener flushOnBackground must be a boolean");
|
|
339
|
+
}
|
|
340
|
+
if (onFlushError !== undefined && typeof onFlushError !== "function") {
|
|
341
|
+
throw new SdkError("configuration_error", "createAppStateListener onFlushError must be a function");
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const subscription = appState.addEventListener("change", nextState => {
|
|
345
|
+
captureAppStateChange(client, nextState, {
|
|
346
|
+
...captureOptions,
|
|
347
|
+
appState
|
|
348
|
+
});
|
|
349
|
+
if (flushOnBackground && BACKGROUND_APP_STATES.has(nextState)) {
|
|
350
|
+
void observeBackgroundFlush(client.flush(), onFlushError);
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
return subscriptionRemover(subscription);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
module.exports = {
|
|
358
|
+
captureAppStateChange,
|
|
359
|
+
captureReactNativeAction,
|
|
360
|
+
captureReactNativeNetwork,
|
|
361
|
+
captureScreenView,
|
|
362
|
+
createAppStateListener,
|
|
363
|
+
createReactNativeActionEvent,
|
|
364
|
+
createReactNativeNetworkEvent
|
|
365
|
+
};
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
const { SdkError } = require("@logbrew/sdk");
|
|
2
|
+
const { captureReactNativeNavigationSpan } = require("./index-spans.cjs");
|
|
3
|
+
const { normalizeMetadataValue, readField, requireClient, stripQueryAndHash, subscriptionRemover } = require("./index-values.cjs");
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @import { ClientLike, ContextOptions } from "./index-values.cjs"
|
|
7
|
+
* @typedef {{ readonly key?: unknown, readonly name?: string | undefined, readonly path?: string | undefined }} RouteSnapshot
|
|
8
|
+
* @typedef {{ readonly actionType: string | undefined, readonly startedAtMs: number, readonly timestamp: string }} PendingNavigation
|
|
9
|
+
* @typedef {{
|
|
10
|
+
* readonly addListener: (eventName: string, listener: (event: unknown) => void) => unknown,
|
|
11
|
+
* readonly getCurrentRoute: () => unknown
|
|
12
|
+
* }} NavigationContainerLike
|
|
13
|
+
* @typedef {ContextOptions & {
|
|
14
|
+
* readonly captureInitialRoute?: boolean | undefined,
|
|
15
|
+
* readonly includeRouteKey?: boolean | undefined,
|
|
16
|
+
* readonly now?: () => string,
|
|
17
|
+
* readonly nowMs?: () => number,
|
|
18
|
+
* readonly onError?: unknown,
|
|
19
|
+
* readonly trace?: unknown
|
|
20
|
+
* }} NavigationListenerOptions
|
|
21
|
+
* @typedef {{ readonly pendingNavigation: PendingNavigation | undefined, readonly previousRoute: RouteSnapshot }} NavigationState
|
|
22
|
+
* @typedef {{ readonly read: () => NavigationState, readonly write: (next: NavigationState) => void }} NavigationStore
|
|
23
|
+
* @typedef {{
|
|
24
|
+
* readonly appState: ContextOptions["appState"],
|
|
25
|
+
* readonly includeRouteKey: boolean,
|
|
26
|
+
* readonly metadata: ContextOptions["metadata"],
|
|
27
|
+
* readonly now: () => string,
|
|
28
|
+
* readonly platform: ContextOptions["platform"],
|
|
29
|
+
* readonly trace: unknown
|
|
30
|
+
* }} NavigationSpanSettings
|
|
31
|
+
* @typedef {{
|
|
32
|
+
* readonly client: ClientLike,
|
|
33
|
+
* readonly container: NavigationContainerLike,
|
|
34
|
+
* readonly nowMs: () => number,
|
|
35
|
+
* readonly onError: unknown,
|
|
36
|
+
* readonly settings: NavigationSpanSettings,
|
|
37
|
+
* readonly store: NavigationStore
|
|
38
|
+
* }} RouteCaptureContext
|
|
39
|
+
* @typedef {{ readonly add: (remover: () => unknown) => void, readonly removeListeners: () => void }} ListenerRemovers
|
|
40
|
+
* @typedef {{
|
|
41
|
+
* readonly captureCurrentRoute: () => void,
|
|
42
|
+
* readonly captureInitialRoute: boolean,
|
|
43
|
+
* readonly container: NavigationContainerLike,
|
|
44
|
+
* readonly now: () => string,
|
|
45
|
+
* readonly nowMs: () => number,
|
|
46
|
+
* readonly removers: ListenerRemovers,
|
|
47
|
+
* readonly store: NavigationStore
|
|
48
|
+
* }} AttachContext
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
const NO_DURATION = 0;
|
|
52
|
+
const NO_FAILURES = 0;
|
|
53
|
+
const FIRST = 0;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @returns {string}
|
|
57
|
+
*/
|
|
58
|
+
function currentTimestamp() {
|
|
59
|
+
return new Date().toISOString();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @returns {number}
|
|
64
|
+
*/
|
|
65
|
+
function currentTimeMs() {
|
|
66
|
+
return Date.now();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {unknown} value
|
|
71
|
+
* @returns {value is NavigationContainerLike}
|
|
72
|
+
*/
|
|
73
|
+
function isNavigationContainer(value) {
|
|
74
|
+
return typeof readField(value, "addListener") === "function" && typeof readField(value, "getCurrentRoute") === "function";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {unknown} value
|
|
79
|
+
* @returns {value is (error: unknown) => unknown}
|
|
80
|
+
*/
|
|
81
|
+
function isErrorHandler(value) {
|
|
82
|
+
return typeof value === "function";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @param {unknown} navigationContainer
|
|
87
|
+
* @returns {unknown}
|
|
88
|
+
*/
|
|
89
|
+
function resolveNavigationContainer(navigationContainer) {
|
|
90
|
+
if (typeof navigationContainer === "object" && navigationContainer !== null && "current" in navigationContainer) {
|
|
91
|
+
return navigationContainer.current;
|
|
92
|
+
}
|
|
93
|
+
return navigationContainer;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {NavigationContainerLike} container
|
|
98
|
+
* @param {string} eventName
|
|
99
|
+
* @param {(event: unknown) => void} listener
|
|
100
|
+
* @returns {() => unknown}
|
|
101
|
+
*/
|
|
102
|
+
function addNavigationListener(container, eventName, listener) {
|
|
103
|
+
const subscription = container.addListener(eventName, listener);
|
|
104
|
+
return subscriptionRemover(subscription);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param {NavigationContainerLike} container
|
|
109
|
+
* @param {string} eventName
|
|
110
|
+
* @param {(event: unknown) => void} listener
|
|
111
|
+
* @returns {(() => unknown) | undefined}
|
|
112
|
+
*/
|
|
113
|
+
function safeNavigationListener(container, eventName, listener) {
|
|
114
|
+
try {
|
|
115
|
+
return addNavigationListener(container, eventName, listener);
|
|
116
|
+
} catch {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {unknown} route
|
|
123
|
+
* @returns {RouteSnapshot}
|
|
124
|
+
*/
|
|
125
|
+
function routeSnapshot(route) {
|
|
126
|
+
const key = normalizeMetadataValue(readField(route, "key"));
|
|
127
|
+
const name = readField(route, "name");
|
|
128
|
+
return {
|
|
129
|
+
key,
|
|
130
|
+
name: typeof name === "string" && name.trim() !== "" ? name : undefined,
|
|
131
|
+
path: stripQueryAndHash(readField(route, "path"))
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @param {unknown} event
|
|
137
|
+
* @returns {string | undefined}
|
|
138
|
+
*/
|
|
139
|
+
function navigationActionType(event) {
|
|
140
|
+
const action = readField(readField(event, "data"), "action") ?? readField(event, "action") ?? event;
|
|
141
|
+
const type = readField(action, "type");
|
|
142
|
+
return typeof type === "string" && type.trim() !== "" ? type : undefined;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @param {PendingNavigation | undefined} pendingNavigation
|
|
147
|
+
* @param {() => number} nowMs
|
|
148
|
+
* @returns {number | undefined}
|
|
149
|
+
*/
|
|
150
|
+
function navigationDuration(pendingNavigation, nowMs) {
|
|
151
|
+
const startedAtMs = pendingNavigation?.startedAtMs;
|
|
152
|
+
return startedAtMs === undefined ? undefined : Math.max(NO_DURATION, nowMs() - startedAtMs);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @param {() => void} removeListeners
|
|
157
|
+
* @returns {boolean}
|
|
158
|
+
*/
|
|
159
|
+
function tryRemoveListeners(removeListeners) {
|
|
160
|
+
try {
|
|
161
|
+
removeListeners();
|
|
162
|
+
return true;
|
|
163
|
+
} catch {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @param {NavigationState} initial
|
|
170
|
+
* @returns {NavigationStore}
|
|
171
|
+
*/
|
|
172
|
+
function createNavigationStore(initial) {
|
|
173
|
+
let state = initial;
|
|
174
|
+
return {
|
|
175
|
+
read: () => state,
|
|
176
|
+
write: next => {
|
|
177
|
+
state = next;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* @returns {ListenerRemovers}
|
|
184
|
+
*/
|
|
185
|
+
function createListenerRemovers() {
|
|
186
|
+
/** @type {(() => unknown)[]} */
|
|
187
|
+
const removers = [];
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @param {() => unknown} remover
|
|
191
|
+
* @returns {void}
|
|
192
|
+
*/
|
|
193
|
+
function add(remover) {
|
|
194
|
+
removers.unshift(remover);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @returns {void}
|
|
199
|
+
*/
|
|
200
|
+
function removeListeners() {
|
|
201
|
+
/** @type {unknown[]} */
|
|
202
|
+
const failures = [];
|
|
203
|
+
/** @type {(() => unknown)[]} */
|
|
204
|
+
const failedRemovers = [];
|
|
205
|
+
for (const remove of removers.splice(FIRST)) {
|
|
206
|
+
try {
|
|
207
|
+
remove();
|
|
208
|
+
} catch (error) {
|
|
209
|
+
failedRemovers.push(remove);
|
|
210
|
+
failures.push(error);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
removers.unshift(...failedRemovers);
|
|
214
|
+
if (failures.length > NO_FAILURES) {
|
|
215
|
+
throw failures[FIRST];
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return { add, removeListeners };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* @param {RouteCaptureContext} context
|
|
224
|
+
* @returns {() => void}
|
|
225
|
+
*/
|
|
226
|
+
function routeCapturer({ client, container, nowMs, onError, settings, store }) {
|
|
227
|
+
/**
|
|
228
|
+
* @returns {void}
|
|
229
|
+
*/
|
|
230
|
+
function recordCurrentRoute() {
|
|
231
|
+
const route = routeSnapshot(container.getCurrentRoute());
|
|
232
|
+
if (route.name === undefined && (route.path === undefined || route.path === "")) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const { pendingNavigation, previousRoute } = store.read();
|
|
236
|
+
captureReactNativeNavigationSpan(client, {
|
|
237
|
+
actionType: pendingNavigation?.actionType,
|
|
238
|
+
appState: settings.appState,
|
|
239
|
+
durationMs: navigationDuration(pendingNavigation, nowMs),
|
|
240
|
+
includeRouteKey: settings.includeRouteKey,
|
|
241
|
+
metadata: settings.metadata,
|
|
242
|
+
now: settings.now,
|
|
243
|
+
platform: settings.platform,
|
|
244
|
+
previousRouteKey: previousRoute.key,
|
|
245
|
+
previousRouteName: previousRoute.name,
|
|
246
|
+
routeKey: route.key,
|
|
247
|
+
routeName: route.name,
|
|
248
|
+
routePath: route.path,
|
|
249
|
+
timestamp: pendingNavigation?.timestamp,
|
|
250
|
+
trace: settings.trace
|
|
251
|
+
});
|
|
252
|
+
store.write({ pendingNavigation: undefined, previousRoute: route });
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* @returns {void}
|
|
257
|
+
*/
|
|
258
|
+
function captureCurrentRoute() {
|
|
259
|
+
try {
|
|
260
|
+
recordCurrentRoute();
|
|
261
|
+
} catch (error) {
|
|
262
|
+
if (!isErrorHandler(onError)) {
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
onError(error);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return captureCurrentRoute;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* @param {AttachContext} context
|
|
274
|
+
* @returns {void}
|
|
275
|
+
*/
|
|
276
|
+
function attachNavigationListeners({ captureCurrentRoute, captureInitialRoute, container, now, nowMs, removers, store }) {
|
|
277
|
+
try {
|
|
278
|
+
const actionSubscription = safeNavigationListener(container, "__unsafe_action__", event => {
|
|
279
|
+
store.write({
|
|
280
|
+
...store.read(),
|
|
281
|
+
pendingNavigation: {
|
|
282
|
+
actionType: navigationActionType(event),
|
|
283
|
+
startedAtMs: nowMs(),
|
|
284
|
+
timestamp: now()
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
if (actionSubscription) {
|
|
289
|
+
removers.add(actionSubscription);
|
|
290
|
+
}
|
|
291
|
+
removers.add(addNavigationListener(container, "state", captureCurrentRoute));
|
|
292
|
+
if (captureInitialRoute) {
|
|
293
|
+
captureCurrentRoute();
|
|
294
|
+
}
|
|
295
|
+
} catch (error) {
|
|
296
|
+
tryRemoveListeners(removers.removeListeners);
|
|
297
|
+
throw error;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* @param {ClientLike} client
|
|
303
|
+
* @param {unknown} navigationContainer
|
|
304
|
+
* @param {NavigationListenerOptions} [options]
|
|
305
|
+
* @returns {() => void}
|
|
306
|
+
*/
|
|
307
|
+
function createReactNavigationSpanListener(client, navigationContainer, {
|
|
308
|
+
captureInitialRoute = false, includeRouteKey = false, metadata = {}, now = currentTimestamp,
|
|
309
|
+
nowMs = currentTimeMs, onError, platform, appState, trace
|
|
310
|
+
} = {}) {
|
|
311
|
+
requireClient(client);
|
|
312
|
+
const container = resolveNavigationContainer(navigationContainer);
|
|
313
|
+
if (!isNavigationContainer(container)) {
|
|
314
|
+
throw new SdkError("configuration_error", "createReactNavigationSpanListener requires a React Navigation container ref with addListener and getCurrentRoute");
|
|
315
|
+
}
|
|
316
|
+
const store = createNavigationStore({
|
|
317
|
+
pendingNavigation: undefined,
|
|
318
|
+
previousRoute: captureInitialRoute ? {} : routeSnapshot(container.getCurrentRoute())
|
|
319
|
+
});
|
|
320
|
+
const settings = { appState, includeRouteKey, metadata, now, platform, trace };
|
|
321
|
+
const captureCurrentRoute = routeCapturer({ client, container, nowMs, onError, settings, store });
|
|
322
|
+
const removers = createListenerRemovers();
|
|
323
|
+
attachNavigationListeners({ captureCurrentRoute, captureInitialRoute, container, now, nowMs, removers, store });
|
|
324
|
+
return removers.removeListeners;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
module.exports = { createReactNavigationSpanListener };
|