Skip to content

Latest commit

 

History

History
282 lines (244 loc) · 7.09 KB

File metadata and controls

282 lines (244 loc) · 7.09 KB
 
Jun 24, 2015
Jun 24, 2015
1
var capability = require('./capability')
Jul 13, 2015
Jul 13, 2015
2
var inherits = require('inherits')
Jun 24, 2015
Jun 24, 2015
3
var response = require('./response')
Apr 27, 2016
Apr 27, 2016
4
var stream = require('readable-stream')
Jan 13, 2016
Jan 13, 2016
5
var toArrayBuffer = require('to-arraybuffer')
Jun 24, 2015
Jun 24, 2015
6
7
var IncomingMessage = response.IncomingMessage
8
var rStates = response.readyStates
9
Sep 1, 2016
Sep 1, 2016
10
function decideMode (preferBinary, useFetch) {
11
if (capability.fetch && useFetch) {
Jul 2, 2015
Jul 2, 2015
12
return 'fetch'
13
} else if (capability.mozchunkedarraybuffer) {
14
return 'moz-chunked-arraybuffer'
15
} else if (capability.msstream) {
16
return 'ms-stream'
17
} else if (capability.arraybuffer && preferBinary) {
18
return 'arraybuffer'
Jul 3, 2015
Jul 3, 2015
19
} else if (capability.vbArray && preferBinary) {
20
return 'text:vbarray'
Jul 2, 2015
Jul 2, 2015
21
} else {
22
return 'text'
23
}
24
}
25
Jun 24, 2015
Jun 24, 2015
26
var ClientRequest = module.exports = function (opts) {
27
var self = this
28
stream.Writable.call(self)
29
30
self._opts = opts
31
self._body = []
Jul 7, 2015
Jul 7, 2015
32
self._headers = {}
Jul 7, 2015
Jul 7, 2015
33
if (opts.auth)
34
self.setHeader('Authorization', 'Basic ' + new Buffer(opts.auth).toString('base64'))
Sep 15, 2015
Sep 15, 2015
35
Object.keys(opts.headers).forEach(function (name) {
Jun 24, 2015
Jun 24, 2015
36
self.setHeader(name, opts.headers[name])
37
})
38
Jul 2, 2015
Jul 2, 2015
39
var preferBinary
Sep 1, 2016
Sep 1, 2016
40
var useFetch = true
41
if (opts.mode === 'disable-fetch') {
42
// If the use of XHR should be preferred and includes preserving the 'content-type' header
43
useFetch = false
44
preferBinary = true
45
} else if (opts.mode === 'prefer-streaming') {
Aug 20, 2015
Aug 20, 2015
46
// If streaming is a high priority but binary compatibility and
47
// the accuracy of the 'content-type' header aren't
Jul 2, 2015
Jul 2, 2015
48
preferBinary = false
Aug 20, 2015
Aug 20, 2015
49
} else if (opts.mode === 'allow-wrong-content-type') {
50
// If streaming is more important than preserving the 'content-type' header
Jul 2, 2015
Jul 2, 2015
51
preferBinary = !capability.overrideMimeType
Aug 20, 2015
Aug 20, 2015
52
} else if (!opts.mode || opts.mode === 'default' || opts.mode === 'prefer-fast') {
53
// Use binary if text streaming may corrupt data or the content-type header, or for speed
54
preferBinary = true
Jul 2, 2015
Jul 2, 2015
55
} else {
56
throw new Error('Invalid value for opts.mode')
57
}
Sep 1, 2016
Sep 1, 2016
58
self._mode = decideMode(preferBinary, useFetch)
Jun 24, 2015
Jun 24, 2015
59
Jul 2, 2015
Jul 2, 2015
60
self.on('finish', function () {
61
self._onFinish()
62
})
Jun 24, 2015
Jun 24, 2015
63
}
64
Jul 3, 2015
Jul 3, 2015
65
inherits(ClientRequest, stream.Writable)
Jun 24, 2015
Jun 24, 2015
66
67
ClientRequest.prototype.setHeader = function (name, value) {
68
var self = this
Jul 13, 2015
Jul 13, 2015
69
var lowerName = name.toLowerCase()
70
// This check is not necessary, but it prevents warnings from browsers about setting unsafe
71
// headers. To be honest I'm not entirely sure hiding these warnings is a good thing, but
72
// http-browserify did it, so I will too.
Sep 15, 2015
Sep 15, 2015
73
if (unsafeHeaders.indexOf(lowerName) !== -1)
Jul 13, 2015
Jul 13, 2015
74
return
75
76
self._headers[lowerName] = {
Jul 7, 2015
Jul 7, 2015
77
name: name,
78
value: value
79
}
Jun 24, 2015
Jun 24, 2015
80
}
81
82
ClientRequest.prototype.getHeader = function (name) {
83
var self = this
Jul 7, 2015
Jul 7, 2015
84
return self._headers[name.toLowerCase()].value
Jun 24, 2015
Jun 24, 2015
85
}
86
87
ClientRequest.prototype.removeHeader = function (name) {
88
var self = this
Jul 7, 2015
Jul 7, 2015
89
delete self._headers[name.toLowerCase()]
Jun 24, 2015
Jun 24, 2015
90
}
91
92
ClientRequest.prototype._onFinish = function () {
93
var self = this
94
Jul 13, 2015
Jul 13, 2015
95
if (self._destroyed)
96
return
Jun 24, 2015
Jun 24, 2015
97
var opts = self._opts
98
Jul 7, 2015
Jul 7, 2015
99
var headersObj = self._headers
Jun 24, 2015
Jun 24, 2015
100
var body
Sep 30, 2015
Sep 30, 2015
101
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
Jul 7, 2015
Jul 7, 2015
102
if (capability.blobConstructor) {
Sep 15, 2015
Sep 15, 2015
103
body = new global.Blob(self._body.map(function (buffer) {
Jan 13, 2016
Jan 13, 2016
104
return toArrayBuffer(buffer)
Jul 8, 2015
Jul 8, 2015
105
}), {
106
type: (headersObj['content-type'] || {}).value || ''
107
})
Jul 1, 2015
Jul 1, 2015
108
} else {
109
// get utf8 string
110
body = Buffer.concat(self._body).toString()
111
}
Jun 24, 2015
Jun 24, 2015
112
}
113
114
if (self._mode === 'fetch') {
Sep 15, 2015
Sep 15, 2015
115
var headers = Object.keys(headersObj).map(function (name) {
Jul 7, 2015
Jul 7, 2015
116
return [headersObj[name].name, headersObj[name].value]
Jun 24, 2015
Jun 24, 2015
117
})
118
Sep 15, 2015
Sep 15, 2015
119
global.fetch(self._opts.url, {
Jun 24, 2015
Jun 24, 2015
120
method: self._opts.method,
121
headers: headers,
122
body: body,
123
mode: 'cors',
Jul 20, 2015
Jul 20, 2015
124
credentials: opts.withCredentials ? 'include' : 'same-origin'
Jun 24, 2015
Jun 24, 2015
125
}).then(function (response) {
126
self._fetchResponse = response
127
self._connect()
Oct 2, 2015
Oct 2, 2015
128
}, function (reason) {
Jul 8, 2015
Jul 8, 2015
129
self.emit('error', reason)
Jun 24, 2015
Jun 24, 2015
130
})
131
} else {
Sep 15, 2015
Sep 15, 2015
132
var xhr = self._xhr = new global.XMLHttpRequest()
Jul 8, 2015
Jul 8, 2015
133
try {
Sep 12, 2015
Sep 12, 2015
134
xhr.open(self._opts.method, self._opts.url, true)
Jul 8, 2015
Jul 8, 2015
135
} catch (err) {
136
process.nextTick(function () {
137
self.emit('error', err)
138
})
139
return
140
}
Jun 24, 2015
Jun 24, 2015
141
142
// Can't set responseType on really old browsers
143
if ('responseType' in xhr)
Jul 3, 2015
Jul 3, 2015
144
xhr.responseType = self._mode.split(':')[0]
Jun 24, 2015
Jun 24, 2015
145
146
if ('withCredentials' in xhr)
Jul 7, 2015
Jul 7, 2015
147
xhr.withCredentials = !!opts.withCredentials
Jun 24, 2015
Jun 24, 2015
148
149
if (self._mode === 'text' && 'overrideMimeType' in xhr)
150
xhr.overrideMimeType('text/plain; charset=x-user-defined')
151
Sep 15, 2015
Sep 15, 2015
152
Object.keys(headersObj).forEach(function (name) {
Jul 7, 2015
Jul 7, 2015
153
xhr.setRequestHeader(headersObj[name].name, headersObj[name].value)
Jun 24, 2015
Jun 24, 2015
154
})
155
Jul 3, 2015
Jul 3, 2015
156
self._response = null
Jun 24, 2015
Jun 24, 2015
157
xhr.onreadystatechange = function () {
158
switch (xhr.readyState) {
159
case rStates.LOADING:
160
case rStates.DONE:
Jul 8, 2015
Jul 8, 2015
161
self._onXHRProgress()
Jun 24, 2015
Jun 24, 2015
162
break
163
}
164
}
Jul 2, 2015
Jul 2, 2015
165
// Necessary for streaming in Firefox, since xhr.response is ONLY defined
166
// in onprogress, not in onreadystatechange with xhr.readyState = 3
167
if (self._mode === 'moz-chunked-arraybuffer') {
168
xhr.onprogress = function () {
Jul 8, 2015
Jul 8, 2015
169
self._onXHRProgress()
Jul 2, 2015
Jul 2, 2015
170
}
171
}
Jun 24, 2015
Jun 24, 2015
172
Jul 8, 2015
Jul 8, 2015
173
xhr.onerror = function () {
Jul 13, 2015
Jul 13, 2015
174
if (self._destroyed)
175
return
Jul 8, 2015
Jul 8, 2015
176
self.emit('error', new Error('XHR error'))
177
}
178
179
try {
180
xhr.send(body)
181
} catch (err) {
182
process.nextTick(function () {
183
self.emit('error', err)
184
})
185
return
186
}
Jun 24, 2015
Jun 24, 2015
187
}
188
}
189
Jul 3, 2015
Jul 3, 2015
190
/**
Oct 13, 2015
Oct 13, 2015
191
* Checks if xhr.status is readable and non-zero, indicating no error.
192
* Even though the spec says it should be available in readyState 3,
193
* accessing it throws an exception in IE8
Jul 3, 2015
Jul 3, 2015
194
*/
195
function statusValid (xhr) {
196
try {
Oct 13, 2015
Oct 13, 2015
197
var status = xhr.status
198
return (status !== null && status !== 0)
Jul 3, 2015
Jul 3, 2015
199
} catch (e) {
200
return false
201
}
202
}
203
Jul 8, 2015
Jul 8, 2015
204
ClientRequest.prototype._onXHRProgress = function () {
205
var self = this
206
Jul 13, 2015
Jul 13, 2015
207
if (!statusValid(self._xhr) || self._destroyed)
Jul 8, 2015
Jul 8, 2015
208
return
209
210
if (!self._response)
211
self._connect()
212
213
self._response._onXHRProgress()
214
}
215
Jun 24, 2015
Jun 24, 2015
216
ClientRequest.prototype._connect = function () {
217
var self = this
218
Jul 13, 2015
Jul 13, 2015
219
if (self._destroyed)
220
return
221
Jun 24, 2015
Jun 24, 2015
222
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode)
223
self.emit('response', self._response)
224
}
225
226
ClientRequest.prototype._write = function (chunk, encoding, cb) {
227
var self = this
228
229
self._body.push(chunk)
230
cb()
231
}
232
Jul 13, 2015
Jul 13, 2015
233
ClientRequest.prototype.abort = ClientRequest.prototype.destroy = function () {
Jun 24, 2015
Jun 24, 2015
234
var self = this
Jul 13, 2015
Jul 13, 2015
235
self._destroyed = true
Jul 13, 2015
Jul 13, 2015
236
if (self._response)
237
self._response._destroyed = true
Jun 24, 2015
Jun 24, 2015
238
if (self._xhr)
239
self._xhr.abort()
Jul 13, 2015
Jul 13, 2015
240
// Currently, there isn't a way to truly abort a fetch.
241
// If you like bikeshedding, see https://github.com/whatwg/fetch/issues/27
Jun 24, 2015
Jun 24, 2015
242
}
243
244
ClientRequest.prototype.end = function (data, encoding, cb) {
245
var self = this
246
if (typeof data === 'function') {
247
cb = data
248
data = undefined
249
}
250
Aug 8, 2015
Aug 8, 2015
251
stream.Writable.prototype.end.call(self, data, encoding, cb)
Jun 24, 2015
Jun 24, 2015
252
}
253
254
ClientRequest.prototype.flushHeaders = function () {}
255
ClientRequest.prototype.setTimeout = function () {}
256
ClientRequest.prototype.setNoDelay = function () {}
257
ClientRequest.prototype.setSocketKeepAlive = function () {}
Jul 13, 2015
Jul 13, 2015
258
259
// Taken from http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader%28%29-method
260
var unsafeHeaders = [
261
'accept-charset',
262
'accept-encoding',
263
'access-control-request-headers',
264
'access-control-request-method',
265
'connection',
266
'content-length',
267
'cookie',
268
'cookie2',
269
'date',
270
'dnt',
271
'expect',
272
'host',
273
'keep-alive',
274
'origin',
275
'referer',
276
'te',
277
'trailer',
278
'transfer-encoding',
279
'upgrade',
280
'user-agent',
281
'via'
Jul 13, 2015
Jul 13, 2015
282
]