@cloudcare/browser-rum 3.2.49 → 3.3.0

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 (192) hide show
  1. package/README.md +89 -1
  2. package/README.zh.md +105 -0
  3. package/bundle/dataflux-rum.js +1 -1
  4. package/cjs/boot/buildEnv.js +1 -1
  5. package/cjs/boot/perStartRum.js +16 -0
  6. package/cjs/boot/perStartRum.js.map +1 -1
  7. package/cjs/boot/recorderApi.js +154 -1
  8. package/cjs/boot/recorderApi.js.map +1 -1
  9. package/cjs/boot/rum.js +17 -2
  10. package/cjs/boot/rum.js.map +1 -1
  11. package/cjs/boot/rumPublicApi.js +52 -3
  12. package/cjs/boot/rumPublicApi.js.map +1 -1
  13. package/cjs/boot/startRecording.js +69 -4
  14. package/cjs/boot/startRecording.js.map +1 -1
  15. package/cjs/domain/configuration.js +99 -1
  16. package/cjs/domain/configuration.js.map +1 -1
  17. package/cjs/domain/deflate/deflateWorker.js +1 -1
  18. package/cjs/domain/replay/record/canvasAutoSnapshot.js +15 -0
  19. package/cjs/domain/replay/record/canvasAutoSnapshot.js.map +1 -0
  20. package/cjs/domain/replay/record/canvasCommandCapture.js +480 -0
  21. package/cjs/domain/replay/record/canvasCommandCapture.js.map +1 -0
  22. package/cjs/domain/replay/record/canvasCommandObserver.js +274 -0
  23. package/cjs/domain/replay/record/canvasCommandObserver.js.map +1 -0
  24. package/cjs/domain/replay/record/canvasCommandSerializeArgs.js +94 -0
  25. package/cjs/domain/replay/record/canvasCommandSerializeArgs.js.map +1 -0
  26. package/cjs/domain/replay/record/canvasContinuousScheduler.js +255 -0
  27. package/cjs/domain/replay/record/canvasContinuousScheduler.js.map +1 -0
  28. package/cjs/domain/replay/record/canvasEncodeBridge.js +247 -0
  29. package/cjs/domain/replay/record/canvasEncodeBridge.js.map +1 -0
  30. package/cjs/domain/replay/record/canvasEncodeWorker.js +154 -0
  31. package/cjs/domain/replay/record/canvasEncodeWorker.js.map +1 -0
  32. package/cjs/domain/replay/record/canvasFrameSignature.js +50 -0
  33. package/cjs/domain/replay/record/canvasFrameSignature.js.map +1 -0
  34. package/cjs/domain/replay/record/canvasMutationRecord.js +33 -0
  35. package/cjs/domain/replay/record/canvasMutationRecord.js.map +1 -0
  36. package/cjs/domain/replay/record/canvasObserver.js +190 -0
  37. package/cjs/domain/replay/record/canvasObserver.js.map +1 -0
  38. package/cjs/domain/replay/record/canvasReplayFixtures.js +105 -0
  39. package/cjs/domain/replay/record/canvasReplayFixtures.js.map +1 -0
  40. package/cjs/domain/replay/record/canvasSerializeArgs.js +110 -0
  41. package/cjs/domain/replay/record/canvasSerializeArgs.js.map +1 -0
  42. package/cjs/domain/replay/record/mutationBatch.js +7 -2
  43. package/cjs/domain/replay/record/mutationBatch.js.map +1 -1
  44. package/cjs/domain/replay/record/mutationObserver.js +369 -55
  45. package/cjs/domain/replay/record/mutationObserver.js.map +1 -1
  46. package/cjs/domain/replay/record/observers.js +8 -3
  47. package/cjs/domain/replay/record/observers.js.map +1 -1
  48. package/cjs/domain/replay/record/record.js +478 -1
  49. package/cjs/domain/replay/record/record.js.map +1 -1
  50. package/cjs/domain/replay/record/serializationUtils.js +60 -4
  51. package/cjs/domain/replay/record/serializationUtils.js.map +1 -1
  52. package/cjs/domain/replay/record/serialize.js +169 -46
  53. package/cjs/domain/replay/record/serialize.js.map +1 -1
  54. package/cjs/domain/replay/segmentCollection/segment.js +58 -1
  55. package/cjs/domain/replay/segmentCollection/segment.js.map +1 -1
  56. package/cjs/domain/replay/segmentCollection/segmentCollection.js +29 -3
  57. package/cjs/domain/replay/segmentCollection/segmentCollection.js.map +1 -1
  58. package/cjs/domain/requestCollection.js +1 -1
  59. package/cjs/domain/requestCollection.js.map +1 -1
  60. package/cjs/domain/rumEventsCollection/actions/clickChain.js +1 -1
  61. package/cjs/domain/rumEventsCollection/actions/clickChain.js.map +1 -1
  62. package/cjs/domain/rumEventsCollection/actions/interactionSelectorCache.js +2 -1
  63. package/cjs/domain/rumEventsCollection/actions/interactionSelectorCache.js.map +1 -1
  64. package/cjs/domain/rumEventsCollection/error/trackReportError.js +2 -2
  65. package/cjs/domain/rumEventsCollection/error/trackReportError.js.map +1 -1
  66. package/cjs/domain/rumEventsCollection/resource/matchRequestResourceEntry.js +43 -39
  67. package/cjs/domain/rumEventsCollection/resource/matchRequestResourceEntry.js.map +1 -1
  68. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js +48 -8
  69. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js.map +1 -1
  70. package/cjs/domain/rumEventsCollection/resource/resourceTrackerManager.js +237 -0
  71. package/cjs/domain/rumEventsCollection/resource/resourceTrackerManager.js.map +1 -0
  72. package/cjs/domain/rumEventsCollection/view/trackCumulativeLayoutShift.js.map +1 -1
  73. package/cjs/domain/rumSessionManager.js +2 -0
  74. package/cjs/domain/rumSessionManager.js.map +1 -1
  75. package/cjs/domain/tracing/skywalkingTracer.js +1 -1
  76. package/cjs/domain/tracing/skywalkingTracer.js.map +1 -1
  77. package/cjs/types/sessionReplayConstants.js +2 -2
  78. package/cjs/types/sessionReplayConstants.js.map +1 -1
  79. package/esm/boot/buildEnv.js +1 -1
  80. package/esm/boot/perStartRum.js +16 -0
  81. package/esm/boot/perStartRum.js.map +1 -1
  82. package/esm/boot/recorderApi.js +154 -1
  83. package/esm/boot/recorderApi.js.map +1 -1
  84. package/esm/boot/rum.js +17 -2
  85. package/esm/boot/rum.js.map +1 -1
  86. package/esm/boot/rumPublicApi.js +52 -3
  87. package/esm/boot/rumPublicApi.js.map +1 -1
  88. package/esm/boot/startRecording.js +70 -5
  89. package/esm/boot/startRecording.js.map +1 -1
  90. package/esm/domain/configuration.js +100 -2
  91. package/esm/domain/configuration.js.map +1 -1
  92. package/esm/domain/deflate/deflateWorker.js +1 -1
  93. package/esm/domain/replay/record/canvasAutoSnapshot.js +9 -0
  94. package/esm/domain/replay/record/canvasAutoSnapshot.js.map +1 -0
  95. package/esm/domain/replay/record/canvasCommandCapture.js +474 -0
  96. package/esm/domain/replay/record/canvasCommandCapture.js.map +1 -0
  97. package/esm/domain/replay/record/canvasCommandObserver.js +268 -0
  98. package/esm/domain/replay/record/canvasCommandObserver.js.map +1 -0
  99. package/esm/domain/replay/record/canvasCommandSerializeArgs.js +87 -0
  100. package/esm/domain/replay/record/canvasCommandSerializeArgs.js.map +1 -0
  101. package/esm/domain/replay/record/canvasContinuousScheduler.js +249 -0
  102. package/esm/domain/replay/record/canvasContinuousScheduler.js.map +1 -0
  103. package/esm/domain/replay/record/canvasEncodeBridge.js +240 -0
  104. package/esm/domain/replay/record/canvasEncodeBridge.js.map +1 -0
  105. package/esm/domain/replay/record/canvasEncodeWorker.js +146 -0
  106. package/esm/domain/replay/record/canvasEncodeWorker.js.map +1 -0
  107. package/esm/domain/replay/record/canvasFrameSignature.js +42 -0
  108. package/esm/domain/replay/record/canvasFrameSignature.js.map +1 -0
  109. package/esm/domain/replay/record/canvasMutationRecord.js +26 -0
  110. package/esm/domain/replay/record/canvasMutationRecord.js.map +1 -0
  111. package/esm/domain/replay/record/canvasObserver.js +184 -0
  112. package/esm/domain/replay/record/canvasObserver.js.map +1 -0
  113. package/esm/domain/replay/record/canvasReplayFixtures.js +96 -0
  114. package/esm/domain/replay/record/canvasReplayFixtures.js.map +1 -0
  115. package/esm/domain/replay/record/canvasSerializeArgs.js +101 -0
  116. package/esm/domain/replay/record/canvasSerializeArgs.js.map +1 -0
  117. package/esm/domain/replay/record/mutationBatch.js +7 -2
  118. package/esm/domain/replay/record/mutationBatch.js.map +1 -1
  119. package/esm/domain/replay/record/mutationObserver.js +369 -56
  120. package/esm/domain/replay/record/mutationObserver.js.map +1 -1
  121. package/esm/domain/replay/record/observers.js +8 -3
  122. package/esm/domain/replay/record/observers.js.map +1 -1
  123. package/esm/domain/replay/record/record.js +479 -2
  124. package/esm/domain/replay/record/record.js.map +1 -1
  125. package/esm/domain/replay/record/serializationUtils.js +58 -4
  126. package/esm/domain/replay/record/serializationUtils.js.map +1 -1
  127. package/esm/domain/replay/record/serialize.js +168 -47
  128. package/esm/domain/replay/record/serialize.js.map +1 -1
  129. package/esm/domain/replay/segmentCollection/segment.js +59 -2
  130. package/esm/domain/replay/segmentCollection/segment.js.map +1 -1
  131. package/esm/domain/replay/segmentCollection/segmentCollection.js +30 -4
  132. package/esm/domain/replay/segmentCollection/segmentCollection.js.map +1 -1
  133. package/esm/domain/requestCollection.js +1 -1
  134. package/esm/domain/requestCollection.js.map +1 -1
  135. package/esm/domain/rumEventsCollection/actions/clickChain.js +1 -1
  136. package/esm/domain/rumEventsCollection/actions/clickChain.js.map +1 -1
  137. package/esm/domain/rumEventsCollection/actions/interactionSelectorCache.js +2 -1
  138. package/esm/domain/rumEventsCollection/actions/interactionSelectorCache.js.map +1 -1
  139. package/esm/domain/rumEventsCollection/error/trackReportError.js +2 -2
  140. package/esm/domain/rumEventsCollection/error/trackReportError.js.map +1 -1
  141. package/esm/domain/rumEventsCollection/resource/matchRequestResourceEntry.js +43 -38
  142. package/esm/domain/rumEventsCollection/resource/matchRequestResourceEntry.js.map +1 -1
  143. package/esm/domain/rumEventsCollection/resource/resourceCollection.js +50 -10
  144. package/esm/domain/rumEventsCollection/resource/resourceCollection.js.map +1 -1
  145. package/esm/domain/rumEventsCollection/resource/resourceTrackerManager.js +231 -0
  146. package/esm/domain/rumEventsCollection/resource/resourceTrackerManager.js.map +1 -0
  147. package/esm/domain/rumEventsCollection/view/trackCumulativeLayoutShift.js.map +1 -1
  148. package/esm/domain/rumSessionManager.js +2 -0
  149. package/esm/domain/rumSessionManager.js.map +1 -1
  150. package/esm/domain/tracing/skywalkingTracer.js +1 -1
  151. package/esm/domain/tracing/skywalkingTracer.js.map +1 -1
  152. package/esm/types/sessionReplayConstants.js +2 -2
  153. package/esm/types/sessionReplayConstants.js.map +1 -1
  154. package/package.json +5 -5
  155. package/src/boot/perStartRum.js +16 -0
  156. package/src/boot/recorderApi.js +162 -1
  157. package/src/boot/rum.js +21 -2
  158. package/src/boot/rumPublicApi.js +59 -3
  159. package/src/boot/startRecording.js +94 -5
  160. package/src/domain/configuration.js +229 -1
  161. package/src/domain/replay/record/canvasAutoSnapshot.js +8 -0
  162. package/src/domain/replay/record/canvasCommandCapture.js +621 -0
  163. package/src/domain/replay/record/canvasCommandObserver.js +355 -0
  164. package/src/domain/replay/record/canvasCommandSerializeArgs.js +124 -0
  165. package/src/domain/replay/record/canvasContinuousScheduler.js +295 -0
  166. package/src/domain/replay/record/canvasEncodeBridge.js +299 -0
  167. package/src/domain/replay/record/canvasEncodeWorker.js +211 -0
  168. package/src/domain/replay/record/canvasFrameSignature.js +83 -0
  169. package/src/domain/replay/record/canvasMutationRecord.js +29 -0
  170. package/src/domain/replay/record/canvasObserver.js +279 -0
  171. package/src/domain/replay/record/canvasReplayFixtures.js +113 -0
  172. package/src/domain/replay/record/canvasSerializeArgs.js +129 -0
  173. package/src/domain/replay/record/mutationBatch.js +8 -2
  174. package/src/domain/replay/record/mutationObserver.js +449 -66
  175. package/src/domain/replay/record/observers.js +15 -3
  176. package/src/domain/replay/record/record.js +658 -2
  177. package/src/domain/replay/record/serializationUtils.js +75 -4
  178. package/src/domain/replay/record/serialize.js +217 -49
  179. package/src/domain/replay/segmentCollection/segment.js +65 -2
  180. package/src/domain/replay/segmentCollection/segmentCollection.js +46 -4
  181. package/src/domain/requestCollection.js +5 -1
  182. package/src/domain/rumEventsCollection/actions/clickChain.js +1 -1
  183. package/src/domain/rumEventsCollection/actions/interactionSelectorCache.js +2 -1
  184. package/src/domain/rumEventsCollection/error/trackReportError.js +3 -2
  185. package/src/domain/rumEventsCollection/resource/matchRequestResourceEntry.js +51 -46
  186. package/src/domain/rumEventsCollection/resource/resourceCollection.js +69 -10
  187. package/src/domain/rumEventsCollection/resource/resourceTrackerManager.js +264 -0
  188. package/src/domain/rumEventsCollection/view/trackCumulativeLayoutShift.js +0 -1
  189. package/src/domain/rumSessionManager.js +2 -0
  190. package/src/domain/tracing/skywalkingTracer.js +1 -1
  191. package/src/types/sessionReplayConstants.js +2 -2
  192. package/types/index.d.ts +133 -1
@@ -0,0 +1,621 @@
1
+ import { serializeCanvasCommandArgs } from './canvasCommandSerializeArgs'
2
+ import { createCanvasMutationRecord } from './canvasMutationRecord'
3
+
4
+ var CANVAS_2D_CAPTURED_METHODS = [
5
+ 'save',
6
+ 'restore',
7
+ 'translate',
8
+ 'scale',
9
+ 'rotate',
10
+ 'transform',
11
+ 'setTransform',
12
+ 'createLinearGradient',
13
+ 'createRadialGradient',
14
+ 'createPattern',
15
+ 'createConicGradient',
16
+ 'clearRect',
17
+ 'fillRect',
18
+ 'strokeRect',
19
+ 'rect',
20
+ 'beginPath',
21
+ 'moveTo',
22
+ 'lineTo',
23
+ 'bezierCurveTo',
24
+ 'quadraticCurveTo',
25
+ 'closePath',
26
+ 'arc',
27
+ 'ellipse',
28
+ 'roundRect',
29
+ 'clip',
30
+ 'fill',
31
+ 'stroke',
32
+ 'fillText',
33
+ 'strokeText',
34
+ 'setLineDash',
35
+ 'drawImage'
36
+ ]
37
+
38
+ var CANVAS_2D_CAPTURED_PROPERTIES = [
39
+ 'fillStyle',
40
+ 'strokeStyle',
41
+ 'lineWidth',
42
+ 'lineCap',
43
+ 'lineJoin',
44
+ 'miterLimit',
45
+ 'font',
46
+ 'globalAlpha',
47
+ 'shadowColor',
48
+ 'shadowBlur',
49
+ 'shadowOffsetX',
50
+ 'shadowOffsetY',
51
+ 'textAlign',
52
+ 'textBaseline',
53
+ 'filter'
54
+ ]
55
+
56
+ var UNSUPPORTED_REPLAY_OBJECT_NAMES = {
57
+ CanvasGradient: true,
58
+ CanvasPattern: true,
59
+ Path2D: true
60
+ }
61
+ var ORIGINAL_ADD_COLOR_STOP_KEY = '__dd_original_addColorStop__'
62
+ var PATCHED_ADD_COLOR_STOP_KEY = '__dd_patched_addColorStop__'
63
+
64
+ function canPatchProperty(target, property) {
65
+ var descriptor = Object.getOwnPropertyDescriptor(target, property)
66
+ return !descriptor || descriptor.configurable !== false
67
+ }
68
+
69
+ function getPropertyDescriptor(target, property) {
70
+ var currentTarget = target
71
+ while (currentTarget) {
72
+ var descriptor = Object.getOwnPropertyDescriptor(currentTarget, property)
73
+ if (descriptor) {
74
+ return descriptor
75
+ }
76
+ currentTarget = Object.getPrototypeOf(currentTarget)
77
+ }
78
+ }
79
+
80
+ function getCanvasObjectConstructorName(value) {
81
+ if (!value || typeof value !== 'object' || !value.constructor) {
82
+ return
83
+ }
84
+ return value.constructor.name || 'Object'
85
+ }
86
+
87
+ function getFallbackReasonForValue(value) {
88
+ if (Array.isArray(value)) {
89
+ for (var index = 0; index < value.length; index += 1) {
90
+ var nestedReason = getFallbackReasonForValue(value[index])
91
+ if (nestedReason) {
92
+ return nestedReason
93
+ }
94
+ }
95
+ return
96
+ }
97
+ var constructorName = getCanvasObjectConstructorName(value)
98
+ if (!constructorName || !UNSUPPORTED_REPLAY_OBJECT_NAMES[constructorName]) {
99
+ return
100
+ }
101
+ if (constructorName === 'CanvasGradient') {
102
+ return 'gradient_style_assignment'
103
+ }
104
+ if (constructorName === 'CanvasPattern') {
105
+ return 'canvas_pattern'
106
+ }
107
+ if (constructorName === 'Path2D') {
108
+ return 'path2d_argument'
109
+ }
110
+ return 'unsupported_canvas_object'
111
+ }
112
+
113
+ function isCrossOriginImageElement(value) {
114
+ if (typeof HTMLImageElement === 'undefined' || !(value instanceof HTMLImageElement)) {
115
+ return false
116
+ }
117
+ var src = value.currentSrc || value.src
118
+ if (!src) {
119
+ return false
120
+ }
121
+ if (src.indexOf('data:') === 0 || src.indexOf('blob:') === 0) {
122
+ return false
123
+ }
124
+ try {
125
+ return new URL(src, window.location.href).origin !== window.location.origin
126
+ } catch (_) {
127
+ return false
128
+ }
129
+ }
130
+
131
+ function isUnsupportedDrawImageSource(value) {
132
+ if (typeof HTMLCanvasElement !== 'undefined' && value instanceof HTMLCanvasElement) {
133
+ return 'draw_image_canvas'
134
+ }
135
+ if (typeof HTMLVideoElement !== 'undefined' && value instanceof HTMLVideoElement) {
136
+ return 'draw_image_video'
137
+ }
138
+ if (isCrossOriginImageElement(value)) {
139
+ return 'draw_image_cross_origin'
140
+ }
141
+ }
142
+
143
+ export function createCanvas2DCommandRecorder(options) {
144
+ options = options || {}
145
+ var emit = options.emit || function () {}
146
+ var now = options.now || Date.now
147
+ var requestAnimationFrameImpl =
148
+ options.requestAnimationFrameImpl ||
149
+ function (callback) {
150
+ return window.requestAnimationFrame(callback)
151
+ }
152
+ var cancelAnimationFrameImpl =
153
+ options.cancelAnimationFrameImpl ||
154
+ function (rafId) {
155
+ return window.cancelAnimationFrame(rafId)
156
+ }
157
+
158
+ var trackedContexts = []
159
+ var trackedContextSet = new Set()
160
+ var patchedPrototypeRecords = []
161
+ var pendingCommandsByContext = new Map()
162
+ var fallbackModeContexts = new WeakSet()
163
+ var fallbackReasonByContext = new WeakMap()
164
+ var patchedOpaqueObjects = new WeakSet()
165
+ var patchedOpaqueObjectRefs = []
166
+ var opaqueObjectContextMap = new WeakMap()
167
+ var weakRefFactory = Object.prototype.hasOwnProperty.call(
168
+ options,
169
+ 'weakRefFactory'
170
+ )
171
+ ? typeof options.weakRefFactory === 'function'
172
+ ? options.weakRefFactory
173
+ : undefined
174
+ : typeof WeakRef === 'function'
175
+ ? function (value) {
176
+ return new WeakRef(value)
177
+ }
178
+ : undefined
179
+ var scheduledFlushId
180
+ var stopped = false
181
+ var paused = false
182
+ var getCanvasId =
183
+ options.getCanvasId ||
184
+ function (context) {
185
+ return context && context.canvas && context.canvas.__sn && context.canvas.__sn.id
186
+ }
187
+ var getCanvasType =
188
+ options.getCanvasType ||
189
+ function () {
190
+ return '2D'
191
+ }
192
+ var requestSnapshotFallback =
193
+ options.requestSnapshotFallback || function () {}
194
+ var shouldRecordContext =
195
+ options.shouldRecordContext ||
196
+ function () {
197
+ return true
198
+ }
199
+
200
+ function scheduleFlush() {
201
+ if (stopped || paused || scheduledFlushId !== undefined) {
202
+ return
203
+ }
204
+ scheduledFlushId = requestAnimationFrameImpl(function () {
205
+ scheduledFlushId = undefined
206
+ flush()
207
+ })
208
+ }
209
+
210
+ function enqueueCommand(context, command) {
211
+ if (paused) {
212
+ return
213
+ }
214
+ if (!pendingCommandsByContext.has(context)) {
215
+ pendingCommandsByContext.set(context, {
216
+ commands: [],
217
+ startedAt: now()
218
+ })
219
+ }
220
+ pendingCommandsByContext.get(context).commands.push(command)
221
+ scheduleFlush()
222
+ }
223
+
224
+ function flush() {
225
+ if (stopped || paused) {
226
+ return
227
+ }
228
+
229
+ pendingCommandsByContext.forEach(function (batch, context) {
230
+ if (!batch.commands.length) {
231
+ return
232
+ }
233
+ if (fallbackModeContexts.has(context)) {
234
+ requestSnapshotFallback(
235
+ context,
236
+ fallbackReasonByContext.get(context) || 'unsupported_canvas_object'
237
+ )
238
+ return
239
+ }
240
+ var id = getCanvasId(context)
241
+ if (id === undefined || id === null || id === -1) {
242
+ return
243
+ }
244
+ var completedAt = now()
245
+ emit(
246
+ createCanvasMutationRecord({
247
+ id: id,
248
+ contextType: getCanvasType(context),
249
+ commands: batch.commands.slice(),
250
+ timestamp: completedAt,
251
+ startedAt: batch.startedAt,
252
+ completedAt: completedAt
253
+ })
254
+ )
255
+ })
256
+ pendingCommandsByContext.clear()
257
+ }
258
+
259
+ function shouldCaptureContext(context) {
260
+ return trackedContextSet.has(context)
261
+ }
262
+
263
+ function registerTrackedContext(context, restoreFns) {
264
+ if (!context || trackedContextSet.has(context)) {
265
+ return false
266
+ }
267
+ restoreFns = restoreFns || []
268
+
269
+ Object.defineProperty(context, '__dd_command_capture__', {
270
+ configurable: true,
271
+ enumerable: false,
272
+ value: true
273
+ })
274
+ restoreFns.push(function () {
275
+ delete context.__dd_command_capture__
276
+ })
277
+
278
+ trackedContexts.push({
279
+ context: context,
280
+ restore: function () {
281
+ trackedContextSet.delete(context)
282
+ pendingCommandsByContext.delete(context)
283
+ fallbackReasonByContext.delete(context)
284
+ while (restoreFns.length) {
285
+ restoreFns.pop()()
286
+ }
287
+ }
288
+ })
289
+ trackedContextSet.add(context)
290
+ return true
291
+ }
292
+
293
+ function maybeTrackContext(context) {
294
+ if (
295
+ !context ||
296
+ trackedContextSet.has(context) ||
297
+ !context.canvas ||
298
+ shouldRecordContext(context) === false
299
+ ) {
300
+ return false
301
+ }
302
+ return registerTrackedContext(context, [])
303
+ }
304
+
305
+ function enableSnapshotFallbackForContext(context, reason) {
306
+ fallbackModeContexts.add(context)
307
+ if (!fallbackReasonByContext.has(context)) {
308
+ fallbackReasonByContext.set(
309
+ context,
310
+ reason || 'unsupported_canvas_object'
311
+ )
312
+ }
313
+ pendingCommandsByContext.delete(context)
314
+ requestSnapshotFallback(
315
+ context,
316
+ fallbackReasonByContext.get(context) || 'unsupported_canvas_object'
317
+ )
318
+ }
319
+
320
+ function patchGradientObjectMethods(value, context) {
321
+ if (
322
+ !value ||
323
+ typeof value !== 'object' ||
324
+ typeof value.addColorStop !== 'function'
325
+ ) {
326
+ return
327
+ }
328
+
329
+ opaqueObjectContextMap.set(value, context)
330
+
331
+ if (patchedOpaqueObjects.has(value)) {
332
+ return
333
+ }
334
+
335
+ var originalAddColorStop = value.addColorStop
336
+ try {
337
+ value[ORIGINAL_ADD_COLOR_STOP_KEY] = originalAddColorStop
338
+ value[PATCHED_ADD_COLOR_STOP_KEY] = true
339
+ value.addColorStop = function () {
340
+ if (stopped || !this[PATCHED_ADD_COLOR_STOP_KEY]) {
341
+ var restoredOriginal = this[ORIGINAL_ADD_COLOR_STOP_KEY]
342
+ if (typeof restoredOriginal === 'function') {
343
+ this.addColorStop = restoredOriginal
344
+ delete this[ORIGINAL_ADD_COLOR_STOP_KEY]
345
+ delete this[PATCHED_ADD_COLOR_STOP_KEY]
346
+ return restoredOriginal.apply(this, arguments)
347
+ }
348
+ }
349
+ var fallbackContext = opaqueObjectContextMap.get(this)
350
+ if (fallbackContext && shouldCaptureContext(fallbackContext)) {
351
+ enableSnapshotFallbackForContext(fallbackContext, 'gradient_color_stop')
352
+ }
353
+ return originalAddColorStop.apply(this, arguments)
354
+ }
355
+ patchedOpaqueObjects.add(value)
356
+ patchedOpaqueObjectRefs.push(
357
+ weakRefFactory ? weakRefFactory(value) : { value: value }
358
+ )
359
+ } catch (_) {}
360
+ }
361
+
362
+ function restorePatchedOpaqueObjects() {
363
+ while (patchedOpaqueObjectRefs.length) {
364
+ var objectRef = patchedOpaqueObjectRefs.pop()
365
+ var object = objectRef && typeof objectRef.deref === 'function'
366
+ ? objectRef.deref()
367
+ : objectRef && objectRef.value
368
+ if (
369
+ object &&
370
+ object[PATCHED_ADD_COLOR_STOP_KEY] &&
371
+ typeof object[ORIGINAL_ADD_COLOR_STOP_KEY] === 'function'
372
+ ) {
373
+ object.addColorStop = object[ORIGINAL_ADD_COLOR_STOP_KEY]
374
+ delete object[ORIGINAL_ADD_COLOR_STOP_KEY]
375
+ delete object[PATCHED_ADD_COLOR_STOP_KEY]
376
+ }
377
+ }
378
+ }
379
+
380
+ function patchMethodOnTarget(target, methodName, restoreFns) {
381
+ if (typeof target[methodName] !== 'function') {
382
+ return
383
+ }
384
+ var originalMethod = target[methodName]
385
+ target[methodName] = function () {
386
+ maybeTrackContext(this)
387
+ var drawImageFallbackReason =
388
+ shouldCaptureContext(this) && methodName === 'drawImage'
389
+ ? isUnsupportedDrawImageSource(arguments[0])
390
+ : undefined
391
+ if (drawImageFallbackReason) {
392
+ enableSnapshotFallbackForContext(this, drawImageFallbackReason)
393
+ return originalMethod.apply(this, arguments)
394
+ }
395
+ var fallbackReason = shouldCaptureContext(this)
396
+ ? getFallbackReasonForValue(Array.prototype.slice.call(arguments))
397
+ : undefined
398
+ if (fallbackReason) {
399
+ enableSnapshotFallbackForContext(this, fallbackReason)
400
+ return originalMethod.apply(this, arguments)
401
+ }
402
+ var serializedArgs
403
+ if (shouldCaptureContext(this)) {
404
+ try {
405
+ serializedArgs = serializeCanvasCommandArgs(arguments, this)
406
+ } catch (error) {
407
+ throw error
408
+ }
409
+ enqueueCommand(this, {
410
+ property: methodName,
411
+ args: serializedArgs
412
+ })
413
+ }
414
+ var result = originalMethod.apply(this, arguments)
415
+ if (
416
+ shouldCaptureContext(this) &&
417
+ (methodName === 'createLinearGradient' ||
418
+ methodName === 'createRadialGradient' ||
419
+ methodName === 'createConicGradient')
420
+ ) {
421
+ patchGradientObjectMethods(result, this)
422
+ }
423
+ return result
424
+ }
425
+ restoreFns.push(function () {
426
+ target[methodName] = originalMethod
427
+ })
428
+ }
429
+
430
+ function patchPropertyOnTarget(target, propertyName, restoreFns) {
431
+ if (!canPatchProperty(target, propertyName)) {
432
+ return
433
+ }
434
+ var originalDescriptor = getPropertyDescriptor(target, propertyName)
435
+ var fallbackValues = new WeakMap()
436
+ Object.defineProperty(target, propertyName, {
437
+ configurable: true,
438
+ enumerable: true,
439
+ get: function () {
440
+ if (originalDescriptor && originalDescriptor.get) {
441
+ return originalDescriptor.get.call(this)
442
+ }
443
+ return fallbackValues.get(this)
444
+ },
445
+ set: function (value) {
446
+ maybeTrackContext(this)
447
+ fallbackValues.set(this, value)
448
+ if (originalDescriptor && originalDescriptor.set) {
449
+ originalDescriptor.set.call(this, value)
450
+ }
451
+ if (shouldCaptureContext(this)) {
452
+ var fallbackReason = getFallbackReasonForValue(value)
453
+ if (fallbackReason) {
454
+ enableSnapshotFallbackForContext(this, fallbackReason)
455
+ return
456
+ }
457
+ enqueueCommand(this, {
458
+ property: propertyName,
459
+ args: [serializeCanvasCommandArgs([value], this)[0]],
460
+ setter: true
461
+ })
462
+ }
463
+ }
464
+ })
465
+ restoreFns.push(function () {
466
+ if (originalDescriptor) {
467
+ Object.defineProperty(target, propertyName, originalDescriptor)
468
+ } else {
469
+ delete target[propertyName]
470
+ }
471
+ })
472
+ }
473
+
474
+ function patchPrototype(context) {
475
+ var prototype = Object.getPrototypeOf(context)
476
+ if (!prototype || prototype === Object.prototype) {
477
+ return
478
+ }
479
+ var existingRecord = patchedPrototypeRecords.find(function (record) {
480
+ return record.prototype === prototype
481
+ })
482
+ if (existingRecord) {
483
+ return
484
+ }
485
+
486
+ var restoreFns = []
487
+ CANVAS_2D_CAPTURED_METHODS.forEach(function (methodName) {
488
+ patchMethodOnTarget(prototype, methodName, restoreFns)
489
+ })
490
+ CANVAS_2D_CAPTURED_PROPERTIES.forEach(function (propertyName) {
491
+ patchPropertyOnTarget(prototype, propertyName, restoreFns)
492
+ })
493
+
494
+ patchedPrototypeRecords.push({
495
+ prototype: prototype,
496
+ restore: function () {
497
+ while (restoreFns.length) {
498
+ restoreFns.pop()()
499
+ }
500
+ }
501
+ })
502
+ }
503
+
504
+ function patchSharedPrototype(prototype) {
505
+ if (!prototype || prototype === Object.prototype) {
506
+ return
507
+ }
508
+ var existingRecord = patchedPrototypeRecords.find(function (record) {
509
+ return record.prototype === prototype
510
+ })
511
+ if (existingRecord) {
512
+ return
513
+ }
514
+
515
+ var restoreFns = []
516
+ CANVAS_2D_CAPTURED_METHODS.forEach(function (methodName) {
517
+ patchMethodOnTarget(prototype, methodName, restoreFns)
518
+ })
519
+ CANVAS_2D_CAPTURED_PROPERTIES.forEach(function (propertyName) {
520
+ patchPropertyOnTarget(prototype, propertyName, restoreFns)
521
+ })
522
+
523
+ patchedPrototypeRecords.push({
524
+ prototype: prototype,
525
+ restore: function () {
526
+ while (restoreFns.length) {
527
+ restoreFns.pop()()
528
+ }
529
+ }
530
+ })
531
+ }
532
+
533
+ function patchContextInstance(context, restoreFns) {
534
+ CANVAS_2D_CAPTURED_METHODS.forEach(function (methodName) {
535
+ patchMethodOnTarget(context, methodName, restoreFns)
536
+ })
537
+
538
+ CANVAS_2D_CAPTURED_PROPERTIES.forEach(function (propertyName) {
539
+ patchPropertyOnTarget(context, propertyName, restoreFns)
540
+ })
541
+ }
542
+
543
+ function patchContext(context) {
544
+ if (!context || context.__dd_command_capture__) {
545
+ return function () {}
546
+ }
547
+
548
+ var restoreFns = []
549
+ var prototype = Object.getPrototypeOf(context)
550
+ if (prototype && prototype !== Object.prototype) {
551
+ patchPrototype(context)
552
+ } else {
553
+ patchContextInstance(context, restoreFns)
554
+ }
555
+
556
+ registerTrackedContext(context, restoreFns)
557
+
558
+ return function () {
559
+ var trackedIndex = trackedContexts.findIndex(function (tracked) {
560
+ return tracked.context === context
561
+ })
562
+ if (trackedIndex >= 0) {
563
+ trackedContexts.splice(trackedIndex, 1)[0].restore()
564
+ }
565
+ }
566
+ }
567
+
568
+ function stop() {
569
+ stopped = true
570
+ if (scheduledFlushId !== undefined) {
571
+ cancelAnimationFrameImpl(scheduledFlushId)
572
+ scheduledFlushId = undefined
573
+ }
574
+ pendingCommandsByContext.clear()
575
+ fallbackModeContexts = new WeakSet()
576
+ fallbackReasonByContext = new WeakMap()
577
+ opaqueObjectContextMap = new WeakMap()
578
+ restorePatchedOpaqueObjects()
579
+ while (trackedContexts.length) {
580
+ trackedContexts.pop().restore()
581
+ }
582
+ while (patchedPrototypeRecords.length) {
583
+ patchedPrototypeRecords.pop().restore()
584
+ }
585
+ }
586
+
587
+ function pause() {
588
+ paused = true
589
+ if (scheduledFlushId !== undefined) {
590
+ cancelAnimationFrameImpl(scheduledFlushId)
591
+ scheduledFlushId = undefined
592
+ }
593
+ pendingCommandsByContext.clear()
594
+ fallbackModeContexts = new WeakSet()
595
+ fallbackReasonByContext = new WeakMap()
596
+ }
597
+
598
+ function resume() {
599
+ paused = false
600
+ }
601
+
602
+ function reset() {
603
+ if (scheduledFlushId !== undefined) {
604
+ cancelAnimationFrameImpl(scheduledFlushId)
605
+ scheduledFlushId = undefined
606
+ }
607
+ pendingCommandsByContext.clear()
608
+ fallbackModeContexts = new WeakSet()
609
+ fallbackReasonByContext = new WeakMap()
610
+ }
611
+
612
+ return {
613
+ patchContext: patchContext,
614
+ patchSharedPrototype: patchSharedPrototype,
615
+ flush: flush,
616
+ pause: pause,
617
+ resume: resume,
618
+ reset: reset,
619
+ stop: stop
620
+ }
621
+ }