@cloudcare/browser-rum 3.3.14 → 3.3.16

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 (106) hide show
  1. package/README.md +60 -0
  2. package/README.zh.md +48 -0
  3. package/bundle/dataflux-rum.js +1 -1
  4. package/cjs/boot/buildEnv.js +1 -1
  5. package/cjs/boot/perStartRum.js +3 -0
  6. package/cjs/boot/perStartRum.js.map +1 -1
  7. package/cjs/boot/recorderApi.js +72 -56
  8. package/cjs/boot/recorderApi.js.map +1 -1
  9. package/cjs/boot/rumPublicApi.js +35 -3
  10. package/cjs/boot/rumPublicApi.js.map +1 -1
  11. package/cjs/boot/startRecording.js +4 -4
  12. package/cjs/boot/startRecording.js.map +1 -1
  13. package/cjs/constants.js +8 -8
  14. package/cjs/constants.js.map +1 -1
  15. package/cjs/domain/assembly.js +14 -1
  16. package/cjs/domain/assembly.js.map +1 -1
  17. package/cjs/domain/configuration.js +62 -0
  18. package/cjs/domain/configuration.js.map +1 -1
  19. package/cjs/domain/deflate/deflateWorker.js +1 -1
  20. package/cjs/domain/encoderStreamId.js +2 -1
  21. package/cjs/domain/encoderStreamId.js.map +1 -1
  22. package/cjs/domain/replay/record/canvasEncodeWorker.js +1 -1
  23. package/cjs/domain/replay/record/record.js +2 -3
  24. package/cjs/domain/replay/record/record.js.map +1 -1
  25. package/cjs/domain/replay/record/serializationUtils.js +8 -15
  26. package/cjs/domain/replay/record/serializationUtils.js.map +1 -1
  27. package/cjs/domain/replay/record/serialize.js +14 -6
  28. package/cjs/domain/replay/record/serialize.js.map +1 -1
  29. package/cjs/domain/requestCollection.js +3 -1
  30. package/cjs/domain/requestCollection.js.map +1 -1
  31. package/cjs/domain/rumEventsCollection/error/errorCollection.js +6 -8
  32. package/cjs/domain/rumEventsCollection/error/errorCollection.js.map +1 -1
  33. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js +5 -0
  34. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js.map +1 -1
  35. package/cjs/domain/rumSessionManager.js +3 -3
  36. package/cjs/domain/rumSessionManager.js.map +1 -1
  37. package/cjs/domain/tracing/tracer.js +5 -1
  38. package/cjs/domain/tracing/tracer.js.map +1 -1
  39. package/cjs/domain/visitorId/index.js +417 -0
  40. package/cjs/domain/visitorId/index.js.map +1 -0
  41. package/cjs/index.js +7 -0
  42. package/cjs/index.js.map +1 -1
  43. package/cjs/transport/startRumBatch.js +44 -3
  44. package/cjs/transport/startRumBatch.js.map +1 -1
  45. package/esm/boot/buildEnv.js +1 -1
  46. package/esm/boot/perStartRum.js +3 -0
  47. package/esm/boot/perStartRum.js.map +1 -1
  48. package/esm/boot/recorderApi.js +72 -56
  49. package/esm/boot/recorderApi.js.map +1 -1
  50. package/esm/boot/rumPublicApi.js +36 -4
  51. package/esm/boot/rumPublicApi.js.map +1 -1
  52. package/esm/boot/startRecording.js +4 -4
  53. package/esm/boot/startRecording.js.map +1 -1
  54. package/esm/constants.js +1 -8
  55. package/esm/constants.js.map +1 -1
  56. package/esm/domain/assembly.js +15 -2
  57. package/esm/domain/assembly.js.map +1 -1
  58. package/esm/domain/configuration.js +62 -1
  59. package/esm/domain/configuration.js.map +1 -1
  60. package/esm/domain/deflate/deflateWorker.js +1 -1
  61. package/esm/domain/encoderStreamId.js +2 -1
  62. package/esm/domain/encoderStreamId.js.map +1 -1
  63. package/esm/domain/replay/record/canvasEncodeWorker.js +1 -1
  64. package/esm/domain/replay/record/record.js +1 -2
  65. package/esm/domain/replay/record/record.js.map +1 -1
  66. package/esm/domain/replay/record/serializationUtils.js +8 -15
  67. package/esm/domain/replay/record/serializationUtils.js.map +1 -1
  68. package/esm/domain/replay/record/serialize.js +13 -5
  69. package/esm/domain/replay/record/serialize.js.map +1 -1
  70. package/esm/domain/requestCollection.js +4 -2
  71. package/esm/domain/requestCollection.js.map +1 -1
  72. package/esm/domain/rumEventsCollection/error/errorCollection.js +6 -8
  73. package/esm/domain/rumEventsCollection/error/errorCollection.js.map +1 -1
  74. package/esm/domain/rumEventsCollection/resource/resourceCollection.js +5 -0
  75. package/esm/domain/rumEventsCollection/resource/resourceCollection.js.map +1 -1
  76. package/esm/domain/rumSessionManager.js +3 -3
  77. package/esm/domain/rumSessionManager.js.map +1 -1
  78. package/esm/domain/tracing/tracer.js +6 -2
  79. package/esm/domain/tracing/tracer.js.map +1 -1
  80. package/esm/domain/visitorId/index.js +406 -0
  81. package/esm/domain/visitorId/index.js.map +1 -0
  82. package/esm/index.js +1 -0
  83. package/esm/index.js.map +1 -1
  84. package/esm/transport/startRumBatch.js +45 -4
  85. package/esm/transport/startRumBatch.js.map +1 -1
  86. package/package.json +4 -4
  87. package/src/boot/perStartRum.js +4 -0
  88. package/src/boot/recorderApi.js +73 -59
  89. package/src/boot/rumPublicApi.js +38 -4
  90. package/src/boot/startRecording.js +7 -3
  91. package/src/constants.js +1 -8
  92. package/src/domain/assembly.js +28 -2
  93. package/src/domain/configuration.js +154 -0
  94. package/src/domain/encoderStreamId.js +2 -2
  95. package/src/domain/replay/record/record.js +1 -2
  96. package/src/domain/replay/record/serializationUtils.js +10 -24
  97. package/src/domain/replay/record/serialize.js +25 -5
  98. package/src/domain/requestCollection.js +4 -1
  99. package/src/domain/rumEventsCollection/error/errorCollection.js +10 -8
  100. package/src/domain/rumEventsCollection/resource/resourceCollection.js +3 -0
  101. package/src/domain/rumSessionManager.js +8 -7
  102. package/src/domain/tracing/tracer.js +15 -2
  103. package/src/domain/visitorId/index.js +571 -0
  104. package/src/index.js +1 -0
  105. package/src/transport/startRumBatch.js +61 -3
  106. package/types/index.d.ts +23 -1
@@ -396,7 +396,7 @@ export function switchToAbsoluteUrl(cssText, cssHref) {
396
396
  }
397
397
  )
398
398
  }
399
- export function getCssRulesString(cssStyleSheet) {
399
+ export function getCssRulesString(cssStyleSheet, maxCharacterCount) {
400
400
  if (!cssStyleSheet) {
401
401
  return null
402
402
  }
@@ -407,13 +407,9 @@ export function getCssRulesString(cssStyleSheet) {
407
407
  var serializationState = {
408
408
  parts: [],
409
409
  visitedStyleSheets: new Set(),
410
- ruleCount: 0,
411
410
  characterCount: 0,
412
411
  characterLimit:
413
- STYLESHEET_SERIALIZATION_MAX_CHARACTER_COUNT - 64,
414
- deadline:
415
- getSerializationTime() + STYLESHEET_SERIALIZATION_MAX_DURATION_MS,
416
- exceeded: false
412
+ typeof maxCharacterCount === 'number' ? maxCharacterCount : Infinity
417
413
  }
418
414
  var completed = appendStyleSheetRules(
419
415
  cssStyleSheet,
@@ -421,9 +417,9 @@ export function getCssRulesString(cssStyleSheet) {
421
417
  cssStyleSheet.href,
422
418
  rootRules
423
419
  )
424
- // `_cssText` replaces the stylesheet in the player, so a rule prefix cannot
425
- // safely represent the source sheet. Let linked stylesheets fall back to
426
- // their href and inline styles fall back to their bounded text child.
420
+ // `_cssText` replaces the stylesheet in the player, so never publish a
421
+ // prefix. The caller may bound allocations by the entire snapshot capacity;
422
+ // CSS has no separate rule-count, character-count, or elapsed-time cutoff.
427
423
  if (!completed) {
428
424
  return null
429
425
  }
@@ -432,7 +428,7 @@ export function getCssRulesString(cssStyleSheet) {
432
428
  )
433
429
  if (
434
430
  styleSheetCssText.length >
435
- STYLESHEET_SERIALIZATION_MAX_CHARACTER_COUNT
431
+ serializationState.characterLimit
436
432
  ) {
437
433
  return null
438
434
  }
@@ -451,9 +447,6 @@ function appendStyleSheetRules(
451
447
  ) {
452
448
  return true
453
449
  }
454
- if (!hasStyleSheetSerializationBudget(serializationState)) {
455
- return false
456
- }
457
450
  serializationState.visitedStyleSheets.add(cssStyleSheet)
458
451
  var styleSheetHref = cssStyleSheet.href || inheritedHref
459
452
  var rules = knownRules || getReadableStyleSheetRules(cssStyleSheet)
@@ -530,10 +523,8 @@ function appendStyleSheetRuleText(
530
523
  return true
531
524
  }
532
525
  if (
533
- !hasStyleSheetSerializationBudget(
534
- serializationState,
535
- rawRuleText.length
536
- )
526
+ serializationState.characterCount + rawRuleText.length >
527
+ serializationState.characterLimit
537
528
  ) {
538
529
  return false
539
530
  }
@@ -543,16 +534,11 @@ function appendStyleSheetRuleText(
543
534
  : rawRuleText
544
535
  ruleText = switchToAbsoluteUrl(ruleText, styleSheetHref)
545
536
  if (
546
- ruleText.length !== rawRuleText.length &&
547
- !hasStyleSheetSerializationBudget(
548
- serializationState,
549
- ruleText.length - rawRuleText.length,
550
- false
551
- )
537
+ serializationState.characterCount + ruleText.length >
538
+ serializationState.characterLimit
552
539
  ) {
553
540
  return false
554
541
  }
555
- serializationState.ruleCount += 1
556
542
  serializationState.characterCount += ruleText.length
557
543
  serializationState.parts.push(ruleText)
558
544
  return true
@@ -64,6 +64,9 @@ export var SERIALIZATION_TRUNCATION_MARK = '…'
64
64
  // enters the asynchronous encoder. Keep that unavoidable main-thread step
65
65
  // well below the segment collection's 4 MiB backlog watermark.
66
66
  export var SERIALIZATION_MAX_APPROX_BYTES = 1024 * 1024
67
+ // Reserve segment metadata below the existing 4 MiB pending-raw budget.
68
+ export var FULL_SNAPSHOT_SERIALIZATION_MAX_APPROX_BYTES =
69
+ SERIALIZATION_MAX_APPROX_BYTES * 4 - 64 * 1024
67
70
  var serializedNodeApproxBytes = new WeakMap()
68
71
 
69
72
  export function serializeDocument(
@@ -677,7 +680,18 @@ function estimateSerializedNodeShallowBytes(serializedNode) {
677
680
  return
678
681
  }
679
682
  approxBytes += estimateJsonStringBytes(key) + 2
680
- pendingValues.push(value[key])
683
+ if (
684
+ value === serializedNode.attributes &&
685
+ (serializedNode.tagName === 'link' || serializedNode.tagName === 'style') &&
686
+ key === '_cssText' &&
687
+ typeof value[key] === 'string'
688
+ ) {
689
+ // Inline CSS belongs to the whole snapshot budget, not the
690
+ // ordinary attribute-string limit. Count JSON escapes as usual.
691
+ approxBytes += estimateJsonStringBytes(value[key], true)
692
+ } else {
693
+ pendingValues.push(value[key])
694
+ }
681
695
  })
682
696
  }
683
697
  } else {
@@ -687,8 +701,8 @@ function estimateSerializedNodeShallowBytes(serializedNode) {
687
701
  return approxBytes
688
702
  }
689
703
 
690
- function estimateJsonStringBytes(value) {
691
- if (value.length > SERIALIZATION_MAX_STRING_LENGTH) {
704
+ function estimateJsonStringBytes(value, isCssText) {
705
+ if (!isCssText && value.length > SERIALIZATION_MAX_STRING_LENGTH) {
692
706
  return SERIALIZATION_MAX_APPROX_BYTES + 1
693
707
  }
694
708
  var bytes = 4
@@ -941,7 +955,10 @@ function getAttributesForPrivacyLevel(element, nodePrivacyLevel, options) {
941
955
  doc,
942
956
  element.href
943
957
  )
944
- var cssText = getCssRulesString(stylesheet)
958
+ var cssText = getCssRulesString(
959
+ stylesheet,
960
+ FULL_SNAPSHOT_SERIALIZATION_MAX_APPROX_BYTES / 2
961
+ )
945
962
  if (cssText && stylesheet) {
946
963
  safeAttrs._cssText = cssText
947
964
  }
@@ -949,7 +966,10 @@ function getAttributesForPrivacyLevel(element, nodePrivacyLevel, options) {
949
966
 
950
967
  // dynamic stylesheet
951
968
  if (tagName === 'style' && element.sheet) {
952
- var cssText = getCssRulesString(element.sheet)
969
+ var cssText = getCssRulesString(
970
+ element.sheet,
971
+ FULL_SNAPSHOT_SERIALIZATION_MAX_APPROX_BYTES / 2
972
+ )
953
973
  if (cssText) {
954
974
  safeAttrs._cssText = cssText
955
975
  }
@@ -2,6 +2,7 @@ import {
2
2
  RequestType,
3
3
  initFetchObservable,
4
4
  initXhrObservable,
5
+ isRequest,
5
6
  LifeCycleEventType,
6
7
  tryToClone,
7
8
  readBytesFromStream,
@@ -51,6 +52,7 @@ export function trackXhr(lifeCycle, configuration, tracer) {
51
52
  status: context.status,
52
53
  traceId: context.traceId,
53
54
  traceSampled: context.traceSampled,
55
+ visitorId: context.visitorId,
54
56
  type: RequestType.XHR,
55
57
  url: context.url,
56
58
  xhr: context.xhr,
@@ -99,9 +101,10 @@ export function trackFetch(lifeCycle, configuration, tracer) {
99
101
  status: context.status,
100
102
  traceId: context.traceId,
101
103
  traceSampled: context.traceSampled,
104
+ visitorId: context.visitorId,
102
105
  requestHeaderContexts:
103
106
  context.init?.headers ||
104
- (context.input instanceof Request
107
+ (isRequest(context.input)
105
108
  ? context.input.headers
106
109
  : undefined),
107
110
  type: RequestType.FETCH,
@@ -26,15 +26,17 @@ export function startErrorCollection(
26
26
  trackConsoleError(errorObservable)
27
27
  trackRuntimeError(errorObservable)
28
28
  trackReportError(configuration, errorObservable)
29
- var session = sessionManager.findTrackedSession()
30
- let hasError = shouldMarkSessionOnError(session) && session.sessionHasError
31
- if (shouldMarkSessionOnError(session)) {
32
- lifeCycle.subscribe(LifeCycleEventType.SESSION_RENEWED, function () {
33
- hasError = false
34
- })
35
- }
29
+ let hasError = false
30
+ lifeCycle.subscribe(LifeCycleEventType.SESSION_RENEWED, function () {
31
+ hasError = false
32
+ })
36
33
  var markSessionOnError = function () {
37
- if (shouldMarkSessionOnError(session) && !hasError) {
34
+ const session = sessionManager.findTrackedSession()
35
+ if (
36
+ shouldMarkSessionOnError(session) &&
37
+ !hasError &&
38
+ !session.sessionHasError
39
+ ) {
38
40
  sessionManager.setErrorForSession()
39
41
  hasError = true
40
42
  }
@@ -243,6 +243,9 @@ function processRequest(request, pageStateHistory, requestResourceEntryMatcher)
243
243
  tracingInfo,
244
244
  correspondingTimingOverrides
245
245
  )
246
+ if (request.visitorId) {
247
+ resourceEvent.context = { visitor_id: request.visitorId }
248
+ }
246
249
  return {
247
250
  startTime: startClocks.relative,
248
251
  rawRumEvent: resourceEvent,
@@ -78,15 +78,17 @@ export function startRumSessionManager(configuration, lifeCycle) {
78
78
  return
79
79
  }
80
80
  const isErrorSession =
81
- session.trackingType ===
81
+ !session.isSessionForced &&
82
+ (session.trackingType ===
82
83
  RumTrackingType.TRACKED_WITH_ERROR_SESSION_AND_WITHOUT_SESSION_REPLAY ||
83
- session.trackingType ===
84
- RumTrackingType.TRACKED_WITH_ERROR_SESSION_AND_WITH_SESSION_REPLAY ||
85
- session.trackingType ===
86
- RumTrackingType.TRACKED_WITH_ERROR_SESSION_AND_WITH_ERROR_SESSION_REPLAY
84
+ session.trackingType ===
85
+ RumTrackingType.TRACKED_WITH_ERROR_SESSION_AND_WITH_SESSION_REPLAY ||
86
+ session.trackingType ===
87
+ RumTrackingType.TRACKED_WITH_ERROR_SESSION_AND_WITH_ERROR_SESSION_REPLAY)
87
88
 
88
89
  let plan = RumSessionPlan.WITHOUT_SESSION_REPLAY
89
90
  if (
91
+ session.isSessionForced ||
90
92
  session.trackingType ===
91
93
  RumTrackingType.TRACKED_WITH_SESSION_AND_WITH_SESSION_REPLAY ||
92
94
  session.trackingType ===
@@ -113,8 +115,7 @@ export function startRumSessionManager(configuration, lifeCycle) {
113
115
  isSessionForced: session.isSessionForced,
114
116
  sessionReplayAllowed:
115
117
  plan === RumSessionPlan.WITH_SESSION_REPLAY ||
116
- plan === RumSessionPlan.WITH_ERROR_SESSION_REPLAY ||
117
- session.isSessionForced
118
+ plan === RumSessionPlan.WITH_ERROR_SESSION_REPLAY
118
119
  }
119
120
  },
120
121
  expire: sessionManager.expire,
@@ -10,7 +10,9 @@ import {
10
10
  find,
11
11
  isMatchOption,
12
12
  matchList,
13
- isString
13
+ isString,
14
+ isRequest,
15
+ display
14
16
  } from '@cloudcare/browser-core'
15
17
  import { DDtraceTracer } from './ddtraceTracer'
16
18
  import { SkyWalkingTracer } from './skywalkingTracer'
@@ -45,7 +47,7 @@ export function startTracer(configuration, sessionManager) {
45
47
  sessionManager,
46
48
  function (tracingHeaders) {
47
49
  if (
48
- context.input instanceof Request &&
50
+ isRequest(context.input) &&
49
51
  (!context.init || !context.init.headers)
50
52
  ) {
51
53
  context.input = new Request(context.input)
@@ -160,6 +162,17 @@ export function injectHeadersIfTracingAllowed(
160
162
  if (getType(result) === 'object') {
161
163
  each(result, function (value, key) {
162
164
  if (getType(value) === 'string') {
165
+ if (
166
+ context.visitorId &&
167
+ configuration.visitorId &&
168
+ configuration.visitorId.enabled &&
169
+ key.toLowerCase() === configuration.visitorId.header
170
+ ) {
171
+ display.warn(
172
+ 'injectTraceHeader cannot overwrite the SDK-managed Visitor ID header.'
173
+ )
174
+ return
175
+ }
163
176
  headers[key] = value
164
177
  }
165
178
  })