@symbo.ls/sdk 3.2.3 → 3.2.7

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 (183) hide show
  1. package/README.md +141 -0
  2. package/dist/cjs/config/environment.js +94 -10
  3. package/dist/cjs/index.js +152 -12
  4. package/dist/cjs/services/AdminService.js +351 -0
  5. package/dist/cjs/services/AuthService.js +738 -305
  6. package/dist/cjs/services/BaseService.js +158 -6
  7. package/dist/cjs/services/BranchService.js +484 -0
  8. package/dist/cjs/services/CollabService.js +439 -116
  9. package/dist/cjs/services/DnsService.js +340 -0
  10. package/dist/cjs/services/FeatureFlagService.js +175 -0
  11. package/dist/cjs/services/FileService.js +201 -0
  12. package/dist/cjs/services/IntegrationService.js +538 -0
  13. package/dist/cjs/services/MetricsService.js +62 -0
  14. package/dist/cjs/services/PaymentService.js +271 -0
  15. package/dist/cjs/services/PlanService.js +426 -0
  16. package/dist/cjs/services/ProjectService.js +1207 -0
  17. package/dist/cjs/services/PullRequestService.js +503 -0
  18. package/dist/cjs/services/ScreenshotService.js +304 -0
  19. package/dist/cjs/services/SubscriptionService.js +396 -0
  20. package/dist/cjs/services/TrackingService.js +661 -0
  21. package/dist/cjs/services/WaitlistService.js +148 -0
  22. package/dist/cjs/services/index.js +60 -4
  23. package/dist/cjs/state/RootStateManager.js +2 -23
  24. package/dist/cjs/state/rootEventBus.js +9 -0
  25. package/dist/cjs/utils/CollabClient.js +78 -12
  26. package/dist/cjs/utils/TokenManager.js +16 -3
  27. package/dist/cjs/utils/changePreprocessor.js +199 -0
  28. package/dist/cjs/utils/jsonDiff.js +46 -4
  29. package/dist/cjs/utils/ordering.js +309 -0
  30. package/dist/cjs/utils/services.js +285 -128
  31. package/dist/cjs/utils/validation.js +0 -3
  32. package/dist/esm/config/environment.js +94 -10
  33. package/dist/esm/index.js +47862 -18248
  34. package/dist/esm/services/AdminService.js +1132 -0
  35. package/dist/esm/services/AuthService.js +1493 -386
  36. package/dist/esm/services/BaseService.js +757 -6
  37. package/dist/esm/services/BranchService.js +1265 -0
  38. package/dist/esm/services/CollabService.js +24956 -16089
  39. package/dist/esm/services/DnsService.js +1121 -0
  40. package/dist/esm/services/FeatureFlagService.js +956 -0
  41. package/dist/esm/services/FileService.js +982 -0
  42. package/dist/esm/services/IntegrationService.js +1319 -0
  43. package/dist/esm/services/MetricsService.js +843 -0
  44. package/dist/esm/services/PaymentService.js +1052 -0
  45. package/dist/esm/services/PlanService.js +1207 -0
  46. package/dist/esm/services/ProjectService.js +2526 -0
  47. package/dist/esm/services/PullRequestService.js +1284 -0
  48. package/dist/esm/services/ScreenshotService.js +1085 -0
  49. package/dist/esm/services/SubscriptionService.js +1177 -0
  50. package/dist/esm/services/TrackingService.js +18454 -0
  51. package/dist/esm/services/WaitlistService.js +929 -0
  52. package/dist/esm/services/index.js +47373 -18027
  53. package/dist/esm/state/RootStateManager.js +11 -23
  54. package/dist/esm/state/rootEventBus.js +9 -0
  55. package/dist/esm/utils/CollabClient.js +17526 -16120
  56. package/dist/esm/utils/TokenManager.js +16 -3
  57. package/dist/esm/utils/changePreprocessor.js +542 -0
  58. package/dist/esm/utils/jsonDiff.js +958 -43
  59. package/dist/esm/utils/ordering.js +291 -0
  60. package/dist/esm/utils/services.js +285 -128
  61. package/dist/esm/utils/validation.js +116 -50
  62. package/dist/node/config/environment.js +94 -10
  63. package/dist/node/index.js +183 -16
  64. package/dist/node/services/AdminService.js +332 -0
  65. package/dist/node/services/AuthService.js +742 -310
  66. package/dist/node/services/BaseService.js +148 -6
  67. package/dist/node/services/BranchService.js +465 -0
  68. package/dist/node/services/CollabService.js +439 -116
  69. package/dist/node/services/DnsService.js +321 -0
  70. package/dist/node/services/FeatureFlagService.js +156 -0
  71. package/dist/node/services/FileService.js +182 -0
  72. package/dist/node/services/IntegrationService.js +519 -0
  73. package/dist/node/services/MetricsService.js +43 -0
  74. package/dist/node/services/PaymentService.js +252 -0
  75. package/dist/node/services/PlanService.js +407 -0
  76. package/dist/node/services/ProjectService.js +1188 -0
  77. package/dist/node/services/PullRequestService.js +484 -0
  78. package/dist/node/services/ScreenshotService.js +285 -0
  79. package/dist/node/services/SubscriptionService.js +377 -0
  80. package/dist/node/services/TrackingService.js +632 -0
  81. package/dist/node/services/WaitlistService.js +129 -0
  82. package/dist/node/services/index.js +60 -4
  83. package/dist/node/state/RootStateManager.js +2 -23
  84. package/dist/node/state/rootEventBus.js +9 -0
  85. package/dist/node/utils/CollabClient.js +77 -11
  86. package/dist/node/utils/TokenManager.js +16 -3
  87. package/dist/node/utils/changePreprocessor.js +180 -0
  88. package/dist/node/utils/jsonDiff.js +46 -4
  89. package/dist/node/utils/ordering.js +290 -0
  90. package/dist/node/utils/services.js +285 -128
  91. package/dist/node/utils/validation.js +0 -3
  92. package/package.json +30 -18
  93. package/src/config/environment.js +95 -10
  94. package/src/index.js +190 -23
  95. package/src/services/AdminService.js +374 -0
  96. package/src/services/AuthService.js +874 -328
  97. package/src/services/BaseService.js +166 -6
  98. package/src/services/BranchService.js +536 -0
  99. package/src/services/CollabService.js +557 -148
  100. package/src/services/DnsService.js +366 -0
  101. package/src/services/FeatureFlagService.js +174 -0
  102. package/src/services/FileService.js +213 -0
  103. package/src/services/IntegrationService.js +548 -0
  104. package/src/services/MetricsService.js +40 -0
  105. package/src/services/PaymentService.js +287 -0
  106. package/src/services/PlanService.js +468 -0
  107. package/src/services/ProjectService.js +1366 -0
  108. package/src/services/PullRequestService.js +537 -0
  109. package/src/services/ScreenshotService.js +258 -0
  110. package/src/services/SubscriptionService.js +425 -0
  111. package/src/services/TrackingService.js +853 -0
  112. package/src/services/WaitlistService.js +130 -0
  113. package/src/services/index.js +79 -5
  114. package/src/services/tests/BranchService/createBranch.test.js +153 -0
  115. package/src/services/tests/BranchService/deleteBranch.test.js +173 -0
  116. package/src/services/tests/BranchService/getBranchChanges.test.js +146 -0
  117. package/src/services/tests/BranchService/listBranches.test.js +87 -0
  118. package/src/services/tests/BranchService/mergeBranch.test.js +210 -0
  119. package/src/services/tests/BranchService/publishVersion.test.js +183 -0
  120. package/src/services/tests/BranchService/renameBranch.test.js +240 -0
  121. package/src/services/tests/BranchService/resetBranch.test.js +152 -0
  122. package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +67 -0
  123. package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +75 -0
  124. package/src/services/tests/FileService/createFileFormData.test.js +74 -0
  125. package/src/services/tests/FileService/getFileUrl.test.js +69 -0
  126. package/src/services/tests/FileService/updateProjectIcon.test.js +109 -0
  127. package/src/services/tests/FileService/uploadDocument.test.js +36 -0
  128. package/src/services/tests/FileService/uploadFile.test.js +78 -0
  129. package/src/services/tests/FileService/uploadFileWithValidation.test.js +114 -0
  130. package/src/services/tests/FileService/uploadImage.test.js +36 -0
  131. package/src/services/tests/FileService/uploadMultipleFiles.test.js +111 -0
  132. package/src/services/tests/FileService/validateFile.test.js +63 -0
  133. package/src/services/tests/PlanService/createPlan.test.js +104 -0
  134. package/src/services/tests/PlanService/createPlanWithValidation.test.js +523 -0
  135. package/src/services/tests/PlanService/deletePlan.test.js +92 -0
  136. package/src/services/tests/PlanService/getActivePlans.test.js +123 -0
  137. package/src/services/tests/PlanService/getAdminPlans.test.js +84 -0
  138. package/src/services/tests/PlanService/getPlan.test.js +50 -0
  139. package/src/services/tests/PlanService/getPlanByKey.test.js +109 -0
  140. package/src/services/tests/PlanService/getPlanWithValidation.test.js +85 -0
  141. package/src/services/tests/PlanService/getPlans.test.js +53 -0
  142. package/src/services/tests/PlanService/getPlansByPriceRange.test.js +109 -0
  143. package/src/services/tests/PlanService/getPlansWithValidation.test.js +48 -0
  144. package/src/services/tests/PlanService/initializePlans.test.js +75 -0
  145. package/src/services/tests/PlanService/updatePlan.test.js +111 -0
  146. package/src/services/tests/PlanService/updatePlanWithValidation.test.js +556 -0
  147. package/src/state/RootStateManager.js +37 -32
  148. package/src/state/rootEventBus.js +19 -0
  149. package/src/utils/CollabClient.js +99 -12
  150. package/src/utils/TokenManager.js +20 -3
  151. package/src/utils/changePreprocessor.js +239 -0
  152. package/src/utils/jsonDiff.js +40 -5
  153. package/src/utils/ordering.js +271 -0
  154. package/src/utils/services.js +306 -139
  155. package/src/utils/validation.js +0 -3
  156. package/dist/cjs/services/AIService.js +0 -155
  157. package/dist/cjs/services/BasedService.js +0 -1185
  158. package/dist/cjs/services/CoreService.js +0 -2295
  159. package/dist/cjs/services/SocketService.js +0 -309
  160. package/dist/cjs/services/SymstoryService.js +0 -571
  161. package/dist/cjs/utils/basedQuerys.js +0 -181
  162. package/dist/cjs/utils/symstoryClient.js +0 -259
  163. package/dist/esm/services/AIService.js +0 -185
  164. package/dist/esm/services/BasedService.js +0 -5262
  165. package/dist/esm/services/CoreService.js +0 -2827
  166. package/dist/esm/services/SocketService.js +0 -456
  167. package/dist/esm/services/SymstoryService.js +0 -7025
  168. package/dist/esm/utils/basedQuerys.js +0 -163
  169. package/dist/esm/utils/symstoryClient.js +0 -354
  170. package/dist/node/services/AIService.js +0 -136
  171. package/dist/node/services/BasedService.js +0 -1156
  172. package/dist/node/services/CoreService.js +0 -2266
  173. package/dist/node/services/SocketService.js +0 -280
  174. package/dist/node/services/SymstoryService.js +0 -542
  175. package/dist/node/utils/basedQuerys.js +0 -162
  176. package/dist/node/utils/symstoryClient.js +0 -230
  177. package/src/services/AIService.js +0 -150
  178. package/src/services/BasedService.js +0 -1302
  179. package/src/services/CoreService.js +0 -2548
  180. package/src/services/SocketService.js +0 -336
  181. package/src/services/SymstoryService.js +0 -649
  182. package/src/utils/basedQuerys.js +0 -164
  183. package/src/utils/symstoryClient.js +0 -252
@@ -0,0 +1,853 @@
1
+ import { BaseService } from './BaseService.js'
2
+ import environment from '../config/environment.js'
3
+
4
+ const DEFAULT_MAX_QUEUE_SIZE = 100
5
+
6
+ const isBrowserEnvironment = () => typeof window !== 'undefined'
7
+
8
+ const DEFAULT_TRACKING_OPTIONS = {
9
+ // Enabled by default unless explicitly disabled via SDK options or runtime config
10
+ enabled: true,
11
+ sessionTracking: true,
12
+ enableTracing: true,
13
+ maxQueueSize: DEFAULT_MAX_QUEUE_SIZE,
14
+ transports: null,
15
+ instrumentations: null,
16
+ instrumentationsFactory: null,
17
+ webInstrumentationOptions: null,
18
+ globalAttributes: {},
19
+ transport: null,
20
+ user: null
21
+ }
22
+
23
+ const sanitizeAttributes = (value) => {
24
+ if (!value || typeof value !== 'object') {
25
+ return {}
26
+ }
27
+
28
+ try {
29
+ return JSON.parse(JSON.stringify(value))
30
+ } catch (error) {
31
+ console.warn('[TrackingService] Failed to sanitize attributes:', error)
32
+ return { ...value }
33
+ }
34
+ }
35
+
36
+ export class TrackingService extends BaseService {
37
+ constructor ({ context, options } = {}) {
38
+ super({ context, options })
39
+
40
+ this._faroClient = null
41
+ this._queue = []
42
+ this._initialized = false
43
+ this._enabled = DEFAULT_TRACKING_OPTIONS.enabled
44
+ this._runtimeConfig = {}
45
+ this._setupPromise = null
46
+
47
+ this._trackingOptions = {
48
+ ...DEFAULT_TRACKING_OPTIONS,
49
+ ...(options?.tracking || {})
50
+ }
51
+
52
+ this._globalAttributes = sanitizeAttributes(
53
+ this._trackingOptions.globalAttributes || {}
54
+ )
55
+ this._trackingOptions.globalAttributes = { ...this._globalAttributes }
56
+ }
57
+
58
+ async init ({ context = {}, options = {} } = {}) {
59
+ this.updateContext(context)
60
+
61
+ // Merge tracking options from constructor, SDK options and runtime overrides
62
+ this._trackingOptions = {
63
+ ...DEFAULT_TRACKING_OPTIONS,
64
+ ...this._trackingOptions,
65
+ ...(this._options?.tracking || {}),
66
+ ...(options.tracking || {})
67
+ }
68
+
69
+ this._globalAttributes = sanitizeAttributes(
70
+ this._trackingOptions.globalAttributes || {}
71
+ )
72
+
73
+ this._enabled = this._trackingOptions.enabled !== false
74
+
75
+ if (!this._enabled) {
76
+ this._setReady()
77
+ return this
78
+ }
79
+
80
+ if (!isBrowserEnvironment()) {
81
+ this._enabled = false
82
+ this._setReady()
83
+ return this
84
+ }
85
+
86
+ this._runtimeConfig = this._buildRuntimeConfig()
87
+ this._trackingOptions.transports = this._runtimeConfig.transports
88
+
89
+ const hasCustomTransports =
90
+ Array.isArray(this._runtimeConfig.transports) &&
91
+ this._runtimeConfig.transports.length > 0
92
+
93
+ if (!this._runtimeConfig.url && !hasCustomTransports) {
94
+ console.warn('[TrackingService] Grafana Faro URL missing. Tracking will stay disabled.')
95
+ this._enabled = false
96
+ this._setReady()
97
+ return this
98
+ }
99
+
100
+ if (this._initialized) {
101
+ this._setReady()
102
+ return this
103
+ }
104
+
105
+ if (!this._setupPromise) {
106
+ this._setupPromise = this._loadFaroClient(this._runtimeConfig)
107
+ }
108
+
109
+ await this._setupPromise
110
+
111
+ return this
112
+ }
113
+
114
+ updateContext (context) {
115
+ super.updateContext(context)
116
+
117
+ const trackingContext = context?.tracking
118
+
119
+ if (trackingContext) {
120
+ if (Object.hasOwn(trackingContext, 'user')) {
121
+ this.setUser(trackingContext.user, { queue: false })
122
+ }
123
+
124
+ if (Object.hasOwn(trackingContext, 'globalAttributes')) {
125
+ this.setGlobalAttributes(trackingContext.globalAttributes)
126
+ }
127
+ }
128
+ }
129
+
130
+ configureTracking (trackingOptions = {}) {
131
+ if (!trackingOptions || typeof trackingOptions !== 'object') {
132
+ return this
133
+ }
134
+
135
+ this._trackingOptions = {
136
+ ...this._trackingOptions,
137
+ ...trackingOptions
138
+ }
139
+
140
+ if (Object.hasOwn(trackingOptions, 'globalAttributes')) {
141
+ this.setGlobalAttributes(trackingOptions.globalAttributes)
142
+ }
143
+
144
+ if (Object.hasOwn(trackingOptions, 'user')) {
145
+ this.setUser(trackingOptions.user)
146
+ this._trackingOptions.user = trackingOptions.user
147
+ }
148
+
149
+ return this
150
+ }
151
+
152
+ configure (trackingOptions = {}) {
153
+ return this.configureTracking(trackingOptions)
154
+ }
155
+
156
+ trackEvent (name, attributes, options = {}) {
157
+ if (!name) {
158
+ throw new Error('Event name is required for trackEvent')
159
+ }
160
+
161
+ const mergedAttributes = this._mergeAttributes(attributes)
162
+ const eventOptions = options && typeof options === 'object' ? options : {}
163
+ const { domain, queue, ...restOptions } = eventOptions
164
+ const eventDomain = Object.hasOwn(eventOptions, 'domain')
165
+ ? domain
166
+ : null
167
+
168
+ const invokeTracking = client => {
169
+ const api = client?.api
170
+ if (!api?.pushEvent) {
171
+ console.warn('[TrackingService] Faro pushEvent API not available')
172
+ return
173
+ }
174
+
175
+ api.pushEvent(name, mergedAttributes, eventDomain, restOptions)
176
+ }
177
+
178
+ if (Object.hasOwn(eventOptions, 'queue')) {
179
+ this._withClient(invokeTracking, { queue: Boolean(queue) })
180
+ } else {
181
+ this._withClient(invokeTracking)
182
+ }
183
+ }
184
+
185
+ trackError (error, options = {}) {
186
+ if (!error) {
187
+ return
188
+ }
189
+
190
+ const err =
191
+ error instanceof Error
192
+ ? error
193
+ : new Error(
194
+ typeof error === 'string' ? error : 'Unknown error captured by TrackingService'
195
+ )
196
+
197
+ const isContextOnly =
198
+ options &&
199
+ typeof options === 'object' &&
200
+ options !== null &&
201
+ !(
202
+ 'context' in options ||
203
+ 'type' in options ||
204
+ 'stackFrames' in options ||
205
+ 'skipDedupe' in options ||
206
+ 'spanContext' in options ||
207
+ 'timestampOverwriteMs' in options ||
208
+ 'originalError' in options
209
+ )
210
+
211
+ const normalizedOptions = isContextOnly
212
+ ? { context: options }
213
+ : options || {}
214
+
215
+ const mergedContext = this._mergeAttributes(normalizedOptions.context)
216
+ const apiOptions = {
217
+ ...normalizedOptions,
218
+ ...(Object.keys(mergedContext).length > 0 ? { context: mergedContext } : {})
219
+ }
220
+
221
+ const queueConfigured = Object.hasOwn(apiOptions, 'queue')
222
+ const { queue, ...errorOptions } = apiOptions
223
+
224
+ const invokeTracking = client => {
225
+ const api = client?.api
226
+ if (!api?.pushError) {
227
+ console.warn('[TrackingService] Faro pushError API not available')
228
+ return
229
+ }
230
+
231
+ api.pushError(err, errorOptions)
232
+ }
233
+
234
+ if (queueConfigured) {
235
+ this._withClient(invokeTracking, { queue: Boolean(queue) })
236
+ } else {
237
+ this._withClient(invokeTracking)
238
+ }
239
+ }
240
+
241
+ captureException (error, options = {}) {
242
+ this.trackError(error, options)
243
+ }
244
+
245
+ logMessage (message, level = 'info', context = null) {
246
+ if (!message) {
247
+ return
248
+ }
249
+
250
+ const payload = Array.isArray(message) ? message : [message]
251
+ const logContext = this._mergeAttributes(context)
252
+ const severity = level || 'info'
253
+
254
+ const options = {
255
+ level: severity,
256
+ ...(Object.keys(logContext).length > 0 ? { context: logContext } : {})
257
+ }
258
+
259
+ this._withClient(client => {
260
+ const api = client?.api
261
+ if (!api?.pushLog) {
262
+ console.warn('[TrackingService] Faro pushLog API not available')
263
+ return
264
+ }
265
+
266
+ api.pushLog(payload, options)
267
+ })
268
+ }
269
+
270
+ logDebug (message, context) {
271
+ this.logMessage(message, 'debug', context)
272
+ }
273
+
274
+ logInfo (message, context) {
275
+ this.logMessage(message, 'info', context)
276
+ }
277
+
278
+ logWarning (message, context) {
279
+ this.logMessage(message, 'warn', context)
280
+ }
281
+
282
+ logWarn (message, context) {
283
+ this.logWarning(message, context)
284
+ }
285
+
286
+ logErrorMessage (message, context) {
287
+ this.logMessage(message, 'error', context)
288
+ }
289
+
290
+ logError (message, context) {
291
+ this.logErrorMessage(message, context)
292
+ }
293
+
294
+ addBreadcrumb (message, attributes) {
295
+ if (!message) {
296
+ return
297
+ }
298
+
299
+ const breadcrumbAttributes = {
300
+ category: 'custom',
301
+ ...sanitizeAttributes(attributes || {}),
302
+ message
303
+ }
304
+
305
+ this.trackEvent('breadcrumb', breadcrumbAttributes)
306
+ }
307
+
308
+ trackMeasurement (type, values, options = {}) {
309
+ if (!type) {
310
+ throw new Error('Measurement type is required for trackMeasurement')
311
+ }
312
+
313
+ if (values == null) {
314
+ throw new Error('Measurement values are required for trackMeasurement')
315
+ }
316
+
317
+ const measurementValues =
318
+ typeof values === 'object' && !Array.isArray(values)
319
+ ? sanitizeAttributes(values)
320
+ : { value: Number(values) }
321
+
322
+ if (typeof measurementValues.value === 'number' && Number.isNaN(measurementValues.value)) {
323
+ throw new Error('Measurement value must be a valid number')
324
+ }
325
+
326
+ const measurementOptions = options && typeof options === 'object' ? options : {}
327
+ const {
328
+ attributes: measurementAttributesOption,
329
+ context: measurementContextOption,
330
+ queue,
331
+ ...transportOptions
332
+ } = measurementOptions
333
+
334
+ const hasGlobalAttributes = Object.keys(this._globalAttributes).length > 0
335
+
336
+ const attributePayload = measurementAttributesOption
337
+ ? this._mergeAttributes(measurementAttributesOption)
338
+ : hasGlobalAttributes
339
+ ? { ...this._globalAttributes }
340
+ : {}
341
+
342
+ const payload = {
343
+ type,
344
+ values: measurementValues,
345
+ ...(attributePayload && Object.keys(attributePayload).length > 0
346
+ ? { attributes: attributePayload }
347
+ : {})
348
+ }
349
+
350
+ const context = measurementContextOption
351
+ ? this._mergeAttributes(measurementContextOption)
352
+ : hasGlobalAttributes
353
+ ? { ...this._globalAttributes }
354
+ : {}
355
+ const apiOptions = {
356
+ ...transportOptions,
357
+ ...(Object.keys(context).length > 0 ? { context } : {})
358
+ }
359
+
360
+ const invokeTracking = client => {
361
+ const api = client?.api
362
+ if (!api?.pushMeasurement) {
363
+ console.warn('[TrackingService] Faro pushMeasurement API not available')
364
+ return
365
+ }
366
+
367
+ api.pushMeasurement(payload, apiOptions)
368
+ }
369
+
370
+ if (Object.hasOwn(measurementOptions, 'queue')) {
371
+ this._withClient(invokeTracking, { queue: Boolean(queue) })
372
+ } else {
373
+ this._withClient(invokeTracking)
374
+ }
375
+ }
376
+
377
+ trackView (name, attributes) {
378
+ if (!name) {
379
+ throw new Error('View name is required for trackView')
380
+ }
381
+
382
+ const viewAttributes = sanitizeAttributes(attributes || {})
383
+ const merged = this._mergeAttributes(viewAttributes)
384
+
385
+ this._withClient(client => {
386
+ const api = client?.api
387
+ if (!api?.setView) {
388
+ console.warn('[TrackingService] Faro setView API not available')
389
+ return
390
+ }
391
+
392
+ api.setView({
393
+ name,
394
+ ...merged
395
+ })
396
+ })
397
+ }
398
+
399
+ setUser (user, options = {}) {
400
+ if (user == null) {
401
+ this.clearUser()
402
+ return
403
+ }
404
+
405
+ if (typeof user !== 'object') {
406
+ throw new Error('User must be an object')
407
+ }
408
+
409
+ const userData = sanitizeAttributes(user)
410
+
411
+ const queueConfigured =
412
+ options && typeof options === 'object' && Object.hasOwn(options, 'queue')
413
+
414
+ const invokeTracking = client => {
415
+ const api = client?.api
416
+ if (!api?.setUser) {
417
+ console.warn('[TrackingService] Faro setUser API not available')
418
+ return
419
+ }
420
+
421
+ api.setUser(userData)
422
+ }
423
+
424
+ if (queueConfigured) {
425
+ this._withClient(invokeTracking, { queue: Boolean(options.queue) })
426
+ } else {
427
+ this._withClient(invokeTracking)
428
+ }
429
+ }
430
+
431
+ clearUser () {
432
+ this._withClient(client => {
433
+ const api = client?.api
434
+ if (api?.setUser) {
435
+ api.setUser(null)
436
+ } else {
437
+ console.warn('[TrackingService] Faro setUser API not available')
438
+ }
439
+ })
440
+ }
441
+
442
+ setSession (session, options = {}) {
443
+ if (session == null) {
444
+ this.clearSession()
445
+ return
446
+ }
447
+
448
+ if (typeof session !== 'object') {
449
+ throw new Error('Session must be an object')
450
+ }
451
+
452
+ const sessionData = sanitizeAttributes(session)
453
+ const queueConfigured = Object.hasOwn(options, 'queue')
454
+ const { queue, ...sessionOptions } = options
455
+
456
+ const invokeTracking = client => {
457
+ const api = client?.api
458
+ if (!api?.setSession) {
459
+ console.warn('[TrackingService] Faro setSession API not available')
460
+ return
461
+ }
462
+
463
+ api.setSession(sessionData, sessionOptions)
464
+ }
465
+
466
+ if (queueConfigured) {
467
+ this._withClient(invokeTracking, { queue: Boolean(queue) })
468
+ } else {
469
+ this._withClient(invokeTracking)
470
+ }
471
+ }
472
+
473
+ clearSession () {
474
+ this._withClient(client => {
475
+ const api = client?.api
476
+ if (api?.setSession) {
477
+ api.setSession(null)
478
+ } else {
479
+ console.warn('[TrackingService] Faro setSession API not available')
480
+ }
481
+ })
482
+ }
483
+
484
+ setGlobalAttributes (attributes) {
485
+ if (attributes == null) {
486
+ this._globalAttributes = {}
487
+ this._trackingOptions.globalAttributes = {}
488
+ return
489
+ }
490
+
491
+ if (typeof attributes !== 'object') {
492
+ throw new Error('Global attributes must be an object')
493
+ }
494
+
495
+ const sanitized = sanitizeAttributes(attributes)
496
+ this._globalAttributes = sanitized
497
+ this._trackingOptions.globalAttributes = sanitized
498
+ }
499
+
500
+ setGlobalAttribute (key, value) {
501
+ if (!key) {
502
+ throw new Error('Global attribute key is required')
503
+ }
504
+
505
+ const sanitized = sanitizeAttributes({ [key]: value })
506
+ this._globalAttributes = {
507
+ ...this._globalAttributes,
508
+ ...sanitized
509
+ }
510
+ this._trackingOptions.globalAttributes = { ...this._globalAttributes }
511
+ }
512
+
513
+ removeGlobalAttribute (key) {
514
+ if (!key) {
515
+ throw new Error('Global attribute key is required to remove it')
516
+ }
517
+
518
+ if (Object.hasOwn(this._globalAttributes, key)) {
519
+ const rest = { ...this._globalAttributes }
520
+ delete rest[key]
521
+ this._globalAttributes = rest
522
+ this._trackingOptions.globalAttributes = rest
523
+ }
524
+ }
525
+
526
+ _mergeAttributes (attributes) {
527
+ const hasInput = attributes && typeof attributes === 'object'
528
+ const sanitized = hasInput ? sanitizeAttributes(attributes) : {}
529
+
530
+ if (!this._globalAttributes || Object.keys(this._globalAttributes).length === 0) {
531
+ return sanitized
532
+ }
533
+
534
+ return {
535
+ ...this._globalAttributes,
536
+ ...sanitized
537
+ }
538
+ }
539
+
540
+ flushQueue () {
541
+ if (!this._queue.length) {
542
+ return
543
+ }
544
+
545
+ const queue = [...this._queue]
546
+ this._queue.length = 0
547
+
548
+ if (!this._faroClient) {
549
+ return
550
+ }
551
+
552
+ queue.forEach(callback => {
553
+ try {
554
+ callback(this._faroClient)
555
+ } catch (error) {
556
+ console.error('[TrackingService] Failed to flush queued tracking call', error)
557
+ }
558
+ })
559
+ }
560
+
561
+ getClient () {
562
+ return this._faroClient
563
+ }
564
+
565
+ isEnabled () {
566
+ return this._enabled && Boolean(this._faroClient)
567
+ }
568
+
569
+ isInitialized () {
570
+ return this._initialized
571
+ }
572
+
573
+ destroy () {
574
+ this._queue.length = 0
575
+
576
+ if (this._faroClient?.destroy) {
577
+ try {
578
+ this._faroClient.destroy()
579
+ } catch (error) {
580
+ console.warn('[TrackingService] Failed to destroy Faro client cleanly', error)
581
+ }
582
+ }
583
+
584
+ if (isBrowserEnvironment() && window.symbols && window.symbols.faro === this._faroClient) {
585
+ delete window.symbols.faro
586
+ }
587
+
588
+ this._faroClient = null
589
+ this._initialized = false
590
+ this._setupPromise = null
591
+ this._setReady(false)
592
+ this._enabled = this._trackingOptions.enabled !== false
593
+ }
594
+
595
+ _buildRuntimeConfig () {
596
+ const contextConfig = this._context?.tracking || {}
597
+ const merged = {
598
+ ...DEFAULT_TRACKING_OPTIONS,
599
+ ...this._trackingOptions,
600
+ ...contextConfig
601
+ }
602
+
603
+ const transportList = []
604
+
605
+ if (Array.isArray(merged.transports)) {
606
+ transportList.push(...merged.transports.filter(Boolean))
607
+ } else if (merged.transport) {
608
+ transportList.push(merged.transport)
609
+ }
610
+
611
+ const configuredUrl =
612
+ merged.url ||
613
+ contextConfig.url ||
614
+ this._trackingOptions.url
615
+
616
+ let url = configuredUrl
617
+
618
+ if (!url && transportList.length === 0) {
619
+ url = environment.grafanaUrl
620
+ }
621
+
622
+ const appName =
623
+ merged.appName ||
624
+ contextConfig.appName ||
625
+ this._trackingOptions.appName ||
626
+ environment.grafanaAppName ||
627
+ 'Symbols Platform'
628
+
629
+ const appVersion =
630
+ merged.appVersion ||
631
+ contextConfig.appVersion ||
632
+ this._trackingOptions.appVersion ||
633
+ this._context?.appVersion
634
+
635
+ const environmentName =
636
+ merged.environment ||
637
+ contextConfig.environment ||
638
+ this._trackingOptions.environment ||
639
+ this._resolveEnvironmentName()
640
+
641
+ const contextGlobalAttributes = sanitizeAttributes(contextConfig.globalAttributes || {})
642
+ const globalAttributes = sanitizeAttributes({
643
+ ...this._globalAttributes,
644
+ ...contextGlobalAttributes
645
+ })
646
+
647
+ const runtimeConfig = {
648
+ url,
649
+ appName,
650
+ appVersion,
651
+ environment: environmentName,
652
+ globalAttributes,
653
+ sessionTracking: merged.sessionTracking !== false,
654
+ enableTracing: merged.enableTracing !== false,
655
+ user: merged.user,
656
+ maxQueueSize:
657
+ typeof merged.maxQueueSize === 'number' && merged.maxQueueSize > 0
658
+ ? merged.maxQueueSize
659
+ : DEFAULT_MAX_QUEUE_SIZE
660
+ }
661
+
662
+ if (typeof merged.isolate === 'boolean') {
663
+ runtimeConfig.isolate = merged.isolate
664
+ }
665
+
666
+ if (Array.isArray(merged.instrumentations)) {
667
+ runtimeConfig.instrumentations = merged.instrumentations
668
+ }
669
+
670
+ if (typeof merged.instrumentationsFactory === 'function') {
671
+ runtimeConfig.instrumentationsFactory = merged.instrumentationsFactory
672
+ }
673
+
674
+ if (merged.webInstrumentationOptions != null) {
675
+ runtimeConfig.webInstrumentationOptions = merged.webInstrumentationOptions
676
+ }
677
+
678
+ if (transportList.length > 0) {
679
+ runtimeConfig.transports = transportList
680
+ }
681
+
682
+ return runtimeConfig
683
+ }
684
+
685
+ _resolveEnvironmentName () {
686
+ if (environment.isProduction) {
687
+ return 'production'
688
+ }
689
+
690
+ if (environment.isStaging) {
691
+ return 'staging'
692
+ }
693
+
694
+ if (environment.isTesting) {
695
+ return 'testing'
696
+ }
697
+
698
+ if (environment.isDevelopment) {
699
+ return 'development'
700
+ }
701
+
702
+ return process.env.SYMBOLS_APP_ENV || process.env.NODE_ENV || 'development'
703
+ }
704
+
705
+ async _loadFaroClient (runtimeConfig) {
706
+ try {
707
+ const tracingImport = runtimeConfig.enableTracing === false
708
+ ? Promise.resolve(null)
709
+ : import('@grafana/faro-web-tracing').catch(error => {
710
+ console.warn('[TrackingService] Tracing instrumentation failed to load:', error)
711
+ return null
712
+ })
713
+
714
+ const [{ initializeFaro, getWebInstrumentations }, tracingModule] = await Promise.all([
715
+ import('@grafana/faro-web-sdk'),
716
+ tracingImport
717
+ ])
718
+
719
+ const TracingInstrumentation = tracingModule?.TracingInstrumentation
720
+
721
+ const instrumentations = await this._resolveInstrumentations({
722
+ runtimeConfig,
723
+ getWebInstrumentations,
724
+ TracingInstrumentation
725
+ })
726
+
727
+ const initializeOptions = {
728
+ app: {
729
+ name: runtimeConfig.appName,
730
+ version: runtimeConfig.appVersion,
731
+ environment: runtimeConfig.environment
732
+ },
733
+ instrumentations,
734
+ globalAttributes: sanitizeAttributes(runtimeConfig.globalAttributes)
735
+ }
736
+
737
+ if (runtimeConfig.url) {
738
+ initializeOptions.url = runtimeConfig.url
739
+ }
740
+
741
+ if (Array.isArray(runtimeConfig.transports) && runtimeConfig.transports.length > 0) {
742
+ initializeOptions.transports = runtimeConfig.transports
743
+ }
744
+
745
+ if (runtimeConfig.sessionTracking === false) {
746
+ initializeOptions.sessionTracking = false
747
+ }
748
+
749
+ if (runtimeConfig.isolate === true) {
750
+ initializeOptions.isolate = true
751
+ }
752
+
753
+ this._faroClient = initializeFaro(initializeOptions)
754
+
755
+ if (runtimeConfig.user) {
756
+ this.setUser(runtimeConfig.user, { queue: false })
757
+ }
758
+
759
+ if (isBrowserEnvironment()) {
760
+ window.symbols ||= {}
761
+ window.symbols.faro = this._faroClient
762
+ }
763
+
764
+ this._initialized = true
765
+ this._setReady()
766
+ this.flushQueue()
767
+ } catch (error) {
768
+ this._enabled = false
769
+ this._setError(error)
770
+ this._ready = true
771
+ console.error('[TrackingService] Failed to initialize Grafana Faro client:', error)
772
+ } finally {
773
+ this._setupPromise = null
774
+ }
775
+ }
776
+
777
+ async _resolveInstrumentations ({
778
+ runtimeConfig,
779
+ getWebInstrumentations,
780
+ TracingInstrumentation
781
+ }) {
782
+ if (Array.isArray(runtimeConfig.instrumentations)) {
783
+ return runtimeConfig.instrumentations
784
+ }
785
+
786
+ if (typeof runtimeConfig.instrumentationsFactory === 'function') {
787
+ try {
788
+ const instrumentations = await runtimeConfig.instrumentationsFactory({
789
+ runtimeConfig,
790
+ getWebInstrumentations,
791
+ TracingInstrumentation
792
+ })
793
+
794
+ if (Array.isArray(instrumentations)) {
795
+ return instrumentations
796
+ }
797
+ } catch (error) {
798
+ console.warn('[TrackingService] Custom instrumentation factory failed:', error)
799
+ }
800
+ }
801
+
802
+ const instrumentationOptions =
803
+ runtimeConfig.webInstrumentationOptions == null
804
+ ? {}
805
+ : runtimeConfig.webInstrumentationOptions
806
+
807
+ const defaultInstrumentations = getWebInstrumentations(instrumentationOptions)
808
+
809
+ if (runtimeConfig.enableTracing !== false && TracingInstrumentation) {
810
+ try {
811
+ defaultInstrumentations.push(new TracingInstrumentation())
812
+ } catch (error) {
813
+ console.warn('[TrackingService] Failed to instantiate tracing instrumentation:', error)
814
+ }
815
+ }
816
+
817
+ return defaultInstrumentations
818
+ }
819
+
820
+ _withClient (callback, options = {}) {
821
+ if (!this._enabled) {
822
+ return null
823
+ }
824
+
825
+ if (this._faroClient) {
826
+ try {
827
+ return callback(this._faroClient)
828
+ } catch (error) {
829
+ console.error('[TrackingService] Tracking callback failed:', error)
830
+ return null
831
+ }
832
+ }
833
+
834
+ if (options.queue === false) {
835
+ return null
836
+ }
837
+
838
+ const queueLimit =
839
+ this._runtimeConfig.maxQueueSize ??
840
+ this._trackingOptions.maxQueueSize ??
841
+ DEFAULT_MAX_QUEUE_SIZE
842
+
843
+ if (this._queue.length >= queueLimit) {
844
+ this._queue.shift()
845
+ }
846
+
847
+ this._queue.push(callback)
848
+
849
+ return null
850
+ }
851
+ }
852
+
853
+