@cloudcare/browser-core 3.2.11 → 3.2.15

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 (41) hide show
  1. package/cjs/dataMap.js +6 -2
  2. package/cjs/dataMap.js.map +1 -1
  3. package/cjs/helper/lifeCycle.js +1 -0
  4. package/cjs/helper/lifeCycle.js.map +1 -1
  5. package/cjs/helper/limitModification.js +39 -38
  6. package/cjs/helper/limitModification.js.map +1 -1
  7. package/cjs/helper/serialisation/const.js +18 -0
  8. package/cjs/helper/serialisation/const.js.map +1 -0
  9. package/cjs/helper/serialisation/contextManager.js +45 -50
  10. package/cjs/helper/serialisation/contextManager.js.map +1 -1
  11. package/cjs/helper/serialisation/customerDataTracker.js +127 -0
  12. package/cjs/helper/serialisation/customerDataTracker.js.map +1 -0
  13. package/cjs/index.js +16 -4
  14. package/cjs/index.js.map +1 -1
  15. package/esm/dataMap.js +6 -2
  16. package/esm/dataMap.js.map +1 -1
  17. package/esm/helper/lifeCycle.js +1 -0
  18. package/esm/helper/lifeCycle.js.map +1 -1
  19. package/esm/helper/limitModification.js +40 -40
  20. package/esm/helper/limitModification.js.map +1 -1
  21. package/esm/helper/serialisation/const.js +11 -0
  22. package/esm/helper/serialisation/const.js.map +1 -0
  23. package/esm/helper/serialisation/contextManager.js +46 -49
  24. package/esm/helper/serialisation/contextManager.js.map +1 -1
  25. package/esm/helper/serialisation/customerDataTracker.js +116 -0
  26. package/esm/helper/serialisation/customerDataTracker.js.map +1 -0
  27. package/esm/index.js +2 -1
  28. package/esm/index.js.map +1 -1
  29. package/package.json +2 -2
  30. package/src/dataMap.js +6 -2
  31. package/src/helper/lifeCycle.js +1 -0
  32. package/src/helper/limitModification.js +43 -48
  33. package/src/helper/serialisation/const.js +10 -0
  34. package/src/helper/serialisation/contextManager.js +41 -64
  35. package/src/helper/serialisation/customerDataTracker.js +139 -0
  36. package/src/index.js +2 -1
  37. package/cjs/helper/serialisation/heavyCustomerDataWarning.js +0 -29
  38. package/cjs/helper/serialisation/heavyCustomerDataWarning.js.map +0 -1
  39. package/esm/helper/serialisation/heavyCustomerDataWarning.js +0 -21
  40. package/esm/helper/serialisation/heavyCustomerDataWarning.js.map +0 -1
  41. package/src/helper/serialisation/heavyCustomerDataWarning.js +0 -28
@@ -1,94 +1,71 @@
1
- import { computeBytesCount } from '../byteUtils'
2
- import { deepClone, throttle, getType } from '../tools'
1
+ import { deepClone, getType } from '../tools'
3
2
  import { sanitize } from '../sanitize'
4
- import { jsonStringify } from './jsonStringify'
5
3
  import { Observable } from '../observable'
6
- import { warnIfCustomerDataLimitReached } from './heavyCustomerDataWarning'
4
+ import { display } from '../display'
7
5
 
8
- export var BYTES_COMPUTATION_THROTTLING_DELAY = 200
6
+ function ensureProperties(context, propertiesConfig, name) {
7
+ const newContext = { ...context }
9
8
 
10
- export function createContextManager(customerDataType, computeBytesCountImpl) {
11
- if (typeof computeBytesCountImpl === 'undefined') {
12
- computeBytesCountImpl = computeBytesCount
13
- }
14
- var context = {}
15
- var bytesCountCache
16
- var alreadyWarned = false
17
- var changeObservable = new Observable()
18
- // Throttle the bytes computation to minimize the impact on performance.
19
- // Especially useful if the user call context APIs synchronously multiple times in a row
20
- var computeBytesCountThrottled = throttle(function (context) {
21
- bytesCountCache = computeBytesCountImpl(jsonStringify(context))
22
- if (!alreadyWarned) {
23
- alreadyWarned = warnIfCustomerDataLimitReached(
24
- bytesCountCache,
25
- customerDataType
26
- )
9
+ for (const [key, { required, type }] of Object.entries(propertiesConfig)) {
10
+ /**
11
+ * Ensure specified properties are strings as defined here:
12
+ */
13
+ if (type === 'string' && key in newContext) {
14
+ newContext[key] = String(newContext[key])
27
15
  }
28
- }, BYTES_COMPUTATION_THROTTLING_DELAY).throttled
29
-
30
- var contextManager = {
31
- getBytesCount: function () {
32
- return bytesCountCache
33
- },
34
- /** @deprecated use getContext instead */
35
- get: function () {
36
- return context
37
- },
38
16
 
39
- /** @deprecated use setContextProperty instead */
40
- add: function (key, value) {
41
- context[key] = value
42
- computeBytesCountThrottled(context)
43
- changeObservable.notify()
44
- },
17
+ if (required && !(key in context)) {
18
+ display.warn(
19
+ `The property ${key} of ${name} context is required; context will not be sent to the intake.`
20
+ )
21
+ }
22
+ }
45
23
 
46
- /** @deprecated renamed to removeContextProperty */
47
- remove: function (key) {
48
- delete context[key]
49
- computeBytesCountThrottled(context)
50
- changeObservable.notify()
51
- },
24
+ return newContext
25
+ }
52
26
 
53
- /** @deprecated use setContext instead */
54
- set: function (newContext) {
55
- context = newContext
56
- computeBytesCountThrottled(context)
57
- changeObservable.notify()
58
- },
27
+ export function createContextManager(
28
+ name = '',
29
+ { customerDataTracker, propertiesConfig = {} } = {}
30
+ ) {
31
+ let context = {}
32
+ const changeObservable = new Observable()
59
33
 
60
- getContext: function () {
61
- return deepClone(context)
62
- },
34
+ const contextManager = {
35
+ getContext: () => deepClone(context),
63
36
 
64
- setContext: function (newContext) {
37
+ setContext: (newContext) => {
65
38
  if (getType(newContext) === 'object') {
66
- context = sanitize(newContext)
67
- computeBytesCountThrottled(context)
39
+ context = sanitize(ensureProperties(newContext, propertiesConfig, name))
40
+ customerDataTracker?.updateCustomerData(context)
68
41
  } else {
69
42
  contextManager.clearContext()
70
43
  }
71
44
  changeObservable.notify()
72
45
  },
73
46
 
74
- setContextProperty: function (key, property) {
75
- context[key] = deepClone(property)
76
- computeBytesCountThrottled(context)
47
+ setContextProperty: (key, property) => {
48
+ context[key] = sanitize(
49
+ ensureProperties({ [key]: property }, propertiesConfig, name)[key]
50
+ )
51
+ customerDataTracker?.updateCustomerData(context)
77
52
  changeObservable.notify()
78
53
  },
79
54
 
80
- removeContextProperty: function (key) {
55
+ removeContextProperty: (key) => {
81
56
  delete context[key]
82
- computeBytesCountThrottled(context)
57
+ customerDataTracker?.updateCustomerData(context)
58
+ ensureProperties(context, propertiesConfig, name)
83
59
  changeObservable.notify()
84
60
  },
85
61
 
86
- clearContext: function () {
62
+ clearContext: () => {
87
63
  context = {}
88
- bytesCountCache = 0
64
+ customerDataTracker?.resetCustomerData()
89
65
  changeObservable.notify()
90
66
  },
91
- changeObservable: changeObservable
67
+
68
+ changeObservable
92
69
  }
93
70
  return contextManager
94
71
  }
@@ -0,0 +1,139 @@
1
+ import { ONE_KIBI_BYTE, computeBytesCount } from '../byteUtils'
2
+ import { throttle, isEmptyObject } from '../tools'
3
+ import { jsonStringify } from './jsonStringify'
4
+ import { display } from '../display'
5
+
6
+ // RUM and logs batch bytes limit is 16KB
7
+ // ensure that we leave room for other event attributes and maintain a decent amount of event per batch
8
+ // (3KB (customer data) + 1KB (other attributes)) * 4 (events per batch) = 16KB
9
+ export const CUSTOMER_DATA_BYTES_LIMIT = 3 * ONE_KIBI_BYTE
10
+
11
+ // We observed that the compression ratio is around 8 in general, but we also want to keep a margin
12
+ // because some data might not be compressed (ex: last view update on page exit). We chose 16KiB
13
+ // because it is also the limit of the 'batchBytesCount' that we use for RUM and Logs data, but this
14
+ // is a bit arbitrary.
15
+ export const CUSTOMER_COMPRESSED_DATA_BYTES_LIMIT = 16 * ONE_KIBI_BYTE
16
+
17
+ export const BYTES_COMPUTATION_THROTTLING_DELAY = 200
18
+
19
+ export const CustomerDataCompressionStatus = {
20
+ Unknown: 0,
21
+ Enabled: 1,
22
+ Disabled: 2
23
+ }
24
+
25
+ export function createCustomerDataTrackerManager(
26
+ compressionStatus = CustomerDataCompressionStatus.Disabled
27
+ ) {
28
+ const customerDataTrackers = new Map()
29
+
30
+ let alreadyWarned = false
31
+ function checkCustomerDataLimit(initialBytesCount = 0) {
32
+ if (
33
+ alreadyWarned ||
34
+ compressionStatus === CustomerDataCompressionStatus.Unknown
35
+ ) {
36
+ return
37
+ }
38
+
39
+ const bytesCountLimit =
40
+ compressionStatus === CustomerDataCompressionStatus.Disabled
41
+ ? CUSTOMER_DATA_BYTES_LIMIT
42
+ : CUSTOMER_COMPRESSED_DATA_BYTES_LIMIT
43
+
44
+ let bytesCount = initialBytesCount
45
+ customerDataTrackers.forEach((tracker) => {
46
+ bytesCount += tracker.getBytesCount()
47
+ })
48
+
49
+ if (bytesCount > bytesCountLimit) {
50
+ displayCustomerDataLimitReachedWarning(bytesCountLimit)
51
+ alreadyWarned = true
52
+ }
53
+ }
54
+
55
+ return {
56
+ /**
57
+ * Creates a detached tracker. The manager will not store a reference to that tracker, and the
58
+ * bytes count will be counted independently from other detached trackers.
59
+ *
60
+ * This is particularly useful when we don't know when the tracker will be unused, so we don't
61
+ * leak memory (ex: when used in Logger instances).
62
+ */
63
+ createDetachedTracker: () => {
64
+ const tracker = createCustomerDataTracker(() =>
65
+ checkCustomerDataLimit(tracker.getBytesCount())
66
+ )
67
+ return tracker
68
+ },
69
+
70
+ /**
71
+ * Creates a tracker if it doesn't exist, and returns it.
72
+ */
73
+ getOrCreateTracker: (type) => {
74
+ if (!customerDataTrackers.has(type)) {
75
+ customerDataTrackers.set(
76
+ type,
77
+ createCustomerDataTracker(checkCustomerDataLimit)
78
+ )
79
+ }
80
+ return customerDataTrackers.get(type)
81
+ },
82
+
83
+ setCompressionStatus: (newCompressionStatus) => {
84
+ if (compressionStatus === CustomerDataCompressionStatus.Unknown) {
85
+ compressionStatus = newCompressionStatus
86
+ checkCustomerDataLimit()
87
+ }
88
+ },
89
+
90
+ getCompressionStatus: () => compressionStatus,
91
+
92
+ stop: () => {
93
+ customerDataTrackers.forEach((tracker) => tracker.stop())
94
+ customerDataTrackers.clear()
95
+ }
96
+ }
97
+ }
98
+
99
+ export function createCustomerDataTracker(checkCustomerDataLimit) {
100
+ let bytesCountCache = 0
101
+
102
+ // Throttle the bytes computation to minimize the impact on performance.
103
+ // Especially useful if the user call context APIs synchronously multiple times in a row
104
+ const {
105
+ throttled: computeBytesCountThrottled,
106
+ cancel: cancelComputeBytesCount
107
+ } = throttle((context) => {
108
+ bytesCountCache = computeBytesCount(jsonStringify(context))
109
+ checkCustomerDataLimit()
110
+ }, BYTES_COMPUTATION_THROTTLING_DELAY)
111
+
112
+ const resetBytesCount = () => {
113
+ cancelComputeBytesCount()
114
+ bytesCountCache = 0
115
+ }
116
+
117
+ return {
118
+ updateCustomerData: (context) => {
119
+ if (isEmptyObject(context)) {
120
+ resetBytesCount()
121
+ } else {
122
+ computeBytesCountThrottled(context)
123
+ }
124
+ },
125
+ resetCustomerData: resetBytesCount,
126
+ getBytesCount: () => bytesCountCache,
127
+ stop: () => {
128
+ cancelComputeBytesCount()
129
+ }
130
+ }
131
+ }
132
+
133
+ function displayCustomerDataLimitReachedWarning(bytesCountLimit) {
134
+ display.warn(
135
+ `Customer data exceeds the recommended ${
136
+ bytesCountLimit / ONE_KIBI_BYTE
137
+ }KiB threshold.`
138
+ )
139
+ }
package/src/index.js CHANGED
@@ -51,10 +51,11 @@ export * from './transport'
51
51
  export * from './synthetics/syntheticsWorkerValues'
52
52
 
53
53
  export * from './helper/serialisation/contextManager'
54
- export * from './helper/serialisation/heavyCustomerDataWarning'
54
+ export * from './helper/serialisation/const'
55
55
  export * from './helper/serialisation/jsonStringify'
56
56
  export * from './helper/serialisation/rowData'
57
57
  export * from './helper/serialisation/storedContextManager'
58
+ export * from './helper/serialisation/customerDataTracker'
58
59
  export * from './helper/encoder'
59
60
  export * from './user'
60
61
  export * from './helper/polyfills'
@@ -1,29 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.CustomerDataType = exports.CUSTOMER_DATA_BYTES_LIMIT = void 0;
7
- exports.warnIfCustomerDataLimitReached = warnIfCustomerDataLimitReached;
8
- var _byteUtils = require("../byteUtils");
9
- var _display = require("../display");
10
- // RUM and logs batch bytes limit is 16KB
11
- // ensure that we leave room for other event attributes and maintain a decent amount of event per batch
12
- // (3KB (customer data) + 1KB (other attributes)) * 4 (events per batch) = 16KB
13
- var CUSTOMER_DATA_BYTES_LIMIT = 3 * _byteUtils.ONE_KIBI_BYTE;
14
- exports.CUSTOMER_DATA_BYTES_LIMIT = CUSTOMER_DATA_BYTES_LIMIT;
15
- var CustomerDataType = {
16
- FeatureFlag: 'feature flag evaluation',
17
- User: 'user',
18
- GlobalContext: 'global context',
19
- LoggerContext: 'logger context'
20
- };
21
- exports.CustomerDataType = CustomerDataType;
22
- function warnIfCustomerDataLimitReached(bytesCount, customerDataType) {
23
- if (bytesCount > CUSTOMER_DATA_BYTES_LIMIT) {
24
- _display.display.warn('The ' + customerDataType + 'data is over ' + CUSTOMER_DATA_BYTES_LIMIT / _byteUtils.ONE_KIBI_BYTE + " KiB. On low connectivity, the SDK has the potential to exhaust the user's upload bandwidth.");
25
- return true;
26
- }
27
- return false;
28
- }
29
- //# sourceMappingURL=heavyCustomerDataWarning.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"heavyCustomerDataWarning.js","names":["_byteUtils","require","_display","CUSTOMER_DATA_BYTES_LIMIT","ONE_KIBI_BYTE","exports","CustomerDataType","FeatureFlag","User","GlobalContext","LoggerContext","warnIfCustomerDataLimitReached","bytesCount","customerDataType","display","warn"],"sources":["../../../src/helper/serialisation/heavyCustomerDataWarning.js"],"sourcesContent":["import { ONE_KIBI_BYTE } from '../byteUtils'\nimport { display } from '../display'\n\n// RUM and logs batch bytes limit is 16KB\n// ensure that we leave room for other event attributes and maintain a decent amount of event per batch\n// (3KB (customer data) + 1KB (other attributes)) * 4 (events per batch) = 16KB\nexport var CUSTOMER_DATA_BYTES_LIMIT = 3 * ONE_KIBI_BYTE\n\nexport var CustomerDataType = {\n FeatureFlag: 'feature flag evaluation',\n User: 'user',\n GlobalContext: 'global context',\n LoggerContext: 'logger context'\n}\n\nexport function warnIfCustomerDataLimitReached(bytesCount, customerDataType) {\n if (bytesCount > CUSTOMER_DATA_BYTES_LIMIT) {\n display.warn(\n 'The ' +\n customerDataType +\n 'data is over ' +\n CUSTOMER_DATA_BYTES_LIMIT / ONE_KIBI_BYTE +\n \" KiB. On low connectivity, the SDK has the potential to exhaust the user's upload bandwidth.\"\n )\n return true\n }\n return false\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,UAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AAEA;AACA;AACA;AACO,IAAIE,yBAAyB,GAAG,CAAC,GAAGC,wBAAa;AAAAC,OAAA,CAAAF,yBAAA,GAAAA,yBAAA;AAEjD,IAAIG,gBAAgB,GAAG;EAC5BC,WAAW,EAAE,yBAAyB;EACtCC,IAAI,EAAE,MAAM;EACZC,aAAa,EAAE,gBAAgB;EAC/BC,aAAa,EAAE;AACjB,CAAC;AAAAL,OAAA,CAAAC,gBAAA,GAAAA,gBAAA;AAEM,SAASK,8BAA8BA,CAACC,UAAU,EAAEC,gBAAgB,EAAE;EAC3E,IAAID,UAAU,GAAGT,yBAAyB,EAAE;IAC1CW,gBAAO,CAACC,IAAI,CACV,MAAM,GACJF,gBAAgB,GAChB,eAAe,GACfV,yBAAyB,GAAGC,wBAAa,GACzC,8FACJ,CAAC;IACD,OAAO,IAAI;EACb;EACA,OAAO,KAAK;AACd","ignoreList":[]}
@@ -1,21 +0,0 @@
1
- import { ONE_KIBI_BYTE } from '../byteUtils';
2
- import { display } from '../display';
3
-
4
- // RUM and logs batch bytes limit is 16KB
5
- // ensure that we leave room for other event attributes and maintain a decent amount of event per batch
6
- // (3KB (customer data) + 1KB (other attributes)) * 4 (events per batch) = 16KB
7
- export var CUSTOMER_DATA_BYTES_LIMIT = 3 * ONE_KIBI_BYTE;
8
- export var CustomerDataType = {
9
- FeatureFlag: 'feature flag evaluation',
10
- User: 'user',
11
- GlobalContext: 'global context',
12
- LoggerContext: 'logger context'
13
- };
14
- export function warnIfCustomerDataLimitReached(bytesCount, customerDataType) {
15
- if (bytesCount > CUSTOMER_DATA_BYTES_LIMIT) {
16
- display.warn('The ' + customerDataType + 'data is over ' + CUSTOMER_DATA_BYTES_LIMIT / ONE_KIBI_BYTE + " KiB. On low connectivity, the SDK has the potential to exhaust the user's upload bandwidth.");
17
- return true;
18
- }
19
- return false;
20
- }
21
- //# sourceMappingURL=heavyCustomerDataWarning.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"heavyCustomerDataWarning.js","names":["ONE_KIBI_BYTE","display","CUSTOMER_DATA_BYTES_LIMIT","CustomerDataType","FeatureFlag","User","GlobalContext","LoggerContext","warnIfCustomerDataLimitReached","bytesCount","customerDataType","warn"],"sources":["../../../src/helper/serialisation/heavyCustomerDataWarning.js"],"sourcesContent":["import { ONE_KIBI_BYTE } from '../byteUtils'\nimport { display } from '../display'\n\n// RUM and logs batch bytes limit is 16KB\n// ensure that we leave room for other event attributes and maintain a decent amount of event per batch\n// (3KB (customer data) + 1KB (other attributes)) * 4 (events per batch) = 16KB\nexport var CUSTOMER_DATA_BYTES_LIMIT = 3 * ONE_KIBI_BYTE\n\nexport var CustomerDataType = {\n FeatureFlag: 'feature flag evaluation',\n User: 'user',\n GlobalContext: 'global context',\n LoggerContext: 'logger context'\n}\n\nexport function warnIfCustomerDataLimitReached(bytesCount, customerDataType) {\n if (bytesCount > CUSTOMER_DATA_BYTES_LIMIT) {\n display.warn(\n 'The ' +\n customerDataType +\n 'data is over ' +\n CUSTOMER_DATA_BYTES_LIMIT / ONE_KIBI_BYTE +\n \" KiB. On low connectivity, the SDK has the potential to exhaust the user's upload bandwidth.\"\n )\n return true\n }\n return false\n}\n"],"mappings":"AAAA,SAASA,aAAa,QAAQ,cAAc;AAC5C,SAASC,OAAO,QAAQ,YAAY;;AAEpC;AACA;AACA;AACA,OAAO,IAAIC,yBAAyB,GAAG,CAAC,GAAGF,aAAa;AAExD,OAAO,IAAIG,gBAAgB,GAAG;EAC5BC,WAAW,EAAE,yBAAyB;EACtCC,IAAI,EAAE,MAAM;EACZC,aAAa,EAAE,gBAAgB;EAC/BC,aAAa,EAAE;AACjB,CAAC;AAED,OAAO,SAASC,8BAA8BA,CAACC,UAAU,EAAEC,gBAAgB,EAAE;EAC3E,IAAID,UAAU,GAAGP,yBAAyB,EAAE;IAC1CD,OAAO,CAACU,IAAI,CACV,MAAM,GACJD,gBAAgB,GAChB,eAAe,GACfR,yBAAyB,GAAGF,aAAa,GACzC,8FACJ,CAAC;IACD,OAAO,IAAI;EACb;EACA,OAAO,KAAK;AACd","ignoreList":[]}
@@ -1,28 +0,0 @@
1
- import { ONE_KIBI_BYTE } from '../byteUtils'
2
- import { display } from '../display'
3
-
4
- // RUM and logs batch bytes limit is 16KB
5
- // ensure that we leave room for other event attributes and maintain a decent amount of event per batch
6
- // (3KB (customer data) + 1KB (other attributes)) * 4 (events per batch) = 16KB
7
- export var CUSTOMER_DATA_BYTES_LIMIT = 3 * ONE_KIBI_BYTE
8
-
9
- export var CustomerDataType = {
10
- FeatureFlag: 'feature flag evaluation',
11
- User: 'user',
12
- GlobalContext: 'global context',
13
- LoggerContext: 'logger context'
14
- }
15
-
16
- export function warnIfCustomerDataLimitReached(bytesCount, customerDataType) {
17
- if (bytesCount > CUSTOMER_DATA_BYTES_LIMIT) {
18
- display.warn(
19
- 'The ' +
20
- customerDataType +
21
- 'data is over ' +
22
- CUSTOMER_DATA_BYTES_LIMIT / ONE_KIBI_BYTE +
23
- " KiB. On low connectivity, the SDK has the potential to exhaust the user's upload bandwidth."
24
- )
25
- return true
26
- }
27
- return false
28
- }