-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathrequest.js
More file actions
171 lines (140 loc) · 3.99 KB
/
request.js
File metadata and controls
171 lines (140 loc) · 3.99 KB
Edit and raw actions
OlderNewer
1
// var Base64 = require('Base64')
2
var capability = require('./capability')
3
var response = require('./response')
4
var stream = require('stream')
5
var util = require('util')
6
7
var IncomingMessage = response.IncomingMessage
8
var rStates = response.readyStates
9
10
// function copy (to, from) {
11
// if (!Array.isArray(from))
12
// from = [from]
13
// from.forEach(function (obj) {
14
// Object.keys(function (key), {
15
// to[key] = from[key]
16
// })
17
// })
18
// return to
19
// }
20
21
var ClientRequest = module.exports = function (opts) {
22
var self = this
23
stream.Writable.call(self)
24
25
self._opts = opts
26
self._body = []
27
self._fullHeaders = {}
28
Object.keys(opts.headers).forEach(function (name) {
29
self.setHeader(name, opts.headers[name])
30
})
31
32
self._mode = capability.mode
33
34
self.on('finish', self._onFinish.bind(self))
35
}
36
37
util.inherits(ClientRequest, stream.Writable)
38
39
ClientRequest.prototype.setHeader = function (name, value) {
40
var self = this
41
self._fullHeaders[name.toLowerCase()] = value
42
}
43
44
ClientRequest.prototype.getHeader = function (name) {
45
var self = this
46
return self._fullHeaders[name.toLowerCase()]
47
}
48
49
ClientRequest.prototype.removeHeader = function (name) {
50
var self = this
51
delete self._fullHeaders[name.toLowerCase()]
52
}
53
54
ClientRequest.prototype._onFinish = function () {
55
var self = this
56
57
var opts = self._opts
58
var url = opts.protocol + '//' + opts.hostname
59
+ (opts.port ? ':' + opts.port : '') + opts.path
60
61
var user, pass
62
if (opts.auth) {
63
var authMatch = opts.auth.match(/^([^:]*):(.*)$)/)
64
user = authMatch[0]
65
pass = authMatch[1]
66
}
67
68
// process and send data
69
var fullHeaders = self._fullHeaders
70
var body
71
if (typeof Blob === 'function') {
72
body = new Blob(self._body.map(function (buffer) {
73
return buffer.toArrayBuffer()
74
}), {
75
type: fullHeaders['content-type'] || ''
76
});
77
} else {
78
// get utf8 string
79
body = Buffer.concat(self._body).toString()
80
}
81
82
if (self._mode === 'fetch') {
83
var headers = Object.keys(fullHeaders).map(function (name) {
84
return [name, fullHeaders[name]]
85
})
86
87
fetch(url, {
88
method: self._opts.method,
89
headers: headers,
90
body: body,
91
mode: 'cors',
92
credentials: opts.credentials ? 'include' : 'omit'
93
}).then(function (response) {
94
self._fetchResponse = response
95
self._connect()
96
})
97
} else {
98
var xhr = self._xhr = new XMLHttpRequest() // TODO: old IE
99
xhr.open(self._opts.method, url, true, user, pass)
100
101
// Can't set responseType on really old browsers
102
if ('responseType' in xhr)
103
xhr.responseType = self._mode
104
105
if ('withCredentials' in xhr)
106
xhr.withCredentials = !!opts.credentials
107
108
if (self._mode === 'text' && 'overrideMimeType' in xhr)
109
xhr.overrideMimeType('text/plain; charset=x-user-defined')
110
111
Object.keys(fullHeaders, function (name) {
112
xhr.setRequestHeader(name, headers[name])
113
})
114
115
xhr.onreadystatechange = function () {
116
switch (xhr.readyState) {
117
case rStates.HEADERS_RECEIVED:
118
self._connect()
119
break
120
case rStates.LOADING:
121
case rStates.DONE:
122
self._response._onXHRReadyStateChange()
123
break
124
}
125
}
126
127
xhr.send(body)
128
// This is the best approximation to where 'socket' should be fired
129
process.nextTick(function () {
130
self.emit('socket')
131
})
132
}
133
}
134
135
ClientRequest.prototype._connect = function () {
136
var self = this
137
138
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode)
139
self.emit('response', self._response)
140
}
141
142
ClientRequest.prototype._write = function (chunk, encoding, cb) {
143
var self = this
144
145
self._body.push(chunk)
146
cb()
147
}
148
149
ClientRequest.prototype.abort = function () {
150
var self = this
151
if (self._xhr)
152
self._xhr.abort()
153
}
154
155
ClientRequest.prototype.end = function (data, encoding, cb) {
156
var self = this
157
if (typeof data === 'function') {
158
cb = data
159
data = undefined
160
}
161
162
if (data)
163
stream.Writable.push.call(self, data, encoding)
164
165
stream.Writable.prototype.end.call(self, cb)
166
}
167
168
ClientRequest.prototype.flushHeaders = function () {}
169
ClientRequest.prototype.setTimeout = function () {}
170
ClientRequest.prototype.setNoDelay = function () {}
171
ClientRequest.prototype.setSocketKeepAlive = function () {}