@cloudcare/browser-core 1.2.11 → 1.2.13-beta
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.
- package/cjs/browser/fetchObservable.js +1 -1
- package/cjs/helper/tools.js +1 -1
- package/cjs/transport/httpRequest.js +4 -2
- package/cjs/transport/sendWithRetryStrategy.js +3 -3
- package/esm/browser/fetchObservable.js +1 -1
- package/esm/helper/tools.js +1 -1
- package/esm/transport/httpRequest.js +4 -2
- package/esm/transport/sendWithRetryStrategy.js +3 -3
- package/package.json +23 -23
- package/src/browser/fetchObservable.js +20 -14
- package/src/helper/tools.js +2 -1
- package/src/transport/httpRequest.js +25 -25
- package/src/transport/sendWithRetryStrategy.js +9 -4
- package/LICENSE +0 -21
|
@@ -80,7 +80,7 @@ function afterSend(observable, responsePromise, startContext) {
|
|
|
80
80
|
observable.notify(context);
|
|
81
81
|
} else if ('status' in response) {
|
|
82
82
|
context.response = response;
|
|
83
|
-
context.responseType = response.type;
|
|
83
|
+
context.responseType = response.constructor === Response && response.type || '';
|
|
84
84
|
context.status = response.status;
|
|
85
85
|
context.isAborted = false;
|
|
86
86
|
observable.notify(context);
|
package/cjs/helper/tools.js
CHANGED
|
@@ -1448,7 +1448,7 @@ var getURLSearchParams = function getURLSearchParams(queryString) {
|
|
|
1448
1448
|
queryString = queryString || '';
|
|
1449
1449
|
|
|
1450
1450
|
var decodeParam = function decodeParam(str) {
|
|
1451
|
-
return decodeURIComponent(str)
|
|
1451
|
+
return str; // return decodeURIComponent(str)
|
|
1452
1452
|
};
|
|
1453
1453
|
|
|
1454
1454
|
var args = {};
|
|
@@ -73,11 +73,13 @@ function fetchKeepAliveStrategy(endpointUrl, bytesLimit, payload, onResponse) {
|
|
|
73
73
|
fetch(url, {
|
|
74
74
|
method: 'POST',
|
|
75
75
|
body: data,
|
|
76
|
-
keepalive: true
|
|
76
|
+
keepalive: true,
|
|
77
|
+
mode: 'cors'
|
|
77
78
|
}).then(function (response) {
|
|
78
79
|
if (typeof onResponse === 'function') {
|
|
79
80
|
onResponse({
|
|
80
|
-
status: response.status
|
|
81
|
+
status: response.status,
|
|
82
|
+
type: response.type
|
|
81
83
|
});
|
|
82
84
|
}
|
|
83
85
|
}, function () {
|
|
@@ -80,7 +80,7 @@ function send(payload, state, sendStrategy, responseData) {
|
|
|
80
80
|
sendStrategy(payload, function (response) {
|
|
81
81
|
state.bandwidthMonitor.remove(payload);
|
|
82
82
|
|
|
83
|
-
if (
|
|
83
|
+
if (!shouldRetryRequest(response)) {
|
|
84
84
|
state.transportStatus = TransportStatus.UP;
|
|
85
85
|
onSuccess();
|
|
86
86
|
} else {
|
|
@@ -110,8 +110,8 @@ function retryQueuedPayloads(reason, state, sendStrategy, reportError) {
|
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
function
|
|
114
|
-
return response.
|
|
113
|
+
function shouldRetryRequest(response) {
|
|
114
|
+
return response.type !== 'opaque' && (response.status === 0 && !navigator.onLine || response.status === 408 || response.status === 429 || response.status >= 500);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
function newRetryState() {
|
|
@@ -66,7 +66,7 @@ function afterSend(observable, responsePromise, startContext) {
|
|
|
66
66
|
observable.notify(context);
|
|
67
67
|
} else if ('status' in response) {
|
|
68
68
|
context.response = response;
|
|
69
|
-
context.responseType = response.type;
|
|
69
|
+
context.responseType = response.constructor === Response && response.type || '';
|
|
70
70
|
context.status = response.status;
|
|
71
71
|
context.isAborted = false;
|
|
72
72
|
observable.notify(context);
|
package/esm/helper/tools.js
CHANGED
|
@@ -1177,7 +1177,7 @@ export var getURLSearchParams = function getURLSearchParams(queryString) {
|
|
|
1177
1177
|
queryString = queryString || '';
|
|
1178
1178
|
|
|
1179
1179
|
var decodeParam = function decodeParam(str) {
|
|
1180
|
-
return decodeURIComponent(str)
|
|
1180
|
+
return str; // return decodeURIComponent(str)
|
|
1181
1181
|
};
|
|
1182
1182
|
|
|
1183
1183
|
var args = {};
|
|
@@ -65,11 +65,13 @@ export function fetchKeepAliveStrategy(endpointUrl, bytesLimit, payload, onRespo
|
|
|
65
65
|
fetch(url, {
|
|
66
66
|
method: 'POST',
|
|
67
67
|
body: data,
|
|
68
|
-
keepalive: true
|
|
68
|
+
keepalive: true,
|
|
69
|
+
mode: 'cors'
|
|
69
70
|
}).then(function (response) {
|
|
70
71
|
if (typeof onResponse === 'function') {
|
|
71
72
|
onResponse({
|
|
72
|
-
status: response.status
|
|
73
|
+
status: response.status,
|
|
74
|
+
type: response.type
|
|
73
75
|
});
|
|
74
76
|
}
|
|
75
77
|
}, function () {
|
|
@@ -63,7 +63,7 @@ function send(payload, state, sendStrategy, responseData) {
|
|
|
63
63
|
sendStrategy(payload, function (response) {
|
|
64
64
|
state.bandwidthMonitor.remove(payload);
|
|
65
65
|
|
|
66
|
-
if (
|
|
66
|
+
if (!shouldRetryRequest(response)) {
|
|
67
67
|
state.transportStatus = TransportStatus.UP;
|
|
68
68
|
onSuccess();
|
|
69
69
|
} else {
|
|
@@ -93,8 +93,8 @@ function retryQueuedPayloads(reason, state, sendStrategy, reportError) {
|
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
function
|
|
97
|
-
return response.
|
|
96
|
+
function shouldRetryRequest(response) {
|
|
97
|
+
return response.type !== 'opaque' && (response.status === 0 && !navigator.onLine || response.status === 408 || response.status === 429 || response.status >= 500);
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
export function newRetryState() {
|
package/package.json
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
2
|
+
"name": "@cloudcare/browser-core",
|
|
3
|
+
"version": "1.2.13-beta",
|
|
4
|
+
"main": "cjs/index.js",
|
|
5
|
+
"module": "esm/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "run-p build:cjs build:esm",
|
|
8
|
+
"build:cjs": "rm -rf cjs && babel --config-file ./babel.cjs.json --out-dir cjs ./src",
|
|
9
|
+
"build:esm": "rm -rf esm && babel --config-file ./babel.esm.json --out-dir esm ./src "
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"dataflux",
|
|
13
|
+
"logs",
|
|
14
|
+
"sdk"
|
|
15
|
+
],
|
|
16
|
+
"repository": {
|
|
17
|
+
"url": "https://github.com/DataFlux-cn/datakit-js",
|
|
18
|
+
"type": "git"
|
|
19
|
+
},
|
|
20
|
+
"author": "dataflux",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"description": "DataFlux RUM Web 端数据指标监控",
|
|
23
|
+
"gitHead": "e73550d0362e289b1c133980dc7458cb0e5175b5"
|
|
24
|
+
}
|
|
@@ -13,7 +13,7 @@ export function initFetchObservable() {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
function createFetchObservable() {
|
|
16
|
-
var observable = new Observable(function() {
|
|
16
|
+
var observable = new Observable(function () {
|
|
17
17
|
if (!window.fetch) {
|
|
18
18
|
return
|
|
19
19
|
}
|
|
@@ -21,19 +21,23 @@ function createFetchObservable() {
|
|
|
21
21
|
var fetchMethod = instrumentMethod(
|
|
22
22
|
window,
|
|
23
23
|
'fetch',
|
|
24
|
-
function(originalFetch) {
|
|
24
|
+
function (originalFetch) {
|
|
25
25
|
return function (input, init) {
|
|
26
26
|
var responsePromise
|
|
27
27
|
var context = beforeSend(observable, input, init)
|
|
28
28
|
if (context) {
|
|
29
|
-
responsePromise = originalFetch.call(
|
|
29
|
+
responsePromise = originalFetch.call(
|
|
30
|
+
this,
|
|
31
|
+
context.input,
|
|
32
|
+
context.init
|
|
33
|
+
)
|
|
30
34
|
afterSend(observable, responsePromise, context)
|
|
31
35
|
} else {
|
|
32
36
|
responsePromise = originalFetch.call(this, input, init)
|
|
33
37
|
}
|
|
34
38
|
return responsePromise
|
|
35
39
|
}
|
|
36
|
-
}
|
|
40
|
+
}
|
|
37
41
|
)
|
|
38
42
|
return fetchMethod.stop
|
|
39
43
|
})
|
|
@@ -42,7 +46,10 @@ function createFetchObservable() {
|
|
|
42
46
|
}
|
|
43
47
|
|
|
44
48
|
function beforeSend(observable, input, init) {
|
|
45
|
-
var method =
|
|
49
|
+
var method =
|
|
50
|
+
(init && init.method) ||
|
|
51
|
+
(typeof input === 'object' && input.method) ||
|
|
52
|
+
'GET'
|
|
46
53
|
var url = normalizeUrl((typeof input === 'object' && input.url) || input)
|
|
47
54
|
var startClocks = clocksNow()
|
|
48
55
|
|
|
@@ -52,7 +59,7 @@ function beforeSend(observable, input, init) {
|
|
|
52
59
|
input: input,
|
|
53
60
|
method: method,
|
|
54
61
|
startClocks: startClocks,
|
|
55
|
-
url: url
|
|
62
|
+
url: url
|
|
56
63
|
}
|
|
57
64
|
|
|
58
65
|
observable.notify(context)
|
|
@@ -60,23 +67,22 @@ function beforeSend(observable, input, init) {
|
|
|
60
67
|
return context
|
|
61
68
|
}
|
|
62
69
|
|
|
63
|
-
function afterSend(
|
|
64
|
-
|
|
65
|
-
responsePromise,
|
|
66
|
-
startContext
|
|
67
|
-
) {
|
|
68
|
-
var reportFetch = function(response) {
|
|
70
|
+
function afterSend(observable, responsePromise, startContext) {
|
|
71
|
+
var reportFetch = function (response) {
|
|
69
72
|
var context = startContext
|
|
70
73
|
context.state = 'complete'
|
|
71
74
|
context.duration = elapsed(context.startClocks.timeStamp, timeStampNow())
|
|
72
75
|
if ('stack' in response || response instanceof Error) {
|
|
73
76
|
context.status = 0
|
|
74
|
-
context.isAborted =
|
|
77
|
+
context.isAborted =
|
|
78
|
+
response instanceof DOMException &&
|
|
79
|
+
response.code === DOMException.ABORT_ERR
|
|
75
80
|
context.error = response
|
|
76
81
|
observable.notify(context)
|
|
77
82
|
} else if ('status' in response) {
|
|
78
83
|
context.response = response
|
|
79
|
-
context.responseType =
|
|
84
|
+
context.responseType =
|
|
85
|
+
(response.constructor === Response && response.type) || ''
|
|
80
86
|
context.status = response.status
|
|
81
87
|
context.isAborted = false
|
|
82
88
|
observable.notify(context)
|
package/src/helper/tools.js
CHANGED
|
@@ -1143,7 +1143,8 @@ export var getQueryParamsFromUrl = function (url) {
|
|
|
1143
1143
|
export var getURLSearchParams = function (queryString) {
|
|
1144
1144
|
queryString = queryString || ''
|
|
1145
1145
|
var decodeParam = function (str) {
|
|
1146
|
-
return
|
|
1146
|
+
return str
|
|
1147
|
+
// return decodeURIComponent(str)
|
|
1147
1148
|
}
|
|
1148
1149
|
var args = {}
|
|
1149
1150
|
var query = queryString.substring(1)
|
|
@@ -12,27 +12,28 @@ function addBatchPrecision(url) {
|
|
|
12
12
|
if (!url) return url
|
|
13
13
|
return url + (url.indexOf('?') === -1 ? '?' : '&') + 'precision=ms'
|
|
14
14
|
}
|
|
15
|
-
export function createHttpRequest(
|
|
16
|
-
endpointUrl,
|
|
17
|
-
bytesLimit,
|
|
18
|
-
reportError
|
|
19
|
-
) {
|
|
15
|
+
export function createHttpRequest(endpointUrl, bytesLimit, reportError) {
|
|
20
16
|
var retryState = newRetryState()
|
|
21
|
-
var sendStrategyForRetry = function(payload, onResponse) {
|
|
17
|
+
var sendStrategyForRetry = function (payload, onResponse) {
|
|
22
18
|
return fetchKeepAliveStrategy(endpointUrl, bytesLimit, payload, onResponse)
|
|
23
19
|
}
|
|
24
|
-
|
|
20
|
+
|
|
25
21
|
return {
|
|
26
|
-
send: function(payload) {
|
|
27
|
-
sendWithRetryStrategy(
|
|
22
|
+
send: function (payload) {
|
|
23
|
+
sendWithRetryStrategy(
|
|
24
|
+
payload,
|
|
25
|
+
retryState,
|
|
26
|
+
sendStrategyForRetry,
|
|
27
|
+
reportError
|
|
28
|
+
)
|
|
28
29
|
},
|
|
29
30
|
/**
|
|
30
31
|
* Since fetch keepalive behaves like regular fetch on Firefox,
|
|
31
32
|
* keep using sendBeaconStrategy on exit
|
|
32
33
|
*/
|
|
33
|
-
sendOnExit: function(payload) {
|
|
34
|
+
sendOnExit: function (payload) {
|
|
34
35
|
sendBeaconStrategy(endpointUrl, bytesLimit, payload)
|
|
35
|
-
}
|
|
36
|
+
}
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
|
|
@@ -55,8 +56,6 @@ function sendBeaconStrategy(endpointUrl, bytesLimit, payload) {
|
|
|
55
56
|
sendXHR(url, data)
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
|
|
59
|
-
|
|
60
59
|
export function fetchKeepAliveStrategy(
|
|
61
60
|
endpointUrl,
|
|
62
61
|
bytesLimit,
|
|
@@ -68,14 +67,18 @@ export function fetchKeepAliveStrategy(
|
|
|
68
67
|
var url = addBatchPrecision(endpointUrl)
|
|
69
68
|
var canUseKeepAlive = isKeepAliveSupported() && bytesCount < bytesLimit
|
|
70
69
|
if (canUseKeepAlive) {
|
|
71
|
-
fetch(url, {
|
|
72
|
-
|
|
73
|
-
|
|
70
|
+
fetch(url, {
|
|
71
|
+
method: 'POST',
|
|
72
|
+
body: data,
|
|
73
|
+
keepalive: true,
|
|
74
|
+
mode: 'cors'
|
|
75
|
+
}).then(
|
|
76
|
+
function (response) {
|
|
74
77
|
if (typeof onResponse === 'function') {
|
|
75
|
-
onResponse({ status: response.status })
|
|
78
|
+
onResponse({ status: response.status, type: response.type })
|
|
76
79
|
}
|
|
77
80
|
},
|
|
78
|
-
function() {
|
|
81
|
+
function () {
|
|
79
82
|
// failed to queue the request
|
|
80
83
|
sendXHR(url, data, onResponse)
|
|
81
84
|
}
|
|
@@ -98,12 +101,9 @@ function sendXHR(url, data, onResponse) {
|
|
|
98
101
|
const request = new XMLHttpRequest()
|
|
99
102
|
request.open('POST', url, true)
|
|
100
103
|
request.send(data)
|
|
101
|
-
request.addEventListener(
|
|
102
|
-
'
|
|
103
|
-
|
|
104
|
-
if (typeof onResponse === 'function') {
|
|
105
|
-
onResponse({ status: request.status })
|
|
106
|
-
}
|
|
104
|
+
request.addEventListener('loadend', function () {
|
|
105
|
+
if (typeof onResponse === 'function') {
|
|
106
|
+
onResponse({ status: request.status })
|
|
107
107
|
}
|
|
108
|
-
)
|
|
108
|
+
})
|
|
109
109
|
}
|
|
@@ -92,7 +92,7 @@ function send(payload, state, sendStrategy, responseData) {
|
|
|
92
92
|
state.bandwidthMonitor.add(payload)
|
|
93
93
|
sendStrategy(payload, function (response) {
|
|
94
94
|
state.bandwidthMonitor.remove(payload)
|
|
95
|
-
if (
|
|
95
|
+
if (!shouldRetryRequest(response)) {
|
|
96
96
|
state.transportStatus = TransportStatus.UP
|
|
97
97
|
onSuccess()
|
|
98
98
|
} else {
|
|
@@ -135,10 +135,15 @@ function retryQueuedPayloads(reason, state, sendStrategy, reportError) {
|
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
function
|
|
139
|
-
return
|
|
138
|
+
function shouldRetryRequest(response) {
|
|
139
|
+
return (
|
|
140
|
+
response.type !== 'opaque' &&
|
|
141
|
+
((response.status === 0 && !navigator.onLine) ||
|
|
142
|
+
response.status === 408 ||
|
|
143
|
+
response.status === 429 ||
|
|
144
|
+
response.status >= 500)
|
|
145
|
+
)
|
|
140
146
|
}
|
|
141
|
-
|
|
142
147
|
export function newRetryState() {
|
|
143
148
|
return {
|
|
144
149
|
transportStatus: TransportStatus.UP,
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020 CloudCare
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|