Skip to content

Latest commit

 

History

History
352 lines (301 loc) · 8.52 KB

File metadata and controls

352 lines (301 loc) · 8.52 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')
Jun 24, 2015
Jun 24, 2015
5
6
var IncomingMessage = response.IncomingMessage
7
var rStates = response.readyStates
8
Sep 1, 2016
Sep 1, 2016
9
function decideMode (preferBinary, useFetch) {
10
if (capability.fetch && useFetch) {
Jul 2, 2015
Jul 2, 2015
11
return 'fetch'
12
} else if (capability.mozchunkedarraybuffer) {
13
return 'moz-chunked-arraybuffer'
14
} else if (capability.msstream) {
15
return 'ms-stream'
16
} else if (capability.arraybuffer && preferBinary) {
17
return 'arraybuffer'
18
} else {
19
return 'text'
20
}
21
}
22
Jun 24, 2015
Jun 24, 2015
23
var ClientRequest = module.exports = function (opts) {
24
var self = this
25
stream.Writable.call(self)
26
27
self._opts = opts
28
self._body = []
Jul 7, 2015
Jul 7, 2015
29
self._headers = {}
Jul 7, 2015
Jul 7, 2015
30
if (opts.auth)
Sep 24, 2018
Sep 24, 2018
31
self.setHeader('Authorization', 'Basic ' + Buffer.from(opts.auth).toString('base64'))
Sep 15, 2015
Sep 15, 2015
32
Object.keys(opts.headers).forEach(function (name) {
Jun 24, 2015
Jun 24, 2015
33
self.setHeader(name, opts.headers[name])
34
})
35
Jul 2, 2015
Jul 2, 2015
36
var preferBinary
Sep 1, 2016
Sep 1, 2016
37
var useFetch = true
Jan 15, 2018
Jan 15, 2018
38
if (opts.mode === 'disable-fetch' || ('requestTimeout' in opts && !capability.abortController)) {
Jan 15, 2018
Jan 15, 2018
39
// If the use of XHR should be preferred. Not typically needed.
Sep 1, 2016
Sep 1, 2016
40
useFetch = false
41
preferBinary = true
42
} else if (opts.mode === 'prefer-streaming') {
Aug 20, 2015
Aug 20, 2015
43
// If streaming is a high priority but binary compatibility and
44
// the accuracy of the 'content-type' header aren't
Jul 2, 2015
Jul 2, 2015
45
preferBinary = false
Aug 20, 2015
Aug 20, 2015
46
} else if (opts.mode === 'allow-wrong-content-type') {
47
// If streaming is more important than preserving the 'content-type' header
Jul 2, 2015
Jul 2, 2015
48
preferBinary = !capability.overrideMimeType
Aug 20, 2015
Aug 20, 2015
49
} else if (!opts.mode || opts.mode === 'default' || opts.mode === 'prefer-fast') {
50
// Use binary if text streaming may corrupt data or the content-type header, or for speed
51
preferBinary = true
Jul 2, 2015
Jul 2, 2015
52
} else {
53
throw new Error('Invalid value for opts.mode')
54
}
Sep 1, 2016
Sep 1, 2016
55
self._mode = decideMode(preferBinary, useFetch)
May 9, 2018
May 9, 2018
56
self._fetchTimer = null
Apr 13, 2021
Apr 13, 2021
57
self._socketTimeout = null
58
self._socketTimer = null
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) {
Apr 26, 2017
Apr 26, 2017
83
var header = this._headers[name.toLowerCase()]
84
if (header)
85
return header.value
86
return null
Jun 24, 2015
Jun 24, 2015
87
}
88
89
ClientRequest.prototype.removeHeader = function (name) {
90
var self = this
Jul 7, 2015
Jul 7, 2015
91
delete self._headers[name.toLowerCase()]
Jun 24, 2015
Jun 24, 2015
92
}
93
94
ClientRequest.prototype._onFinish = function () {
95
var self = this
96
Jul 13, 2015
Jul 13, 2015
97
if (self._destroyed)
98
return
Jun 24, 2015
Jun 24, 2015
99
var opts = self._opts
100
Apr 14, 2021
Apr 14, 2021
101
if ('timeout' in opts && opts.timeout !== 0) {
102
self.setTimeout(opts.timeout)
103
}
104
Jul 7, 2015
Jul 7, 2015
105
var headersObj = self._headers
Jan 16, 2017
Jan 16, 2017
106
var body = null
Apr 4, 2017
Apr 4, 2017
107
if (opts.method !== 'GET' && opts.method !== 'HEAD') {
Sep 24, 2018
Sep 24, 2018
108
body = new Blob(self._body, {
109
type: (headersObj['content-type'] || {}).value || ''
110
});
111
}
Jun 24, 2015
Jun 24, 2015
112
Apr 4, 2017
Apr 4, 2017
113
// create flattened list of headers
114
var headersList = []
115
Object.keys(headersObj).forEach(function (keyName) {
116
var name = headersObj[keyName].name
117
var value = headersObj[keyName].value
118
if (Array.isArray(value)) {
119
value.forEach(function (v) {
120
headersList.push([name, v])
121
})
122
} else {
123
headersList.push([name, value])
124
}
125
})
Jun 24, 2015
Jun 24, 2015
126
Apr 4, 2017
Apr 4, 2017
127
if (self._mode === 'fetch') {
Jan 15, 2018
Jan 15, 2018
128
var signal = null
129
if (capability.abortController) {
130
var controller = new AbortController()
131
signal = controller.signal
132
self._fetchAbortController = controller
133
Jan 15, 2018
Jan 15, 2018
134
if ('requestTimeout' in opts && opts.requestTimeout !== 0) {
May 9, 2018
May 9, 2018
135
self._fetchTimer = global.setTimeout(function () {
Jan 15, 2018
Jan 15, 2018
136
self.emit('requestTimeout')
Jan 15, 2018
Jan 15, 2018
137
if (self._fetchAbortController)
138
self._fetchAbortController.abort()
Jan 15, 2018
Jan 15, 2018
139
}, opts.requestTimeout)
Jan 15, 2018
Jan 15, 2018
140
}
141
}
142
Sep 15, 2015
Sep 15, 2015
143
global.fetch(self._opts.url, {
Jun 24, 2015
Jun 24, 2015
144
method: self._opts.method,
Apr 4, 2017
Apr 4, 2017
145
headers: headersList,
Jan 18, 2017
Jan 18, 2017
146
body: body || undefined,
Jun 24, 2015
Jun 24, 2015
147
mode: 'cors',
Jan 15, 2018
Jan 15, 2018
148
credentials: opts.withCredentials ? 'include' : 'same-origin',
149
signal: signal
Jun 24, 2015
Jun 24, 2015
150
}).then(function (response) {
151
self._fetchResponse = response
Apr 13, 2021
Apr 13, 2021
152
self._resetTimers(false)
Jun 24, 2015
Jun 24, 2015
153
self._connect()
Oct 2, 2015
Oct 2, 2015
154
}, function (reason) {
Apr 13, 2021
Apr 13, 2021
155
self._resetTimers(true)
May 9, 2018
May 9, 2018
156
if (!self._destroyed)
157
self.emit('error', reason)
Jun 24, 2015
Jun 24, 2015
158
})
159
} else {
Sep 15, 2015
Sep 15, 2015
160
var xhr = self._xhr = new global.XMLHttpRequest()
Jul 8, 2015
Jul 8, 2015
161
try {
Sep 12, 2015
Sep 12, 2015
162
xhr.open(self._opts.method, self._opts.url, true)
Jul 8, 2015
Jul 8, 2015
163
} catch (err) {
164
process.nextTick(function () {
165
self.emit('error', err)
166
})
167
return
168
}
Jun 24, 2015
Jun 24, 2015
169
170
// Can't set responseType on really old browsers
171
if ('responseType' in xhr)
Sep 24, 2018
Sep 24, 2018
172
xhr.responseType = self._mode
Jun 24, 2015
Jun 24, 2015
173
174
if ('withCredentials' in xhr)
Jul 7, 2015
Jul 7, 2015
175
xhr.withCredentials = !!opts.withCredentials
Jun 24, 2015
Jun 24, 2015
176
177
if (self._mode === 'text' && 'overrideMimeType' in xhr)
178
xhr.overrideMimeType('text/plain; charset=x-user-defined')
179
Jan 15, 2018
Jan 15, 2018
180
if ('requestTimeout' in opts) {
181
xhr.timeout = opts.requestTimeout
Oct 18, 2016
Oct 18, 2016
182
xhr.ontimeout = function () {
Jan 15, 2018
Jan 15, 2018
183
self.emit('requestTimeout')
Oct 18, 2016
Oct 18, 2016
184
}
185
}
186
Apr 4, 2017
Apr 4, 2017
187
headersList.forEach(function (header) {
188
xhr.setRequestHeader(header[0], header[1])
Jun 24, 2015
Jun 24, 2015
189
})
190
Jul 3, 2015
Jul 3, 2015
191
self._response = null
Jun 24, 2015
Jun 24, 2015
192
xhr.onreadystatechange = function () {
193
switch (xhr.readyState) {
194
case rStates.LOADING:
195
case rStates.DONE:
Jul 8, 2015
Jul 8, 2015
196
self._onXHRProgress()
Jun 24, 2015
Jun 24, 2015
197
break
198
}
199
}
Jul 2, 2015
Jul 2, 2015
200
// Necessary for streaming in Firefox, since xhr.response is ONLY defined
201
// in onprogress, not in onreadystatechange with xhr.readyState = 3
202
if (self._mode === 'moz-chunked-arraybuffer') {
203
xhr.onprogress = function () {
Jul 8, 2015
Jul 8, 2015
204
self._onXHRProgress()
Jul 2, 2015
Jul 2, 2015
205
}
206
}
Jun 24, 2015
Jun 24, 2015
207
Jul 8, 2015
Jul 8, 2015
208
xhr.onerror = function () {
Jul 13, 2015
Jul 13, 2015
209
if (self._destroyed)
210
return
Apr 13, 2021
Apr 13, 2021
211
self._resetTimers(true)
Jul 8, 2015
Jul 8, 2015
212
self.emit('error', new Error('XHR error'))
213
}
214
215
try {
216
xhr.send(body)
217
} catch (err) {
218
process.nextTick(function () {
219
self.emit('error', err)
220
})
221
return
222
}
Jun 24, 2015
Jun 24, 2015
223
}
224
}
225
Jul 3, 2015
Jul 3, 2015
226
/**
Oct 13, 2015
Oct 13, 2015
227
* Checks if xhr.status is readable and non-zero, indicating no error.
228
* Even though the spec says it should be available in readyState 3,
229
* accessing it throws an exception in IE8
Jul 3, 2015
Jul 3, 2015
230
*/
231
function statusValid (xhr) {
232
try {
Oct 13, 2015
Oct 13, 2015
233
var status = xhr.status
234
return (status !== null && status !== 0)
Jul 3, 2015
Jul 3, 2015
235
} catch (e) {
236
return false
237
}
238
}
239
Jul 8, 2015
Jul 8, 2015
240
ClientRequest.prototype._onXHRProgress = function () {
241
var self = this
242
Apr 13, 2021
Apr 13, 2021
243
self._resetTimers(false)
244
Jul 13, 2015
Jul 13, 2015
245
if (!statusValid(self._xhr) || self._destroyed)
Jul 8, 2015
Jul 8, 2015
246
return
247
248
if (!self._response)
249
self._connect()
250
Apr 14, 2021
Apr 14, 2021
251
self._response._onXHRProgress(self._resetTimers.bind(self))
Jul 8, 2015
Jul 8, 2015
252
}
253
Jun 24, 2015
Jun 24, 2015
254
ClientRequest.prototype._connect = function () {
255
var self = this
256
Jul 13, 2015
Jul 13, 2015
257
if (self._destroyed)
258
return
259
Apr 13, 2021
Apr 13, 2021
260
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode, self._resetTimers.bind(self))
Jan 4, 2017
Jan 4, 2017
261
self._response.on('error', function(err) {
262
self.emit('error', err)
263
})
264
Jun 24, 2015
Jun 24, 2015
265
self.emit('response', self._response)
266
}
267
268
ClientRequest.prototype._write = function (chunk, encoding, cb) {
269
var self = this
270
271
self._body.push(chunk)
272
cb()
273
}
274
Apr 13, 2021
Apr 13, 2021
275
ClientRequest.prototype._resetTimers = function (done) {
276
var self = this
277
278
global.clearTimeout(self._socketTimer)
279
self._socketTimer = null
280
281
if (done) {
282
global.clearTimeout(self._fetchTimer)
283
self._fetchTimer = null
Apr 14, 2021
Apr 14, 2021
284
} else if (self._socketTimeout) {
Apr 13, 2021
Apr 13, 2021
285
self._socketTimer = global.setTimeout(function () {
286
self.emit('timeout')
287
}, self._socketTimeout)
288
}
289
}
290
Apr 13, 2021
Apr 13, 2021
291
ClientRequest.prototype.abort = ClientRequest.prototype.destroy = function (err) {
Jun 24, 2015
Jun 24, 2015
292
var self = this
Jul 13, 2015
Jul 13, 2015
293
self._destroyed = true
Apr 13, 2021
Apr 13, 2021
294
self._resetTimers(true)
Jul 13, 2015
Jul 13, 2015
295
if (self._response)
296
self._response._destroyed = true
Jun 24, 2015
Jun 24, 2015
297
if (self._xhr)
298
self._xhr.abort()
Jan 15, 2018
Jan 15, 2018
299
else if (self._fetchAbortController)
300
self._fetchAbortController.abort()
Apr 13, 2021
Apr 13, 2021
301
302
if (err)
303
self.emit('error', err)
Jun 24, 2015
Jun 24, 2015
304
}
305
306
ClientRequest.prototype.end = function (data, encoding, cb) {
307
var self = this
308
if (typeof data === 'function') {
309
cb = data
310
data = undefined
311
}
312
Aug 8, 2015
Aug 8, 2015
313
stream.Writable.prototype.end.call(self, data, encoding, cb)
Jun 24, 2015
Jun 24, 2015
314
}
315
Apr 13, 2021
Apr 13, 2021
316
ClientRequest.prototype.setTimeout = function (timeout, cb) {
317
var self = this
318
319
if (cb)
320
self.once('timeout', cb)
321
322
self._socketTimeout = timeout
323
self._resetTimers(false)
324
}
325
Jun 24, 2015
Jun 24, 2015
326
ClientRequest.prototype.flushHeaders = function () {}
327
ClientRequest.prototype.setNoDelay = function () {}
328
ClientRequest.prototype.setSocketKeepAlive = function () {}
Jul 13, 2015
Jul 13, 2015
329
330
// Taken from http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader%28%29-method
331
var unsafeHeaders = [
332
'accept-charset',
333
'accept-encoding',
334
'access-control-request-headers',
335
'access-control-request-method',
336
'connection',
337
'content-length',
338
'cookie',
339
'cookie2',
340
'date',
341
'dnt',
342
'expect',
343
'host',
344
'keep-alive',
345
'origin',
346
'referer',
347
'te',
348
'trailer',
349
'transfer-encoding',
350
'upgrade',
351
'via'
Jul 13, 2015
Jul 13, 2015
352
]