@cloudcare/browser-rum 3.0.19 → 3.0.22

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 (46) hide show
  1. package/bundle/dataflux-rum.js +1 -1
  2. package/cjs/boot/buildEnv.js +1 -1
  3. package/cjs/boot/recorderApi.js +1 -1
  4. package/cjs/boot/rum.js +10 -2
  5. package/cjs/boot/rumPublicApi.js +17 -4
  6. package/cjs/domain/replay/record/mutationBatch.js +1 -1
  7. package/cjs/domain/replay/record/mutationObserver.js +1 -1
  8. package/cjs/domain/replay/record/serializationUtils.js +13 -1
  9. package/cjs/domain/replay/record/serialize.js +31 -2
  10. package/cjs/domain/replay/segmentCollection/segment.js +34 -22
  11. package/cjs/domain/replay/segmentCollection/startDeflateWorker.js +28 -9
  12. package/cjs/domain/rumSessionManager.js +7 -2
  13. package/cjs/transport/startRumEventBridge.js +16 -0
  14. package/esm/boot/buildEnv.js +1 -1
  15. package/esm/boot/recorderApi.js +2 -2
  16. package/esm/boot/rum.js +11 -4
  17. package/esm/boot/rumPublicApi.js +21 -6
  18. package/esm/domain/replay/record/mutationBatch.js +16 -1
  19. package/esm/domain/replay/record/mutationObserver.js +102 -64
  20. package/esm/domain/replay/record/observers.js +5 -19
  21. package/esm/domain/replay/record/record.js +2 -2
  22. package/esm/domain/replay/record/serializationUtils.js +11 -1
  23. package/esm/domain/replay/record/serialize.js +42 -11
  24. package/esm/domain/replay/record/shadowRootsController.js +1 -1
  25. package/esm/domain/replay/replayStats.js +2 -2
  26. package/esm/domain/replay/segmentCollection/buildReplayPayload.js +3 -1
  27. package/esm/domain/replay/segmentCollection/segment.js +34 -22
  28. package/esm/domain/replay/segmentCollection/startDeflateWorker.js +22 -7
  29. package/esm/domain/rumEventsCollection/error/errorCollection.js +1 -1
  30. package/esm/domain/rumSessionManager.js +8 -3
  31. package/esm/domain/tracing/traceIdentifier.js +1 -2
  32. package/esm/transport/startRumEventBridge.js +8 -0
  33. package/package.json +8 -7
  34. package/src/boot/buildEnv.js +1 -1
  35. package/src/boot/recorderApi.js +3 -3
  36. package/src/boot/rum.js +22 -10
  37. package/src/boot/rumPublicApi.js +16 -4
  38. package/src/domain/replay/record/serializationUtils.js +10 -1
  39. package/src/domain/replay/record/serialize.js +37 -5
  40. package/src/domain/replay/segmentCollection/segment.js +41 -22
  41. package/src/domain/replay/segmentCollection/startDeflateWorker.js +26 -8
  42. package/src/domain/rumSessionManager.js +9 -3
  43. package/src/transport/startRumEventBridge.js +17 -0
  44. package/cjs/domain/replay/segmentCollection/deflateWorker.js +0 -4398
  45. package/esm/domain/replay/segmentCollection/deflateWorker.js +0 -4391
  46. package/src/domain/replay/segmentCollection/deflateWorker.js +0 -4583
package/package.json CHANGED
@@ -1,20 +1,21 @@
1
1
  {
2
2
  "name": "@cloudcare/browser-rum",
3
- "version": "3.0.19",
3
+ "version": "3.0.22",
4
4
  "main": "cjs/index.js",
5
5
  "module": "esm/index.js",
6
6
  "dependencies": {
7
- "@cloudcare/browser-core": "2.0.17"
7
+ "@cloudcare/browser-core": "^3.0.22",
8
+ "@cloudcare/browser-worker": "^3.0.22"
8
9
  },
9
10
  "scripts": {
10
11
  "dev": "webpack serve --open --mode=development",
11
12
  "build": "run-p build:cjs build:esm build:bundle",
12
13
  "build:cjs": "rm -rf cjs && babel --config-file ./babel.cjs.json --out-dir cjs ./src && npm run replace-build-env cjs",
13
14
  "build:esm": "rm -rf esm && babel --config-file ./babel.esm.json --out-dir esm ./src && npm run replace-build-env esm",
14
- "build:bundle": "rm -rf bundle && webpack --config webpack.config.js --mode=production && npm run replace-build-env bundle ",
15
- "replace-build-env": "node scripts/replace-build-env.js",
16
- "publish:oss": "npm run build && node ./scripts/publish-oss.js",
17
- "publish:oss:test": "npm run build && node ./scripts/publish-oss.js test"
15
+ "build:bundle": "rm -rf bundle && webpack --mode=production",
16
+ "replace-build-env": "node ../../scripts/replace-build-env.js",
17
+ "publish:oss": "npm run build:bundle && node ./scripts/publish-oss.js",
18
+ "publish:oss:test": "npm run build:bundle && node ./scripts/publish-oss.js test"
18
19
  },
19
20
  "keywords": [
20
21
  "dataflux",
@@ -29,5 +30,5 @@
29
30
  "license": "MIT",
30
31
  "homepage": "https://github.com/CloudCare/dataflux-rum-sdk-javascript/blob/master/packages/rum/README.md",
31
32
  "description": "DataFlux RUM Web 端数据指标监控",
32
- "gitHead": "ea5e097ee7a211e948b0766be016b50d671e4724"
33
+ "gitHead": "8aedb21df0524848f219d75b79af3239cff88711"
33
34
  }
@@ -1,4 +1,4 @@
1
1
  export var buildEnv = {
2
- sdkVersion: '<<< SDK_VERSION >>>',
2
+ sdkVersion: __BUILD_ENV__SDK_VERSION__,
3
3
  sdkName: 'df_web_rum_sdk'
4
4
  }
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  noop,
3
3
  runOnReadyState,
4
- LifeCycleEventType
4
+ LifeCycleEventType,
5
+ canUseEventBridge
5
6
  } from '@cloudcare/browser-core'
6
7
 
7
8
  import { getReplayStats } from '../domain/replay/replayStats'
@@ -23,7 +24,7 @@ export function makeRecorderApi(startRecordingImpl, startDeflateWorkerImpl) {
23
24
  if (typeof startDeflateWorkerImpl === 'undefined') {
24
25
  startDeflateWorkerImpl = startDeflateWorker
25
26
  }
26
- if (!isBrowserSupported()) {
27
+ if (canUseEventBridge() || !isBrowserSupported()) {
27
28
  return {
28
29
  start: noop,
29
30
  stop: noop,
@@ -110,7 +111,6 @@ export function makeRecorderApi(startRecordingImpl, startDeflateWorkerImpl) {
110
111
  }
111
112
  return
112
113
  }
113
-
114
114
  var recordingImpl = startRecordingImpl(
115
115
  lifeCycle,
116
116
  configuration,
package/src/boot/rum.js CHANGED
@@ -1,9 +1,13 @@
1
- import { startRumSessionManager } from '../domain/rumSessionManager'
1
+ import {
2
+ startRumSessionManager,
3
+ startRumSessionManagerStub
4
+ } from '../domain/rumSessionManager'
2
5
  import { startCacheUsrCache } from '../domain/usr'
3
6
  import {
4
7
  LifeCycle,
5
8
  LifeCycleEventType,
6
- createPageExitObservable
9
+ createPageExitObservable,
10
+ canUseEventBridge
7
11
  } from '@cloudcare/browser-core'
8
12
  import { startPerformanceCollection } from '../domain/performanceCollection'
9
13
  import { createDOMMutationObservable } from '../domain/domMutationCollection'
@@ -11,6 +15,7 @@ import { createLocationChangeObservable } from '../domain/locationChangeObservab
11
15
  import { startLongTaskCollection } from '../domain/rumEventsCollection/longTask/longTaskCollection'
12
16
  import { startActionCollection } from '../domain/rumEventsCollection/actions/actionCollection'
13
17
  import { startRumBatch } from '../transport/startRumBatch'
18
+ import { startRumEventBridge } from '../transport/startRumEventBridge'
14
19
  import { startRumAssembly } from '../domain/assembly'
15
20
  import { startInternalContext } from '../domain/contexts/internalContext'
16
21
  import { startForegroundContexts } from '../domain/contexts/foregroundContexts'
@@ -37,15 +42,22 @@ export function startRum(
37
42
  pageExitObservable.subscribe(function (event) {
38
43
  lifeCycle.notify(LifeCycleEventType.PAGE_EXITED, event)
39
44
  })
40
- var session = startRumSessionManager(configuration, lifeCycle)
41
45
 
42
- var batch = startRumBatch(
43
- configuration,
44
- lifeCycle,
45
- reportError,
46
- pageExitObservable,
47
- session.expireObservable
48
- )
46
+ var session = !canUseEventBridge()
47
+ ? startRumSessionManager(configuration, lifeCycle)
48
+ : startRumSessionManagerStub()
49
+ if (!canUseEventBridge()) {
50
+ var batch = startRumBatch(
51
+ configuration,
52
+ lifeCycle,
53
+ reportError,
54
+ pageExitObservable,
55
+ session.expireObservable
56
+ )
57
+ } else {
58
+ startRumEventBridge(lifeCycle)
59
+ }
60
+
49
61
  var userSession = startCacheUsrCache(configuration)
50
62
  var domMutationObservable = createDOMMutationObservable()
51
63
  var locationChangeObservable = createLocationChangeObservable(location)
@@ -13,7 +13,9 @@ import {
13
13
  createHandlingStack,
14
14
  sanitizeUser,
15
15
  checkUser,
16
- noop
16
+ noop,
17
+ assign,
18
+ canUseEventBridge
17
19
  } from '@cloudcare/browser-core'
18
20
  import { validateAndBuildRumConfiguration } from '../domain/configuration'
19
21
  import { buildCommonContext } from '../domain/contexts/commonContext'
@@ -77,6 +79,13 @@ export function makeRumPublicApi(startRumImpl, recorderApi) {
77
79
  }
78
80
 
79
81
  function initRum(initConfiguration) {
82
+ getInitConfigurationStrategy = function () {
83
+ return deepClone(initConfiguration)
84
+ }
85
+ var eventBridgeAvailable = canUseEventBridge()
86
+ if (eventBridgeAvailable) {
87
+ initConfiguration = overrideInitConfigurationForBridge(initConfiguration)
88
+ }
80
89
  if (!canHandleSession(initConfiguration)) {
81
90
  return
82
91
  }
@@ -104,9 +113,6 @@ export function makeRumPublicApi(startRumImpl, recorderApi) {
104
113
  }
105
114
  beforeInitCalls.drain()
106
115
  }
107
- getInitConfigurationStrategy = function () {
108
- return deepClone(initConfiguration)
109
- }
110
116
 
111
117
  isAlreadyInitialized = true
112
118
  }
@@ -235,4 +241,10 @@ export function makeRumPublicApi(startRumImpl, recorderApi) {
235
241
  function isLocalFile() {
236
242
  return window.location.protocol === 'file:'
237
243
  }
244
+ function overrideInitConfigurationForBridge(initConfiguration) {
245
+ return assign({}, initConfiguration, {
246
+ applicationId: '00000000-aaaa-0000-aaaa-000000000000',
247
+ sessionSampleRate: 100
248
+ })
249
+ }
238
250
  }
@@ -124,7 +124,16 @@ export function makeUrlAbsolute(url, baseUrl) {
124
124
  return url
125
125
  }
126
126
  }
127
+ export function validateStringifiedCssRule(cssStringified) {
128
+ // Safari does not escape selectors with : properly
129
+ if (cssStringified.includes(':')) {
130
+ // Replace e.g. [aa:bb] with [aa\\:bb]
131
+ const regex = /(\[(?:[\w-]+)[^\\])(:(?:[\w-]+)\])/gm
132
+ return cssStringified.replace(regex, '$1\\$2')
133
+ }
127
134
 
135
+ return cssStringified
136
+ }
128
137
  export function serializeStyleSheets(cssStyleSheets) {
129
138
  if (cssStyleSheets === undefined || cssStyleSheets.length === 0) {
130
139
  return undefined
@@ -132,7 +141,7 @@ export function serializeStyleSheets(cssStyleSheets) {
132
141
  return cssStyleSheets.map(function (cssStyleSheet) {
133
142
  var rules = cssStyleSheet.cssRules || cssStyleSheet.rules
134
143
  var cssRules = Array.from(rules, function (cssRule) {
135
- return cssRule.cssText
144
+ return cssRule.cssText ? validateStringifiedCssRule(rule.cssText) : ''
136
145
  })
137
146
 
138
147
  var styleSheet = {
@@ -28,7 +28,8 @@ import {
28
28
  switchToAbsoluteUrl,
29
29
  serializeStyleSheets,
30
30
  absoluteToDoc,
31
- getAbsoluteSrcsetString
31
+ getAbsoluteSrcsetString,
32
+ validateStringifiedCssRule
32
33
  } from './serializationUtils'
33
34
 
34
35
  // Those values are the only one that can be used when inheriting privacy levels from parent to
@@ -337,6 +338,29 @@ function getValidTagName(tagName) {
337
338
 
338
339
  return processedTagName
339
340
  }
341
+ /**
342
+ * Browsers sometimes destructively modify the css rules they receive.
343
+ * This function tries to rectify the modifications the browser made to make it more cross platform compatible.
344
+ * @param cssText - output of `CSSStyleRule.cssText`
345
+ * @returns `cssText` with browser inconsistencies fixed.
346
+ */
347
+ function fixBrowserCompatibilityIssuesInCSS(cssText) {
348
+ /**
349
+ * Chrome outputs `-webkit-background-clip` as `background-clip` in `CSSStyleRule.cssText`.
350
+ * But then Chrome ignores `background-clip` as css input.
351
+ * Re-introduce `-webkit-background-clip` to fix this issue.
352
+ */
353
+ if (
354
+ cssText.includes(' background-clip: text;') &&
355
+ !cssText.includes(' -webkit-background-clip: text;')
356
+ ) {
357
+ cssText = cssText.replace(
358
+ ' background-clip: text;',
359
+ ' -webkit-background-clip: text; background-clip: text;'
360
+ )
361
+ }
362
+ return cssText
363
+ }
340
364
 
341
365
  function getCssRulesString(cssStyleSheet) {
342
366
  if (!cssStyleSheet) {
@@ -351,14 +375,22 @@ function getCssRulesString(cssStyleSheet) {
351
375
  if (!rules) {
352
376
  return null
353
377
  }
354
- var styleSheetCssText = Array.from(rules, getCssRuleString).join('')
378
+ var styleSheetCssText = fixBrowserCompatibilityIssuesInCSS(
379
+ Array.from(rules, getCssRuleString).join('')
380
+ )
355
381
  return switchToAbsoluteUrl(styleSheetCssText, cssStyleSheet.href)
356
382
  }
357
383
 
358
384
  function getCssRuleString(rule) {
359
- return isCSSImportRule(rule)
360
- ? getCssRulesString(rule.styleSheet) || ''
361
- : rule.cssText
385
+ var cssStringified = rule.cssText
386
+ if (isCSSImportRule(rule)) {
387
+ try {
388
+ cssStringified = getCssRulesString(rule.styleSheet) || cssStringified
389
+ } catch {
390
+ // ignore
391
+ }
392
+ }
393
+ return validateStringifiedCssRule(cssStringified)
362
394
  }
363
395
 
364
396
  function isCSSImportRule(rule) {
@@ -1,7 +1,11 @@
1
- import { assign } from '@cloudcare/browser-core'
1
+ import {
2
+ assign,
3
+ addEventListener,
4
+ concatBuffers
5
+ } from '@cloudcare/browser-core'
2
6
  import { RecordType } from '../../../types'
3
7
  import * as replayStats from '../replayStats'
4
-
8
+ var STREAM_ID = 1
5
9
  var nextId = 0
6
10
  export function Segment(
7
11
  worker,
@@ -12,6 +16,7 @@ export function Segment(
12
16
  onFlushed
13
17
  ) {
14
18
  this.worker = worker
19
+ this.pendingWriteCount = 0
15
20
  this.id = nextId++
16
21
  var viewId = context.view.id
17
22
 
@@ -29,19 +34,28 @@ export function Segment(
29
34
  )
30
35
  replayStats.addSegment(viewId)
31
36
  replayStats.addRecord(viewId)
37
+ var rawBytesCount = 0
38
+ var compressedBytesCount = 0
39
+ var compressedData = []
32
40
  var _this = this
33
- var listener = function (params) {
41
+ var wokerListener = addEventListener(worker, 'message', function (params) {
34
42
  var data = params.data
35
- if (data.type === 'errored' || data.type === 'initialized') {
43
+ if (data.type !== 'wrote') {
36
44
  return
37
45
  }
46
+
38
47
  if (data.id === _this.id) {
48
+ _this.pendingWriteCount -= 1
39
49
  replayStats.addWroteData(viewId, data.additionalBytesCount)
40
- if (data.type === 'flushed') {
41
- onFlushed(data.result, data.rawBytesCount)
42
- worker.removeEventListener('message', listener)
50
+ rawBytesCount += data.additionalBytesCount
51
+ compressedBytesCount += data.result.length
52
+ compressedData.push(data.result)
53
+ if (_this.flushReason && _this.pendingWriteCount === 0) {
54
+ compressedData.push(data.trailer)
55
+ onFlushed(concatBuffers(compressedData), rawBytesCount)
56
+ removeMessageListener()
43
57
  } else {
44
- onWrote(data.compressedBytesCount)
58
+ onWrote(compressedBytesCount)
45
59
  }
46
60
  } else if (data.id > _this.id) {
47
61
  // Messages should be received in the same order as they are sent, so if we receive a
@@ -52,15 +66,13 @@ export function Segment(
52
66
  // should already have been removed. But if something goes wrong and we didn't receive a
53
67
  // "flush" response, remove the listener to avoid any leak, and send a monitor message to
54
68
  // help investigate the issue.
55
- worker.removeEventListener('message', listener)
69
+ removeMessageListener()
56
70
  }
57
- }
58
- worker.addEventListener('message', listener)
59
- this.worker.postMessage({
60
- data: '{"records":[' + JSON.stringify(initialRecord),
61
- id: this.id,
62
- action: 'write'
63
71
  })
72
+ var removeMessageListener = wokerListener.stop
73
+ // worker.addEventListener('message', listener)
74
+
75
+ this.write('{"records":[' + JSON.stringify(initialRecord))
64
76
  }
65
77
  Segment.prototype.addRecord = function (record) {
66
78
  this.metadata.start = Math.min(this.metadata.start, record.timestamp)
@@ -70,17 +82,24 @@ Segment.prototype.addRecord = function (record) {
70
82
  if (!this.metadata.has_full_snapshot) {
71
83
  this.metadata.has_full_snapshot = record.type === RecordType.FullSnapshot
72
84
  }
85
+ this.write(',' + JSON.stringify(record))
86
+ }
87
+ Segment.prototype.flush = function (reason) {
88
+ this.write('],' + JSON.stringify(this.metadata).slice(1) + '\n')
89
+
73
90
  this.worker.postMessage({
74
- data: ',' + JSON.stringify(record),
75
- id: this.id,
76
- action: 'write'
91
+ action: 'reset',
92
+ streamId: STREAM_ID
77
93
  })
94
+ this.flushReason = reason
78
95
  }
79
- Segment.prototype.flush = function (reason) {
96
+
97
+ Segment.prototype.write = function (data) {
98
+ this.pendingWriteCount += 1
80
99
  this.worker.postMessage({
81
- data: '],' + JSON.stringify(this.metadata).slice(1) + '\n',
100
+ data: data,
82
101
  id: this.id,
83
- action: 'flush'
102
+ streamId: STREAM_ID,
103
+ action: 'write'
84
104
  })
85
- this.flushReason = reason
86
105
  }
@@ -4,8 +4,16 @@ import {
4
4
  each,
5
5
  addEventListener
6
6
  } from '@cloudcare/browser-core'
7
- import { createDeflateWorker } from './deflateWorker'
7
+ import { workerString } from '@cloudcare/browser-worker/string'
8
+ var workerURL
8
9
 
10
+ export function createDeflateWorker() {
11
+ // Lazily compute the worker URL to allow importing the SDK in NodeJS
12
+ if (!workerURL) {
13
+ workerURL = URL.createObjectURL(new Blob([workerString]))
14
+ }
15
+ return new Worker(workerURL)
16
+ }
9
17
  /**
10
18
  * In order to be sure that the worker is correctly working, we need a round trip of
11
19
  * initialization messages, making the creation asynchronous.
@@ -64,9 +72,9 @@ export function doStartDeflateWorker(createDeflateWorkerImpl) {
64
72
  addEventListener(worker, 'message', function (event) {
65
73
  var data = event.data
66
74
  if (data.type === 'errored') {
67
- onError(data.error)
75
+ onError(data.error, data.streamId)
68
76
  } else if (data.type === 'initialized') {
69
- onInitialized(worker)
77
+ onInitialized(worker, data.version)
70
78
  }
71
79
  })
72
80
  worker.postMessage({ action: 'init' })
@@ -76,16 +84,20 @@ export function doStartDeflateWorker(createDeflateWorkerImpl) {
76
84
  }
77
85
  }
78
86
 
79
- function onInitialized(worker) {
87
+ function onInitialized(worker, version) {
80
88
  if (state.status === DeflateWorkerStatus.Loading) {
81
89
  each(state.callbacks, function (callback) {
82
90
  callback(worker)
83
91
  })
84
- state = { status: DeflateWorkerStatus.Initialized, worker: worker }
92
+ state = {
93
+ status: DeflateWorkerStatus.Initialized,
94
+ worker: worker,
95
+ version: version
96
+ }
85
97
  }
86
98
  }
87
99
 
88
- function onError(error) {
100
+ function onError(error, streamId) {
89
101
  if (state.status === DeflateWorkerStatus.Loading) {
90
102
  display.error(
91
103
  'Session Replay recording failed to start: an error occurred while creating the Worker:',
@@ -93,8 +105,7 @@ function onError(error) {
93
105
  )
94
106
  if (
95
107
  error instanceof Event ||
96
- (error instanceof Error &&
97
- includes(error.message, 'Content Security Policy'))
108
+ (error instanceof Error && isMessageCspRelated(error.message))
98
109
  ) {
99
110
  display.error('Please make sure CSP is correctly configured !!!')
100
111
  }
@@ -104,3 +115,10 @@ function onError(error) {
104
115
  state = { status: DeflateWorkerStatus.Error }
105
116
  }
106
117
  }
118
+ function isMessageCspRelated(message) {
119
+ return (
120
+ includes(message, 'Content Security Policy') ||
121
+ // Related to `require-trusted-types-for` CSP: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/require-trusted-types-for
122
+ includes(message, "requires 'TrustedScriptURL'")
123
+ )
124
+ }
@@ -1,7 +1,9 @@
1
1
  import {
2
2
  performDraw,
3
3
  startSessionManager,
4
- LifeCycleEventType
4
+ LifeCycleEventType,
5
+ noop,
6
+ Observable
5
7
  } from '@cloudcare/browser-core'
6
8
 
7
9
  export var RUM_SESSION_KEY = 'rum'
@@ -64,12 +66,16 @@ export function startRumSessionManager(configuration, lifeCycle) {
64
66
  */
65
67
  export function startRumSessionManagerStub() {
66
68
  var session = {
67
- id: '00000000-aaaa-0000-aaaa-000000000000'
69
+ id: '00000000-aaaa-0000-aaaa-000000000000',
70
+ plan: RumSessionPlan.WITHOUT_SESSION_REPLAY, // plan value should not be taken into account for mobile
71
+ sessionReplayAllowed: false
68
72
  }
69
73
  return {
70
74
  findTrackedSession: function () {
71
75
  return session
72
- }
76
+ },
77
+ expire: noop,
78
+ expireObservable: new Observable()
73
79
  }
74
80
  }
75
81
 
@@ -0,0 +1,17 @@
1
+ import {
2
+ getEventBridge,
3
+ LifeCycleEventType,
4
+ processedMessageByDataMap
5
+ } from '@cloudcare/browser-core'
6
+
7
+ export function startRumEventBridge(lifeCycle) {
8
+ var bridge = getEventBridge()
9
+
10
+ lifeCycle.subscribe(
11
+ LifeCycleEventType.RUM_EVENT_COLLECTED,
12
+ function (serverRumEvent) {
13
+ var data = processedMessageByDataMap(serverRumEvent).rowData
14
+ bridge.send('rum', data)
15
+ }
16
+ )
17
+ }