forked from pyrsmk/qwest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
470 lines (420 loc) · 12.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
'use strict'
function getXHR() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest()
}
let xhr
try {
xhr = new ActiveXObject('Msxml2.XMLHTTP')
} catch (e) {
try {
xhr = new ActiveXObject('Microsoft.XMLHTTP')
} catch (e) {}
}
if (!xhr) {
console.error('ajax.js: your browser does not support XMLHttpRequest')
}
return xhr
}
function AjaxPromise(executor = function() {}) {
let _resolve, _reject
let p = new Promise(function (resolve, reject) {
_resolve = resolve
_reject = reject
return executor(resolve, reject)
})
p.__proto__ = AjaxPromise.prototype
p._resolve = _resolve
p._reject = _reject
return p
}
AjaxPromise.__proto__ = Promise
AjaxPromise.prototype.__proto__ = Promise.prototype
AjaxPromise.prototype.then = function(onFulfilled, onRejected) {
let p = Promise.prototype.then.call(this, onFulfilled, onRejected)
return this._prepare(p)
}
AjaxPromise.prototype.catch = function(onFulfilled, onRejected) {
let p = Promise.prototype.catch.call(this, onFulfilled, onRejected)
return this._prepare(p)
}
AjaxPromise.prototype._prepare = function(instance) {
if (!instance._prepared) {
instance.abort = this.abort
instance.send = this.send
instance.xhr = this.xhr
instance._prepared = true
}
return instance
}
AjaxPromise.prototype.resolve = function() {
this._resolve.apply(this, arguments)
}
AjaxPromise.prototype.reject = function() {
this._reject.apply(this, arguments)
}
function queryString(obj) {
let buf = []
for (let p in obj) {
if (obj.hasOwnProperty(p)) {
buf.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]))
}
}
return buf.join('&')
}
// Variables for limit mechanism
let requests = 0
let request_stack = []
// Guess XHR version
const xhr2 = (function() {
let xhr = getXHR()
return xhr && xhr.responseType === ''
})()
const MimeTypes = {
text: '*/*',
xml: 'text/xml',
json: 'application/json',
post: 'application/x-www-form-urlencoded'
}
const Accept = {
text: '*/*',
xml: 'application/xml; q=1.0, text/xml; q=0.8, */*; q=0.1',
json: 'application/json; q=1.0, text/*; q=0.8, */*; q=0.1'
}
/**
* @param {String} method
* @param {String} url
* @param {Object|String} data
* @param {Object} options
* @return {Promise}
*/
function request(method, url, data = null, options = {}) {
method = method.toUpperCase()
// Define variables
let nativeResponseParsing = false,
crossOrigin,
xhr,
xdr = false,
timeoutInterval,
aborted = false,
attempts = 0,
headers = {},
i, j,
serialized,
response,
sending = false,
delayed = false,
timeout_start
let promise = new AjaxPromise()
promise.abort = abort
promise.send = function() {
// Prevent further send() calls
if (sending) {
return
}
// Reached request limit, get out!
if (requests == ajax.config.limit) {
request_stack.push(promise)
return
}
++requests
sending = true
// Start the chrono
timeout_start = new Date().getTime()
// Open connection
if (xdr) {
xhr.open(method, url)
} else {
xhr.open(method, url, true, options.user, options.password)
if (xhr2) {
xhr.withCredentials = options.withCredentials
}
}
// Set headers
if (!xdr) {
for (let i in headers) {
if (headers[i]) {
xhr.setRequestHeader(i, headers[i])
}
}
}
// Verify if the response type is supported by the current browser
if (xhr2 && options.responseType != 'document' && options.responseType != 'auto') { // Don't verify for 'document' since we're using an internal routine
try {
xhr.responseType = options.responseType
nativeResponseParsing = (xhr.responseType == options.responseType)
} catch (e) {}
}
// Plug response handler
if (xhr2 || xdr) {
xhr.onload = handleResponse
xhr.onerror = handleError
} else {
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
handleResponse()
}
}
}
// Override mime type to ensure the response is well parsed
if (options.responseType != 'auto' && 'overrideMimeType' in xhr) {
xhr.overrideMimeType(MimeTypes[options.responseType])
}
// Send request
if (xdr) {
// http://cypressnorth.com/programming/internet-explorer-aborting-ajax-requests-fixed/
xhr.onprogress = function() {}
xhr.ontimeout = function() {}
xhr.onerror = function() {}
// https://developer.mozilla.org/en-US/docs/Web/API/XDomainRequest
setTimeout(function() {
xhr.send(method != 'GET' ? data : null)
})
} else {
xhr.send(method != 'GET' ? data : null)
}
}
// Get XHR object
xhr = getXHR()
if (crossOrigin) {
if (!('withCredentials' in xhr) && window.XDomainRequest) {
xhr = new XDomainRequest() // CORS with IE8/9
xdr = true
if (method != 'GET' && method != 'POST') {
console.warn('ajax.js: for request '+method+' '+url+' method will be forced to POST')
method = 'POST'
}
}
}
promise.xhr = xhr
promise._prepared = true
function abort() {
if (xhr) {
xhr.abort()
--requests
aborted = true
}
}
// Handle the response
function handleResponse() {
// Prepare
let i, responseType
--requests
sending = false
// Verify timeout state
// --- https://stackoverflow.com/questions/7287706/ie-9-javascript-error-c00c023f
if (new Date().getTime() - timeout_start >= options.timeout) {
if (!options.attempts || ++attempts != options.attempts) {
promise.send()
} else {
promise.reject(new Error('Timeout ('+url+')'), response)
}
return
}
// Launch next stacked request
if (request_stack.length) {
request_stack.shift().send()
}
// Handle response
try {
// Process response
if (nativeResponseParsing && ('response' in xhr) && xhr.response !== null) {
response = xhr.response
} else if (options.responseType == 'document') {
let frame = document.createElement('iframe')
frame.style.display = 'none'
document.body.appendChild(frame)
frame.contentDocument.open()
frame.contentDocument.write(xhr.response)
frame.contentDocument.close()
response = frame.contentDocument
document.body.removeChild(frame)
} else {
// Guess response type
responseType = options.responseType
if (responseType == 'auto') {
if (xdr) {
responseType = ajax.config.defaultXdrResponseType
} else {
let ct = xhr.getResponseHeader('Content-Type') || ''
if (ct.indexOf(MimeTypes.json) >- 1) {
responseType = 'json'
} else if (ct.indexOf(MimeTypes.xml) >- 1) {
responseType = 'xml'
} else {
responseType = ajax.config.defaultResponseType
}
}
}
// Handle response type
switch (responseType) {
case 'json':
if (xhr.responseText.length) {
try {
if ('JSON' in window) {
response = JSON.parse(xhr.responseText)
} else {
response = eval('('+xhr.responseText+')')
}
} catch (e) {
throw 'Error while parsing JSON body : ' + e
}
}
break
case 'xml':
// Based on jQuery's parseXML() function
try {
// Standard
if (window.DOMParser) {
response = (new DOMParser()).parseFromString(xhr.responseText, 'text/xml')
}
// IE<9
else {
response = new ActiveXObject('Microsoft.XMLDOM')
response.async = 'false'
response.loadXML(xhr.responseText)
}
}
catch (e) {
response = undefined
console.error('ajax.js: failed to parse XML', e)
}
if (!response || !response.documentElement || response.getElementsByTagName('parsererror').length) {
throw 'Invalid XML'
}
break
default:
response = xhr.responseText
break
}
}
// Late status code verification to allow passing data when, per example,
// a 409 is returned
// --- https://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request
if ('status' in xhr && !/^2|1223/.test(xhr.status)) {
throw xhr.status+' ('+xhr.statusText+')'
}
// Fulfilled
promise.resolve(response)
} catch (e) {
// Rejected
promise.reject(e, response)
}
}
// Handle errors
function handleError(e) {
--requests
promise.reject(e, null)
}
// Normalize options
options.cache = 'cache' in options ? !!options.cache : false
options.dataType = 'dataType' in options ? options.dataType.toLowerCase() : ajax.config.defaultDataType
options.responseType = 'responseType' in options ? options.responseType.toLowerCase() : 'auto'
options.user = options.user || ''
options.password = options.password || ''
options.withCredentials = !!options.withCredentials
options.timeout = 'timeout' in options ? parseInt(options.timeout, 10) : 30000
options.attempts = 'attempts' in options ? parseInt(options.attempts, 10) : 1
options.send = 'send' in options ? !!options.send : true
// Guess if we're dealing with a cross-origin request
i = url.match(/\/\/(.+?)\//)
crossOrigin = i && (i[1] ? i[1] != location.host : false)
// Prepare data
if ('ArrayBuffer' in window && data instanceof ArrayBuffer) {
options.dataType = 'arraybuffer'
}
else if ('Blob' in window && data instanceof Blob) {
options.dataType = 'blob'
}
else if ('Document' in window && data instanceof Document) {
options.dataType = 'document'
}
else if ('FormData' in window && data instanceof FormData) {
options.dataType = 'formdata'
}
switch (options.dataType) {
case 'json':
data = (data !== null ? JSON.stringify(data) : data)
break
case 'post':
if (typeof data == 'object') {
data = queryString(data)
}
break
}
// Prepare headers
if (options.headers) {
let format = (match, p1, p2) => p1 + p2.toUpperCase()
for (i in options.headers) {
headers[i.replace(/(^|-)([^-])/g, format)] = options.headers[i]
}
}
if (!('Content-Type' in headers) && method != 'GET') {
if (options.dataType in MimeTypes) {
if (MimeTypes[options.dataType]) {
headers['Content-Type'] = MimeTypes[options.dataType]
}
}
}
if (!headers.Accept) {
headers['Accept'] = (options.responseType in Accept) ? Accept[options.responseType] : '*/*'
}
if (!crossOrigin && !('X-Requested-With' in headers) && ajax.config.autoXRequestedWith) { // (that header breaks in legacy browsers with CORS)
headers['X-Requested-With'] = 'XMLHttpRequest'
}
if (!options.cache && !('Cache-Control' in headers)) {
headers['Cache-Control'] = 'no-cache'
}
// Prepare URL
if (method == 'GET' && data && typeof data == 'string') {
url += (url.indexOf('?') != -1 ? '&' : '?') + data
}
// Start the request
if (options.send) {
promise.send()
}
// Return promise
return promise
}
const ajax = {
config: {
// Default data type
defaultDataType: 'post',
// Default response type in auto mode
defaultResponseType: 'text',
// Default response type for XDR in auto mode
defaultXdrResponseType: 'text',
// Whether to add X-Requested-With: XMLHttpRequest header
autoXRequestedWith: true,
// Simultaneous requests limit
limit: null
},
xhr2,
get() {
let args = ['GET']
args.push.apply(args, arguments)
return request.apply(window, args)
},
post() {
let args = ['POST']
args.push.apply(args, arguments)
return request.apply(window, args)
},
put() {
let args = ['PUT']
args.push.apply(args, arguments)
return request.apply(window, args)
},
delete() {
let args = ['DELETE']
args.push.apply(args, arguments)
return request.apply(window, args)
},
map() {
return request.apply(window, arguments)
},
getOpenRequests() {
return requests
}
}
module.exports = ajax