forked from kamicane/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
354 lines (266 loc) · 8.85 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
/*
Agent
- heavily inspired by superagent by visionmedia https://github.com/visionmedia/superagent, released under the MIT license
- code derived from MooTools 1.4 Request.js && superagent
- MIT-License
*/"use strict"
var prime = require("prime"),
array = require("prime/es5/array"),
string = require("prime/shell/string"),
Emitter = require("prime/emitter")
var trim = string.trim,
capitalize = string.capitalize
// MooTools
var getRequest = (function(){
var XMLHTTP = function(){
return new XMLHttpRequest()
}, MSXML2 = function(){
return new ActiveXObject("MSXML2.XMLHTTP")
}, MSXML = function(){
return new ActiveXObject("Microsoft.XMLHTTP")
}
try {XMLHTTP(); return XMLHTTP} catch(e){}
try {MSXML2(); return MSXML2} catch(e){}
try {MSXML(); return MSXML} catch(e){}
return null
})()
var encodeJSON = function(object){
if (object == null) return ""
if (object.toJSON) return object.toJSON()
return JSON.stringify(object)
}
// MooTools
var encodeQueryString = function(object, base){
if (object == null) return ""
if (object.toQueryString) return object.toQueryString()
var queryString = []
for (var key in object) (function(key, value){
if (base) key = base + "[" + key + "]"
var result
if (value == null) return
if (array.isArray(value)){
var qs = {}
for (var i = 0; i < value.length; i++) qs[i] = value[i]
result = encodeQueryString(qs, key)
} else if (typeof value === "object"){
result = encodeQueryString(value, key)
} else {
result = key + "=" + encodeURIComponent(value)
}
queryString.push(result)
})(key, object[key])
return queryString.join("&")
}
var decodeJSON = JSON.parse
// decodeQueryString by Brian Donovan
// http://stackoverflow.com/users/549363/brian-donovan
var decodeQueryString = function(params){
var pairs = params.split('&'),
result = {}
for (var i = 0; i < pairs.length; i++){
var pair = pairs[i].split('='),
key = decodeURIComponent(pair[0]),
value = decodeURIComponent(pair[1]),
isArray = /\[\]$/.test(key),
dictMatch = key.match(/^(.+)\[([^\]]+)\]$/)
if (dictMatch){
key = dictMatch[1]
var subkey = dictMatch[2]
result[key] = result[key] || {}
result[key][subkey] = value
} else if (isArray){
key = key.substring(0, key.length - 2)
result[key] = result[key] || []
result[key].push(value)
} else {
result[key] = value
}
}
return result
}
var encoders = {
"application/json" : encodeJSON,
"application/x-www-form-urlencoded" : encodeQueryString
}
var decoders = {
"application/json": decodeJSON,
"application/x-www-form-urlencoded": decodeQueryString
}
// parseHeader from superagent
// https://github.com/visionmedia/superagent
// MIT
var parseHeader = function(str){
var lines = str.split(/\r?\n/), fields = {}
lines.pop(); // trailing CRLF
for (var i = 0, l = lines.length; i < l; ++i){
var line = lines[i],
index = line.indexOf(':'),
field = capitalize(line.slice(0, index)),
value = trim(line.slice(index + 1))
fields[field] = value
}
return fields
}
var Request = prime({
constructor: function Request(){
var xhr = this._xhr = getRequest(),
self = this
if (xhr.addEventListener) array.forEach("progress|load|error|abort|loadend".split("|"), function(method){
xhr.addEventListener(method, function(event){
self.emit(method, event);
}, false)
})
this._header = {
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/x-www-form-urlencoded"
}
},
header: function(name, value){
if (typeof name === "object") for (var key in name) this.header(key, name[key])
else if (!arguments.length) return this._header
else if (arguments.length === 1) return this._header[capitalize(name)]
else if (arguments.length === 2){
if (value == null) delete this._header[capitalize(name)]
else this._header[capitalize(name)] = value
}
return this
},
running: function(){
return !!this._running
},
abort: function(){
if (this._running){
this._xhr.abort()
delete this._running
this._xhr.onreadystatechange = function(){}
this._xhr = getRequest()
}
return this
},
method: function(m){
if (!arguments.length) return this._method
this._method = m.toUpperCase()
return this
},
data: function(d){
if (!arguments.length) return this._data
this._data = d
return this
},
url: function(u){
if (!arguments.length) return this._url
this._url = u
return this
},
user: function(u){
if (!arguments.length) return this._user
this._user = u
return this
},
password: function(p){
if (!arguments.length) return this._password
this._password = p
return this
},
send: function(callback){
if (this._running) this.abort()
this._running = true
var method = this._method || "POST",
data = this._data || null,
url = this._url,
user = this._user || null,
password = this._password || null
var self = this, xhr = this._xhr
if (data && typeof data !== "string"){
var type = this._header['Content-Type'].split(/ *; */).shift(),
encode = encoders[type]
if (encode) data = encode(data)
}
if (/GET|HEAD/.test(method) && data) url += (url.indexOf("?") > -1 ? "&" : "?") + data
xhr.open(method, url, true, user, password)
if (user != null && "withCredentials" in xhr) xhr.withCredentials = true
xhr.onreadystatechange = function(){
if (xhr.readyState === 4){
delete self._running
xhr.onreadystatechange = function(){}
if (callback) callback(new Response(xhr.responseText, xhr.status, parseHeader(xhr.getAllResponseHeaders())))
}
}
for (var field in this._header) xhr.setRequestHeader(field, this._header[field])
xhr.send(data || null)
return this
}
})
Request.implement(new Emitter)
var Response = prime({
constructor: function Response(text, status, header){
this.text = text
this.status = status
var type = header['Content-Type'] ? header['Content-Type'].split(/ *; */).shift() : '',
decode = decoders[type]
this.body = decode ? decode(this.text) : this.text
this._header = header
// statuses from superagent
// https://github.com/visionmedia/superagent
// MIT
var t = status / 100 | 0
this.info = t === 1
this.ok = t === 2
this.clientError = t === 4
this.serverError = t === 5
this.error = t === 4 || t === 5
// sugar
this.accepted = status === 202
this.noContent = status === 204 || status === 1223
this.badRequest = status === 400
this.unauthorized = status === 401
this.notAcceptable = status === 406
this.notFound = status === 404
},
header: function(name){
return (name) ? this._header[capitalize(name)] : null
}
})
var methods = "get|post|put|delete|head|patch|options",
rMethods = RegExp("^" + methods + "$", "i")
var agent = function(method, url, data, callback){
var request = new Request()
agent.emit('request', request)
if (!rMethods.test(method)){ // shift
callback = data
data = url
url = method
method = "post"
}
if (typeof data === "function"){
callback = data
data = null
}
request.method(method)
if (url) request.url(url)
if (data) request.data(data)
request.send(callback)
return request
}
agent.encoder = function(ct, encode){
if (arguments.length === 1) return encoders[ct]
encoders[ct] = encode
return agent
}
agent.decoder = function(ct, decode){
if (arguments.length === 1) return decoders[ct]
decoders[ct] = decode
return agent
}
var emitter = new Emitter
agent.on = emitter.on
agent.off = emitter.off
agent.emit = emitter.emit
array.forEach(methods.split("|"), function(method){
agent[method] = function(url, data, callback){
return agent(method, url, data, callback)
}
})
agent.Request = Request
agent.Response = Response
module.exports = agent