@cloudcare/browser-core 1.0.29 → 1.1.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.
@@ -8,7 +8,9 @@ exports.UUID = UUID;
8
8
  exports.addEvent = exports._URL = void 0;
9
9
  exports.addEventListener = addEventListener;
10
10
  exports.addEventListeners = addEventListeners;
11
- exports.base64Encode = exports.autoExeQueue = exports.ajax = exports.addSinglePageEvent = exports.addHashEvent = void 0;
11
+ exports.ajax = exports.addSinglePageEvent = exports.addHashEvent = void 0;
12
+ exports.assign = assign;
13
+ exports.base64Encode = exports.autoExeQueue = void 0;
12
14
  exports.clocksNow = clocksNow;
13
15
  exports.clocksOrigin = clocksOrigin;
14
16
  exports.coverExtend = exports.cookie = void 0;
@@ -58,7 +60,10 @@ exports.replaceNumberCharByPath = replaceNumberCharByPath;
58
60
  exports.round = round;
59
61
  exports.safeJSONParse = void 0;
60
62
  exports.safeTruncate = safeTruncate;
61
- exports.throttle = exports.strip_sa_properties = exports.strip_empty_properties = exports.stringSplice = exports.strToUnicode = exports.sessionStorage = exports.searchObjString = exports.searchObjDate = exports.searchConfigData = void 0;
63
+ exports.sessionStorage = exports.searchObjString = exports.searchObjDate = exports.searchConfigData = void 0;
64
+ exports.shallowClone = shallowClone;
65
+ exports.strip_empty_properties = exports.stringSplice = exports.strToUnicode = void 0;
66
+ exports.throttle = exports.strip_sa_properties = void 0;
62
67
  exports.timeStampNow = timeStampNow;
63
68
  exports.toArray = exports.tirm = void 0;
64
69
  exports.toServerDuration = toServerDuration;
@@ -105,6 +110,21 @@ var each = function each(obj, iterator, context) {
105
110
 
106
111
  exports.each = each;
107
112
 
113
+ function assign(target) {
114
+ each(slice.call(arguments, 1), function (source) {
115
+ for (var prop in source) {
116
+ if (Object.prototype.hasOwnProperty.call(source, prop)) {
117
+ target[prop] = source[prop];
118
+ }
119
+ }
120
+ });
121
+ return target;
122
+ }
123
+
124
+ function shallowClone(object) {
125
+ return assign({}, object);
126
+ }
127
+
108
128
  var extend = function extend(obj) {
109
129
  each(slice.call(arguments, 1), function (source) {
110
130
  for (var prop in source) {
package/cjs/transport.js CHANGED
@@ -11,6 +11,8 @@ var _enums = require("./helper/enums");
11
11
 
12
12
  var _dataMap = require("./dataMap");
13
13
 
14
+ var _init = require("./init");
15
+
14
16
  // https://en.wikipedia.org/wiki/UTF-8
15
17
  var HAS_MULTI_BYTES_CHARACTERS = /[^\u0000-\u007F]/;
16
18
  var CUSTOM_KEYS = 'custom_keys';
@@ -20,27 +22,37 @@ function addBatchPrecision(url) {
20
22
  return url + (url.indexOf('?') === -1 ? '?' : '&') + 'precision=ms';
21
23
  }
22
24
 
25
+ var global = (0, _init.getGlobalObject)();
26
+
23
27
  var httpRequest = function httpRequest(endpointUrl, bytesLimit, isLineProtocolToJson) {
24
28
  this.endpointUrl = endpointUrl;
25
29
  this.bytesLimit = bytesLimit;
26
30
  this.isLineProtocolToJson = isLineProtocolToJson;
31
+ var isRealNavigator = Object.prototype.toString.call(global && global.navigator) === '[object Navigator]';
32
+ var hasSendBeacon = isRealNavigator && typeof global.navigator.sendBeacon === 'function';
33
+
34
+ if (hasSendBeacon) {
35
+ this.sendBeacon = global.navigator.sendBeacon.bind(global.navigator);
36
+ } else {
37
+ this.sendBeacon = undefined;
38
+ }
27
39
  };
28
40
 
29
41
  httpRequest.prototype = {
30
42
  send: function send(data, size) {
31
43
  var url = addBatchPrecision(this.endpointUrl);
32
44
 
33
- if (navigator.sendBeacon && size < this.bytesLimit && !this.isLineProtocolToJson) {
34
- var isQueued = navigator.sendBeacon(url, data);
45
+ if (this.sendBeacon && size < this.bytesLimit && !this.isLineProtocolToJson) {
46
+ var isQueued = this.sendBeacon(url, data);
35
47
 
36
48
  if (isQueued) {
37
49
  return;
38
50
  }
39
- } else if (navigator.sendBeacon && size < this.bytesLimit && this.isLineProtocolToJson) {
51
+ } else if (this.sendBeacon && size < this.bytesLimit && this.isLineProtocolToJson) {
40
52
  var blob = new Blob([JSON.stringify(data)], {
41
53
  type: 'application/json'
42
54
  });
43
- var isQueued = navigator.sendBeacon(url, blob);
55
+ var isQueued = this.sendBeacon(url, blob);
44
56
 
45
57
  if (isQueued) {
46
58
  return;
@@ -176,20 +188,21 @@ var processedMessageByDataMap = function processedMessageByDataMap(message) {
176
188
 
177
189
  exports.processedMessageByDataMap = processedMessageByDataMap;
178
190
 
179
- function batch(request, maxSize, bytesLimit, maxMessageSize, flushTimeout, isLineProtocolToJson, beforeUnloadCallback) {
191
+ var batch = function batch(request, maxSize, bytesLimit, maxMessageSize, flushTimeout, isLineProtocolToJson, beforeUnloadCallback) {
180
192
  this.request = request;
181
193
  this.maxSize = maxSize;
182
194
  this.bytesLimit = bytesLimit;
183
195
  this.maxMessageSize = maxMessageSize;
184
196
  this.flushTimeout = flushTimeout;
185
- this.isLineProtocolToJson = isLineProtocolToJson, this.beforeUnloadCallback = beforeUnloadCallback;
197
+ this.isLineProtocolToJson = isLineProtocolToJson;
198
+ this.beforeUnloadCallback = beforeUnloadCallback;
186
199
  this.pushOnlyBuffer = [];
187
200
  this.upsertBuffer = {};
188
201
  this.bufferBytesSize = 0;
189
202
  this.bufferMessageCount = 0;
190
203
  this.flushOnVisibilityHidden();
191
204
  this.flushPeriodically();
192
- }
205
+ };
193
206
 
194
207
  batch.prototype = {
195
208
  add: function add(message) {
@@ -321,7 +334,7 @@ batch.prototype = {
321
334
  // @ts-ignore this function is not always defined
322
335
 
323
336
 
324
- if (navigator.sendBeacon) {
337
+ if (global.navigator.sendBeacon) {
325
338
  /**
326
339
  * beforeunload is called before visibilitychange
327
340
  * register first to be sure to be called before flush on beforeunload
@@ -30,6 +30,19 @@ export var each = function each(obj, iterator, context) {
30
30
  }
31
31
  }
32
32
  };
33
+ export function assign(target) {
34
+ each(slice.call(arguments, 1), function (source) {
35
+ for (var prop in source) {
36
+ if (Object.prototype.hasOwnProperty.call(source, prop)) {
37
+ target[prop] = source[prop];
38
+ }
39
+ }
40
+ });
41
+ return target;
42
+ }
43
+ export function shallowClone(object) {
44
+ return assign({}, object);
45
+ }
33
46
  export var extend = function extend(obj) {
34
47
  each(slice.call(arguments, 1), function (source) {
35
48
  for (var prop in source) {
package/esm/transport.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { addEventListener, values, findByPath, escapeRowData, each, isNumber, isArray, extend, isString, toServerDuration, isBoolean, isEmptyObject, isObject, map, escapeJsonValue } from './helper/tools';
2
2
  import { DOM_EVENT, RumEventType } from './helper/enums';
3
- import { commonTags, dataMap } from './dataMap'; // https://en.wikipedia.org/wiki/UTF-8
3
+ import { commonTags, dataMap } from './dataMap';
4
+ import { getGlobalObject } from './init'; // https://en.wikipedia.org/wiki/UTF-8
4
5
 
5
6
  var HAS_MULTI_BYTES_CHARACTERS = /[^\u0000-\u007F]/;
6
7
  var CUSTOM_KEYS = 'custom_keys';
@@ -10,27 +11,37 @@ function addBatchPrecision(url) {
10
11
  return url + (url.indexOf('?') === -1 ? '?' : '&') + 'precision=ms';
11
12
  }
12
13
 
14
+ var global = getGlobalObject();
15
+
13
16
  var httpRequest = function httpRequest(endpointUrl, bytesLimit, isLineProtocolToJson) {
14
17
  this.endpointUrl = endpointUrl;
15
18
  this.bytesLimit = bytesLimit;
16
19
  this.isLineProtocolToJson = isLineProtocolToJson;
20
+ var isRealNavigator = Object.prototype.toString.call(global && global.navigator) === '[object Navigator]';
21
+ var hasSendBeacon = isRealNavigator && typeof global.navigator.sendBeacon === 'function';
22
+
23
+ if (hasSendBeacon) {
24
+ this.sendBeacon = global.navigator.sendBeacon.bind(global.navigator);
25
+ } else {
26
+ this.sendBeacon = undefined;
27
+ }
17
28
  };
18
29
 
19
30
  httpRequest.prototype = {
20
31
  send: function send(data, size) {
21
32
  var url = addBatchPrecision(this.endpointUrl);
22
33
 
23
- if (navigator.sendBeacon && size < this.bytesLimit && !this.isLineProtocolToJson) {
24
- var isQueued = navigator.sendBeacon(url, data);
34
+ if (this.sendBeacon && size < this.bytesLimit && !this.isLineProtocolToJson) {
35
+ var isQueued = this.sendBeacon(url, data);
25
36
 
26
37
  if (isQueued) {
27
38
  return;
28
39
  }
29
- } else if (navigator.sendBeacon && size < this.bytesLimit && this.isLineProtocolToJson) {
40
+ } else if (this.sendBeacon && size < this.bytesLimit && this.isLineProtocolToJson) {
30
41
  var blob = new Blob([JSON.stringify(data)], {
31
42
  type: 'application/json'
32
43
  });
33
- var isQueued = navigator.sendBeacon(url, blob);
44
+ var isQueued = this.sendBeacon(url, blob);
34
45
 
35
46
  if (isQueued) {
36
47
  return;
@@ -162,20 +173,21 @@ export var processedMessageByDataMap = function processedMessageByDataMap(messag
162
173
  };
163
174
  };
164
175
 
165
- function batch(request, maxSize, bytesLimit, maxMessageSize, flushTimeout, isLineProtocolToJson, beforeUnloadCallback) {
176
+ var batch = function batch(request, maxSize, bytesLimit, maxMessageSize, flushTimeout, isLineProtocolToJson, beforeUnloadCallback) {
166
177
  this.request = request;
167
178
  this.maxSize = maxSize;
168
179
  this.bytesLimit = bytesLimit;
169
180
  this.maxMessageSize = maxMessageSize;
170
181
  this.flushTimeout = flushTimeout;
171
- this.isLineProtocolToJson = isLineProtocolToJson, this.beforeUnloadCallback = beforeUnloadCallback;
182
+ this.isLineProtocolToJson = isLineProtocolToJson;
183
+ this.beforeUnloadCallback = beforeUnloadCallback;
172
184
  this.pushOnlyBuffer = [];
173
185
  this.upsertBuffer = {};
174
186
  this.bufferBytesSize = 0;
175
187
  this.bufferMessageCount = 0;
176
188
  this.flushOnVisibilityHidden();
177
189
  this.flushPeriodically();
178
- }
190
+ };
179
191
 
180
192
  batch.prototype = {
181
193
  add: function add(message) {
@@ -307,7 +319,7 @@ batch.prototype = {
307
319
  // @ts-ignore this function is not always defined
308
320
 
309
321
 
310
- if (navigator.sendBeacon) {
322
+ if (global.navigator.sendBeacon) {
311
323
  /**
312
324
  * beforeunload is called before visibilitychange
313
325
  * register first to be sure to be called before flush on beforeunload
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudcare/browser-core",
3
- "version": "1.0.29",
3
+ "version": "1.1.0",
4
4
  "main": "cjs/index.js",
5
5
  "module": "esm/index.js",
6
6
  "scripts": {
@@ -20,5 +20,5 @@
20
20
  "author": "dataflux",
21
21
  "license": "MIT",
22
22
  "description": "DataFlux RUM Web 端数据指标监控",
23
- "gitHead": "34770c27f33e4fbaaafc8743aa5e9f5ffd14bc14"
23
+ "gitHead": "f952b9eeba6f5cfe85fe0073700a35cfdc35454b"
24
24
  }
@@ -29,6 +29,20 @@ export var each = function (obj, iterator, context) {
29
29
  }
30
30
  }
31
31
  }
32
+ export function assign(target) {
33
+ each(slice.call(arguments, 1), function (source) {
34
+ for (var prop in source) {
35
+ if (Object.prototype.hasOwnProperty.call(source, prop)) {
36
+ target[prop] = source[prop]
37
+ }
38
+ }
39
+ })
40
+ return target
41
+ }
42
+
43
+ export function shallowClone(object){
44
+ return assign({}, object)
45
+ }
32
46
  export var extend = function (obj) {
33
47
  each(slice.call(arguments, 1), function (source) {
34
48
  for (var prop in source) {
@@ -1709,4 +1723,4 @@ export function escapeJsonValue(value) {
1709
1723
  } else {
1710
1724
  return jsonStringify(value)
1711
1725
  }
1712
- }
1726
+ }
package/src/transport.js CHANGED
@@ -17,6 +17,7 @@ import {
17
17
  } from './helper/tools'
18
18
  import { DOM_EVENT, RumEventType } from './helper/enums'
19
19
  import { commonTags, dataMap } from './dataMap'
20
+ import { getGlobalObject } from'./init'
20
21
  // https://en.wikipedia.org/wiki/UTF-8
21
22
  var HAS_MULTI_BYTES_CHARACTERS = /[^\u0000-\u007F]/
22
23
  var CUSTOM_KEYS = 'custom_keys'
@@ -24,29 +25,40 @@ function addBatchPrecision(url) {
24
25
  if (!url) return url
25
26
  return url + (url.indexOf('?') === -1 ? '?' : '&') + 'precision=ms'
26
27
  }
28
+ var global = getGlobalObject()
27
29
  var httpRequest = function (endpointUrl, bytesLimit, isLineProtocolToJson) {
28
30
  this.endpointUrl = endpointUrl
29
31
  this.bytesLimit = bytesLimit
30
32
  this.isLineProtocolToJson = isLineProtocolToJson
33
+ var isRealNavigator = Object.prototype.toString.call(global && global.navigator) === '[object Navigator]';
34
+ var hasSendBeacon = isRealNavigator && typeof global.navigator.sendBeacon === 'function';
35
+ if (hasSendBeacon) {
36
+ this.sendBeacon = global.navigator.sendBeacon.bind(global.navigator);
37
+ } else {
38
+ this.sendBeacon = undefined
39
+ }
40
+
31
41
  }
32
42
  httpRequest.prototype = {
33
43
  send: function (data, size) {
34
44
 
35
45
  var url = addBatchPrecision(this.endpointUrl)
36
- if (navigator.sendBeacon && size < this.bytesLimit && !this.isLineProtocolToJson) {
37
- var isQueued = navigator.sendBeacon(url, data)
46
+
47
+ if (this.sendBeacon && size < this.bytesLimit && !this.isLineProtocolToJson) {
48
+ var isQueued = this.sendBeacon(url, data)
38
49
  if (isQueued) {
39
- return
50
+ return
40
51
  }
41
- } else if (navigator.sendBeacon && size < this.bytesLimit && this.isLineProtocolToJson) {
52
+ } else if (this.sendBeacon && size < this.bytesLimit && this.isLineProtocolToJson) {
42
53
  const blob = new Blob([JSON.stringify(data)], {
43
54
  type: 'application/json',
44
55
  });
45
- var isQueued = navigator.sendBeacon(url, blob)
56
+ var isQueued = this.sendBeacon(url, blob)
46
57
  if (isQueued) {
47
58
  return
48
59
  }
49
60
  }
61
+
50
62
  var request = new XMLHttpRequest()
51
63
  request.open('POST', url, true)
52
64
  request.withCredentials = true
@@ -54,6 +66,7 @@ httpRequest.prototype = {
54
66
  request.setRequestHeader('Content-type', 'application/json')
55
67
  request.send(JSON.stringify(data))
56
68
  } else {
69
+
57
70
  request.setRequestHeader('Content-type', 'text/plain;charset=UTF-8')
58
71
  request.send(data)
59
72
  }
@@ -165,7 +178,7 @@ export var processedMessageByDataMap = function (message) {
165
178
  rowData: hasFileds ? rowData : undefined
166
179
  }
167
180
  }
168
- function batch(
181
+ var batch = function(
169
182
  request,
170
183
  maxSize,
171
184
  bytesLimit,
@@ -174,30 +187,37 @@ function batch(
174
187
  isLineProtocolToJson,
175
188
  beforeUnloadCallback
176
189
  ) {
190
+
177
191
  this.request = request
178
192
  this.maxSize = maxSize
179
193
  this.bytesLimit = bytesLimit
180
194
  this.maxMessageSize = maxMessageSize
181
195
  this.flushTimeout = flushTimeout
182
- this.isLineProtocolToJson = isLineProtocolToJson,
196
+ this.isLineProtocolToJson = isLineProtocolToJson
183
197
  this.beforeUnloadCallback = beforeUnloadCallback
198
+
184
199
  this.pushOnlyBuffer = []
185
200
  this.upsertBuffer = {}
186
201
  this.bufferBytesSize = 0
187
202
  this.bufferMessageCount = 0
203
+
188
204
  this.flushOnVisibilityHidden()
189
205
  this.flushPeriodically()
190
206
  }
191
207
  batch.prototype = {
208
+
192
209
  add: function (message) {
210
+
193
211
  this.addOrUpdate(message)
194
212
  },
195
213
 
196
214
  upsert: function (message, key) {
215
+
197
216
  this.addOrUpdate(message, key)
198
217
  },
199
218
 
200
219
  flush: function () {
220
+
201
221
  if (this.bufferMessageCount !== 0) {
202
222
  var messages = this.pushOnlyBuffer.concat(values(this.upsertBuffer))
203
223
  if (messages.length === 0) return
@@ -206,6 +226,7 @@ batch.prototype = {
206
226
  return JSON.parse(rowdataStr)
207
227
  }), this.bufferBytesSize)
208
228
  } else {
229
+
209
230
  this.request.send(messages.join('\n'), this.bufferBytesSize)
210
231
  }
211
232
 
@@ -220,6 +241,7 @@ batch.prototype = {
220
241
  if (this.isLineProtocolToJson) {
221
242
  return JSON.stringify(processedMessageByDataMap(message).rowData)
222
243
  }
244
+
223
245
  return processedMessageByDataMap(message).rowStr
224
246
  },
225
247
  sizeInBytes: function (candidate) {
@@ -323,7 +345,7 @@ batch.prototype = {
323
345
  * With sendBeacon, requests are guaranteed to be successfully sent during document unload
324
346
  */
325
347
  // @ts-ignore this function is not always defined
326
- if (navigator.sendBeacon) {
348
+ if (global.navigator.sendBeacon) {
327
349
  /**
328
350
  * beforeunload is called before visibilitychange
329
351
  * register first to be sure to be called before flush on beforeunload