@webex/http-core 2.59.2 → 2.59.3-next.1
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/.eslintrc.js +6 -6
- package/README.md +64 -64
- package/babel.config.js +3 -3
- package/dist/http-error-subtypes.js +57 -57
- package/dist/http-error-subtypes.js.map +1 -1
- package/dist/http-error.js +25 -25
- package/dist/http-error.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/interceptors/http-status.js +13 -13
- package/dist/interceptors/http-status.js.map +1 -1
- package/dist/lib/detect.js +6 -6
- package/dist/lib/detect.js.map +1 -1
- package/dist/lib/interceptor.js +34 -34
- package/dist/lib/interceptor.js.map +1 -1
- package/dist/lib/xhr.js +2 -2
- package/dist/lib/xhr.js.map +1 -1
- package/dist/progress-event.js +6 -6
- package/dist/progress-event.js.map +1 -1
- package/dist/request/index.js +11 -11
- package/dist/request/index.js.map +1 -1
- package/dist/request/request.js +14 -14
- package/dist/request/request.js.map +1 -1
- package/dist/request/request.shim.js +65 -65
- package/dist/request/request.shim.js.map +1 -1
- package/jest.config.js +3 -3
- package/package.json +18 -17
- package/process +1 -1
- package/src/http-error-subtypes.js +187 -187
- package/src/http-error.js +147 -147
- package/src/index.js +58 -58
- package/src/interceptors/http-status.js +63 -63
- package/src/lib/detect.js +33 -33
- package/src/lib/interceptor.js +95 -95
- package/src/lib/xhr.js +258 -258
- package/src/progress-event.js +37 -37
- package/src/request/index.js +62 -62
- package/src/request/request.js +109 -109
- package/src/request/request.shim.js +304 -304
- package/test/integration/spec/http-error.js +188 -188
- package/test/integration/spec/interceptor.js +71 -71
- package/test/integration/spec/progress-event.js +83 -83
- package/test/integration/spec/request.js +310 -310
- package/test/unit/spec/interceptors/http-status.js +49 -49
package/src/lib/xhr.js
CHANGED
|
@@ -1,258 +1,258 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// Need to fork xhr to support environments with full object freezing; namely,
|
|
6
|
-
// SalesForce's Aura and Locker environment.
|
|
7
|
-
|
|
8
|
-
// See https://github.com/naugtur/xhr for license information
|
|
9
|
-
|
|
10
|
-
// Maintain the original code style of https://github.com/naugtur/xhr since
|
|
11
|
-
// we're trying to diverge as little as possible.
|
|
12
|
-
/* eslint-disable */
|
|
13
|
-
|
|
14
|
-
'use strict';
|
|
15
|
-
var window = require('global/window');
|
|
16
|
-
var isFunction = require('is-function');
|
|
17
|
-
var parseHeaders = require('parse-headers');
|
|
18
|
-
var xtend = require('xtend');
|
|
19
|
-
|
|
20
|
-
createXHR.XMLHttpRequest = window.XMLHttpRequest || noop;
|
|
21
|
-
createXHR.XDomainRequest =
|
|
22
|
-
'withCredentials' in new createXHR.XMLHttpRequest()
|
|
23
|
-
? createXHR.XMLHttpRequest
|
|
24
|
-
: window.XDomainRequest;
|
|
25
|
-
|
|
26
|
-
forEachArray(['get', 'put', 'post', 'patch', 'head', 'delete'], function (method) {
|
|
27
|
-
createXHR[method === 'delete' ? 'del' : method] = function (uri, options, callback) {
|
|
28
|
-
options = initParams(uri, options, callback);
|
|
29
|
-
options.method = method.toUpperCase();
|
|
30
|
-
return _createXHR(options);
|
|
31
|
-
};
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
function forEachArray(array, iterator) {
|
|
35
|
-
for (var i = 0; i < array.length; i += 1) {
|
|
36
|
-
iterator(array[i]);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function isEmpty(obj) {
|
|
41
|
-
for (var i in obj) {
|
|
42
|
-
if (obj.hasOwnProperty(i)) return false;
|
|
43
|
-
}
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function initParams(uri, options, callback) {
|
|
48
|
-
var params = uri;
|
|
49
|
-
|
|
50
|
-
if (isFunction(options)) {
|
|
51
|
-
callback = options;
|
|
52
|
-
if (typeof uri === 'string') {
|
|
53
|
-
params = {uri: uri};
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
params = xtend(options, {uri: uri});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
params.callback = callback;
|
|
60
|
-
return params;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function createXHR(uri, options, callback) {
|
|
64
|
-
options = initParams(uri, options, callback);
|
|
65
|
-
return _createXHR(options);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function _createXHR(options) {
|
|
69
|
-
if (typeof options.callback === 'undefined') {
|
|
70
|
-
throw new Error('callback argument missing');
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
var called = false;
|
|
74
|
-
var callback = function cbOnce(err, response, body) {
|
|
75
|
-
if (!called) {
|
|
76
|
-
called = true;
|
|
77
|
-
options.callback(err, response, body);
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
function readystatechange() {
|
|
82
|
-
if (xhr.readyState === 4) {
|
|
83
|
-
setTimeout(loadFunc, 0);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function getBody() {
|
|
88
|
-
// Chrome with requestType=blob throws errors arround when even testing access to responseText
|
|
89
|
-
var body = undefined;
|
|
90
|
-
|
|
91
|
-
if (xhr.response) {
|
|
92
|
-
body = xhr.response;
|
|
93
|
-
} else {
|
|
94
|
-
body = xhr.responseText || getXml(xhr);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (isJson) {
|
|
98
|
-
try {
|
|
99
|
-
body = JSON.parse(body);
|
|
100
|
-
} catch (e) {}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return body;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function errorFunc(evt) {
|
|
107
|
-
clearTimeout(timeoutTimer);
|
|
108
|
-
if (!(evt instanceof Error)) {
|
|
109
|
-
evt = new Error('' + (evt || 'Unknown XMLHttpRequest Error'));
|
|
110
|
-
}
|
|
111
|
-
evt.statusCode = 0;
|
|
112
|
-
return callback(evt, failureResponse);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// will load the data & process the response in a special response object
|
|
116
|
-
function loadFunc() {
|
|
117
|
-
if (aborted) return;
|
|
118
|
-
var status;
|
|
119
|
-
clearTimeout(timeoutTimer);
|
|
120
|
-
if (options.useXDR && xhr.status === undefined) {
|
|
121
|
-
//IE8 CORS GET successful response doesn't have a status field, but body is fine
|
|
122
|
-
status = 200;
|
|
123
|
-
} else {
|
|
124
|
-
status = xhr.status === 1223 ? 204 : xhr.status;
|
|
125
|
-
}
|
|
126
|
-
var response = failureResponse;
|
|
127
|
-
var err = null;
|
|
128
|
-
|
|
129
|
-
if (status !== 0) {
|
|
130
|
-
response = {
|
|
131
|
-
body: getBody(),
|
|
132
|
-
statusCode: status,
|
|
133
|
-
method: method,
|
|
134
|
-
headers: {},
|
|
135
|
-
url: uri,
|
|
136
|
-
rawRequest: xhr,
|
|
137
|
-
};
|
|
138
|
-
if (xhr.getAllResponseHeaders) {
|
|
139
|
-
//remember xhr can in fact be XDR for CORS in IE
|
|
140
|
-
response.headers = parseHeaders(xhr.getAllResponseHeaders());
|
|
141
|
-
}
|
|
142
|
-
} else {
|
|
143
|
-
err = new Error('Internal XMLHttpRequest Error');
|
|
144
|
-
}
|
|
145
|
-
return callback(err, response, response.body);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
var xhr = options.xhr || null;
|
|
149
|
-
|
|
150
|
-
if (!xhr) {
|
|
151
|
-
if (options.cors || options.useXDR) {
|
|
152
|
-
xhr = new createXHR.XDomainRequest();
|
|
153
|
-
} else {
|
|
154
|
-
xhr = new createXHR.XMLHttpRequest();
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
var key;
|
|
159
|
-
var aborted;
|
|
160
|
-
var uri = options.uri || options.url;
|
|
161
|
-
var method = options.method || 'GET';
|
|
162
|
-
var body = options.body || options.data;
|
|
163
|
-
var headers = options.headers || {};
|
|
164
|
-
var sync = !!options.sync;
|
|
165
|
-
var isJson = false;
|
|
166
|
-
var timeoutTimer;
|
|
167
|
-
var failureResponse = {
|
|
168
|
-
body: undefined,
|
|
169
|
-
headers: {},
|
|
170
|
-
statusCode: 0,
|
|
171
|
-
method: method,
|
|
172
|
-
url: uri,
|
|
173
|
-
rawRequest: xhr,
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
if ('json' in options && options.json !== false) {
|
|
177
|
-
isJson = true;
|
|
178
|
-
headers['accept'] || headers['Accept'] || (headers['Accept'] = 'application/json'); //Don't override existing accept header declared by user
|
|
179
|
-
if (method !== 'GET' && method !== 'HEAD') {
|
|
180
|
-
headers['content-type'] ||
|
|
181
|
-
headers['Content-Type'] ||
|
|
182
|
-
(headers['Content-Type'] = 'application/json'); //Don't override existing accept header declared by user
|
|
183
|
-
body = JSON.stringify(options.json === true ? body : options.json);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
xhr.onreadystatechange = readystatechange;
|
|
188
|
-
xhr.onload = loadFunc;
|
|
189
|
-
xhr.onerror = errorFunc;
|
|
190
|
-
// IE9 must have onprogress be set to a unique function.
|
|
191
|
-
xhr.onprogress = function () {
|
|
192
|
-
// IE must die
|
|
193
|
-
};
|
|
194
|
-
xhr.onabort = function () {
|
|
195
|
-
aborted = true;
|
|
196
|
-
};
|
|
197
|
-
xhr.ontimeout = errorFunc;
|
|
198
|
-
xhr.open(method, uri, !sync, options.username, options.password);
|
|
199
|
-
//has to be after open
|
|
200
|
-
if (!sync) {
|
|
201
|
-
xhr.withCredentials = !!options.withCredentials;
|
|
202
|
-
}
|
|
203
|
-
// Cannot set timeout with sync request
|
|
204
|
-
// not setting timeout on the xhr object, because of old webkits etc. not handling that correctly
|
|
205
|
-
// both npm's request and jquery 1.x use this kind of timeout, so this is being consistent
|
|
206
|
-
if (!sync && options.timeout > 0) {
|
|
207
|
-
timeoutTimer = setTimeout(function () {
|
|
208
|
-
if (aborted) return;
|
|
209
|
-
aborted = true; //IE9 may still call readystatechange
|
|
210
|
-
xhr.abort('timeout');
|
|
211
|
-
var e = new Error('XMLHttpRequest timeout');
|
|
212
|
-
e.code = 'ETIMEDOUT';
|
|
213
|
-
errorFunc(e);
|
|
214
|
-
}, options.timeout);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (xhr.setRequestHeader) {
|
|
218
|
-
for (key in headers) {
|
|
219
|
-
if (headers.hasOwnProperty(key)) {
|
|
220
|
-
xhr.setRequestHeader(key, headers[key]);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
} else if (options.headers && !isEmpty(options.headers)) {
|
|
224
|
-
throw new Error('Headers cannot be set on an XDomainRequest object');
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if ('responseType' in options) {
|
|
228
|
-
xhr.responseType = options.responseType;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
if ('beforeSend' in options && typeof options.beforeSend === 'function') {
|
|
232
|
-
options.beforeSend(xhr);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// Microsoft Edge browser sends "undefined" when send is called with undefined value.
|
|
236
|
-
// XMLHttpRequest spec says to pass null as body to indicate no body
|
|
237
|
-
// See https://github.com/naugtur/xhr/issues/100.
|
|
238
|
-
xhr.send(body || null);
|
|
239
|
-
|
|
240
|
-
return xhr;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function getXml(xhr) {
|
|
244
|
-
if (xhr.responseType === 'document') {
|
|
245
|
-
return xhr.responseXML;
|
|
246
|
-
}
|
|
247
|
-
var firefoxBugTakenEffect =
|
|
248
|
-
xhr.responseXML && xhr.responseXML.documentElement.nodeName === 'parsererror';
|
|
249
|
-
if (xhr.responseType === '' && !firefoxBugTakenEffect) {
|
|
250
|
-
return xhr.responseXML;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
return null;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
function noop() {}
|
|
257
|
-
|
|
258
|
-
export default createXHR;
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// Need to fork xhr to support environments with full object freezing; namely,
|
|
6
|
+
// SalesForce's Aura and Locker environment.
|
|
7
|
+
|
|
8
|
+
// See https://github.com/naugtur/xhr for license information
|
|
9
|
+
|
|
10
|
+
// Maintain the original code style of https://github.com/naugtur/xhr since
|
|
11
|
+
// we're trying to diverge as little as possible.
|
|
12
|
+
/* eslint-disable */
|
|
13
|
+
|
|
14
|
+
'use strict';
|
|
15
|
+
var window = require('global/window');
|
|
16
|
+
var isFunction = require('is-function');
|
|
17
|
+
var parseHeaders = require('parse-headers');
|
|
18
|
+
var xtend = require('xtend');
|
|
19
|
+
|
|
20
|
+
createXHR.XMLHttpRequest = window.XMLHttpRequest || noop;
|
|
21
|
+
createXHR.XDomainRequest =
|
|
22
|
+
'withCredentials' in new createXHR.XMLHttpRequest()
|
|
23
|
+
? createXHR.XMLHttpRequest
|
|
24
|
+
: window.XDomainRequest;
|
|
25
|
+
|
|
26
|
+
forEachArray(['get', 'put', 'post', 'patch', 'head', 'delete'], function (method) {
|
|
27
|
+
createXHR[method === 'delete' ? 'del' : method] = function (uri, options, callback) {
|
|
28
|
+
options = initParams(uri, options, callback);
|
|
29
|
+
options.method = method.toUpperCase();
|
|
30
|
+
return _createXHR(options);
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
function forEachArray(array, iterator) {
|
|
35
|
+
for (var i = 0; i < array.length; i += 1) {
|
|
36
|
+
iterator(array[i]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isEmpty(obj) {
|
|
41
|
+
for (var i in obj) {
|
|
42
|
+
if (obj.hasOwnProperty(i)) return false;
|
|
43
|
+
}
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function initParams(uri, options, callback) {
|
|
48
|
+
var params = uri;
|
|
49
|
+
|
|
50
|
+
if (isFunction(options)) {
|
|
51
|
+
callback = options;
|
|
52
|
+
if (typeof uri === 'string') {
|
|
53
|
+
params = {uri: uri};
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
params = xtend(options, {uri: uri});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
params.callback = callback;
|
|
60
|
+
return params;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function createXHR(uri, options, callback) {
|
|
64
|
+
options = initParams(uri, options, callback);
|
|
65
|
+
return _createXHR(options);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function _createXHR(options) {
|
|
69
|
+
if (typeof options.callback === 'undefined') {
|
|
70
|
+
throw new Error('callback argument missing');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
var called = false;
|
|
74
|
+
var callback = function cbOnce(err, response, body) {
|
|
75
|
+
if (!called) {
|
|
76
|
+
called = true;
|
|
77
|
+
options.callback(err, response, body);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
function readystatechange() {
|
|
82
|
+
if (xhr.readyState === 4) {
|
|
83
|
+
setTimeout(loadFunc, 0);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getBody() {
|
|
88
|
+
// Chrome with requestType=blob throws errors arround when even testing access to responseText
|
|
89
|
+
var body = undefined;
|
|
90
|
+
|
|
91
|
+
if (xhr.response) {
|
|
92
|
+
body = xhr.response;
|
|
93
|
+
} else {
|
|
94
|
+
body = xhr.responseText || getXml(xhr);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (isJson) {
|
|
98
|
+
try {
|
|
99
|
+
body = JSON.parse(body);
|
|
100
|
+
} catch (e) {}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return body;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function errorFunc(evt) {
|
|
107
|
+
clearTimeout(timeoutTimer);
|
|
108
|
+
if (!(evt instanceof Error)) {
|
|
109
|
+
evt = new Error('' + (evt || 'Unknown XMLHttpRequest Error'));
|
|
110
|
+
}
|
|
111
|
+
evt.statusCode = 0;
|
|
112
|
+
return callback(evt, failureResponse);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// will load the data & process the response in a special response object
|
|
116
|
+
function loadFunc() {
|
|
117
|
+
if (aborted) return;
|
|
118
|
+
var status;
|
|
119
|
+
clearTimeout(timeoutTimer);
|
|
120
|
+
if (options.useXDR && xhr.status === undefined) {
|
|
121
|
+
//IE8 CORS GET successful response doesn't have a status field, but body is fine
|
|
122
|
+
status = 200;
|
|
123
|
+
} else {
|
|
124
|
+
status = xhr.status === 1223 ? 204 : xhr.status;
|
|
125
|
+
}
|
|
126
|
+
var response = failureResponse;
|
|
127
|
+
var err = null;
|
|
128
|
+
|
|
129
|
+
if (status !== 0) {
|
|
130
|
+
response = {
|
|
131
|
+
body: getBody(),
|
|
132
|
+
statusCode: status,
|
|
133
|
+
method: method,
|
|
134
|
+
headers: {},
|
|
135
|
+
url: uri,
|
|
136
|
+
rawRequest: xhr,
|
|
137
|
+
};
|
|
138
|
+
if (xhr.getAllResponseHeaders) {
|
|
139
|
+
//remember xhr can in fact be XDR for CORS in IE
|
|
140
|
+
response.headers = parseHeaders(xhr.getAllResponseHeaders());
|
|
141
|
+
}
|
|
142
|
+
} else {
|
|
143
|
+
err = new Error('Internal XMLHttpRequest Error');
|
|
144
|
+
}
|
|
145
|
+
return callback(err, response, response.body);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
var xhr = options.xhr || null;
|
|
149
|
+
|
|
150
|
+
if (!xhr) {
|
|
151
|
+
if (options.cors || options.useXDR) {
|
|
152
|
+
xhr = new createXHR.XDomainRequest();
|
|
153
|
+
} else {
|
|
154
|
+
xhr = new createXHR.XMLHttpRequest();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
var key;
|
|
159
|
+
var aborted;
|
|
160
|
+
var uri = options.uri || options.url;
|
|
161
|
+
var method = options.method || 'GET';
|
|
162
|
+
var body = options.body || options.data;
|
|
163
|
+
var headers = options.headers || {};
|
|
164
|
+
var sync = !!options.sync;
|
|
165
|
+
var isJson = false;
|
|
166
|
+
var timeoutTimer;
|
|
167
|
+
var failureResponse = {
|
|
168
|
+
body: undefined,
|
|
169
|
+
headers: {},
|
|
170
|
+
statusCode: 0,
|
|
171
|
+
method: method,
|
|
172
|
+
url: uri,
|
|
173
|
+
rawRequest: xhr,
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
if ('json' in options && options.json !== false) {
|
|
177
|
+
isJson = true;
|
|
178
|
+
headers['accept'] || headers['Accept'] || (headers['Accept'] = 'application/json'); //Don't override existing accept header declared by user
|
|
179
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
180
|
+
headers['content-type'] ||
|
|
181
|
+
headers['Content-Type'] ||
|
|
182
|
+
(headers['Content-Type'] = 'application/json'); //Don't override existing accept header declared by user
|
|
183
|
+
body = JSON.stringify(options.json === true ? body : options.json);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
xhr.onreadystatechange = readystatechange;
|
|
188
|
+
xhr.onload = loadFunc;
|
|
189
|
+
xhr.onerror = errorFunc;
|
|
190
|
+
// IE9 must have onprogress be set to a unique function.
|
|
191
|
+
xhr.onprogress = function () {
|
|
192
|
+
// IE must die
|
|
193
|
+
};
|
|
194
|
+
xhr.onabort = function () {
|
|
195
|
+
aborted = true;
|
|
196
|
+
};
|
|
197
|
+
xhr.ontimeout = errorFunc;
|
|
198
|
+
xhr.open(method, uri, !sync, options.username, options.password);
|
|
199
|
+
//has to be after open
|
|
200
|
+
if (!sync) {
|
|
201
|
+
xhr.withCredentials = !!options.withCredentials;
|
|
202
|
+
}
|
|
203
|
+
// Cannot set timeout with sync request
|
|
204
|
+
// not setting timeout on the xhr object, because of old webkits etc. not handling that correctly
|
|
205
|
+
// both npm's request and jquery 1.x use this kind of timeout, so this is being consistent
|
|
206
|
+
if (!sync && options.timeout > 0) {
|
|
207
|
+
timeoutTimer = setTimeout(function () {
|
|
208
|
+
if (aborted) return;
|
|
209
|
+
aborted = true; //IE9 may still call readystatechange
|
|
210
|
+
xhr.abort('timeout');
|
|
211
|
+
var e = new Error('XMLHttpRequest timeout');
|
|
212
|
+
e.code = 'ETIMEDOUT';
|
|
213
|
+
errorFunc(e);
|
|
214
|
+
}, options.timeout);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (xhr.setRequestHeader) {
|
|
218
|
+
for (key in headers) {
|
|
219
|
+
if (headers.hasOwnProperty(key)) {
|
|
220
|
+
xhr.setRequestHeader(key, headers[key]);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
} else if (options.headers && !isEmpty(options.headers)) {
|
|
224
|
+
throw new Error('Headers cannot be set on an XDomainRequest object');
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if ('responseType' in options) {
|
|
228
|
+
xhr.responseType = options.responseType;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if ('beforeSend' in options && typeof options.beforeSend === 'function') {
|
|
232
|
+
options.beforeSend(xhr);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Microsoft Edge browser sends "undefined" when send is called with undefined value.
|
|
236
|
+
// XMLHttpRequest spec says to pass null as body to indicate no body
|
|
237
|
+
// See https://github.com/naugtur/xhr/issues/100.
|
|
238
|
+
xhr.send(body || null);
|
|
239
|
+
|
|
240
|
+
return xhr;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function getXml(xhr) {
|
|
244
|
+
if (xhr.responseType === 'document') {
|
|
245
|
+
return xhr.responseXML;
|
|
246
|
+
}
|
|
247
|
+
var firefoxBugTakenEffect =
|
|
248
|
+
xhr.responseXML && xhr.responseXML.documentElement.nodeName === 'parsererror';
|
|
249
|
+
if (xhr.responseType === '' && !firefoxBugTakenEffect) {
|
|
250
|
+
return xhr.responseXML;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function noop() {}
|
|
257
|
+
|
|
258
|
+
export default createXHR;
|
package/src/progress-event.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import {isNumber} from 'lodash';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Object of the same shape as web browser ProgressEvents
|
|
9
|
-
* @class ProgressEvent
|
|
10
|
-
* @param {integer} loaded
|
|
11
|
-
* @param {integer} total
|
|
12
|
-
* @returns {ProgressEvent}
|
|
13
|
-
*/
|
|
14
|
-
export default function ProgressEvent(loaded, total) {
|
|
15
|
-
Object.defineProperties(this, {
|
|
16
|
-
loaded: {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
value: loaded,
|
|
19
|
-
writable: false,
|
|
20
|
-
},
|
|
21
|
-
total: {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
value: total,
|
|
24
|
-
writable: false,
|
|
25
|
-
},
|
|
26
|
-
lengthComputable: {
|
|
27
|
-
enumerable: true,
|
|
28
|
-
value:
|
|
29
|
-
isNumber(loaded) &&
|
|
30
|
-
!Number.isNaN(loaded) &&
|
|
31
|
-
isNumber(total) &&
|
|
32
|
-
!Number.isNaN(total) &&
|
|
33
|
-
total > 0,
|
|
34
|
-
writable: false,
|
|
35
|
-
},
|
|
36
|
-
});
|
|
37
|
-
}
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {isNumber} from 'lodash';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Object of the same shape as web browser ProgressEvents
|
|
9
|
+
* @class ProgressEvent
|
|
10
|
+
* @param {integer} loaded
|
|
11
|
+
* @param {integer} total
|
|
12
|
+
* @returns {ProgressEvent}
|
|
13
|
+
*/
|
|
14
|
+
export default function ProgressEvent(loaded, total) {
|
|
15
|
+
Object.defineProperties(this, {
|
|
16
|
+
loaded: {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
value: loaded,
|
|
19
|
+
writable: false,
|
|
20
|
+
},
|
|
21
|
+
total: {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
value: total,
|
|
24
|
+
writable: false,
|
|
25
|
+
},
|
|
26
|
+
lengthComputable: {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
value:
|
|
29
|
+
isNumber(loaded) &&
|
|
30
|
+
!Number.isNaN(loaded) &&
|
|
31
|
+
isNumber(total) &&
|
|
32
|
+
!Number.isNaN(total) &&
|
|
33
|
+
total > 0,
|
|
34
|
+
writable: false,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|