-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.js
357 lines (320 loc) · 10 KB
/
http.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
/*
* http v1.0.0 ~ (c) 2016-2020 LeoZhang
*/
(function(exports) {
var REJECTED=-1,PENDING=0,RESOLVED=1;
function Promise(resolver) {
if (! (this instanceof Promise)) return new Promise(resolver)
var self = this;
self.callbacks = [] ;
self.status = PENDING;
function resolve(value) {
if (value instanceof Promise) {
return value.then(resolve, reject)
}
setTimeout(function() {
if (self.status !== PENDING) {
return;
}
self.status = RESOLVED;
self.data = value;
for (var i = 0; i < self.callbacks.length; i++) {
self.callbacks[i].onResolved(value);
}
})
}
function reject(reason) {
setTimeout(function() {
if (self.status !== PENDING) {
return
}
self.status = REJECTED;
self.data = reason;
for (var i = 0; i < self.callbacks.length; i++) {
self.callbacks[i].onRejected(reason)
}
})
}
try {
resolver(resolve, reject)
} catch(e) {
reject(e)
}
}
function resolvePromise(promise, x, resolve, reject) {
var then
var thenCalledOrThrow = false
if (promise === x) {
return reject(new TypeError('Chaining cycle detected for promise!'))
}
if (x instanceof Promise) {
if (x.status === PENDING) {
x.then(function(v) {
resolvePromise(promise, v, resolve, reject);
},
reject);
} else {
x.then(resolve, reject)
}
return;
}
if ((x !== null) && ((typeof x === 'object') || (typeof x === 'function'))) {
try {
then = x.then
if (typeof then === 'function') {
then.call(x,
function rs(y) {
if (thenCalledOrThrow) return thenCalledOrThrow = true;
return resolvePromise(promise, y, resolve, reject)
},
function rj(r) {
if (thenCalledOrThrow) return thenCalledOrThrow = true;
return reject(r);
})
} else {
return resolve(x)
}
} catch(e) {
if (thenCalledOrThrow) return thenCalledOrThrow = true;
return reject(e);
}
} else {
return resolve(x);
}
}
Promise.prototype.then = function(onResolved, onRejected) {
onResolved = typeof onResolved === 'function' ? onResolved: function(v) {
return v;
}
onRejected = typeof onRejected === 'function' ? onRejected: function(r) {
throw r;
}
var self = this;
var promise2;
if (self.status === RESOLVED) {
return promise2 = new Promise(function(resolve, reject) {
setTimeout(function() {
try {
var value = onResolved(self.data) ;
resolvePromise(promise2, value, resolve, reject);
} catch(e) {
return reject(e);
}
})
})
}
if (self.status === REJECTED) {
return promise2 = new Promise(function(resolve, reject) {
setTimeout(function() {
try {
var value = onRejected(self.data);
resolvePromise(promise2, value, resolve, reject);
} catch(e) {
return reject(e);
}
})
})
}
if (self.status === PENDING) {
return promise2 = new Promise(function(resolve, reject) {
self.callbacks.push({
onResolved: function(value) {
try {
var value = onResolved(value);
resolvePromise(promise2, value, resolve, reject);
} catch(e) {
return reject(e);
}
},
onRejected: function(reason) {
try {
var value = onRejected(reason);
resolvePromise(promise2, value, resolve, reject);
} catch(e) {
return reject(e);
}
}
})
})
}
}
Promise.prototype.catch = function(onRejected) {
return this.then(null, onRejected);
}
Promise.prototype.finally = function(fn) {
return this.then(function(v) {
setTimeout(fn);
return v;
},
function(r) {
setTimeout(fn);
throw r;
})
}
Promise.prototype.success = function(fn) {
//assertArgFn(fn, 'fn');
return this.then(fn);
};
Promise.prototype.error = function(fn) {
//assertArgFn(fn, 'fn');
return this.then(null,fn);
};
Promise.prototype.delay = function(duration) {
return this.then(function(value) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(value);
},
duration);
});
},
function(reason) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
reject(reason)
},
duration);
});
});
}
Promise.resolve = function(value) {
return new Promise(function(resolve) {
resolve(value);
});
}
Promise.reject = function(reason) {
return new Promise(function(resolve, reject) {
reject(reason);
});
}
Promise.done = Promise.stop = function() {
return new Promise(function() {});
}
Promise.deferred = Promise.defer = function() {
var dfd = {};
dfd.promise = new Promise(function(resolve, reject) {
dfd.resolve = resolve;
dfd.reject = reject;
});
return dfd;
}
/*
* AJAX requests
*/
function _encode(data) {
var payload = "";
if (typeof data === "string") {
payload = data;
} else {
var e = encodeURIComponent;
var params = [];
for (var k in data) {
if (data.hasOwnProperty(k)) {
params.push(e(k) + '=' + e(data[k]));
}
}
payload = params.join('&');
}
return payload;
}
function _createXhr() {
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xhr;
}
function _ajax(method, url, data, headers) {
var p = Promise.deferred();
var xhr, payload;
data = data || {};
headers = headers || {};
try {
xhr = _createXhr();
} catch(e) {
p.reject(e);
return p;
}
payload = _encode(data);
if (method === 'GET' && payload) {
url += '?' + payload;
payload = null;
}
xhr.open(method, url);
var content_type = 'application/x-www-form-urlencoded';
for (var h in headers) {
if (headers.hasOwnProperty(h)) {
if (h.toLowerCase() === 'content-type') content_type = headers[h];
else xhr.setRequestHeader(h, headers[h]);
}
}
xhr.setRequestHeader('Content-type', content_type);
function onTimeout() {
xhr.abort();
p.reject($http.ETIMEOUT);
}
var timeout = $http.ajaxTimeout;
if (timeout) {
var tid = setTimeout(onTimeout, timeout);
}
xhr.onreadystatechange = function() {
if (timeout) {
clearTimeout(tid);
}
if (xhr.readyState === 4) {
//var err = ;
if (!xhr.status || (xhr.status < 200 || xhr.status >= 300) && xhr.status !== 304) p.reject(xhr.status);
else p.resolve(xhr.responseText);
}
};
xhr.send(payload);
return p.promise;
}
function _ajaxer(method) {
return function(url, data, headers) {
return _ajax(method, url, data, headers);
};
}
function request(options){
// User defined options
var _options={
method:"GET",
url:"",
data:{},
headers:""
}
for (i in options) _options[i] = options[i];
return _ajax(_options.method, _options.url, _options.data, _options.headers);
}
var $http = {
request:request,
promise: Promise.deferred,
get: _ajaxer('GET'),
post: _ajaxer('POST'),
put: _ajaxer('PUT'),
delete: _ajaxer('DELETE'),
/* Error codes */
ENOXHR: 1,
ETIMEOUT: 2,
/**
* Aborted requests resolve the promise with a ETIMEOUT error
* code.
*/
ajaxTimeout: 0
};
if (typeof module != 'undefined' && module.exports) {
module.exports = $http;
} else if (typeof define == 'function' && define.amd) {
define(function() {
return $http;
});
} else {
exports.$http = $http;
}
})(this);