-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathrequest.js
More file actions
173 lines (142 loc) · 4.07 KB
/
request.js
File metadata and controls
173 lines (142 loc) · 4.07 KB
Edit and raw actions
OlderNewer
1
// var Base64 = require('Base64')
2
var capability = require('./capability')
3
var response = require('./response')
4
var stream = require('stream')
5
var util = require('util')
6
7
var IncomingMessage = response.IncomingMessage
8
var rStates = response.readyStates
9
10
// function copy (to, from) {
11
// if (!Array.isArray(from))
12
// from = [from]
13
// from.forEach(function (obj) {
14
// Object.keys(function (key), {
15
// to[key] = from[key]
16
// })
17
// })
18
// return to
19
// }
20
21
var ClientRequest = module.exports = function (opts) {
22
var self = this
23
stream.Writable.call(self)
24
25
self._opts = opts
26
self._body = []
27
self._fullHeaders = {}
28
Object.keys(opts.headers).forEach(function (name) {
29
self.setHeader(name, opts.headers[name])
30
})
31
32
self._mode = capability.mode
33
34
self.on('finish', self._onFinish.bind(self))
35
}
36
37
util.inherits(ClientRequest, stream.Writable)
38
39
ClientRequest.prototype.setHeader = function (name, value) {
40
var self = this
41
self._fullHeaders[name.toLowerCase()] = value
42
}
43
44
ClientRequest.prototype.getHeader = function (name) {
45
var self = this
46
return self._fullHeaders[name.toLowerCase()]
47
}
48
49
ClientRequest.prototype.removeHeader = function (name) {
50
var self = this
51
delete self._fullHeaders[name.toLowerCase()]
52
}
53
54
ClientRequest.prototype._onFinish = function () {
55
var self = this
56
57
var opts = self._opts
58
var url = opts.protocol + '//' + opts.hostname +
59
(opts.port ? ':' + opts.port : '') + opts.path
60
61
var user, pass
62
if (opts.auth) {
63
var authMatch = opts.auth.match(/^([^:]*):(.*)$/)
64
user = authMatch[0]
65
pass = authMatch[1]
66
}
67
68
// process and send data
69
var fullHeaders = self._fullHeaders
70
var body
71
if (opts.method in ['PUT', 'POST']) {
72
if (typeof window.Blob === 'function') {
73
body = new window.Blob(self._body.map(function (buffer) {
74
return buffer.toArrayBuffer()
75
}), {
76
type: fullHeaders['content-type'] || ''
77
})
78
} else {
79
// get utf8 string
80
body = Buffer.concat(self._body).toString()
81
}
82
}
83
84
if (self._mode === 'fetch') {
85
var headers = Object.keys(fullHeaders).map(function (name) {
86
return [name, fullHeaders[name]]
87
})
88
89
window.fetch(url, {
90
method: self._opts.method,
91
headers: headers,
92
body: body,
93
mode: 'cors',
94
credentials: opts.credentials ? 'include' : 'omit'
95
}).then(function (response) {
96
self._fetchResponse = response
97
self._connect()
98
})
99
} else {
100
var xhr = self._xhr = new window.XMLHttpRequest() // TODO: old IE
101
xhr.open(self._opts.method, url, true, user, pass)
102
103
// Can't set responseType on really old browsers
104
if ('responseType' in xhr)
105
xhr.responseType = self._mode
106
107
if ('withCredentials' in xhr)
108
xhr.withCredentials = !!opts.credentials
109
110
if (self._mode === 'text' && 'overrideMimeType' in xhr)
111
xhr.overrideMimeType('text/plain; charset=x-user-defined')
112
113
Object.keys(fullHeaders, function (name) {
114
xhr.setRequestHeader(name, headers[name])
115
})
116
117
xhr.onreadystatechange = function () {
118
switch (xhr.readyState) {
119
case rStates.HEADERS_RECEIVED:
120
self._connect()
121
break
122
case rStates.LOADING:
123
case rStates.DONE:
124
self._response._onXHRReadyStateChange()
125
break
126
}
127
}
128
129
xhr.send(body)
130
// This is the best approximation to where 'socket' should be fired
131
process.nextTick(function () {
132
self.emit('socket')
133
})
134
}
135
}
136
137
ClientRequest.prototype._connect = function () {
138
var self = this
139
140
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode)
141
self.emit('response', self._response)
142
}
143
144
ClientRequest.prototype._write = function (chunk, encoding, cb) {
145
var self = this
146
147
self._body.push(chunk)
148
cb()
149
}
150
151
ClientRequest.prototype.abort = function () {
152
var self = this
153
if (self._xhr)
154
self._xhr.abort()
155
}
156
157
ClientRequest.prototype.end = function (data, encoding, cb) {
158
var self = this
159
if (typeof data === 'function') {
160
cb = data
161
data = undefined
162
}
163
164
if (data)
165
stream.Writable.push.call(self, data, encoding)
166
167
stream.Writable.prototype.end.call(self, cb)
168
}
169
170
ClientRequest.prototype.flushHeaders = function () {}
171
ClientRequest.prototype.setTimeout = function () {}
172
ClientRequest.prototype.setNoDelay = function () {}
173
ClientRequest.prototype.setSocketKeepAlive = function () {}