Skip to content

Latest commit

 

History

History
239 lines (202 loc) · 5.78 KB

File metadata and controls

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