@cloudcare/browser-rum 3.3.15 → 3.3.17

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 (81) hide show
  1. package/README.md +12 -0
  2. package/README.zh.md +13 -0
  3. package/bundle/dataflux-rum.js +1 -1
  4. package/cjs/boot/buildEnv.js +1 -1
  5. package/cjs/boot/startRecording.js +4 -4
  6. package/cjs/boot/startRecording.js.map +1 -1
  7. package/cjs/constants.js +8 -8
  8. package/cjs/constants.js.map +1 -1
  9. package/cjs/domain/assembly.js +7 -0
  10. package/cjs/domain/assembly.js.map +1 -1
  11. package/cjs/domain/deflate/deflateWorker.js +1 -1
  12. package/cjs/domain/encoderStreamId.js +2 -1
  13. package/cjs/domain/encoderStreamId.js.map +1 -1
  14. package/cjs/domain/replay/record/canvasEncodeWorker.js +1 -1
  15. package/cjs/domain/replay/record/record.js +18 -11
  16. package/cjs/domain/replay/record/record.js.map +1 -1
  17. package/cjs/domain/replay/record/serializationUtils.js +8 -15
  18. package/cjs/domain/replay/record/serializationUtils.js.map +1 -1
  19. package/cjs/domain/replay/record/serialize.js +11 -6
  20. package/cjs/domain/replay/record/serialize.js.map +1 -1
  21. package/cjs/domain/replay/segmentCollection/segment.js +14 -10
  22. package/cjs/domain/replay/segmentCollection/segment.js.map +1 -1
  23. package/cjs/domain/replay/segmentCollection/segmentCollection.js +9 -8
  24. package/cjs/domain/replay/segmentCollection/segmentCollection.js.map +1 -1
  25. package/cjs/domain/rumEventsCollection/error/errorCollection.js +6 -8
  26. package/cjs/domain/rumEventsCollection/error/errorCollection.js.map +1 -1
  27. package/cjs/domain/rumSessionManager.js +3 -3
  28. package/cjs/domain/rumSessionManager.js.map +1 -1
  29. package/cjs/domain/visitorId/index.js +38 -0
  30. package/cjs/domain/visitorId/index.js.map +1 -1
  31. package/cjs/index.js +7 -0
  32. package/cjs/index.js.map +1 -1
  33. package/cjs/transport/startRumBatch.js +44 -3
  34. package/cjs/transport/startRumBatch.js.map +1 -1
  35. package/esm/boot/buildEnv.js +1 -1
  36. package/esm/boot/startRecording.js +4 -4
  37. package/esm/boot/startRecording.js.map +1 -1
  38. package/esm/constants.js +1 -8
  39. package/esm/constants.js.map +1 -1
  40. package/esm/domain/assembly.js +8 -1
  41. package/esm/domain/assembly.js.map +1 -1
  42. package/esm/domain/deflate/deflateWorker.js +1 -1
  43. package/esm/domain/encoderStreamId.js +2 -1
  44. package/esm/domain/encoderStreamId.js.map +1 -1
  45. package/esm/domain/replay/record/canvasEncodeWorker.js +1 -1
  46. package/esm/domain/replay/record/record.js +18 -11
  47. package/esm/domain/replay/record/record.js.map +1 -1
  48. package/esm/domain/replay/record/serializationUtils.js +8 -15
  49. package/esm/domain/replay/record/serializationUtils.js.map +1 -1
  50. package/esm/domain/replay/record/serialize.js +11 -6
  51. package/esm/domain/replay/record/serialize.js.map +1 -1
  52. package/esm/domain/replay/segmentCollection/segment.js +14 -10
  53. package/esm/domain/replay/segmentCollection/segment.js.map +1 -1
  54. package/esm/domain/replay/segmentCollection/segmentCollection.js +9 -8
  55. package/esm/domain/replay/segmentCollection/segmentCollection.js.map +1 -1
  56. package/esm/domain/rumEventsCollection/error/errorCollection.js +6 -8
  57. package/esm/domain/rumEventsCollection/error/errorCollection.js.map +1 -1
  58. package/esm/domain/rumSessionManager.js +3 -3
  59. package/esm/domain/rumSessionManager.js.map +1 -1
  60. package/esm/domain/visitorId/index.js +39 -1
  61. package/esm/domain/visitorId/index.js.map +1 -1
  62. package/esm/index.js +1 -0
  63. package/esm/index.js.map +1 -1
  64. package/esm/transport/startRumBatch.js +45 -4
  65. package/esm/transport/startRumBatch.js.map +1 -1
  66. package/package.json +4 -4
  67. package/src/boot/startRecording.js +7 -3
  68. package/src/constants.js +1 -8
  69. package/src/domain/assembly.js +15 -1
  70. package/src/domain/encoderStreamId.js +2 -2
  71. package/src/domain/replay/record/record.js +21 -19
  72. package/src/domain/replay/record/serializationUtils.js +10 -24
  73. package/src/domain/replay/record/serialize.js +16 -6
  74. package/src/domain/replay/segmentCollection/segment.js +16 -9
  75. package/src/domain/replay/segmentCollection/segmentCollection.js +18 -14
  76. package/src/domain/rumEventsCollection/error/errorCollection.js +10 -8
  77. package/src/domain/rumSessionManager.js +8 -7
  78. package/src/domain/visitorId/index.js +49 -0
  79. package/src/index.js +1 -0
  80. package/src/transport/startRumBatch.js +61 -3
  81. package/types/index.d.ts +22 -2
@@ -12,7 +12,8 @@ import {
12
12
  limitModification,
13
13
  display,
14
14
  assign,
15
- round
15
+ round,
16
+ WeakSet
16
17
  } from '@cloudcare/browser-core'
17
18
  var SessionType = {
18
19
  SYNTHETICS: 'synthetics',
@@ -93,6 +94,9 @@ export function startRumAssembly(
93
94
  reportError
94
95
  )
95
96
  var pendingRawEvents = createBoundedBuffer()
97
+ // Context objects belong to the existing View history. Weak references do
98
+ // not retain ended Views or require a second history of every collected ID.
99
+ var collectedViews = new WeakSet()
96
100
  var resolvedDeviceInfo
97
101
  getDeviceInfoImp(function (deviceInfo) {
98
102
  resolvedDeviceInfo = deviceInfo
@@ -248,6 +252,16 @@ export function startRumAssembly(
248
252
  LifeCycleEventType.RUM_EVENT_COLLECTED,
249
253
  serverRumEvent
250
254
  )
255
+ if (
256
+ serverRumEvent.type === RumEventType.VIEW &&
257
+ !collectedViews.has(viewContext)
258
+ ) {
259
+ collectedViews.add(viewContext)
260
+ lifeCycle.notify(
261
+ LifeCycleEventType.RUM_VIEW_FIRST_COLLECTED,
262
+ serverRumEvent
263
+ )
264
+ }
251
265
  }
252
266
  }
253
267
  }
@@ -1,5 +1,5 @@
1
1
  export var EncoderStreamId = {
2
2
  REPLAY: 1,
3
- RUM: 2
3
+ RUM: 2,
4
+ RUM_INITIAL_VIEW: 3
4
5
  }
5
-
@@ -28,7 +28,7 @@ import {
28
28
  SerializationContextStatus,
29
29
  getSerializedNodeApproxBytes,
30
30
  SERIALIZATION_MAX_NODE_COUNT,
31
- SERIALIZATION_MAX_APPROX_BYTES,
31
+ SERIALIZATION_MAX_APPROX_BYTES
32
32
  } from './serialize'
33
33
  import { initInputObserver, initObservers } from './observers'
34
34
 
@@ -85,8 +85,6 @@ var FULL_SNAPSHOT_SYNCHRONOUS_FINALIZATION_BUDGET_MS = 12
85
85
  // record must still fit below the segment layer's 4 MiB pending-raw budget.
86
86
  var FULL_SNAPSHOT_SERIALIZATION_MAX_NODE_COUNT =
87
87
  SERIALIZATION_MAX_NODE_COUNT * 4
88
- var FULL_SNAPSHOT_SERIALIZATION_MAX_APPROX_BYTES =
89
- SERIALIZATION_MAX_APPROX_BYTES * 4 - 64 * 1024
90
88
  var FULL_SNAPSHOT_MAX_RESTART_COUNT = 3
91
89
  var FULL_SNAPSHOT_MAX_TOTAL_DURATION_MS = 1000
92
90
  var FULL_SNAPSHOT_MAX_WALL_DURATION_MS = 30000
@@ -1959,10 +1957,11 @@ export function record(options) {
1959
1957
  serializerTask.processedNodeCount + SERIALIZATION_MAX_NODE_COUNT,
1960
1958
  FULL_SNAPSHOT_SERIALIZATION_MAX_NODE_COUNT
1961
1959
  )
1962
- var maxApproxBytes = Math.min(
1963
- serializerTask.approxBytes + SERIALIZATION_MAX_APPROX_BYTES,
1964
- FULL_SNAPSHOT_SERIALIZATION_MAX_APPROX_BYTES
1965
- )
1960
+ // The byte watermark yields traversal work; it must not reject an entire
1961
+ // baseline just because its DOM and complete CSS exceed the encoder's
1962
+ // backlog watermark. Keep advancing until the snapshot is complete.
1963
+ var maxApproxBytes =
1964
+ serializerTask.approxBytes + SERIALIZATION_MAX_APPROX_BYTES
1966
1965
  var completed = serializerTask.advance(
1967
1966
  deadline,
1968
1967
  maxNodeCount,
@@ -1975,10 +1974,7 @@ export function record(options) {
1975
1974
  (serializerTask.isMaxDepthReached ||
1976
1975
  serializerTask.isMaxNodeComplexityReached ||
1977
1976
  (serializerTask.isMaxNodeCountReached &&
1978
- maxNodeCount >= FULL_SNAPSHOT_SERIALIZATION_MAX_NODE_COUNT) ||
1979
- (serializerTask.isMaxApproxBytesReached &&
1980
- maxApproxBytes >=
1981
- FULL_SNAPSHOT_SERIALIZATION_MAX_APPROX_BYTES))
1977
+ maxNodeCount >= FULL_SNAPSHOT_SERIALIZATION_MAX_NODE_COUNT))
1982
1978
  }
1983
1979
  }
1984
1980
 
@@ -2022,7 +2018,6 @@ export function record(options) {
2022
2018
  // result from an earlier record: only the segment/encoder result for this
2023
2019
  // exact baseline may classify its rejection as transient backpressure.
2024
2020
  lastEmitWasLocallyBackpressured = false
2025
- var timestamp = baselineRecords[0].timestamp
2026
2021
  var fullSnapshotAccepted
2027
2022
  if (rawEmitBaseline) {
2028
2023
  fullSnapshotAccepted = rawEmitBaseline(baselineRecords) !== false
@@ -2038,13 +2033,6 @@ export function record(options) {
2038
2033
  fullSnapshotWasPublished = true
2039
2034
  currentFullSnapshotPublicationToken.$rPublished = true
2040
2035
 
2041
- if (window.visualViewport) {
2042
- rawEmit({
2043
- data: getVisualViewport(),
2044
- type: RecordType.VisualViewport,
2045
- timestamp: timestamp
2046
- })
2047
- }
2048
2036
  if (__BUILD_ENV__REPLAY_DEBUG_ENABLED__) {
2049
2037
  updateReplayPerformanceStats({
2050
2038
  fullSnapshotCount: 1,
@@ -2054,6 +2042,18 @@ export function record(options) {
2054
2042
  return true
2055
2043
  }
2056
2044
 
2045
+ function emitFullSnapshotViewport(timestamp) {
2046
+ if (window.visualViewport) {
2047
+ // Call after leaving the previous replay tree so encoder backpressure
2048
+ // retains this record on the new baseline's normal retry queue.
2049
+ emit({
2050
+ data: getVisualViewport(),
2051
+ type: RecordType.VisualViewport,
2052
+ timestamp: timestamp
2053
+ })
2054
+ }
2055
+ }
2056
+
2057
2057
  function invalidateFullSnapshotCanvasBaseline() {
2058
2058
  fullSnapshotCanvasBaselineGeneration += 1
2059
2059
  }
@@ -2231,6 +2231,7 @@ export function record(options) {
2231
2231
  serializerTask.serializedNode
2232
2232
  )
2233
2233
  if (emitFullSnapshot(baselineRecords, fullSnapshotStartedAt)) {
2234
+ emitFullSnapshotViewport(timestamp)
2234
2235
  return { status: 'completed' }
2235
2236
  }
2236
2237
  if (wasLastEmitBackpressured()) {
@@ -3313,6 +3314,7 @@ export function record(options) {
3313
3314
  resetPreviousReplayTreeRecords()
3314
3315
  previousReplayTreeAdmissionActive = false
3315
3316
  captureRecordsAgainstPreviousReplayTree = false
3317
+ emitFullSnapshotViewport(baselineRecords[0].timestamp)
3316
3318
  flushDeferredRecords()
3317
3319
  if (!suppressRecordsUntilFullSnapshot && !recordStopped) {
3318
3320
  takeFullSnapshotCanvasBaseline()
@@ -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
@@ -60,9 +60,8 @@ export var SERIALIZATION_MAX_ATTRIBUTE_COUNT = 1000
60
60
  export var SERIALIZATION_MAX_NODE_SOURCE_LENGTH = 200000
61
61
  export var SERIALIZATION_MAX_MUTATION_SOURCE_LENGTH = 64 * 1024
62
62
  export var SERIALIZATION_TRUNCATION_MARK = '…'
63
- // A full snapshot is followed by a synchronous JSON serialization before it
64
- // enters the asynchronous encoder. Keep that unavoidable main-thread step
65
- // well below the segment collection's 4 MiB backlog watermark.
63
+ // Bound each traversal slice. A complete snapshot may span multiple slices;
64
+ // this is not a size limit for the resulting DOM and CSS record.
66
65
  export var SERIALIZATION_MAX_APPROX_BYTES = 1024 * 1024
67
66
  var serializedNodeApproxBytes = new WeakMap()
68
67
 
@@ -677,7 +676,18 @@ function estimateSerializedNodeShallowBytes(serializedNode) {
677
676
  return
678
677
  }
679
678
  approxBytes += estimateJsonStringBytes(key) + 2
680
- pendingValues.push(value[key])
679
+ if (
680
+ value === serializedNode.attributes &&
681
+ (serializedNode.tagName === 'link' || serializedNode.tagName === 'style') &&
682
+ key === '_cssText' &&
683
+ typeof value[key] === 'string'
684
+ ) {
685
+ // Inline CSS belongs to the whole snapshot budget, not the
686
+ // ordinary attribute-string limit. Count JSON escapes as usual.
687
+ approxBytes += estimateJsonStringBytes(value[key], true)
688
+ } else {
689
+ pendingValues.push(value[key])
690
+ }
681
691
  })
682
692
  }
683
693
  } else {
@@ -687,8 +697,8 @@ function estimateSerializedNodeShallowBytes(serializedNode) {
687
697
  return approxBytes
688
698
  }
689
699
 
690
- function estimateJsonStringBytes(value) {
691
- if (value.length > SERIALIZATION_MAX_STRING_LENGTH) {
700
+ function estimateJsonStringBytes(value, isCssText) {
701
+ if (!isCssText && value.length > SERIALIZATION_MAX_STRING_LENGTH) {
692
702
  return SERIALIZATION_MAX_APPROX_BYTES + 1
693
703
  }
694
704
  var bytes = 4
@@ -38,10 +38,11 @@ export function createSegment(options) {
38
38
  previouslySerializedRecordData
39
39
  ) {
40
40
  const prefix = metadata.records_count === 0 ? '{"records":[' : ','
41
+ const isFullSnapshot = record.type === RecordType.FullSnapshot
41
42
  var preflightResult = preflightRecordSerialization(
42
43
  record,
43
44
  prefix,
44
- pendingRawBytesLimit
45
+ isFullSnapshot ? undefined : pendingRawBytesLimit
45
46
  )
46
47
  if (preflightResult) {
47
48
  return preflightResult
@@ -80,6 +81,7 @@ export function createSegment(options) {
80
81
 
81
82
  if (
82
83
  pendingRawBytesLimit !== undefined &&
84
+ !isFullSnapshot &&
83
85
  rawRecordBytesCount > pendingRawBytesLimit
84
86
  ) {
85
87
  return {
@@ -122,7 +124,9 @@ export function createSegment(options) {
122
124
  : 0
123
125
  if (
124
126
  pendingRawBytesLimit !== undefined &&
125
- pendingRawBytesCount + rawRecordBytesCount > pendingRawBytesLimit
127
+ (isFullSnapshot
128
+ ? pendingRawBytesCount >= pendingRawBytesLimit
129
+ : pendingRawBytesCount + rawRecordBytesCount > pendingRawBytesLimit)
126
130
  ) {
127
131
  return {
128
132
  status: 'encoder_backpressure',
@@ -218,23 +222,26 @@ function preflightFullSnapshotAdmission(
218
222
  if (!record || record.type !== RecordType.FullSnapshot) {
219
223
  return
220
224
  }
221
- var knownApproxBytes = getKnownReplayRecordApproxBytes(record)
222
- if (typeof knownApproxBytes !== 'number') {
223
- return
224
- }
225
- var approximateRawBytes = prefix.length * 2 + knownApproxBytes
226
225
  var pendingRawBytesCount = encoder.getPendingRawBytesCount
227
226
  ? encoder.getPendingRawBytesCount()
228
227
  : 0
229
228
  if (
230
229
  pendingRawBytesLimit !== undefined &&
231
- pendingRawBytesCount + approximateRawBytes > pendingRawBytesLimit
230
+ pendingRawBytesCount >= pendingRawBytesLimit
232
231
  ) {
233
232
  return {
234
233
  status: 'encoder_backpressure',
235
- estimatedBytesCount: approximateRawBytes
234
+ estimatedBytesCount: pendingRawBytesCount
236
235
  }
237
236
  }
237
+ // Like Datadog, admit a complete FullSnapshot into the asynchronous encoder.
238
+ // The watermark limits existing backlog, not the size of one atomic record.
239
+ // If that write exceeds the watermark, later admissions wait for it to drain.
240
+ var knownApproxBytes = getKnownReplayRecordApproxBytes(record)
241
+ if (typeof knownApproxBytes !== 'number') {
242
+ return
243
+ }
244
+ var approximateRawBytes = prefix.length * 2 + knownApproxBytes
238
245
  if (
239
246
  bytesLimit !== undefined &&
240
247
  recordsCount > 0 &&
@@ -452,7 +452,10 @@ export function doStartSegmentCollection(
452
452
  unloadingDeliveryBytesRemaining - deliveryResult.$rBytesCount,
453
453
  0
454
454
  )
455
- if (deliveryResult.$rAllPayloadsSent === false) {
455
+ if (
456
+ deliveryResult.$rAllPayloadsSent === false &&
457
+ !deliveryResult.$rPending
458
+ ) {
456
459
  // Replay segments are ordered. If an older queued payload does not fit,
457
460
  // no current tail may overtake it.
458
461
  unloadingDeliveryBudgetExhausted = true
@@ -518,10 +521,7 @@ export function doStartSegmentCollection(
518
521
  encoderResult.outputBytesCount +
519
522
  SEGMENT_FORM_DATA_ESTIMATED_OVERHEAD
520
523
  if (shouldUseExitTransport && unloadingTransportLatched) {
521
- if (
522
- unloadingDeliveryBudgetExhausted ||
523
- estimatedDeliveryBytesCount > unloadingDeliveryBytesRemaining
524
- ) {
524
+ if (unloadingDeliveryBudgetExhausted) {
525
525
  unloadingDeliveryBudgetExhausted = true
526
526
  reportDroppedRecord(
527
527
  'page_exit_delivery_budget',
@@ -573,7 +573,10 @@ export function doStartSegmentCollection(
573
573
  exitDeliveryResult.$rBytesCount,
574
574
  0
575
575
  )
576
- if (!exitDeliveryResult.$rCurrentPayloadSent) {
576
+ if (
577
+ !exitDeliveryResult.$rCurrentPayloadSent &&
578
+ !exitDeliveryResult.$rPending
579
+ ) {
577
580
  unloadingDeliveryBudgetExhausted = true
578
581
  reportDroppedRecord(
579
582
  'page_exit_delivery_budget',
@@ -583,7 +586,10 @@ export function doStartSegmentCollection(
583
586
  } else {
584
587
  // Preserve compatibility with injected transports that predate the
585
588
  // aggregate-budget result contract.
586
- unloadingDeliveryBytesRemaining -= estimatedDeliveryBytesCount
589
+ unloadingDeliveryBytesRemaining = Math.max(
590
+ unloadingDeliveryBytesRemaining - estimatedDeliveryBytesCount,
591
+ 0
592
+ )
587
593
  }
588
594
  }
589
595
  } else {
@@ -1356,13 +1362,11 @@ export function doStartSegmentCollection(
1356
1362
  } else {
1357
1363
  flushSegment('encoder_backpressure')
1358
1364
  }
1359
- // See the synchronous incremental rejection rationale above. Full
1360
- // snapshots and non-incremental baseline records retain the existing
1361
- // recovery semantics.
1362
- if (
1363
- record.type !== RecordType.IncrementalSnapshot &&
1364
- !isTimeAnchor
1365
- ) {
1365
+ // Later records may be waiting behind a large FullSnapshot write.
1366
+ // Their temporary rejection must not invalidate that already-admitted
1367
+ // baseline. The recorder retries them after the encoder drains;
1368
+ // Meta/Focus within an atomic baseline use rejectBaselineTransaction.
1369
+ if (record.type === RecordType.FullSnapshot) {
1366
1370
  failBaselineGeneration(
1367
1371
  addResult.$rGeneration,
1368
1372
  record.type === RecordType.FullSnapshot
@@ -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
  }
@@ -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,
@@ -4,7 +4,9 @@ import {
4
4
  UUID,
5
5
  WeakSet,
6
6
  addEventListener,
7
+ canUseEventBridge,
7
8
  display,
9
+ getEventBridge,
8
10
  getOrigin,
9
11
  initFetchObservable,
10
12
  initXhrObservable,
@@ -52,6 +54,53 @@ export function startVisitorId(configuration) {
52
54
  }
53
55
 
54
56
  export function createVisitorIdManager(visitorIdConfiguration, options) {
57
+ var browserManager = getNativeVisitorId()
58
+ ? undefined
59
+ : createBrowserVisitorIdManager(visitorIdConfiguration, options)
60
+
61
+ function getBrowserManager() {
62
+ if (!browserManager) {
63
+ browserManager = createBrowserVisitorIdManager(visitorIdConfiguration, options)
64
+ }
65
+ return browserManager
66
+ }
67
+
68
+ return {
69
+ getId: function () {
70
+ return getNativeVisitorId() || getBrowserManager().getId()
71
+ },
72
+ reset: function () {
73
+ // The App owns native identity rotation. A Web-only reset would split it.
74
+ if (!getNativeVisitorId()) {
75
+ getBrowserManager().reset()
76
+ }
77
+ },
78
+ stop: function () {
79
+ if (browserManager) {
80
+ browserManager.stop()
81
+ }
82
+ }
83
+ }
84
+ }
85
+
86
+ function getNativeVisitorId() {
87
+ try {
88
+ if (canUseEventBridge()) {
89
+ var id = getEventBridge().getVisitorId()
90
+ // Preserve the App value exactly, while keeping it safe for HTTP headers.
91
+ if (
92
+ typeof id === 'string' && id.length > 0 && id.length <= 256 &&
93
+ !/[^\x21-\x7e]/.test(id)
94
+ ) {
95
+ return id
96
+ }
97
+ }
98
+ } catch (error) {
99
+ // Older or not-yet-ready native bridges must not break Web collection.
100
+ }
101
+ }
102
+
103
+ function createBrowserVisitorIdManager(visitorIdConfiguration, options) {
55
104
  if (options === undefined) {
56
105
  options = {}
57
106
  }
package/src/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  import { datafluxRum } from './boot/rum.entry'
2
2
  export { datafluxRum }
3
+ export { TraceType } from '@cloudcare/browser-core'
@@ -1,6 +1,8 @@
1
1
  import {
2
2
  createIdentityEncoder,
3
+ createHttpRequest,
3
4
  LifeCycleEventType,
5
+ Observable,
4
6
  RumEventType,
5
7
  startBatchWithReplica
6
8
  } from '@cloudcare/browser-core'
@@ -15,10 +17,21 @@ export function startRumBatch(
15
17
  createEncoder
16
18
  ) {
17
19
  const rumEncoder = createEncoder(EncoderStreamId.RUM)
20
+ // Both batches share the same retry queue and bandwidth limits.
21
+ const request = createHttpRequest(
22
+ configuration.rumEndpoint,
23
+ configuration.batchBytesLimit,
24
+ configuration.retryMaxSize,
25
+ reportError
26
+ )
27
+ const flushRequestObservable = new Observable()
28
+ let initialViewBatch
29
+ let initialViewEncoder
18
30
  const batch = startBatchWithReplica(
19
31
  configuration,
20
32
  {
21
33
  endpoint: configuration.rumEndpoint,
34
+ request,
22
35
  encoder: rumEncoder,
23
36
  encoderFallback: rumEncoder.isAsync
24
37
  ? createIdentityEncoder
@@ -28,7 +41,7 @@ export function startRumBatch(
28
41
  pageExitObservable,
29
42
  sessionExpireObservable
30
43
  )
31
- lifeCycle.subscribe(
44
+ const rumEventSubscription = lifeCycle.subscribe(
32
45
  LifeCycleEventType.RUM_EVENT_COLLECTED,
33
46
  function (serverRumEvent) {
34
47
  if (serverRumEvent.type === RumEventType.VIEW) {
@@ -38,8 +51,53 @@ export function startRumBatch(
38
51
  }
39
52
  }
40
53
  )
41
- telemetryEventObservable.subscribe(function (event) {
54
+ const initialViewSubscription = lifeCycle.subscribe(
55
+ LifeCycleEventType.RUM_VIEW_FIRST_COLLECTED,
56
+ function (event) {
57
+ if (!initialViewBatch) {
58
+ initialViewEncoder = createEncoder(EncoderStreamId.RUM_INITIAL_VIEW)
59
+ initialViewBatch = startBatchWithReplica(
60
+ configuration,
61
+ {
62
+ request,
63
+ encoder: initialViewEncoder,
64
+ encoderFallback: initialViewEncoder.isAsync
65
+ ? createIdentityEncoder
66
+ : undefined,
67
+ flushRequestObservable
68
+ },
69
+ reportError,
70
+ // The regular batch already owns the exit copy. Do not promote an
71
+ // older first version from this encoder's journal on the same exit.
72
+ new Observable(),
73
+ sessionExpireObservable
74
+ )
75
+ }
76
+ // Send the first eligible version separately. The normal batch retains
77
+ // each View's latest version for its original deadline and exit beacon,
78
+ // including rapid routes whose first fetches have not completed yet.
79
+ initialViewBatch.upsert(event, event.view.id)
80
+ flushRequestObservable.notify('view_created')
81
+ }
82
+ )
83
+ const telemetrySubscription = telemetryEventObservable.subscribe(function (event) {
42
84
  batch.add(event)
43
85
  })
44
- return batch
86
+ return {
87
+ flushObservable: batch.flushObservable,
88
+ add: batch.add,
89
+ upsert: batch.upsert,
90
+ stop: function () {
91
+ rumEventSubscription.unsubscribe()
92
+ initialViewSubscription.unsubscribe()
93
+ telemetrySubscription.unsubscribe()
94
+ batch.stop()
95
+ if (initialViewBatch) {
96
+ initialViewBatch.stop()
97
+ if (initialViewEncoder.stop) {
98
+ initialViewEncoder.stop()
99
+ }
100
+ }
101
+ }
102
+ }
45
103
  }