summaryrefslogtreecommitdiff
path: root/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/request.js
blob: 6e9b9e51224fcf0e8570f542edd610a33f1eab21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import * as capability from './capability';
import {inherits} from 'util';
import {IncomingMessage, readyStates as rStates} from './response';
import {Writable} from 'stream';
import toArrayBuffer from './to-arraybuffer';

function decideMode(preferBinary, useFetch) {
  if (capability.hasFetch && useFetch) {
    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 if (capability.vbArray && preferBinary) {
    return 'text:vbarray'
  } else {
    return 'text'
  }
}
export default ClientRequest;

function ClientRequest(opts) {
  var self = this
  Writable.call(self)

  self._opts = opts
  self._body = []
  self._headers = {}
  if (opts.auth)
    self.setHeader('Authorization', 'Basic ' + new Buffer(opts.auth).toString('base64'))
  Object.keys(opts.headers).forEach(function(name) {
    self.setHeader(name, opts.headers[name])
  })

  var preferBinary
  var useFetch = true
  if (opts.mode === 'disable-fetch') {
    // If the use of XHR should be preferred and includes preserving the 'content-type' header
    useFetch = false
    preferBinary = true
  } else if (opts.mode === 'prefer-streaming') {
    // If streaming is a high priority but binary compatibility and
    // the accuracy of the 'content-type' header aren't
    preferBinary = false
  } else if (opts.mode === 'allow-wrong-content-type') {
    // If streaming is more important than preserving the 'content-type' header
    preferBinary = !capability.overrideMimeType
  } 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
  } else {
    throw new Error('Invalid value for opts.mode')
  }
  self._mode = decideMode(preferBinary, useFetch)

  self.on('finish', function() {
    self._onFinish()
  })
}

inherits(ClientRequest, Writable)
// Taken from http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader%28%29-method
var unsafeHeaders = [
  'accept-charset',
  'accept-encoding',
  'access-control-request-headers',
  'access-control-request-method',
  'connection',
  'content-length',
  'cookie',
  'cookie2',
  'date',
  'dnt',
  'expect',
  'host',
  'keep-alive',
  'origin',
  'referer',
  'te',
  'trailer',
  'transfer-encoding',
  'upgrade',
  'user-agent',
  'via'
]
ClientRequest.prototype.setHeader = function(name, value) {
  var self = this
  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.
  if (unsafeHeaders.indexOf(lowerName) !== -1)
    return

  self._headers[lowerName] = {
    name: name,
    value: value
  }
}

ClientRequest.prototype.getHeader = function(name) {
  var self = this
  return self._headers[name.toLowerCase()].value
}

ClientRequest.prototype.removeHeader = function(name) {
  var self = this
  delete self._headers[name.toLowerCase()]
}

ClientRequest.prototype._onFinish = function() {
  var self = this

  if (self._destroyed)
    return
  var opts = self._opts

  var headersObj = self._headers
  var body
  if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
    if (capability.blobConstructor()) {
      body = new global.Blob(self._body.map(function(buffer) {
        return toArrayBuffer(buffer)
      }), {
        type: (headersObj['content-type'] || {}).value || ''
      })
    } else {
      // get utf8 string
      body = Buffer.concat(self._body).toString()
    }
  }

  if (self._mode === 'fetch') {
    var headers = Object.keys(headersObj).map(function(name) {
      return [headersObj[name].name, headersObj[name].value]
    })

    global.fetch(self._opts.url, {
      method: self._opts.method,
      headers: headers,
      body: body,
      mode: 'cors',
      credentials: opts.withCredentials ? 'include' : 'same-origin'
    }).then(function(response) {
      self._fetchResponse = response
      self._connect()
    }, function(reason) {
      self.emit('error', reason)
    })
  } else {
    var xhr = self._xhr = new global.XMLHttpRequest()
    try {
      xhr.open(self._opts.method, self._opts.url, true)
    } catch (err) {
      process.nextTick(function() {
        self.emit('error', err)
      })
      return
    }

    // Can't set responseType on really old browsers
    if ('responseType' in xhr)
      xhr.responseType = self._mode.split(':')[0]

    if ('withCredentials' in xhr)
      xhr.withCredentials = !!opts.withCredentials

    if (self._mode === 'text' && 'overrideMimeType' in xhr)
      xhr.overrideMimeType('text/plain; charset=x-user-defined')

    Object.keys(headersObj).forEach(function(name) {
      xhr.setRequestHeader(headersObj[name].name, headersObj[name].value)
    })

    self._response = null
    xhr.onreadystatechange = function() {
      switch (xhr.readyState) {
      case rStates.LOADING:
      case rStates.DONE:
        self._onXHRProgress()
        break
      }
    }
      // Necessary for streaming in Firefox, since xhr.response is ONLY defined
      // in onprogress, not in onreadystatechange with xhr.readyState = 3
    if (self._mode === 'moz-chunked-arraybuffer') {
      xhr.onprogress = function() {
        self._onXHRProgress()
      }
    }

    xhr.onerror = function() {
      if (self._destroyed)
        return
      self.emit('error', new Error('XHR error'))
    }

    try {
      xhr.send(body)
    } catch (err) {
      process.nextTick(function() {
        self.emit('error', err)
      })
      return
    }
  }
}

/**
 * Checks if xhr.status is readable and non-zero, indicating no error.
 * Even though the spec says it should be available in readyState 3,
 * accessing it throws an exception in IE8
 */
function statusValid(xhr) {
  try {
    var status = xhr.status
    return (status !== null && status !== 0)
  } catch (e) {
    return false
  }
}

ClientRequest.prototype._onXHRProgress = function() {
  var self = this

  if (!statusValid(self._xhr) || self._destroyed)
    return

  if (!self._response)
    self._connect()

  self._response._onXHRProgress()
}

ClientRequest.prototype._connect = function() {
  var self = this

  if (self._destroyed)
    return

  self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode)
  self.emit('response', self._response)
}

ClientRequest.prototype._write = function(chunk, encoding, cb) {
  var self = this

  self._body.push(chunk)
  cb()
}

ClientRequest.prototype.abort = ClientRequest.prototype.destroy = function() {
  var self = this
  self._destroyed = true
  if (self._response)
    self._response._destroyed = true
  if (self._xhr)
    self._xhr.abort()
    // Currently, there isn't a way to truly abort a fetch.
    // If you like bikeshedding, see https://github.com/whatwg/fetch/issues/27
}

ClientRequest.prototype.end = function(data, encoding, cb) {
  var self = this
  if (typeof data === 'function') {
    cb = data
    data = undefined
  }

  Writable.prototype.end.call(self, data, encoding, cb)
}

ClientRequest.prototype.flushHeaders = function() {}
ClientRequest.prototype.setTimeout = function() {}
ClientRequest.prototype.setNoDelay = function() {}
ClientRequest.prototype.setSocketKeepAlive = function() {}