@cloudcare/browser-rum 2.2.15 → 3.0.1

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 (166) hide show
  1. package/bundle/dataflux-rum.js +1 -1
  2. package/cjs/boot/buildEnv.js +1 -1
  3. package/cjs/boot/recorderApi.js +161 -0
  4. package/cjs/boot/rum.entry.js +6 -1
  5. package/cjs/boot/rum.js +15 -5
  6. package/cjs/boot/rumPublicApi.js +8 -4
  7. package/cjs/boot/startRecording.js +55 -0
  8. package/cjs/constants.js +52 -0
  9. package/cjs/domain/assembly.js +1 -7
  10. package/cjs/domain/configuration.js +3 -2
  11. package/cjs/domain/contexts/foregroundContexts.js +138 -0
  12. package/cjs/domain/contexts/internalContext.js +1 -1
  13. package/cjs/domain/contexts/urlContexts.js +9 -2
  14. package/cjs/domain/domMutationCollection.js +4 -5
  15. package/cjs/domain/initViewportObservable.js +52 -0
  16. package/cjs/domain/locationChangeObservable.js +12 -26
  17. package/cjs/domain/performanceCollection.js +2 -13
  18. package/cjs/domain/replay/record/elementsScrollPositions.js +27 -0
  19. package/cjs/domain/replay/record/index.js +41 -0
  20. package/cjs/domain/replay/record/mutationBatch.js +58 -0
  21. package/cjs/domain/replay/record/mutationObserver.js +388 -0
  22. package/cjs/domain/replay/record/observers.js +470 -0
  23. package/cjs/domain/replay/record/privacy.js +256 -0
  24. package/cjs/domain/replay/record/record.js +174 -0
  25. package/cjs/domain/replay/record/serializationUtils.js +137 -0
  26. package/cjs/domain/replay/record/serialize.js +488 -0
  27. package/cjs/domain/replay/record/shadowRootsController.js +61 -0
  28. package/cjs/domain/replay/record/utils.js +53 -0
  29. package/cjs/domain/replay/record/viewports.js +104 -0
  30. package/cjs/domain/replay/replayStats.js +83 -0
  31. package/cjs/domain/replay/segmentCollection/buildReplayPayload.js +43 -0
  32. package/cjs/domain/replay/segmentCollection/deflateWorker.js +4400 -0
  33. package/cjs/domain/replay/segmentCollection/index.js +33 -0
  34. package/cjs/domain/replay/segmentCollection/segment.js +101 -0
  35. package/cjs/domain/replay/segmentCollection/segmentCollection.js +179 -0
  36. package/cjs/domain/replay/segmentCollection/startDeflateWorker.js +127 -0
  37. package/cjs/domain/requestCollection.js +35 -17
  38. package/cjs/domain/rumEventsCollection/actions/actionCollection.js +12 -4
  39. package/cjs/domain/rumEventsCollection/actions/clickChain.js +3 -3
  40. package/cjs/domain/rumEventsCollection/actions/computeFrustration.js +6 -5
  41. package/cjs/domain/rumEventsCollection/actions/getActionNameFromElement.js +8 -6
  42. package/cjs/domain/rumEventsCollection/actions/getSelectorsFromElement.js +111 -75
  43. package/cjs/domain/rumEventsCollection/actions/listenActionEvents.js +22 -28
  44. package/cjs/domain/rumEventsCollection/actions/trackClickActions.js +94 -54
  45. package/cjs/domain/rumEventsCollection/error/errorCollection.js +13 -5
  46. package/cjs/domain/rumEventsCollection/view/trackViewEventCounts.js +45 -0
  47. package/cjs/domain/rumEventsCollection/view/trackViewMetrics.js +1 -19
  48. package/cjs/domain/rumEventsCollection/view/trackViews.js +21 -18
  49. package/cjs/domain/rumEventsCollection/view/viewCollection.js +12 -9
  50. package/cjs/domain/rumSessionManager.js +6 -3
  51. package/cjs/domain/tracing/getDocumentTraceId.js +2 -10
  52. package/cjs/domain/trackEventCounts.js +10 -2
  53. package/cjs/domain/waitPageActivityEnd.js +5 -1
  54. package/cjs/transport/startRumBatch.js +5 -7
  55. package/cjs/types/index.js +18 -0
  56. package/cjs/types/sessionReplayConstants.js +56 -0
  57. package/esm/boot/buildEnv.js +1 -1
  58. package/esm/boot/recorderApi.js +149 -0
  59. package/esm/boot/rum.entry.js +4 -1
  60. package/esm/boot/rum.js +15 -6
  61. package/esm/boot/rumPublicApi.js +8 -4
  62. package/esm/boot/startRecording.js +44 -0
  63. package/esm/constants.js +31 -0
  64. package/esm/domain/assembly.js +1 -7
  65. package/esm/domain/configuration.js +4 -3
  66. package/esm/domain/contexts/foregroundContexts.js +122 -0
  67. package/esm/domain/contexts/internalContext.js +1 -1
  68. package/esm/domain/contexts/urlContexts.js +10 -3
  69. package/esm/domain/domMutationCollection.js +5 -6
  70. package/esm/domain/initViewportObservable.js +39 -0
  71. package/esm/domain/locationChangeObservable.js +13 -27
  72. package/esm/domain/performanceCollection.js +1 -12
  73. package/esm/domain/replay/record/elementsScrollPositions.js +20 -0
  74. package/esm/domain/replay/record/index.js +3 -0
  75. package/esm/domain/replay/record/mutationBatch.js +35 -0
  76. package/esm/domain/replay/record/mutationObserver.js +336 -0
  77. package/esm/domain/replay/record/observers.js +454 -0
  78. package/esm/domain/replay/record/privacy.js +233 -0
  79. package/esm/domain/replay/record/record.js +158 -0
  80. package/esm/domain/replay/record/serializationUtils.js +107 -0
  81. package/esm/domain/replay/record/serialize.js +461 -0
  82. package/esm/domain/replay/record/shadowRootsController.js +49 -0
  83. package/esm/domain/replay/record/utils.js +38 -0
  84. package/esm/domain/replay/record/viewports.js +87 -0
  85. package/esm/domain/replay/replayStats.js +64 -0
  86. package/esm/domain/replay/segmentCollection/buildReplayPayload.js +31 -0
  87. package/esm/domain/replay/segmentCollection/deflateWorker.js +4393 -0
  88. package/esm/domain/replay/segmentCollection/index.js +3 -0
  89. package/esm/domain/replay/segmentCollection/segment.js +84 -0
  90. package/esm/domain/replay/segmentCollection/segmentCollection.js +158 -0
  91. package/esm/domain/replay/segmentCollection/startDeflateWorker.js +114 -0
  92. package/esm/domain/requestCollection.js +36 -18
  93. package/esm/domain/rumEventsCollection/actions/actionCollection.js +12 -4
  94. package/esm/domain/rumEventsCollection/actions/clickChain.js +3 -3
  95. package/esm/domain/rumEventsCollection/actions/computeFrustration.js +6 -5
  96. package/esm/domain/rumEventsCollection/actions/getActionNameFromElement.js +6 -6
  97. package/esm/domain/rumEventsCollection/actions/getSelectorsFromElement.js +105 -74
  98. package/esm/domain/rumEventsCollection/actions/listenActionEvents.js +22 -28
  99. package/esm/domain/rumEventsCollection/actions/trackClickActions.js +99 -59
  100. package/esm/domain/rumEventsCollection/error/errorCollection.js +13 -5
  101. package/esm/domain/rumEventsCollection/view/trackViewEventCounts.js +33 -0
  102. package/esm/domain/rumEventsCollection/view/trackViewMetrics.js +1 -18
  103. package/esm/domain/rumEventsCollection/view/trackViews.js +21 -19
  104. package/esm/domain/rumEventsCollection/view/viewCollection.js +12 -9
  105. package/esm/domain/rumSessionManager.js +6 -3
  106. package/esm/domain/tracing/getDocumentTraceId.js +1 -9
  107. package/esm/domain/trackEventCounts.js +10 -2
  108. package/esm/domain/waitPageActivityEnd.js +5 -1
  109. package/esm/transport/startRumBatch.js +5 -7
  110. package/esm/types/index.js +1 -0
  111. package/esm/types/sessionReplayConstants.js +45 -0
  112. package/package.json +3 -3
  113. package/src/boot/recorderApi.js +166 -0
  114. package/src/boot/rum.entry.js +4 -1
  115. package/src/boot/rum.js +38 -10
  116. package/src/boot/rumPublicApi.js +13 -3
  117. package/src/boot/startRecording.js +77 -0
  118. package/src/constants.js +37 -0
  119. package/src/domain/assembly.js +1 -6
  120. package/src/domain/configuration.js +10 -2
  121. package/src/domain/contexts/foregroundContexts.js +140 -0
  122. package/src/domain/contexts/internalContext.js +10 -4
  123. package/src/domain/contexts/urlContexts.js +29 -15
  124. package/src/domain/domMutationCollection.js +14 -6
  125. package/src/domain/initViewportObservable.js +46 -0
  126. package/src/domain/locationChangeObservable.js +17 -25
  127. package/src/domain/performanceCollection.js +42 -42
  128. package/src/domain/replay/record/elementsScrollPositions.js +22 -0
  129. package/src/domain/replay/record/index.js +7 -0
  130. package/src/domain/replay/record/mutationBatch.js +38 -0
  131. package/src/domain/replay/record/mutationObserver.js +403 -0
  132. package/src/domain/replay/record/observers.js +565 -0
  133. package/src/domain/replay/record/privacy.js +304 -0
  134. package/src/domain/replay/record/record.js +158 -0
  135. package/src/domain/replay/record/serializationUtils.js +134 -0
  136. package/src/domain/replay/record/serialize.js +497 -0
  137. package/src/domain/replay/record/shadowRootsController.js +58 -0
  138. package/src/domain/replay/record/utils.js +44 -0
  139. package/src/domain/replay/record/viewports.js +89 -0
  140. package/src/domain/replay/replayStats.js +67 -0
  141. package/src/domain/replay/segmentCollection/buildReplayPayload.js +34 -0
  142. package/src/domain/replay/segmentCollection/deflateWorker.js +4584 -0
  143. package/src/domain/replay/segmentCollection/index.js +6 -0
  144. package/src/domain/replay/segmentCollection/segment.js +86 -0
  145. package/src/domain/replay/segmentCollection/segmentCollection.js +205 -0
  146. package/src/domain/replay/segmentCollection/startDeflateWorker.js +102 -0
  147. package/src/domain/requestCollection.js +64 -28
  148. package/src/domain/rumEventsCollection/actions/actionCollection.js +11 -4
  149. package/src/domain/rumEventsCollection/actions/clickChain.js +3 -3
  150. package/src/domain/rumEventsCollection/actions/computeFrustration.js +7 -4
  151. package/src/domain/rumEventsCollection/actions/getActionNameFromElement.js +9 -6
  152. package/src/domain/rumEventsCollection/actions/getSelectorsFromElement.js +130 -108
  153. package/src/domain/rumEventsCollection/actions/listenActionEvents.js +26 -27
  154. package/src/domain/rumEventsCollection/actions/trackClickActions.js +162 -92
  155. package/src/domain/rumEventsCollection/error/errorCollection.js +11 -5
  156. package/src/domain/rumEventsCollection/view/trackViewEventCounts.js +38 -0
  157. package/src/domain/rumEventsCollection/view/trackViewMetrics.js +1 -19
  158. package/src/domain/rumEventsCollection/view/trackViews.js +98 -68
  159. package/src/domain/rumEventsCollection/view/viewCollection.js +15 -7
  160. package/src/domain/rumSessionManager.js +33 -19
  161. package/src/domain/tracing/getDocumentTraceId.js +6 -9
  162. package/src/domain/trackEventCounts.js +10 -4
  163. package/src/domain/waitPageActivityEnd.js +4 -1
  164. package/src/transport/startRumBatch.js +29 -24
  165. package/src/types/index.js +1 -0
  166. package/src/types/sessionReplayConstants.js +49 -0
@@ -0,0 +1,454 @@
1
+ import { instrumentSetter, instrumentMethodAndCallOriginal, assign, throttle, DOM_EVENT, addEventListeners, addEventListener, noop, LifeCycleEventType, RumEventType, ActionType, isNodeShadowHost } from '@cloudcare/browser-core';
2
+ import { initViewportObservable } from '../../initViewportObservable';
3
+ import { NodePrivacyLevel } from '../../../constants';
4
+ import { RecordType, IncrementalSource, MediaInteractionType, MouseInteractionType } from '../../../types';
5
+ import { getNodePrivacyLevel, shouldMaskNode } from './privacy';
6
+ import { getElementInputValue, getSerializedNodeId, hasSerializedNode } from './serializationUtils';
7
+ import { assembleIncrementalSnapshot, forEach, getPathToNestedCSSRule, isTouchEvent } from './utils';
8
+ import { startMutationObserver } from './mutationObserver';
9
+ import { getVisualViewport, getScrollX, getScrollY, convertMouseEventToLayoutCoordinates } from './viewports';
10
+ var MOUSE_MOVE_OBSERVER_THRESHOLD = 50;
11
+ var SCROLL_OBSERVER_THRESHOLD = 100;
12
+ var VISUAL_VIEWPORT_OBSERVER_THRESHOLD = 200;
13
+ var recordIds = new WeakMap();
14
+ var nextId = 1;
15
+ export function getRecordIdForEvent(event) {
16
+ if (!recordIds.has(event)) {
17
+ recordIds.set(event, nextId++);
18
+ }
19
+
20
+ return recordIds.get(event);
21
+ }
22
+ export function initObservers(o) {
23
+ var mutationHandler = initMutationObserver(o.mutationCb, o.configuration, o.shadowRootsController);
24
+ var mousemoveHandler = initMoveObserver(o.mousemoveCb);
25
+ var mouseInteractionHandler = initMouseInteractionObserver(o.mouseInteractionCb, o.configuration.defaultPrivacyLevel);
26
+ var scrollHandler = initScrollObserver(o.scrollCb, o.configuration.defaultPrivacyLevel, o.elementsScrollPositions);
27
+ var viewportResizeHandler = initViewportResizeObserver(o.viewportResizeCb);
28
+ var inputHandler = initInputObserver(o.inputCb, o.configuration.defaultPrivacyLevel);
29
+ var mediaInteractionHandler = initMediaInteractionObserver(o.mediaInteractionCb, o.configuration.defaultPrivacyLevel);
30
+ var styleSheetObserver = initStyleSheetObserver(o.styleSheetCb);
31
+ var focusHandler = initFocusObserver(o.focusCb);
32
+ var visualViewportResizeHandler = initVisualViewportResizeObserver(o.visualViewportResizeCb);
33
+ var frustrationHandler = initFrustrationObserver(o.lifeCycle, o.frustrationCb);
34
+ return {
35
+ flush: function flush() {
36
+ mutationHandler.flush();
37
+ },
38
+ stop: function stop() {
39
+ mutationHandler.stop();
40
+ mousemoveHandler();
41
+ mouseInteractionHandler();
42
+ scrollHandler();
43
+ viewportResizeHandler();
44
+ inputHandler();
45
+ mediaInteractionHandler();
46
+ styleSheetObserver();
47
+ focusHandler();
48
+ visualViewportResizeHandler();
49
+ frustrationHandler();
50
+ }
51
+ };
52
+ }
53
+ export function initMutationObserver(cb, configuration, shadowRootsController) {
54
+ return startMutationObserver(cb, configuration, shadowRootsController, document);
55
+ }
56
+ export function initMoveObserver(cb) {
57
+ var _updatePosition = throttle(function (event) {
58
+ var target = getEventTarget(event);
59
+
60
+ if (hasSerializedNode(target)) {
61
+ var coordinates = tryToComputeCoordinates(event);
62
+
63
+ if (!coordinates) {
64
+ return;
65
+ }
66
+
67
+ var position = {
68
+ id: getSerializedNodeId(target),
69
+ timeOffset: 0,
70
+ x: coordinates.x,
71
+ y: coordinates.y
72
+ };
73
+ cb([position], isTouchEvent(event) ? IncrementalSource.TouchMove : IncrementalSource.MouseMove);
74
+ }
75
+ }, MOUSE_MOVE_OBSERVER_THRESHOLD, {
76
+ trailing: false
77
+ });
78
+
79
+ return addEventListeners(document, [DOM_EVENT.MOUSE_MOVE, DOM_EVENT.TOUCH_MOVE], _updatePosition.throttled, {
80
+ capture: true,
81
+ passive: true
82
+ }).stop;
83
+ }
84
+ var eventTypeToMouseInteraction = {
85
+ // Listen for pointerup DOM events instead of mouseup for MouseInteraction/MouseUp records. This
86
+ // allows to reference such records from Frustration records.
87
+ //
88
+ // In the context of supporting Mobile Session Replay, we introduced `PointerInteraction` records
89
+ // used by the Mobile SDKs in place of `MouseInteraction`. In the future, we should replace
90
+ // `MouseInteraction` by `PointerInteraction` in the Browser SDK so we have an uniform way to
91
+ // convey such interaction. This would cleanly solve the issue since we would have
92
+ // `PointerInteraction/Up` records that we could reference from `Frustration` records.
93
+ [DOM_EVENT.POINTER_UP]: MouseInteractionType.MouseUp,
94
+ [DOM_EVENT.MOUSE_DOWN]: MouseInteractionType.MouseDown,
95
+ [DOM_EVENT.CLICK]: MouseInteractionType.Click,
96
+ [DOM_EVENT.CONTEXT_MENU]: MouseInteractionType.ContextMenu,
97
+ [DOM_EVENT.DBL_CLICK]: MouseInteractionType.DblClick,
98
+ [DOM_EVENT.FOCUS]: MouseInteractionType.Focus,
99
+ [DOM_EVENT.BLUR]: MouseInteractionType.Blur,
100
+ [DOM_EVENT.TOUCH_START]: MouseInteractionType.TouchStart,
101
+ [DOM_EVENT.TOUCH_END]: MouseInteractionType.TouchEnd
102
+ };
103
+ export function initMouseInteractionObserver(cb, defaultPrivacyLevel) {
104
+ var handler = function handler(event) {
105
+ var target = getEventTarget(event);
106
+
107
+ if (getNodePrivacyLevel(target, defaultPrivacyLevel) === NodePrivacyLevel.HIDDEN || !hasSerializedNode(target)) {
108
+ return;
109
+ }
110
+
111
+ var id = getSerializedNodeId(target);
112
+ var type = eventTypeToMouseInteraction[event.type];
113
+ var interaction;
114
+
115
+ if (type !== MouseInteractionType.Blur && type !== MouseInteractionType.Focus) {
116
+ var coordinates = tryToComputeCoordinates(event);
117
+
118
+ if (!coordinates) {
119
+ return;
120
+ }
121
+
122
+ interaction = {
123
+ id: id,
124
+ type: type,
125
+ x: coordinates.x,
126
+ y: coordinates.y
127
+ };
128
+ } else {
129
+ interaction = {
130
+ id: id,
131
+ type: type
132
+ };
133
+ }
134
+
135
+ var record = assign({
136
+ id: getRecordIdForEvent(event)
137
+ }, assembleIncrementalSnapshot(IncrementalSource.MouseInteraction, interaction));
138
+ cb(record);
139
+ };
140
+
141
+ return addEventListeners(document, Object.keys(eventTypeToMouseInteraction), handler, {
142
+ capture: true,
143
+ passive: true
144
+ }).stop;
145
+ }
146
+
147
+ function tryToComputeCoordinates(event) {
148
+ var _event = isTouchEvent(event) ? event.changedTouches[0] : event;
149
+
150
+ var x = _event.clientX;
151
+ var y = _event.clientY;
152
+
153
+ if (window.visualViewport) {
154
+ var _convertMouseEventToLayoutCoordinates = convertMouseEventToLayoutCoordinates(x, y);
155
+
156
+ x = _convertMouseEventToLayoutCoordinates.visualViewportX;
157
+ y = _convertMouseEventToLayoutCoordinates.visualViewportY;
158
+ }
159
+
160
+ if (!Number.isFinite(x) || !Number.isFinite(y)) {
161
+ return undefined;
162
+ }
163
+
164
+ return {
165
+ x: x,
166
+ y: y
167
+ };
168
+ }
169
+
170
+ function initScrollObserver(cb, defaultPrivacyLevel, elementsScrollPositions) {
171
+ var _updatePosition = throttle(function (event) {
172
+ var target = getEventTarget(event);
173
+
174
+ if (!target || getNodePrivacyLevel(target, defaultPrivacyLevel) === NodePrivacyLevel.HIDDEN || !hasSerializedNode(target)) {
175
+ return;
176
+ }
177
+
178
+ var id = getSerializedNodeId(target);
179
+ var scrollPositions = target === document ? {
180
+ scrollTop: getScrollY(),
181
+ scrollLeft: getScrollX()
182
+ } : {
183
+ scrollTop: Math.round(target.scrollTop),
184
+ scrollLeft: Math.round(target.scrollLeft)
185
+ };
186
+ elementsScrollPositions.set(target, scrollPositions);
187
+ cb({
188
+ id: id,
189
+ x: scrollPositions.scrollLeft,
190
+ y: scrollPositions.scrollTop
191
+ });
192
+ }, SCROLL_OBSERVER_THRESHOLD);
193
+
194
+ return addEventListener(document, DOM_EVENT.SCROLL, _updatePosition.throttled, {
195
+ capture: true,
196
+ passive: true
197
+ }).stop;
198
+ }
199
+
200
+ function initViewportResizeObserver(cb) {
201
+ return initViewportObservable().subscribe(cb).unsubscribe;
202
+ }
203
+
204
+ export function initInputObserver(cb, defaultPrivacyLevel, options) {
205
+ var domEvents = options && options.domEvents || [DOM_EVENT.INPUT, DOM_EVENT.CHANGE];
206
+ var target = options && options.target || document;
207
+ var lastInputStateMap = new WeakMap();
208
+
209
+ function onElementChange(target) {
210
+ var nodePrivacyLevel = getNodePrivacyLevel(target, defaultPrivacyLevel);
211
+
212
+ if (nodePrivacyLevel === NodePrivacyLevel.HIDDEN) {
213
+ return;
214
+ }
215
+
216
+ var type = target.type;
217
+ var inputState;
218
+
219
+ if (type === 'radio' || type === 'checkbox') {
220
+ if (shouldMaskNode(target, nodePrivacyLevel)) {
221
+ return;
222
+ }
223
+
224
+ inputState = {
225
+ isChecked: target.checked
226
+ };
227
+ } else {
228
+ var value = getElementInputValue(target, nodePrivacyLevel);
229
+
230
+ if (value === undefined) {
231
+ return;
232
+ }
233
+
234
+ inputState = {
235
+ text: value
236
+ };
237
+ } // Can be multiple changes on the same node within the same batched mutation observation.
238
+
239
+
240
+ cbWithDedup(target, inputState); // If a radio was checked, other radios with the same name attribute will be unchecked.
241
+
242
+ var name = target.name;
243
+
244
+ if (type === 'radio' && name && target.checked) {
245
+ forEach(document.querySelectorAll("input[type=\"radio\"][name=\"".concat(name, "\"]")), function (el) {
246
+ if (el !== target) {
247
+ // TODO: Consider the privacy implications for various differing input privacy levels
248
+ cbWithDedup(el, {
249
+ isChecked: false
250
+ });
251
+ }
252
+ });
253
+ }
254
+ }
255
+ /**
256
+ * There can be multiple changes on the same node within the same batched mutation observation.
257
+ */
258
+
259
+
260
+ function cbWithDedup(target, inputState) {
261
+ if (!hasSerializedNode(target)) {
262
+ return;
263
+ }
264
+
265
+ var lastInputState = lastInputStateMap.get(target);
266
+
267
+ if (!lastInputState || lastInputState.text !== inputState.text || lastInputState.isChecked !== inputState.isChecked) {
268
+ lastInputStateMap.set(target, inputState);
269
+ cb(assign({
270
+ id: getSerializedNodeId(target)
271
+ }, inputState));
272
+ }
273
+ }
274
+
275
+ var _addEventListeners = addEventListeners(target, domEvents, function (event) {
276
+ var target = getEventTarget(event);
277
+
278
+ if (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement) {
279
+ onElementChange(target);
280
+ }
281
+ }, {
282
+ capture: true,
283
+ passive: true
284
+ });
285
+
286
+ var instrumentationStoppers = [instrumentSetter(HTMLInputElement.prototype, 'value', onElementChange), instrumentSetter(HTMLInputElement.prototype, 'checked', onElementChange), instrumentSetter(HTMLSelectElement.prototype, 'value', onElementChange), instrumentSetter(HTMLTextAreaElement.prototype, 'value', onElementChange), instrumentSetter(HTMLSelectElement.prototype, 'selectedIndex', onElementChange)];
287
+ return function () {
288
+ instrumentationStoppers.forEach(function (stopper) {
289
+ stopper.stop();
290
+ });
291
+
292
+ _addEventListeners.stop();
293
+ };
294
+ }
295
+ export function initStyleSheetObserver(cb) {
296
+ function checkStyleSheetAndCallback(styleSheet, callback) {
297
+ if (styleSheet && hasSerializedNode(styleSheet.ownerNode)) {
298
+ callback(getSerializedNodeId(styleSheet.ownerNode));
299
+ }
300
+ }
301
+
302
+ var instrumentationStoppers = [instrumentMethodAndCallOriginal(CSSStyleSheet.prototype, 'insertRule', {
303
+ before: function before(rule, index) {
304
+ checkStyleSheetAndCallback(this, function (id) {
305
+ return cb({
306
+ id: id,
307
+ adds: [{
308
+ rule: rule,
309
+ index: index
310
+ }]
311
+ });
312
+ });
313
+ }
314
+ }), instrumentMethodAndCallOriginal(CSSStyleSheet.prototype, 'deleteRule', {
315
+ before: function before(index) {
316
+ checkStyleSheetAndCallback(this, function (id) {
317
+ return cb({
318
+ id: id,
319
+ removes: [{
320
+ index: index
321
+ }]
322
+ });
323
+ });
324
+ }
325
+ })];
326
+
327
+ if (typeof CSSGroupingRule !== 'undefined') {
328
+ instrumentGroupingCSSRuleClass(CSSGroupingRule);
329
+ } else {
330
+ instrumentGroupingCSSRuleClass(CSSMediaRule);
331
+ instrumentGroupingCSSRuleClass(CSSSupportsRule);
332
+ }
333
+
334
+ function instrumentGroupingCSSRuleClass(cls) {
335
+ instrumentationStoppers.push(instrumentMethodAndCallOriginal(cls.prototype, 'insertRule', {
336
+ before: function before(rule, index) {
337
+ var _this = this;
338
+
339
+ checkStyleSheetAndCallback(this.parentStyleSheet, function (id) {
340
+ var path = getPathToNestedCSSRule(_this);
341
+
342
+ if (path) {
343
+ path.push(index || 0);
344
+ cb({
345
+ id: id,
346
+ adds: [{
347
+ rule: rule,
348
+ index: path
349
+ }]
350
+ });
351
+ }
352
+ });
353
+ }
354
+ }), instrumentMethodAndCallOriginal(cls.prototype, 'deleteRule', {
355
+ before: function before(index) {
356
+ var _this = this;
357
+
358
+ checkStyleSheetAndCallback(this.parentStyleSheet, function (id) {
359
+ var path = getPathToNestedCSSRule(_this);
360
+
361
+ if (path) {
362
+ path.push(index);
363
+ cb({
364
+ id: id,
365
+ removes: [{
366
+ index: path
367
+ }]
368
+ });
369
+ }
370
+ });
371
+ }
372
+ }));
373
+ }
374
+
375
+ return function () {
376
+ instrumentationStoppers.forEach(function (stopper) {
377
+ stopper.stop();
378
+ });
379
+ };
380
+ }
381
+
382
+ function initMediaInteractionObserver(mediaInteractionCb, defaultPrivacyLevel) {
383
+ var handler = function handler(event) {
384
+ var target = getEventTarget(event);
385
+
386
+ if (!target || getNodePrivacyLevel(target, defaultPrivacyLevel) === NodePrivacyLevel.HIDDEN || !hasSerializedNode(target)) {
387
+ return;
388
+ }
389
+
390
+ mediaInteractionCb({
391
+ id: getSerializedNodeId(target),
392
+ type: event.type === DOM_EVENT.PLAY ? MediaInteractionType.Play : MediaInteractionType.Pause
393
+ });
394
+ };
395
+
396
+ return addEventListeners(document, [DOM_EVENT.PLAY, DOM_EVENT.PAUSE], handler, {
397
+ capture: true,
398
+ passive: true
399
+ }).stop;
400
+ }
401
+
402
+ function initFocusObserver(focusCb) {
403
+ return addEventListeners(window, [DOM_EVENT.FOCUS, DOM_EVENT.BLUR], function () {
404
+ focusCb({
405
+ has_focus: document.hasFocus()
406
+ });
407
+ }).stop;
408
+ }
409
+
410
+ function initVisualViewportResizeObserver(cb) {
411
+ if (!window.visualViewport) {
412
+ return noop;
413
+ }
414
+
415
+ var _updateDimension = throttle(function () {
416
+ cb(getVisualViewport());
417
+ }, VISUAL_VIEWPORT_OBSERVER_THRESHOLD, {
418
+ trailing: false
419
+ });
420
+
421
+ var removeListener = addEventListeners(window.visualViewport, [DOM_EVENT.RESIZE, DOM_EVENT.SCROLL], _updateDimension.throttled, {
422
+ capture: true,
423
+ passive: true
424
+ }).stop;
425
+ var cancelThrottle = _updateDimension.cancel;
426
+ return function stop() {
427
+ removeListener();
428
+ cancelThrottle();
429
+ };
430
+ }
431
+
432
+ export function initFrustrationObserver(lifeCycle, frustrationCb) {
433
+ return lifeCycle.subscribe(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, function (data) {
434
+ if (data.rawRumEvent.type === RumEventType.ACTION && data.rawRumEvent.action.type === ActionType.CLICK && data.rawRumEvent.action.frustration && data.rawRumEvent.action.frustration.type && data.rawRumEvent.action.frustration.type.length && 'events' in data.domainContext && data.domainContext.events && data.domainContext.events.length) {// frustrationCb({
435
+ // timestamp: data.rawRumEvent.date,
436
+ // type: RecordType.FrustrationRecord,
437
+ // data: {
438
+ // frustrationTypes: data.rawRumEvent.action.frustration.type,
439
+ // recordIds: data.domainContext.events.map(function (e) {
440
+ // return getRecordIdForEvent(e)
441
+ // })
442
+ // }
443
+ // })
444
+ }
445
+ }).unsubscribe;
446
+ }
447
+
448
+ function getEventTarget(event) {
449
+ if (event.composed === true && isNodeShadowHost(event.target)) {
450
+ return event.composedPath()[0];
451
+ }
452
+
453
+ return event.target;
454
+ }
@@ -0,0 +1,233 @@
1
+ import { isElementNode, getParentNode, isTextNode } from '@cloudcare/browser-core';
2
+ import { NodePrivacyLevel, PRIVACY_ATTR_NAME, PRIVACY_ATTR_VALUE_ALLOW, PRIVACY_ATTR_VALUE_MASK, PRIVACY_ATTR_VALUE_MASK_USER_INPUT, PRIVACY_ATTR_VALUE_HIDDEN, PRIVACY_CLASS_ALLOW, PRIVACY_CLASS_MASK, PRIVACY_CLASS_MASK_USER_INPUT, PRIVACY_CLASS_HIDDEN, FORM_PRIVATE_TAG_NAMES, CENSORED_STRING_MARK } from '../../../constants';
3
+ export var MAX_ATTRIBUTE_VALUE_CHAR_LENGTH = 100000;
4
+ var TEXT_MASKING_CHAR = 'x';
5
+ /**
6
+ * Get node privacy level by iterating over its ancestors. When the direct parent privacy level is
7
+ * know, it is best to use something like:
8
+ *
9
+ * derivePrivacyLevelGivenParent(getNodeSelfPrivacyLevel(node), parentNodePrivacyLevel)
10
+ */
11
+
12
+ export function getNodePrivacyLevel(node, defaultPrivacyLevel) {
13
+ var parentNode = getParentNode(node);
14
+ var parentNodePrivacyLevel = parentNode ? getNodePrivacyLevel(parentNode, defaultPrivacyLevel) : defaultPrivacyLevel;
15
+ var selfNodePrivacyLevel = getNodeSelfPrivacyLevel(node);
16
+ return reducePrivacyLevel(selfNodePrivacyLevel, parentNodePrivacyLevel);
17
+ }
18
+ /**
19
+ * Reduces the next privacy level based on self + parent privacy levels
20
+ */
21
+
22
+ export function reducePrivacyLevel(childPrivacyLevel, parentNodePrivacyLevel) {
23
+ switch (parentNodePrivacyLevel) {
24
+ // These values cannot be overridden
25
+ case NodePrivacyLevel.HIDDEN:
26
+ case NodePrivacyLevel.IGNORE:
27
+ return parentNodePrivacyLevel;
28
+ }
29
+
30
+ switch (childPrivacyLevel) {
31
+ case NodePrivacyLevel.ALLOW:
32
+ case NodePrivacyLevel.MASK:
33
+ case NodePrivacyLevel.MASK_USER_INPUT:
34
+ case NodePrivacyLevel.HIDDEN:
35
+ case NodePrivacyLevel.IGNORE:
36
+ return childPrivacyLevel;
37
+
38
+ default:
39
+ return parentNodePrivacyLevel;
40
+ }
41
+ }
42
+ /**
43
+ * Determines the node's own privacy level without checking for ancestors.
44
+ */
45
+
46
+ export function getNodeSelfPrivacyLevel(node) {
47
+ // Only Element types can have a privacy level set
48
+ if (!isElementNode(node)) {
49
+ return;
50
+ }
51
+
52
+ var privAttr = node.getAttribute(PRIVACY_ATTR_NAME); // Overrules for replay purpose
53
+
54
+ if (node.tagName === 'BASE') {
55
+ return NodePrivacyLevel.ALLOW;
56
+ } // Overrules to enforce end-user protection
57
+
58
+
59
+ if (node.tagName === 'INPUT') {
60
+ var inputElement = node;
61
+
62
+ if (inputElement.type === 'password' || inputElement.type === 'email' || inputElement.type === 'tel') {
63
+ return NodePrivacyLevel.MASK;
64
+ }
65
+
66
+ if (inputElement.type === 'hidden') {
67
+ return NodePrivacyLevel.MASK;
68
+ }
69
+
70
+ var autocomplete = inputElement.getAttribute('autocomplete'); // Handle input[autocomplete=cc-number/cc-csc/cc-exp/cc-exp-month/cc-exp-year]
71
+
72
+ if (autocomplete && autocomplete.indexOf('cc-') === 0) {
73
+ return NodePrivacyLevel.MASK;
74
+ }
75
+ } // Check HTML privacy attributes and classes
76
+
77
+
78
+ if (privAttr === PRIVACY_ATTR_VALUE_HIDDEN || node.classList.contains(PRIVACY_CLASS_HIDDEN)) {
79
+ return NodePrivacyLevel.HIDDEN;
80
+ }
81
+
82
+ if (privAttr === PRIVACY_ATTR_VALUE_MASK || node.classList.contains(PRIVACY_CLASS_MASK)) {
83
+ return NodePrivacyLevel.MASK;
84
+ }
85
+
86
+ if (privAttr === PRIVACY_ATTR_VALUE_MASK_USER_INPUT || node.classList.contains(PRIVACY_CLASS_MASK_USER_INPUT)) {
87
+ return NodePrivacyLevel.MASK_USER_INPUT;
88
+ }
89
+
90
+ if (privAttr === PRIVACY_ATTR_VALUE_ALLOW || node.classList.contains(PRIVACY_CLASS_ALLOW)) {
91
+ return NodePrivacyLevel.ALLOW;
92
+ }
93
+
94
+ if (shouldIgnoreElement(node)) {
95
+ return NodePrivacyLevel.IGNORE;
96
+ }
97
+ }
98
+ /**
99
+ * Helper aiming to unify `mask` and `mask-user-input` privacy levels:
100
+ *
101
+ * In the `mask` case, it is trivial: we should mask the element.
102
+ *
103
+ * In the `mask-user-input` case, we should mask the element only if it is a "form" element or the
104
+ * direct parent is a form element for text nodes).
105
+ *
106
+ * Other `shouldMaskNode` cases are edge cases that should not matter too much (ex: should we mask a
107
+ * node if it is ignored or hidden? it doesn't matter since it won't be serialized).
108
+ */
109
+
110
+ export function shouldMaskNode(node, privacyLevel) {
111
+ switch (privacyLevel) {
112
+ case NodePrivacyLevel.MASK:
113
+ case NodePrivacyLevel.HIDDEN:
114
+ case NodePrivacyLevel.IGNORE:
115
+ return true;
116
+
117
+ case NodePrivacyLevel.MASK_USER_INPUT:
118
+ return isTextNode(node) ? isFormElement(node.parentNode) : isFormElement(node);
119
+
120
+ default:
121
+ return false;
122
+ }
123
+ }
124
+
125
+ function isFormElement(node) {
126
+ if (!node || node.nodeType !== node.ELEMENT_NODE) {
127
+ return false;
128
+ }
129
+
130
+ var element = node;
131
+
132
+ if (element.tagName === 'INPUT') {
133
+ switch (element.type) {
134
+ case 'button':
135
+ case 'color':
136
+ case 'reset':
137
+ case 'submit':
138
+ return false;
139
+ }
140
+ }
141
+
142
+ return !!FORM_PRIVATE_TAG_NAMES[element.tagName];
143
+ }
144
+ /**
145
+ * Text censoring non-destructively maintains whitespace characters in order to preserve text shape
146
+ * during replay.
147
+ */
148
+
149
+
150
+ export var censorText = function censorText(text) {
151
+ return text.replace(/\S/g, TEXT_MASKING_CHAR);
152
+ };
153
+ export function getTextContent(textNode, ignoreWhiteSpace, parentNodePrivacyLevel) {
154
+ var _textNode$parentEleme;
155
+
156
+ // The parent node may not be a html element which has a tagName attribute.
157
+ // So just let it be undefined which is ok in this use case.
158
+ var parentTagName = (_textNode$parentEleme = textNode.parentElement) === null || _textNode$parentEleme === void 0 ? void 0 : _textNode$parentEleme.tagName;
159
+ var textContent = textNode.textContent || '';
160
+
161
+ if (ignoreWhiteSpace && !textContent.trim()) {
162
+ return;
163
+ }
164
+
165
+ var nodePrivacyLevel = parentNodePrivacyLevel;
166
+ var isStyle = parentTagName === 'STYLE' ? true : undefined;
167
+ var isScript = parentTagName === 'SCRIPT';
168
+
169
+ if (isScript) {
170
+ // For perf reasons, we don't record script (heuristic)
171
+ textContent = CENSORED_STRING_MARK;
172
+ } else if (nodePrivacyLevel === NodePrivacyLevel.HIDDEN) {
173
+ // Should never occur, but just in case, we set to CENSORED_MARK.
174
+ textContent = CENSORED_STRING_MARK;
175
+ } else if (shouldMaskNode(textNode, nodePrivacyLevel) && // Style tags are `overruled` (Use `hide` to enforce privacy)
176
+ !isStyle) {
177
+ if ( // Scrambling the child list breaks text nodes for DATALIST/SELECT/OPTGROUP
178
+ parentTagName === 'DATALIST' || parentTagName === 'SELECT' || parentTagName === 'OPTGROUP') {
179
+ if (!textContent.trim()) {
180
+ return;
181
+ }
182
+ } else if (parentTagName === 'OPTION') {
183
+ // <Option> has low entropy in charset + text length, so use `CENSORED_STRING_MARK` when masked
184
+ textContent = CENSORED_STRING_MARK;
185
+ } else {
186
+ textContent = censorText(textContent);
187
+ }
188
+ }
189
+
190
+ return textContent;
191
+ }
192
+ /**
193
+ * TODO: Preserve CSS element order, and record the presence of the tag, just don't render
194
+ * We don't need this logic on the recorder side.
195
+ * For security related meta's, customer can mask themmanually given they
196
+ * are easy to identify in the HEAD tag.
197
+ */
198
+
199
+ export function shouldIgnoreElement(element) {
200
+ if (element.nodeName === 'SCRIPT') {
201
+ return true;
202
+ }
203
+
204
+ if (element.nodeName === 'LINK') {
205
+ var relAttribute = getLowerCaseAttribute('rel');
206
+ return (// Link as script - Ignore only when rel=preload, modulepreload or prefetch
207
+ /preload|prefetch/i.test(relAttribute) && getLowerCaseAttribute('as') === 'script' || // Favicons
208
+ relAttribute === 'shortcut icon' || relAttribute === 'icon'
209
+ );
210
+ }
211
+
212
+ if (element.nodeName === 'META') {
213
+ var nameAttribute = getLowerCaseAttribute('name');
214
+ var relAttribute = getLowerCaseAttribute('rel');
215
+ var propertyAttribute = getLowerCaseAttribute('property');
216
+ return (// Favicons
217
+ /^msapplication-tile(image|color)$/.test(nameAttribute) || nameAttribute === 'application-name' || relAttribute === 'icon' || relAttribute === 'apple-touch-icon' || relAttribute === 'shortcut icon' || // Description
218
+ nameAttribute === 'keywords' || nameAttribute === 'description' || // Social
219
+ /^(og|twitter|fb):/.test(propertyAttribute) || /^(og|twitter):/.test(nameAttribute) || nameAttribute === 'pinterest' || // Robots
220
+ nameAttribute === 'robots' || nameAttribute === 'googlebot' || nameAttribute === 'bingbot' || // Http headers. Ex: X-UA-Compatible, Content-Type, Content-Language, cache-control,
221
+ // X-Translated-By
222
+ element.hasAttribute('http-equiv') || // Authorship
223
+ nameAttribute === 'author' || nameAttribute === 'generator' || nameAttribute === 'framework' || nameAttribute === 'publisher' || nameAttribute === 'progid' || /^article:/.test(propertyAttribute) || /^product:/.test(propertyAttribute) || // Verification
224
+ nameAttribute === 'google-site-verification' || nameAttribute === 'yandex-verification' || nameAttribute === 'csrf-token' || nameAttribute === 'p:domain_verify' || nameAttribute === 'verify-v1' || nameAttribute === 'verification' || nameAttribute === 'shopify-checkout-api-token'
225
+ );
226
+ }
227
+
228
+ function getLowerCaseAttribute(name) {
229
+ return (element.getAttribute(name) || '').toLowerCase();
230
+ }
231
+
232
+ return false;
233
+ }