@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
@@ -0,0 +1,26 @@
1
+ import { Batch } from './batch'
2
+ import { createHttpRequest } from './httpRequest'
3
+
4
+ export function startBatchWithReplica(
5
+ configuration,
6
+ endpointUrl,
7
+ reportError
8
+ ) {
9
+ var primaryBatch = createBatch(endpointUrl)
10
+
11
+ function createBatch(endpointUrl) {
12
+ return new Batch(
13
+ createHttpRequest(endpointUrl, configuration.batchBytesLimit, reportError),
14
+ configuration.batchMessagesLimit,
15
+ configuration.batchBytesLimit,
16
+ configuration.messageBytesLimit,
17
+ configuration.flushTimeout
18
+ )
19
+ }
20
+
21
+ return {
22
+ add: function(message) {
23
+ primaryBatch.add(message)
24
+ },
25
+ }
26
+ }
@@ -1,193 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.DEFAULT_CONFIGURATION = void 0;
7
- exports.buildCookieOptions = buildCookieOptions;
8
- exports.commonInit = commonInit;
9
- exports.isIntakeRequest = isIntakeRequest;
10
-
11
- var _tools = require("./helper/tools");
12
-
13
- var _cookie = require("./cookie");
14
-
15
- var _urlPolyfill = require("./helper/urlPolyfill");
16
-
17
- var _enums = require("./helper/enums");
18
-
19
- var TRIM_REGIX = /^\s+|\s+$/g;
20
- var DEFAULT_CONFIGURATION = {
21
- resourceSampleRate: 100,
22
- sampleRate: 100,
23
- flushTimeout: 30 * _tools.ONE_SECOND,
24
- maxErrorsByMinute: 3000,
25
-
26
- /**
27
- * Logs intake limit
28
- */
29
- maxBatchSize: 50,
30
- maxMessageSize: 256 * _tools.ONE_KILO_BYTE,
31
-
32
- /**
33
- * arbitrary value, byte precision not needed
34
- */
35
- requestErrorResponseLengthLimit: 32 * _tools.ONE_KILO_BYTE,
36
-
37
- /**
38
- * beacon payload max queue size implementation is 64kb
39
- * ensure that we leave room for logs, rum and potential other users
40
- */
41
- batchBytesLimit: 16 * _tools.ONE_KILO_BYTE,
42
- datakitUrl: '',
43
- logsEndpoint: '',
44
- traceType: _enums.TraceType.DDTRACE,
45
- traceId128Bit: false,
46
- trackInteractions: false,
47
- //是否开启交互action收集
48
- allowedDDTracingOrigins: [],
49
- //废弃
50
- allowedTracingOrigins: [],
51
- // 新增
52
- isServiceSampling: false,
53
- // 是否不抛弃采样是数据, 采用在服务端菜样的方式
54
- isJsBirdge: false,
55
- // 是否需要对webview 发送数据,需要装我们对应ios sdk
56
- isLineProtocolToJson: false,
57
- beforeSend: function beforeSend(event) {},
58
- isServerError: function isServerError(request) {
59
- return false;
60
- },
61
- // 判断请求是否为error 请求
62
- isIntakeUrl: function isIntakeUrl(url) {
63
- return false;
64
- } // 自定义方法根据请求资源 url 判断是否需要采集对应资源数据,默认都采集。 返回:false 表示要采集,true 表示不需要采集
65
-
66
- };
67
- exports.DEFAULT_CONFIGURATION = DEFAULT_CONFIGURATION;
68
-
69
- function trim(str) {
70
- return str.replace(TRIM_REGIX, '');
71
- }
72
-
73
- function buildCookieOptions(userConfiguration) {
74
- var cookieOptions = {};
75
- cookieOptions.secure = mustUseSecureCookie(userConfiguration);
76
- cookieOptions.crossSite = !!userConfiguration.useCrossSiteSessionCookie;
77
-
78
- if (!!userConfiguration.trackSessionAcrossSubdomains) {
79
- cookieOptions.domain = (0, _cookie.getCurrentSite)();
80
- }
81
-
82
- return cookieOptions;
83
- }
84
-
85
- function getDatakitUrl(url) {
86
- if (url.lastIndexOf('/') === url.length - 1) return trim(url) + 'v1/write/rum';
87
- return trim(url) + '/v1/write/rum';
88
- }
89
-
90
- function getLogsEndPoint(url) {
91
- if (url.lastIndexOf('/') === url.length - 1) return trim(url) + 'v1/write/logging';
92
- return trim(url) + '/v1/write/logging';
93
- }
94
-
95
- function commonInit(userConfiguration, buildEnv) {
96
- var enableExperimentalFeatures = (0, _tools.isArray)(userConfiguration.enableExperimentalFeatures) ? userConfiguration.enableExperimentalFeatures : [];
97
- var transportConfiguration = {
98
- applicationId: userConfiguration.applicationId,
99
- env: userConfiguration.env || '',
100
- version: userConfiguration.version || '',
101
- service: userConfiguration.service || 'browser',
102
- sdkVersion: buildEnv.sdkVersion,
103
- sdkName: buildEnv.sdkName,
104
- datakitUrl: getDatakitUrl(userConfiguration.datakitUrl || userConfiguration.datakitOrigin),
105
- logsEndpoint: getLogsEndPoint(userConfiguration.datakitOrigin),
106
- isEnabled: function isEnabled(feature) {
107
- return (0, _tools.includes)(enableExperimentalFeatures, feature);
108
- },
109
- cookieOptions: buildCookieOptions(userConfiguration)
110
- };
111
-
112
- if ('isJsBirdge' in userConfiguration) {
113
- transportConfiguration.isJsBirdge = userConfiguration.isJsBirdge;
114
- }
115
-
116
- if ('isLineProtocolToJson' in userConfiguration) {
117
- transportConfiguration.isLineProtocolToJson = userConfiguration.isLineProtocolToJson;
118
- }
119
-
120
- if ('allowedDDTracingOrigins' in userConfiguration) {
121
- transportConfiguration.allowedTracingOrigins = userConfiguration.allowedDDTracingOrigins;
122
- }
123
-
124
- if ('allowedTracingOrigins' in userConfiguration) {
125
- transportConfiguration.allowedTracingOrigins = userConfiguration.allowedTracingOrigins;
126
- }
127
-
128
- if ('sampleRate' in userConfiguration) {
129
- transportConfiguration.sampleRate = userConfiguration.sampleRate;
130
- }
131
-
132
- if ('resourceSampleRate' in userConfiguration) {
133
- transportConfiguration.resourceSampleRate = userConfiguration.resourceSampleRate;
134
- }
135
-
136
- if ('trackInteractions' in userConfiguration) {
137
- transportConfiguration.trackInteractions = !!userConfiguration.trackInteractions;
138
- }
139
-
140
- if ('isServerError' in userConfiguration && (0, _tools.isFunction)(userConfiguration.isServerError) && (0, _tools.isBoolean)(userConfiguration.isServerError())) {
141
- transportConfiguration.isServerError = userConfiguration.isServerError;
142
- }
143
-
144
- if ('isIntakeUrl' in userConfiguration && (0, _tools.isFunction)(userConfiguration.isIntakeUrl) && (0, _tools.isBoolean)(userConfiguration.isIntakeUrl())) {
145
- transportConfiguration.isIntakeUrl = userConfiguration.isIntakeUrl;
146
- }
147
-
148
- if ('traceId128Bit' in userConfiguration) {
149
- transportConfiguration.traceId128Bit = !!userConfiguration.traceId128Bit;
150
- }
151
-
152
- if ('traceType' in userConfiguration && hasTraceType(userConfiguration.traceType)) {
153
- transportConfiguration.traceType = userConfiguration.traceType;
154
- }
155
-
156
- if ('isServiceSampling' in userConfiguration && (0, _tools.isBoolean)(userConfiguration.isServiceSampling)) {
157
- transportConfiguration.isServiceSampling = userConfiguration.isServiceSampling;
158
- }
159
-
160
- return (0, _tools.extend2Lev)({}, DEFAULT_CONFIGURATION, transportConfiguration);
161
- }
162
-
163
- function hasTraceType(traceType) {
164
- if (traceType && (0, _tools.values)(_enums.TraceType).indexOf(traceType) > -1) return true;
165
- return false;
166
- }
167
-
168
- function mustUseSecureCookie(userConfiguration) {
169
- return !!userConfiguration.useSecureSessionCookie || !!userConfiguration.useCrossSiteSessionCookie;
170
- }
171
-
172
- function isIntakeRequest(url, configuration) {
173
- // return haveSameOrigin(url, configuration.datakitUrl)
174
- var notTakeRequest = [configuration.datakitUrl];
175
-
176
- if (configuration.logsEndpoint) {
177
- notTakeRequest.push(configuration.logsEndpoint);
178
- }
179
-
180
- var isIntake = false;
181
-
182
- for (var _i = 0, _notTakeRequest = notTakeRequest; _i < _notTakeRequest.length; _i++) {
183
- var _url = _notTakeRequest[_i];
184
-
185
- if (url.indexOf(_url) === 0) {
186
- isIntake = true;
187
- break;
188
- }
189
- } // datakit 地址,log 地址,以及客户自定义过滤方法定义url
190
-
191
-
192
- return isIntake || configuration.isIntakeUrl(url);
193
- }
@@ -1,167 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.startAutomaticErrorCollection = startAutomaticErrorCollection;
7
- exports.startConsoleTracking = startConsoleTracking;
8
- exports.startRuntimeErrorTracking = startRuntimeErrorTracking;
9
- exports.stopConsoleTracking = stopConsoleTracking;
10
- exports.stopRuntimeErrorTracking = stopRuntimeErrorTracking;
11
- exports.trackNetworkError = trackNetworkError;
12
-
13
- var _tools = require("./helper/tools");
14
-
15
- var _errorTools = require("./helper/errorTools");
16
-
17
- var _tracekit = require("./tracekit");
18
-
19
- var _observable = require("./helper/observable");
20
-
21
- var _configuration = require("./configuration");
22
-
23
- var _enums = require("./helper/enums");
24
-
25
- var _xhrProxy = require("./xhrProxy");
26
-
27
- var _fetchProxy = require("./fetchProxy");
28
-
29
- var errorObservable;
30
-
31
- function startAutomaticErrorCollection(configuration) {
32
- if (!errorObservable) {
33
- errorObservable = new _observable.Observable();
34
- trackNetworkError(configuration, errorObservable);
35
- startConsoleTracking(errorObservable);
36
- startRuntimeErrorTracking(errorObservable);
37
- }
38
-
39
- return errorObservable;
40
- }
41
-
42
- var originalConsoleError;
43
-
44
- function startConsoleTracking(errorObservable) {
45
- originalConsoleError = console.error;
46
-
47
- console.error = function () {
48
- originalConsoleError.apply(console, arguments);
49
- var handlingStack = (0, _errorTools.createHandlingStack)();
50
- errorObservable.notify((0, _tools.extend)({}, buildErrorFromParams((0, _tools.toArray)(arguments), handlingStack), {
51
- source: _errorTools.ErrorSource.CONSOLE,
52
- startClocks: (0, _tools.clocksNow)(),
53
- handling: _enums.ErrorHandling.HANDLED
54
- }));
55
- };
56
- }
57
-
58
- function buildErrorFromParams(params, handlingStack) {
59
- var firstErrorParam = (0, _tools.find)(params, function (param) {
60
- return param instanceof Error;
61
- });
62
- var message = '';
63
- message = (0, _tools.map)(['console error:'].concat(params), function (param) {
64
- return formatConsoleParameters(param);
65
- }).join(' ');
66
- return {
67
- message: message,
68
- stack: firstErrorParam ? (0, _errorTools.toStackTraceString)((0, _tracekit.computeStackTrace)(firstErrorParam)) : undefined,
69
- handlingStack: handlingStack
70
- };
71
- }
72
-
73
- function stopConsoleTracking() {
74
- console.error = originalConsoleError;
75
- }
76
-
77
- function formatConsoleParameters(param) {
78
- if (typeof param === 'string') {
79
- return param;
80
- }
81
-
82
- if (param instanceof Error) {
83
- return (0, _errorTools.formatErrorMessage)((0, _tracekit.computeStackTrace)(param));
84
- }
85
-
86
- return (0, _tools.jsonStringify)(param, undefined, 2);
87
- }
88
-
89
- var traceKitReportHandler;
90
-
91
- function startRuntimeErrorTracking(errorObservable) {
92
- traceKitReportHandler = function traceKitReportHandler(stackTrace, _, errorObject) {
93
- var error = (0, _errorTools.formatUnknownError)(stackTrace, errorObject, 'Uncaught');
94
- errorObservable.notify({
95
- message: error.message,
96
- stack: error.stack,
97
- type: error.type,
98
- source: _errorTools.ErrorSource.SOURCE,
99
- startClocks: (0, _tools.clocksNow)(),
100
- handling: _enums.ErrorHandling.UNHANDLED
101
- });
102
- };
103
-
104
- (0, _tracekit.subscribe)(traceKitReportHandler);
105
- }
106
-
107
- function stopRuntimeErrorTracking() {
108
- (0, _tracekit.unsubscribe)(traceKitReportHandler);
109
- }
110
-
111
- function trackNetworkError(configuration, errorObservable) {
112
- (0, _xhrProxy.startXhrProxy)().onRequestComplete(function (context) {
113
- handleCompleteRequest(_enums.RequestType.XHR, context);
114
- });
115
- (0, _fetchProxy.startFetchProxy)().onRequestComplete(function (context) {
116
- handleCompleteRequest(_enums.RequestType.FETCH, context);
117
- });
118
-
119
- function handleCompleteRequest(type, request) {
120
- if (!(0, _configuration.isIntakeRequest)(request.url, configuration) && !request.isAborted && (isRejected(request) || isServerError(request) || configuration.isServerError(request))) {
121
- errorObservable.notify({
122
- message: format(type) + ' error ' + request.method + ' ' + request.url,
123
- resource: {
124
- method: request.method,
125
- statusCode: request.status,
126
- url: request.url,
127
- traceId: request.traceId,
128
- spanId: request.spanId
129
- },
130
- source: _errorTools.ErrorSource.NETWORK,
131
- stack: truncateResponse(request.response, configuration) || 'Failed to load',
132
- startClocks: request.startClocks
133
- });
134
- }
135
- }
136
-
137
- return {
138
- stop: function stop() {
139
- (0, _xhrProxy.resetXhrProxy)();
140
- (0, _fetchProxy.resetFetchProxy)();
141
- }
142
- };
143
- }
144
-
145
- function isRejected(request) {
146
- return request.status === 0 && request.responseType !== 'opaque';
147
- }
148
-
149
- function isServerError(request) {
150
- return request.status >= 500;
151
- }
152
-
153
- function truncateResponse(response, configuration) {
154
- if (response && response.length > configuration.requestErrorResponseLengthLimit) {
155
- return response.substring(0, configuration.requestErrorResponseLengthLimit) + '...';
156
- }
157
-
158
- return response;
159
- }
160
-
161
- function format(type) {
162
- if (_enums.RequestType.XHR === type) {
163
- return 'XHR';
164
- }
165
-
166
- return 'Fetch';
167
- }
package/cjs/fetchProxy.js DELETED
@@ -1,161 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.resetFetchProxy = resetFetchProxy;
7
- exports.startFetchProxy = startFetchProxy;
8
-
9
- var _tools = require("./helper/tools");
10
-
11
- var _tracekit = require("./tracekit");
12
-
13
- var _errorTools = require("./helper/errorTools");
14
-
15
- var _urlPolyfill = require("./helper/urlPolyfill");
16
-
17
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
18
-
19
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
20
-
21
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
22
-
23
- var fetchProxySingleton;
24
- var originalFetch;
25
- var beforeSendCallbacks = [];
26
- var onRequestCompleteCallbacks = [];
27
-
28
- function startFetchProxy() {
29
- if (!fetchProxySingleton) {
30
- proxyFetch();
31
- fetchProxySingleton = {
32
- beforeSend: function beforeSend(callback) {
33
- beforeSendCallbacks.push(callback);
34
- },
35
- onRequestComplete: function onRequestComplete(callback) {
36
- onRequestCompleteCallbacks.push(callback);
37
- }
38
- };
39
- }
40
-
41
- return fetchProxySingleton;
42
- }
43
-
44
- function resetFetchProxy() {
45
- if (fetchProxySingleton) {
46
- fetchProxySingleton = undefined;
47
- beforeSendCallbacks.splice(0, beforeSendCallbacks.length);
48
- onRequestCompleteCallbacks.splice(0, onRequestCompleteCallbacks.length);
49
- window.fetch = originalFetch;
50
- }
51
- }
52
-
53
- function proxyFetch() {
54
- if (!window.fetch) {
55
- return;
56
- }
57
-
58
- originalFetch = window.fetch;
59
-
60
- window.fetch = function (input, init) {
61
- var responsePromise;
62
- var context = beforeSend.apply(null, [input, init]);
63
-
64
- if (context) {
65
- responsePromise = originalFetch.call(this, context.input, context.init);
66
- afterSend.apply(null, [responsePromise, context]);
67
- } else {
68
- responsePromise = originalFetch.call(this, input, init);
69
- }
70
-
71
- return responsePromise;
72
- };
73
- }
74
-
75
- function beforeSend(input, init) {
76
- var method = init && init.method || _typeof(input) === 'object' && input.method || 'GET';
77
- var url = (0, _urlPolyfill.normalizeUrl)(_typeof(input) === 'object' && input.url || input);
78
- var startClocks = (0, _tools.clocksNow)();
79
- var context = {
80
- init: init,
81
- input: input,
82
- method: method,
83
- startClocks: startClocks,
84
- url: url
85
- };
86
- (0, _tools.each)(beforeSendCallbacks, function (callback) {
87
- callback(context);
88
- });
89
- return context;
90
- }
91
-
92
- function afterSend(responsePromise, context) {
93
- var reportFetch = /*#__PURE__*/function () {
94
- var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(response) {
95
- var text;
96
- return regeneratorRuntime.wrap(function _callee$(_context) {
97
- while (1) {
98
- switch (_context.prev = _context.next) {
99
- case 0:
100
- context.duration = (0, _tools.elapsed)(context.startClocks.timeStamp, (0, _tools.timeStampNow)());
101
-
102
- if (!('stack' in response || response instanceof Error)) {
103
- _context.next = 9;
104
- break;
105
- }
106
-
107
- context.status = 0;
108
- context.response = (0, _errorTools.toStackTraceString)((0, _tracekit.computeStackTrace)(response));
109
- context.headers = response.headers;
110
- context.isAborted = response instanceof DOMException && response.code === DOMException.ABORT_ERR;
111
- (0, _tools.each)(onRequestCompleteCallbacks, function (callback) {
112
- callback(context);
113
- });
114
- _context.next = 25;
115
- break;
116
-
117
- case 9:
118
- if (!('status' in response)) {
119
- _context.next = 25;
120
- break;
121
- }
122
-
123
- _context.prev = 10;
124
- _context.next = 13;
125
- return response.clone().text();
126
-
127
- case 13:
128
- text = _context.sent;
129
- _context.next = 19;
130
- break;
131
-
132
- case 16:
133
- _context.prev = 16;
134
- _context.t0 = _context["catch"](10);
135
- text = 'Unable to retrieve response: ' + _context.t0;
136
-
137
- case 19:
138
- context.response = text;
139
- context.responseType = response.type;
140
- context.status = response.status;
141
- context.headers = response.headers;
142
- context.isAborted = false;
143
- (0, _tools.each)(onRequestCompleteCallbacks, function (callback) {
144
- callback(context);
145
- });
146
-
147
- case 25:
148
- case "end":
149
- return _context.stop();
150
- }
151
- }
152
- }, _callee, null, [[10, 16]]);
153
- }));
154
-
155
- return function reportFetch(_x) {
156
- return _ref.apply(this, arguments);
157
- };
158
- }();
159
-
160
- responsePromise.then(reportFetch, reportFetch);
161
- }
@@ -1,47 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createErrorFilter = createErrorFilter;
7
-
8
- var _errorTools = require("./errorTools");
9
-
10
- var _tools = require("./tools");
11
-
12
- function createErrorFilter(configuration, onLimitReached) {
13
- var errorCount = 0;
14
- var allowNextError = false;
15
- return {
16
- isLimitReached: function isLimitReached() {
17
- if (errorCount === 0) {
18
- setTimeout(function () {
19
- errorCount = 0;
20
- }, _tools.ONE_MINUTE);
21
- }
22
-
23
- errorCount += 1;
24
-
25
- if (errorCount <= configuration.maxErrorsByMinute || allowNextError) {
26
- allowNextError = false;
27
- return false;
28
- }
29
-
30
- if (errorCount === configuration.maxErrorsByMinute + 1) {
31
- allowNextError = true;
32
-
33
- try {
34
- onLimitReached({
35
- message: "Reached max number of errors by minute: ".concat(configuration.maxErrorsByMinute),
36
- source: _errorTools.ErrorSource.AGENT,
37
- startClocks: (0, _tools.clocksNow)()
38
- });
39
- } finally {
40
- allowNextError = false;
41
- }
42
- }
43
-
44
- return true;
45
- }
46
- };
47
- }