Skip to content

Latest commit

 

History

History
216 lines (184 loc) · 5.42 KB

File metadata and controls

216 lines (184 loc) · 5.42 KB
 
Jun 24, 2015
Jun 24, 2015
1
// var Base64 = require('Base64')
2
var capability = require('./capability')
Jul 2, 2015
Jul 2, 2015
3
var foreach = require('foreach')
4
var keys = require('object-keys')
Jun 24, 2015
Jun 24, 2015
5
var response = require('./response')
6
var stream = require('stream')
7
var util = require('util')
8
9
var IncomingMessage = response.IncomingMessage
10
var rStates = response.readyStates
11
Jul 2, 2015
Jul 2, 2015
12
function decideMode (preferBinary) {
13
if (capability.fetch) {
14
return 'fetch'
15
} else if (capability.mozchunkedarraybuffer) {
16
return 'moz-chunked-arraybuffer'
17
} else if (capability.msstream) {
18
return 'ms-stream'
19
} else if (capability.arraybuffer && preferBinary) {
20
return 'arraybuffer'
Jul 3, 2015
Jul 3, 2015
21
} else if (capability.vbArray && preferBinary) {
22
return 'text:vbarray'
Jul 2, 2015
Jul 2, 2015
23
} else {
24
return 'text'
25
}
26
}
27
Jun 24, 2015
Jun 24, 2015
28
var ClientRequest = module.exports = function (opts) {
29
var self = this
30
stream.Writable.call(self)
31
32
self._opts = opts
Jul 7, 2015
Jul 7, 2015
33
self._url = opts.protocol + '//' + opts.hostname + ':' + opts.port + opts.path
Jun 24, 2015
Jun 24, 2015
34
self._body = []
Jul 7, 2015
Jul 7, 2015
35
self._headers = {}
Jul 2, 2015
Jul 2, 2015
36
foreach(keys(opts.headers), function (name) {
Jun 24, 2015
Jun 24, 2015
37
self.setHeader(name, opts.headers[name])
38
})
39
Jul 2, 2015
Jul 2, 2015
40
var preferBinary
Jul 3, 2015
Jul 3, 2015
41
if (opts.mode === 'prefer-streaming') {
Jul 2, 2015
Jul 2, 2015
42
// If streaming is a high priority but binary compatibility isn't
43
preferBinary = false
44
} else if (opts.mode === 'prefer-binary') {
45
// If binary compatibility is the highest priority
46
preferBinary = true
47
} else if (!opts.mode || opts.mode === 'default') {
48
// By default, use binary if text streaming may corrupt data
49
preferBinary = !capability.overrideMimeType
50
} else {
51
throw new Error('Invalid value for opts.mode')
52
}
53
self._mode = decideMode(preferBinary)
Jun 24, 2015
Jun 24, 2015
54
Jul 2, 2015
Jul 2, 2015
55
self.on('finish', function () {
56
self._onFinish()
57
})
Jun 24, 2015
Jun 24, 2015
58
}
59
60
util.inherits(ClientRequest, stream.Writable)
61
62
ClientRequest.prototype.setHeader = function (name, value) {
63
var self = this
Jul 7, 2015
Jul 7, 2015
64
self._headers[name.toLowerCase()] = {
65
name: name,
66
value: value
67
}
Jun 24, 2015
Jun 24, 2015
68
}
69
70
ClientRequest.prototype.getHeader = function (name) {
71
var self = this
Jul 7, 2015
Jul 7, 2015
72
return self._headers[name.toLowerCase()].value
Jun 24, 2015
Jun 24, 2015
73
}
74
75
ClientRequest.prototype.removeHeader = function (name) {
76
var self = this
Jul 7, 2015
Jul 7, 2015
77
delete self._headers[name.toLowerCase()]
Jun 24, 2015
Jun 24, 2015
78
}
79
80
ClientRequest.prototype._onFinish = function () {
81
var self = this
82
83
var opts = self._opts
84
85
var user, pass
86
if (opts.auth) {
Jul 1, 2015
Jul 1, 2015
87
var authMatch = opts.auth.match(/^([^:]*):(.*)$/)
Jun 24, 2015
Jun 24, 2015
88
user = authMatch[0]
89
pass = authMatch[1]
90
}
91
Jul 7, 2015
Jul 7, 2015
92
var headersObj = self._headers
Jun 24, 2015
Jun 24, 2015
93
var body
Jul 1, 2015
Jul 1, 2015
94
if (opts.method in ['PUT', 'POST']) {
Jul 7, 2015
Jul 7, 2015
95
if (util.isFunction(window.Blob)) {
Jul 1, 2015
Jul 1, 2015
96
body = new window.Blob(self._body.map(function (buffer) {
97
return buffer.toArrayBuffer()
98
}), {
Jul 7, 2015
Jul 7, 2015
99
type: (headersObj['content-type'] || {}).value || ''
Jul 1, 2015
Jul 1, 2015
100
})
101
} else {
102
// get utf8 string
103
body = Buffer.concat(self._body).toString()
104
}
Jun 24, 2015
Jun 24, 2015
105
}
106
107
if (self._mode === 'fetch') {
Jul 7, 2015
Jul 7, 2015
108
var headers = keys(headersObj).map(function (name) {
109
return [headersObj[name].name, headersObj[name].value]
Jun 24, 2015
Jun 24, 2015
110
})
111
Jul 7, 2015
Jul 7, 2015
112
window.fetch(self._url, {
Jun 24, 2015
Jun 24, 2015
113
method: self._opts.method,
114
headers: headers,
115
body: body,
116
mode: 'cors',
117
credentials: opts.credentials ? 'include' : 'omit'
118
}).then(function (response) {
119
self._fetchResponse = response
120
self._connect()
121
})
122
} else {
Jul 7, 2015
Jul 7, 2015
123
var xhr = self._xhr = new window.XMLHttpRequest()
Jul 7, 2015
Jul 7, 2015
124
xhr.open(self._opts.method, self._url, true, user, pass)
Jun 24, 2015
Jun 24, 2015
125
126
// Can't set responseType on really old browsers
127
if ('responseType' in xhr)
Jul 3, 2015
Jul 3, 2015
128
xhr.responseType = self._mode.split(':')[0]
Jun 24, 2015
Jun 24, 2015
129
130
if ('withCredentials' in xhr)
131
xhr.withCredentials = !!opts.credentials
132
133
if (self._mode === 'text' && 'overrideMimeType' in xhr)
134
xhr.overrideMimeType('text/plain; charset=x-user-defined')
135
Jul 7, 2015
Jul 7, 2015
136
foreach(keys(headersObj), function (name) {
137
xhr.setRequestHeader(headersObj[name].name, headersObj[name].value)
Jun 24, 2015
Jun 24, 2015
138
})
139
Jul 3, 2015
Jul 3, 2015
140
self._response = null
Jun 24, 2015
Jun 24, 2015
141
xhr.onreadystatechange = function () {
142
switch (xhr.readyState) {
143
case rStates.LOADING:
144
case rStates.DONE:
Jul 3, 2015
Jul 3, 2015
145
if (!self._response && statusValid(xhr))
146
self._connect()
147
if (self._response)
148
self._response._onXHRReadyStateChange()
Jun 24, 2015
Jun 24, 2015
149
break
150
}
151
}
Jul 2, 2015
Jul 2, 2015
152
// Necessary for streaming in Firefox, since xhr.response is ONLY defined
153
// in onprogress, not in onreadystatechange with xhr.readyState = 3
154
if (self._mode === 'moz-chunked-arraybuffer') {
155
xhr.onprogress = function () {
156
self._response._onXHRReadyStateChange()
157
}
158
}
Jun 24, 2015
Jun 24, 2015
159
160
xhr.send(body)
161
// This is the best approximation to where 'socket' should be fired
162
process.nextTick(function () {
163
self.emit('socket')
164
})
165
}
166
}
167
Jul 3, 2015
Jul 3, 2015
168
/**
169
* Checks if xhr.status is readable. Even though the spec says it should
170
* be available in readyState 3, accessing it throws an exception in IE8
171
*/
172
function statusValid (xhr) {
173
try {
174
return (xhr.status !== null)
175
} catch (e) {
176
return false
177
}
178
}
179
Jun 24, 2015
Jun 24, 2015
180
ClientRequest.prototype._connect = function () {
181
var self = this
182
183
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode)
184
self.emit('response', self._response)
185
}
186
187
ClientRequest.prototype._write = function (chunk, encoding, cb) {
188
var self = this
189
190
self._body.push(chunk)
191
cb()
192
}
193
194
ClientRequest.prototype.abort = function () {
195
var self = this
196
if (self._xhr)
197
self._xhr.abort()
198
}
199
200
ClientRequest.prototype.end = function (data, encoding, cb) {
201
var self = this
202
if (typeof data === 'function') {
203
cb = data
204
data = undefined
205
}
206
207
if (data)
208
stream.Writable.push.call(self, data, encoding)
209
210
stream.Writable.prototype.end.call(self, cb)
211
}
212
213
ClientRequest.prototype.flushHeaders = function () {}
214
ClientRequest.prototype.setTimeout = function () {}
215
ClientRequest.prototype.setNoDelay = function () {}
216
ClientRequest.prototype.setSocketKeepAlive = function () {}