Skip to content

Files

Latest commit

 

History

History
352 lines (301 loc) · 8.52 KB

request.js

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
7
8
var IncomingMessage = response.IncomingMessage
var rStates = response.readyStates
Sep 1, 2016
Sep 1, 2016
9
10
function decideMode (preferBinary, useFetch) {
if (capability.fetch && useFetch) {
Jul 2, 2015
Jul 2, 2015
11
12
13
14
15
16
17
18
19
20
21
22
return 'fetch'
} else if (capability.mozchunkedarraybuffer) {
return 'moz-chunked-arraybuffer'
} else if (capability.msstream) {
return 'ms-stream'
} else if (capability.arraybuffer && preferBinary) {
return 'arraybuffer'
} else {
return 'text'
}
}
Jun 24, 2015
Jun 24, 2015
23
24
25
26
27
28
var ClientRequest = module.exports = function (opts) {
var self = this
stream.Writable.call(self)
self._opts = opts
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
34
35
self.setHeader(name, opts.headers[name])
})
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
41
42
useFetch = false
preferBinary = true
} else if (opts.mode === 'prefer-streaming') {
Aug 20, 2015
Aug 20, 2015
43
44
// If streaming is a high priority but binary compatibility and
// 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
47
} else if (opts.mode === 'allow-wrong-content-type') {
// 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
50
51
} else if (!opts.mode || opts.mode === 'default' || opts.mode === 'prefer-fast') {
// Use binary if text streaming may corrupt data or the content-type header, or for speed
preferBinary = true
Jul 2, 2015
Jul 2, 2015
52
53
54
} else {
throw new Error('Invalid value for opts.mode')
}
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
58
self._socketTimeout = null
self._socketTimer = null
Jun 24, 2015
Jun 24, 2015
59
Jul 2, 2015
Jul 2, 2015
60
61
62
self.on('finish', function () {
self._onFinish()
})
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
68
ClientRequest.prototype.setHeader = function (name, value) {
var self = this
Jul 13, 2015
Jul 13, 2015
69
70
71
72
var lowerName = name.toLowerCase()
// This check is not necessary, but it prevents warnings from browsers about setting unsafe
// headers. To be honest I'm not entirely sure hiding these warnings is a good thing, but
// 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
75
76
return
self._headers[lowerName] = {
Jul 7, 2015
Jul 7, 2015
77
78
79
name: name,
value: value
}
Jun 24, 2015
Jun 24, 2015
80
81
82
}
ClientRequest.prototype.getHeader = function (name) {
Apr 26, 2017
Apr 26, 2017
83
84
85
86
var header = this._headers[name.toLowerCase()]
if (header)
return header.value
return null
Jun 24, 2015
Jun 24, 2015
87
88
89
90
}
ClientRequest.prototype.removeHeader = function (name) {
var self = this
Jul 7, 2015
Jul 7, 2015
91
delete self._headers[name.toLowerCase()]
Jun 24, 2015
Jun 24, 2015
92
93
94
95
96
}
ClientRequest.prototype._onFinish = function () {
var self = this
Jul 13, 2015
Jul 13, 2015
97
98
if (self._destroyed)
return
Jun 24, 2015
Jun 24, 2015
99
100
var opts = self._opts
Apr 14, 2021
Apr 14, 2021
101
if ('timeout' in opts && opts.timeout !== 0) {