@cloudcare/browser-core 1.1.3 → 1.2.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 (134) hide show
  1. package/cjs/{cookie.js → browser/cookie.js} +6 -37
  2. package/cjs/browser/fetchObservable.js +91 -0
  3. package/cjs/browser/xhrObservable.js +118 -0
  4. package/cjs/configuration/configuration.js +70 -0
  5. package/cjs/configuration/transportConfiguration.js +64 -0
  6. package/cjs/console/consoleObservable.js +84 -0
  7. package/cjs/dataMap.js +26 -21
  8. package/cjs/error/trackRuntimeError.js +27 -0
  9. package/cjs/helper/catchUserErrors.js +20 -0
  10. package/cjs/helper/contextHistory.js +134 -0
  11. package/cjs/helper/createEventRateLimiter.js +47 -0
  12. package/cjs/helper/deviceInfo.js +8 -25
  13. package/cjs/helper/display.js +31 -0
  14. package/cjs/helper/enums.js +28 -2
  15. package/cjs/helper/errorTools.js +63 -2
  16. package/cjs/helper/instrumentMethod.js +93 -0
  17. package/cjs/helper/lifeCycle.js +5 -3
  18. package/cjs/helper/observable.js +40 -2
  19. package/cjs/helper/tools.js +185 -9
  20. package/cjs/index.js +153 -23
  21. package/cjs/init.js +17 -27
  22. package/cjs/report/reportObservable.js +112 -0
  23. package/cjs/session/sessionConstants.js +13 -0
  24. package/cjs/session/sessionCookieStore.js +188 -0
  25. package/cjs/session/sessionManagement.js +96 -0
  26. package/cjs/session/sessionStore.js +150 -0
  27. package/cjs/synthetics/syntheticsWorkerValues.js +32 -0
  28. package/cjs/synthetics/usr.js +32 -0
  29. package/cjs/tracekit/computeStackTrace.js +73 -563
  30. package/cjs/tracekit/index.js +3 -15
  31. package/cjs/tracekit/tracekit.js +113 -0
  32. package/cjs/transport/batch.js +332 -0
  33. package/cjs/transport/httpRequest.js +112 -0
  34. package/cjs/transport/index.js +29 -0
  35. package/cjs/transport/sendWithRetryStrategy.js +177 -0
  36. package/cjs/transport/startBatchWithReplica.js +24 -0
  37. package/esm/{cookie.js → browser/cookie.js} +4 -35
  38. package/esm/browser/fetchObservable.js +77 -0
  39. package/esm/browser/xhrObservable.js +106 -0
  40. package/esm/configuration/configuration.js +55 -0
  41. package/esm/configuration/transportConfiguration.js +54 -0
  42. package/esm/console/consoleObservable.js +70 -0
  43. package/esm/dataMap.js +26 -21
  44. package/esm/error/trackRuntimeError.js +16 -0
  45. package/esm/helper/catchUserErrors.js +12 -0
  46. package/esm/helper/contextHistory.js +106 -0
  47. package/esm/helper/createEventRateLimiter.js +38 -0
  48. package/esm/helper/deviceInfo.js +6 -25
  49. package/esm/helper/display.js +21 -0
  50. package/esm/helper/enums.js +25 -1
  51. package/esm/helper/errorTools.js +58 -3
  52. package/esm/helper/instrumentMethod.js +81 -0
  53. package/esm/helper/lifeCycle.js +5 -3
  54. package/esm/helper/observable.js +39 -3
  55. package/esm/helper/tools.js +147 -1
  56. package/esm/index.js +17 -7
  57. package/esm/init.js +17 -22
  58. package/esm/report/reportObservable.js +98 -0
  59. package/esm/session/sessionConstants.js +3 -0
  60. package/esm/session/sessionCookieStore.js +168 -0
  61. package/esm/session/sessionManagement.js +78 -0
  62. package/esm/session/sessionStore.js +139 -0
  63. package/esm/synthetics/syntheticsWorkerValues.js +15 -0
  64. package/esm/synthetics/usr.js +18 -0
  65. package/esm/tracekit/computeStackTrace.js +73 -557
  66. package/esm/tracekit/index.js +1 -1
  67. package/esm/tracekit/tracekit.js +103 -0
  68. package/esm/transport/batch.js +316 -0
  69. package/esm/transport/httpRequest.js +104 -0
  70. package/esm/transport/index.js +3 -0
  71. package/esm/transport/sendWithRetryStrategy.js +165 -0
  72. package/esm/transport/startBatchWithReplica.js +15 -0
  73. package/package.json +2 -2
  74. package/src/.DS_Store +0 -0
  75. package/src/{cookie.js → browser/cookie.js} +6 -34
  76. package/src/browser/fetchObservable.js +86 -0
  77. package/src/browser/xhrObservable.js +102 -0
  78. package/src/configuration/configuration.js +61 -0
  79. package/src/configuration/transportConfiguration.js +42 -0
  80. package/src/console/consoleObservable.js +65 -0
  81. package/src/dataMap.js +26 -21
  82. package/src/error/trackRuntimeError.js +19 -0
  83. package/src/helper/catchUserErrors.js +12 -0
  84. package/src/helper/contextHistory.js +85 -0
  85. package/src/helper/createEventRateLimiter.js +37 -0
  86. package/src/helper/deviceInfo.js +5 -51
  87. package/src/helper/display.js +21 -0
  88. package/src/helper/enums.js +38 -14
  89. package/src/helper/errorTools.js +76 -12
  90. package/src/helper/instrumentMethod.js +97 -0
  91. package/src/helper/lifeCycle.js +5 -3
  92. package/src/helper/observable.js +33 -2
  93. package/src/helper/tools.js +135 -4
  94. package/src/index.js +18 -7
  95. package/src/init.js +17 -18
  96. package/src/report/reportObservable.js +113 -0
  97. package/src/session/sessionConstants.js +3 -0
  98. package/src/session/sessionCookieStore.js +159 -0
  99. package/src/session/sessionManagement.js +89 -0
  100. package/src/session/sessionStore.js +139 -0
  101. package/src/synthetics/syntheticsWorkerValues.js +22 -0
  102. package/src/synthetics/usr.js +18 -0
  103. package/src/tracekit/computeStackTrace.js +75 -551
  104. package/src/tracekit/index.js +1 -1
  105. package/src/tracekit/tracekit.js +100 -0
  106. package/src/transport/batch.js +289 -0
  107. package/src/transport/httpRequest.js +109 -0
  108. package/src/transport/index.js +3 -0
  109. package/src/transport/sendWithRetryStrategy.js +186 -0
  110. package/src/transport/startBatchWithReplica.js +26 -0
  111. package/cjs/configuration.js +0 -193
  112. package/cjs/errorCollection.js +0 -167
  113. package/cjs/fetchProxy.js +0 -161
  114. package/cjs/helper/errorFilter.js +0 -47
  115. package/cjs/sessionManagement.js +0 -221
  116. package/cjs/tracekit/report.js +0 -289
  117. package/cjs/transport.js +0 -373
  118. package/cjs/xhrProxy.js +0 -122
  119. package/esm/configuration.js +0 -176
  120. package/esm/errorCollection.js +0 -141
  121. package/esm/fetchProxy.js +0 -115
  122. package/esm/helper/errorFilter.js +0 -38
  123. package/esm/sessionManagement.js +0 -195
  124. package/esm/tracekit/report.js +0 -273
  125. package/esm/transport.js +0 -357
  126. package/esm/xhrProxy.js +0 -110
  127. package/src/configuration.js +0 -161
  128. package/src/errorCollection.js +0 -170
  129. package/src/fetchProxy.js +0 -108
  130. package/src/helper/errorFilter.js +0 -38
  131. package/src/sessionManagement.js +0 -224
  132. package/src/tracekit/report.js +0 -284
  133. package/src/transport.js +0 -380
  134. package/src/xhrProxy.js +0 -106
package/cjs/transport.js DELETED
@@ -1,373 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.processedMessageByDataMap = exports.HttpRequest = exports.Batch = void 0;
7
-
8
- var _tools = require("./helper/tools");
9
-
10
- var _enums = require("./helper/enums");
11
-
12
- var _dataMap = require("./dataMap");
13
-
14
- var _init = require("./init");
15
-
16
- // https://en.wikipedia.org/wiki/UTF-8
17
- var HAS_MULTI_BYTES_CHARACTERS = /[^\u0000-\u007F]/;
18
- var CUSTOM_KEYS = 'custom_keys';
19
-
20
- function addBatchPrecision(url) {
21
- if (!url) return url;
22
- return url + (url.indexOf('?') === -1 ? '?' : '&') + 'precision=ms';
23
- }
24
-
25
- var global = (0, _init.getGlobalObject)();
26
-
27
- var httpRequest = function httpRequest(endpointUrl, bytesLimit, isLineProtocolToJson) {
28
- this.endpointUrl = endpointUrl;
29
- this.bytesLimit = bytesLimit;
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
- }
39
- };
40
-
41
- httpRequest.prototype = {
42
- send: function send(data, size) {
43
- var url = addBatchPrecision(this.endpointUrl);
44
-
45
- if (this.sendBeacon && size < this.bytesLimit && !this.isLineProtocolToJson) {
46
- var isQueued = this.sendBeacon(url, data);
47
-
48
- if (isQueued) {
49
- return;
50
- }
51
- } else if (this.sendBeacon && size < this.bytesLimit && this.isLineProtocolToJson) {
52
- var blob = new Blob([JSON.stringify(data)], {
53
- type: 'application/json'
54
- });
55
- var isQueued = this.sendBeacon(url, blob);
56
-
57
- if (isQueued) {
58
- return;
59
- }
60
- }
61
-
62
- var request = new XMLHttpRequest();
63
- request.open('POST', url, true);
64
- request.withCredentials = true;
65
-
66
- if (this.isLineProtocolToJson) {
67
- request.setRequestHeader('Content-type', 'application/json');
68
- request.send(JSON.stringify(data));
69
- } else {
70
- request.setRequestHeader('Content-type', 'text/plain;charset=UTF-8');
71
- request.send(data);
72
- }
73
- }
74
- };
75
- var HttpRequest = httpRequest;
76
- exports.HttpRequest = HttpRequest;
77
-
78
- var processedMessageByDataMap = function processedMessageByDataMap(message) {
79
- if (!message || !message.type) return {
80
- rowStr: '',
81
- rowData: undefined
82
- };
83
- var rowData = {
84
- tags: {},
85
- fields: {}
86
- };
87
- var hasFileds = false;
88
- var rowStr = '';
89
- (0, _tools.each)(_dataMap.dataMap, function (value, key) {
90
- if (value.type === message.type) {
91
- rowStr += key + ',';
92
- rowData.measurement = key;
93
- var tagsStr = [];
94
- var tags = (0, _tools.extend)({}, _dataMap.commonTags, value.tags);
95
- var filterFileds = ['date', 'type', CUSTOM_KEYS]; // 已经在datamap中定义过的fields和tags
96
-
97
- (0, _tools.each)(tags, function (value_path, _key) {
98
- var _value = (0, _tools.findByPath)(message, value_path);
99
-
100
- filterFileds.push(_key);
101
-
102
- if (_value || (0, _tools.isNumber)(_value)) {
103
- rowData.tags[_key] = (0, _tools.escapeJsonValue)(_value);
104
- tagsStr.push((0, _tools.escapeRowData)(_key) + '=' + (0, _tools.escapeRowData)(_value));
105
- }
106
- });
107
- var fieldsStr = [];
108
- (0, _tools.each)(value.fields, function (_value, _key) {
109
- if ((0, _tools.isArray)(_value) && _value.length === 2) {
110
- var type = _value[0],
111
- value_path = _value[1];
112
-
113
- var _valueData = (0, _tools.findByPath)(message, value_path);
114
-
115
- filterFileds.push(_key);
116
-
117
- if (_valueData || (0, _tools.isNumber)(_valueData)) {
118
- rowData.fields[_key] = _valueData; // 这里不需要转译
119
- // _valueData =
120
- // type === 'string'
121
- // ? '"' +
122
- // _valueData.replace(/[\\]*"/g, '"').replace(/"/g, '\\"') +
123
- // '"'
124
- // : escapeRowData(_valueData)
125
-
126
- fieldsStr.push((0, _tools.escapeRowData)(_key) + '=' + (0, _tools.escapeRowField)(_valueData));
127
- }
128
- } else if ((0, _tools.isString)(_value)) {
129
- var _valueData = (0, _tools.findByPath)(message, _value);
130
-
131
- filterFileds.push(_key);
132
-
133
- if (_valueData || (0, _tools.isNumber)(_valueData)) {
134
- rowData.fields[_key] = _valueData; // 这里不需要转译
135
-
136
- fieldsStr.push((0, _tools.escapeRowData)(_key) + '=' + (0, _tools.escapeRowField)(_valueData));
137
- }
138
- }
139
- });
140
-
141
- if (message.tags && (0, _tools.isObject)(message.tags) && !(0, _tools.isEmptyObject)(message.tags)) {
142
- // 自定义tag, 存储成field
143
- var _tagKeys = [];
144
- (0, _tools.each)(message.tags, function (_value, _key) {
145
- // 如果和之前tag重名,则舍弃
146
- if (filterFileds.indexOf(_key) > -1) return;
147
- filterFileds.push(_key);
148
-
149
- if (_value || (0, _tools.isNumber)(_value)) {
150
- _tagKeys.push(_key);
151
-
152
- rowData.fields[_key] = _value; // 这里不需要转译
153
-
154
- fieldsStr.push((0, _tools.escapeRowData)(_key) + '=' + (0, _tools.escapeRowField)(_value));
155
- }
156
- });
157
-
158
- if (_tagKeys.length) {
159
- rowData.fields[CUSTOM_KEYS] = (0, _tools.escapeRowField)(_tagKeys);
160
- fieldsStr.push((0, _tools.escapeRowData)(CUSTOM_KEYS) + '=' + (0, _tools.escapeRowField)(_tagKeys));
161
- }
162
- }
163
-
164
- if (message.type === _enums.RumEventType.LOGGER) {
165
- // 这里处理日志类型数据自定义字段
166
- (0, _tools.each)(message, function (value, key) {
167
- if (filterFileds.indexOf(key) === -1 && ((0, _tools.isNumber)(value) || (0, _tools.isString)(value) || (0, _tools.isBoolean)(value))) {
168
- rowData.fields[key] = value; // 这里不需要转译
169
-
170
- fieldsStr.push((0, _tools.escapeRowData)(key) + '=' + (0, _tools.escapeRowField)(value));
171
- }
172
- });
173
- }
174
-
175
- if (tagsStr.length) {
176
- rowStr += tagsStr.join(',');
177
- }
178
-
179
- if (fieldsStr.length) {
180
- rowStr += ' ';
181
- rowStr += fieldsStr.join(',');
182
- hasFileds = true;
183
- }
184
-
185
- rowStr = rowStr + ' ' + message.date;
186
- rowData.time = (0, _tools.toServerDuration)(message.date); // 这里不需要转译
187
- }
188
- });
189
- return {
190
- rowStr: hasFileds ? rowStr : '',
191
- rowData: hasFileds ? rowData : undefined
192
- };
193
- };
194
-
195
- exports.processedMessageByDataMap = processedMessageByDataMap;
196
-
197
- var batch = function batch(request, maxSize, bytesLimit, maxMessageSize, flushTimeout, isLineProtocolToJson, beforeUnloadCallback) {
198
- this.request = request;
199
- this.maxSize = maxSize;
200
- this.bytesLimit = bytesLimit;
201
- this.maxMessageSize = maxMessageSize;
202
- this.flushTimeout = flushTimeout;
203
- this.isLineProtocolToJson = isLineProtocolToJson;
204
- this.beforeUnloadCallback = beforeUnloadCallback;
205
- this.pushOnlyBuffer = [];
206
- this.upsertBuffer = {};
207
- this.bufferBytesSize = 0;
208
- this.bufferMessageCount = 0;
209
- this.flushOnVisibilityHidden();
210
- this.flushPeriodically();
211
- };
212
-
213
- batch.prototype = {
214
- add: function add(message) {
215
- this.addOrUpdate(message);
216
- },
217
- upsert: function upsert(message, key) {
218
- this.addOrUpdate(message, key);
219
- },
220
- flush: function flush() {
221
- if (this.bufferMessageCount !== 0) {
222
- var messages = this.pushOnlyBuffer.concat((0, _tools.values)(this.upsertBuffer));
223
- if (messages.length === 0) return;
224
-
225
- if (this.isLineProtocolToJson) {
226
- this.request.send((0, _tools.map)(messages, function (rowdataStr) {
227
- return JSON.parse(rowdataStr);
228
- }), this.bufferBytesSize);
229
- } else {
230
- this.request.send(messages.join('\n'), this.bufferBytesSize);
231
- }
232
-
233
- this.pushOnlyBuffer = [];
234
- this.upsertBuffer = {};
235
- this.bufferBytesSize = 0;
236
- this.bufferMessageCount = 0;
237
- }
238
- },
239
- processSendData: function processSendData(message) {
240
- if (this.isLineProtocolToJson) {
241
- return JSON.stringify(processedMessageByDataMap(message).rowData);
242
- }
243
-
244
- return processedMessageByDataMap(message).rowStr;
245
- },
246
- sizeInBytes: function sizeInBytes(candidate) {
247
- // Accurate byte size computations can degrade performances when there is a lot of events to process
248
- if (!HAS_MULTI_BYTES_CHARACTERS.test(candidate)) {
249
- return candidate.length;
250
- }
251
-
252
- if (window.TextEncoder !== undefined) {
253
- return new TextEncoder().encode(candidate).length;
254
- }
255
-
256
- return new Blob([candidate]).size;
257
- },
258
- addOrUpdate: function addOrUpdate(message, key) {
259
- var process = this.process(message);
260
- if (!process.processedMessage || process.processedMessage === '') return;
261
-
262
- if (process.messageBytesSize >= this.maxMessageSize) {
263
- console.warn('Discarded a message whose size was bigger than the maximum allowed size' + this.maxMessageSize + 'KB.');
264
- return;
265
- }
266
-
267
- if (this.hasMessageFor(key)) {
268
- this.remove(key);
269
- }
270
-
271
- if (this.willReachedBytesLimitWith(process.messageBytesSize)) {
272
- this.flush();
273
- }
274
-
275
- this.push(process.processedMessage, process.messageBytesSize, key);
276
-
277
- if (this.isFull()) {
278
- this.flush();
279
- }
280
- },
281
- process: function process(message) {
282
- var processedMessage = '';
283
- var processedMessage = this.processSendData(message);
284
- var messageBytesSize = this.sizeInBytes(processedMessage);
285
- return {
286
- processedMessage: processedMessage,
287
- messageBytesSize: messageBytesSize
288
- };
289
- },
290
- push: function push(processedMessage, messageBytesSize, key) {
291
- if (this.bufferMessageCount > 0) {
292
- // \n separator at serialization
293
- this.bufferBytesSize += 1;
294
- }
295
-
296
- if (key !== undefined) {
297
- this.upsertBuffer[key] = processedMessage;
298
- } else {
299
- this.pushOnlyBuffer.push(processedMessage);
300
- }
301
-
302
- this.bufferBytesSize += messageBytesSize;
303
- this.bufferMessageCount += 1;
304
- },
305
- remove: function remove(key) {
306
- var removedMessage = this.upsertBuffer[key];
307
- delete this.upsertBuffer[key];
308
- var messageBytesSize = this.sizeInBytes(removedMessage);
309
- this.bufferBytesSize -= messageBytesSize;
310
- this.bufferMessageCount -= 1;
311
-
312
- if (this.bufferMessageCount > 0) {
313
- this.bufferBytesSize -= 1;
314
- }
315
- },
316
- hasMessageFor: function hasMessageFor(key) {
317
- return key !== undefined && this.upsertBuffer[key] !== undefined;
318
- },
319
- willReachedBytesLimitWith: function willReachedBytesLimitWith(messageBytesSize) {
320
- // byte of the separator at the end of the message
321
- return this.bufferBytesSize + messageBytesSize + 1 >= this.bytesLimit;
322
- },
323
- isFull: function isFull() {
324
- return this.bufferMessageCount === this.maxSize || this.bufferBytesSize >= this.bytesLimit;
325
- },
326
- flushPeriodically: function flushPeriodically() {
327
- var _this = this;
328
-
329
- setTimeout(function () {
330
- _this.flush();
331
-
332
- _this.flushPeriodically();
333
- }, _this.flushTimeout);
334
- },
335
- flushOnVisibilityHidden: function flushOnVisibilityHidden() {
336
- var _this = this;
337
- /**
338
- * With sendBeacon, requests are guaranteed to be successfully sent during document unload
339
- */
340
- // @ts-ignore this function is not always defined
341
-
342
-
343
- if (global.navigator.sendBeacon) {
344
- /**
345
- * beforeunload is called before visibilitychange
346
- * register first to be sure to be called before flush on beforeunload
347
- * caveat: unload can still be canceled by another listener
348
- */
349
- (0, _tools.addEventListener)(window, _enums.DOM_EVENT.BEFORE_UNLOAD, _this.beforeUnloadCallback);
350
- /**
351
- * Only event that guarantee to fire on mobile devices when the page transitions to background state
352
- * (e.g. when user switches to a different application, goes to homescreen, etc), or is being unloaded.
353
- */
354
-
355
- (0, _tools.addEventListener)(document, _enums.DOM_EVENT.VISIBILITY_CHANGE, function () {
356
- if (document.visibilityState === 'hidden') {
357
- _this.flush();
358
- }
359
- });
360
- /**
361
- * Safari does not support yet to send a request during:
362
- * - a visibility change during doc unload (cf: https://bugs.webkit.org/show_bug.cgi?id=194897)
363
- * - a page hide transition (cf: https://bugs.webkit.org/show_bug.cgi?id=188329)
364
- */
365
-
366
- (0, _tools.addEventListener)(window, _enums.DOM_EVENT.BEFORE_UNLOAD, function () {
367
- _this.flush();
368
- });
369
- }
370
- }
371
- };
372
- var Batch = batch;
373
- exports.Batch = Batch;
package/cjs/xhrProxy.js DELETED
@@ -1,122 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.resetXhrProxy = resetXhrProxy;
7
- exports.startXhrProxy = startXhrProxy;
8
-
9
- var _urlPolyfill = require("./helper/urlPolyfill");
10
-
11
- var _tools = require("./helper/tools");
12
-
13
- var xhrProxySingleton;
14
- var beforeSendCallbacks = [];
15
- var onRequestCompleteCallbacks = [];
16
- var originalXhrOpen;
17
- var originalXhrSend;
18
- var originalXhrAbort;
19
-
20
- function startXhrProxy() {
21
- if (!xhrProxySingleton) {
22
- proxyXhr();
23
- xhrProxySingleton = {
24
- beforeSend: function beforeSend(callback) {
25
- beforeSendCallbacks.push(callback);
26
- },
27
- onRequestComplete: function onRequestComplete(callback) {
28
- onRequestCompleteCallbacks.push(callback);
29
- }
30
- };
31
- }
32
-
33
- return xhrProxySingleton;
34
- }
35
-
36
- function resetXhrProxy() {
37
- if (xhrProxySingleton) {
38
- xhrProxySingleton = undefined;
39
- beforeSendCallbacks.length = 0;
40
- onRequestCompleteCallbacks.length = 0;
41
- XMLHttpRequest.prototype.open = originalXhrOpen;
42
- XMLHttpRequest.prototype.send = originalXhrSend;
43
- XMLHttpRequest.prototype.abort = originalXhrAbort;
44
- }
45
- }
46
-
47
- function proxyXhr() {
48
- originalXhrOpen = XMLHttpRequest.prototype.open;
49
- originalXhrSend = XMLHttpRequest.prototype.send;
50
- originalXhrAbort = XMLHttpRequest.prototype.abort;
51
-
52
- XMLHttpRequest.prototype.open = function (method, url) {
53
- // WARN: since this data structure is tied to the instance, it is shared by both logs and rum
54
- // and can be used by different code versions depending on customer setup
55
- // so it should stay compatible with older versions
56
- this._dataflux_xhr = {
57
- method: method,
58
- startTime: -1,
59
- // computed in send call
60
- url: (0, _urlPolyfill.normalizeUrl)(url)
61
- };
62
- return originalXhrOpen.apply(this, arguments);
63
- };
64
-
65
- XMLHttpRequest.prototype.send = function (body) {
66
- var _this = this;
67
-
68
- if (this._dataflux_xhr) {
69
- var xhrPendingContext = this._dataflux_xhr;
70
- xhrPendingContext.startTime = (0, _tools.relativeNow)();
71
- xhrPendingContext.startClocks = (0, _tools.clocksNow)();
72
- xhrPendingContext.isAborted = false;
73
- var originalOnreadystatechange = this.onreadystatechange;
74
-
75
- this.onreadystatechange = function () {
76
- if (this.readyState === XMLHttpRequest.DONE) {
77
- reportXhr();
78
- }
79
-
80
- if (originalOnreadystatechange) {
81
- originalOnreadystatechange.apply(this, arguments);
82
- }
83
- };
84
-
85
- var hasBeenReported = false;
86
-
87
- var reportXhr = function reportXhr() {
88
- _this.removeEventListener('loadend', reportXhr);
89
-
90
- if (hasBeenReported) {
91
- return;
92
- }
93
-
94
- hasBeenReported = true;
95
- var xhrCompleteContext = (0, _tools.extend)({}, xhrPendingContext, {
96
- duration: (0, _tools.elapsed)(xhrPendingContext.startClocks.timeStamp, (0, _tools.timeStampNow)()),
97
- response: _this.response,
98
- status: _this.status
99
- });
100
- (0, _tools.each)(onRequestCompleteCallbacks, function (callback) {
101
- callback(xhrCompleteContext, _this);
102
- });
103
- };
104
-
105
- _this.addEventListener('loadend', reportXhr);
106
-
107
- (0, _tools.each)(beforeSendCallbacks, function (callback) {
108
- callback(xhrPendingContext, _this);
109
- });
110
- }
111
-
112
- return originalXhrSend.apply(_this, arguments);
113
- };
114
-
115
- XMLHttpRequest.prototype.abort = function () {
116
- if (this._dataflux_xhr) {
117
- this._dataflux_xhr.isAborted = true;
118
- }
119
-
120
- return originalXhrAbort.apply(this, arguments);
121
- };
122
- }
@@ -1,176 +0,0 @@
1
- import { ONE_KILO_BYTE, ONE_SECOND, extend2Lev, isArray, includes, isFunction, isBoolean, values } from './helper/tools';
2
- import { getCurrentSite } from './cookie';
3
- import { haveSameOrigin } from './helper/urlPolyfill';
4
- import { TraceType } from './helper/enums';
5
- var TRIM_REGIX = /^\s+|\s+$/g;
6
- export var DEFAULT_CONFIGURATION = {
7
- resourceSampleRate: 100,
8
- sampleRate: 100,
9
- flushTimeout: 30 * ONE_SECOND,
10
- maxErrorsByMinute: 3000,
11
-
12
- /**
13
- * Logs intake limit
14
- */
15
- maxBatchSize: 50,
16
- maxMessageSize: 256 * ONE_KILO_BYTE,
17
-
18
- /**
19
- * arbitrary value, byte precision not needed
20
- */
21
- requestErrorResponseLengthLimit: 32 * ONE_KILO_BYTE,
22
-
23
- /**
24
- * beacon payload max queue size implementation is 64kb
25
- * ensure that we leave room for logs, rum and potential other users
26
- */
27
- batchBytesLimit: 16 * ONE_KILO_BYTE,
28
- datakitUrl: '',
29
- logsEndpoint: '',
30
- traceType: TraceType.DDTRACE,
31
- traceId128Bit: false,
32
- trackInteractions: false,
33
- //是否开启交互action收集
34
- allowedDDTracingOrigins: [],
35
- //废弃
36
- allowedTracingOrigins: [],
37
- // 新增
38
- isServiceSampling: false,
39
- // 是否不抛弃采样是数据, 采用在服务端菜样的方式
40
- isJsBirdge: false,
41
- // 是否需要对webview 发送数据,需要装我们对应ios sdk
42
- isLineProtocolToJson: false,
43
- beforeSend: function beforeSend(event) {},
44
- isServerError: function isServerError(request) {
45
- return false;
46
- },
47
- // 判断请求是否为error 请求
48
- isIntakeUrl: function isIntakeUrl(url) {
49
- return false;
50
- } // 自定义方法根据请求资源 url 判断是否需要采集对应资源数据,默认都采集。 返回:false 表示要采集,true 表示不需要采集
51
-
52
- };
53
-
54
- function trim(str) {
55
- return str.replace(TRIM_REGIX, '');
56
- }
57
-
58
- export function buildCookieOptions(userConfiguration) {
59
- var cookieOptions = {};
60
- cookieOptions.secure = mustUseSecureCookie(userConfiguration);
61
- cookieOptions.crossSite = !!userConfiguration.useCrossSiteSessionCookie;
62
-
63
- if (!!userConfiguration.trackSessionAcrossSubdomains) {
64
- cookieOptions.domain = getCurrentSite();
65
- }
66
-
67
- return cookieOptions;
68
- }
69
-
70
- function getDatakitUrl(url) {
71
- if (url.lastIndexOf('/') === url.length - 1) return trim(url) + 'v1/write/rum';
72
- return trim(url) + '/v1/write/rum';
73
- }
74
-
75
- function getLogsEndPoint(url) {
76
- if (url.lastIndexOf('/') === url.length - 1) return trim(url) + 'v1/write/logging';
77
- return trim(url) + '/v1/write/logging';
78
- }
79
-
80
- export function commonInit(userConfiguration, buildEnv) {
81
- var enableExperimentalFeatures = isArray(userConfiguration.enableExperimentalFeatures) ? userConfiguration.enableExperimentalFeatures : [];
82
- var transportConfiguration = {
83
- applicationId: userConfiguration.applicationId,
84
- env: userConfiguration.env || '',
85
- version: userConfiguration.version || '',
86
- service: userConfiguration.service || 'browser',
87
- sdkVersion: buildEnv.sdkVersion,
88
- sdkName: buildEnv.sdkName,
89
- datakitUrl: getDatakitUrl(userConfiguration.datakitUrl || userConfiguration.datakitOrigin),
90
- logsEndpoint: getLogsEndPoint(userConfiguration.datakitOrigin),
91
- isEnabled: function isEnabled(feature) {
92
- return includes(enableExperimentalFeatures, feature);
93
- },
94
- cookieOptions: buildCookieOptions(userConfiguration)
95
- };
96
-
97
- if ('isJsBirdge' in userConfiguration) {
98
- transportConfiguration.isJsBirdge = userConfiguration.isJsBirdge;
99
- }
100
-
101
- if ('isLineProtocolToJson' in userConfiguration) {
102
- transportConfiguration.isLineProtocolToJson = userConfiguration.isLineProtocolToJson;
103
- }
104
-
105
- if ('allowedDDTracingOrigins' in userConfiguration) {
106
- transportConfiguration.allowedTracingOrigins = userConfiguration.allowedDDTracingOrigins;
107
- }
108
-
109
- if ('allowedTracingOrigins' in userConfiguration) {
110
- transportConfiguration.allowedTracingOrigins = userConfiguration.allowedTracingOrigins;
111
- }
112
-
113
- if ('sampleRate' in userConfiguration) {
114
- transportConfiguration.sampleRate = userConfiguration.sampleRate;
115
- }
116
-
117
- if ('resourceSampleRate' in userConfiguration) {
118
- transportConfiguration.resourceSampleRate = userConfiguration.resourceSampleRate;
119
- }
120
-
121
- if ('trackInteractions' in userConfiguration) {
122
- transportConfiguration.trackInteractions = !!userConfiguration.trackInteractions;
123
- }
124
-
125
- if ('isServerError' in userConfiguration && isFunction(userConfiguration.isServerError) && isBoolean(userConfiguration.isServerError())) {
126
- transportConfiguration.isServerError = userConfiguration.isServerError;
127
- }
128
-
129
- if ('isIntakeUrl' in userConfiguration && isFunction(userConfiguration.isIntakeUrl) && isBoolean(userConfiguration.isIntakeUrl())) {
130
- transportConfiguration.isIntakeUrl = userConfiguration.isIntakeUrl;
131
- }
132
-
133
- if ('traceId128Bit' in userConfiguration) {
134
- transportConfiguration.traceId128Bit = !!userConfiguration.traceId128Bit;
135
- }
136
-
137
- if ('traceType' in userConfiguration && hasTraceType(userConfiguration.traceType)) {
138
- transportConfiguration.traceType = userConfiguration.traceType;
139
- }
140
-
141
- if ('isServiceSampling' in userConfiguration && isBoolean(userConfiguration.isServiceSampling)) {
142
- transportConfiguration.isServiceSampling = userConfiguration.isServiceSampling;
143
- }
144
-
145
- return extend2Lev({}, DEFAULT_CONFIGURATION, transportConfiguration);
146
- }
147
-
148
- function hasTraceType(traceType) {
149
- if (traceType && values(TraceType).indexOf(traceType) > -1) return true;
150
- return false;
151
- }
152
-
153
- function mustUseSecureCookie(userConfiguration) {
154
- return !!userConfiguration.useSecureSessionCookie || !!userConfiguration.useCrossSiteSessionCookie;
155
- }
156
-
157
- export function isIntakeRequest(url, configuration) {
158
- // return haveSameOrigin(url, configuration.datakitUrl)
159
- var notTakeRequest = [configuration.datakitUrl];
160
-
161
- if (configuration.logsEndpoint) {
162
- notTakeRequest.push(configuration.logsEndpoint);
163
- }
164
-
165
- var isIntake = false;
166
-
167
- for (var _url of notTakeRequest) {
168
- if (url.indexOf(_url) === 0) {
169
- isIntake = true;
170
- break;
171
- }
172
- } // datakit 地址,log 地址,以及客户自定义过滤方法定义url
173
-
174
-
175
- return isIntake || configuration.isIntakeUrl(url);
176
- }