fetch-rails 1.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e298d1be2c523d9c32dcaf0412de5db06353f76
4
- data.tar.gz: 75cb0e4ff5f46742988a8fd0c639a31961f69a77
3
+ metadata.gz: 457c5049d18da21f5e0584781fb6e6b285c7e354
4
+ data.tar.gz: b40217fe4736c2492ab3cda33feda13d22bc08ac
5
5
  SHA512:
6
- metadata.gz: 74883d98c2ae22ab903ef8d7f83ffd133201e6af5ced7b6b7f5041eee7d1b527f47d6f858f1844540163d0936fd463bad58537d34dbe71d58b97ebba698e5d08
7
- data.tar.gz: ab8215a2f4145f8ee92799dd429ad5189b70494f3a625f7e62126a579a606182bc16251b256a1c23e99b7069f2c2aed417114f75b724d5b84dbe99f55db37ff4
6
+ metadata.gz: cd5ccddf7444cfba03d0dc4be893616be495d0599bfb0a12f500d3f69937057b48c75f5f22c32e90cd0db0abb24805d80c6453e93b99f7c69160490e74bebed6
7
+ data.tar.gz: ccbe6a2995c20e1c88efea75e40ef4a2ef7b7a4f8ddfc7f6691927280e2fd15d1916d393394ab73a24b91b7af5356234822b3971773acc19b336e724d9f52b3a
@@ -20,6 +20,28 @@
20
20
  arrayBuffer: 'ArrayBuffer' in self
21
21
  }
22
22
 
23
+ if (support.arrayBuffer) {
24
+ var viewClasses = [
25
+ '[object Int8Array]',
26
+ '[object Uint8Array]',
27
+ '[object Uint8ClampedArray]',
28
+ '[object Int16Array]',
29
+ '[object Uint16Array]',
30
+ '[object Int32Array]',
31
+ '[object Uint32Array]',
32
+ '[object Float32Array]',
33
+ '[object Float64Array]'
34
+ ]
35
+
36
+ var isDataView = function(obj) {
37
+ return obj && DataView.prototype.isPrototypeOf(obj)
38
+ }
39
+
40
+ var isArrayBufferView = ArrayBuffer.isView || function(obj) {
41
+ return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1
42
+ }
43
+ }
44
+
23
45
  function normalizeName(name) {
24
46
  if (typeof name !== 'string') {
25
47
  name = String(name)
@@ -73,12 +95,8 @@
73
95
  Headers.prototype.append = function(name, value) {
74
96
  name = normalizeName(name)
75
97
  value = normalizeValue(value)
76
- var list = this.map[name]
77
- if (!list) {
78
- list = []
79
- this.map[name] = list
80
- }
81
- list.push(value)
98
+ var oldValue = this.map[name]
99
+ this.map[name] = oldValue ? oldValue+','+value : value
82
100
  }
83
101
 
84
102
  Headers.prototype['delete'] = function(name) {
@@ -86,12 +104,8 @@
86
104
  }
87
105
 
88
106
  Headers.prototype.get = function(name) {
89
- var values = this.map[normalizeName(name)]
90
- return values ? values[0] : null
91
- }
92
-
93
- Headers.prototype.getAll = function(name) {
94
- return this.map[normalizeName(name)] || []
107
+ name = normalizeName(name)
108
+ return this.has(name) ? this.map[name] : null
95
109
  }
96
110
 
97
111
  Headers.prototype.has = function(name) {
@@ -99,15 +113,15 @@
99
113
  }
100
114
 
101
115
  Headers.prototype.set = function(name, value) {
102
- this.map[normalizeName(name)] = [normalizeValue(value)]
116
+ this.map[normalizeName(name)] = normalizeValue(value)
103
117
  }
104
118
 
105
119
  Headers.prototype.forEach = function(callback, thisArg) {
106
- Object.getOwnPropertyNames(this.map).forEach(function(name) {
107
- this.map[name].forEach(function(value) {
108
- callback.call(thisArg, value, name, this)
109
- }, this)
110
- }, this)
120
+ for (var name in this.map) {
121
+ if (this.map.hasOwnProperty(name)) {
122
+ callback.call(thisArg, this.map[name], name, this)
123
+ }
124
+ }
111
125
  }
112
126
 
113
127
  Headers.prototype.keys = function() {
@@ -152,14 +166,36 @@
152
166
 
153
167
  function readBlobAsArrayBuffer(blob) {
154
168
  var reader = new FileReader()
169
+ var promise = fileReaderReady(reader)
155
170
  reader.readAsArrayBuffer(blob)
156
- return fileReaderReady(reader)
171
+ return promise
157
172
  }
158
173
 
159
174
  function readBlobAsText(blob) {
160
175
  var reader = new FileReader()
176
+ var promise = fileReaderReady(reader)
161
177
  reader.readAsText(blob)
162
- return fileReaderReady(reader)
178
+ return promise
179
+ }
180
+
181
+ function readArrayBufferAsText(buf) {
182
+ var view = new Uint8Array(buf)
183
+ var chars = new Array(view.length)
184
+
185
+ for (var i = 0; i < view.length; i++) {
186
+ chars[i] = String.fromCharCode(view[i])
187
+ }
188
+ return chars.join('')
189
+ }
190
+
191
+ function bufferClone(buf) {
192
+ if (buf.slice) {
193
+ return buf.slice(0)
194
+ } else {
195
+ var view = new Uint8Array(buf.byteLength)
196
+ view.set(new Uint8Array(buf))
197
+ return view.buffer
198
+ }
163
199
  }
164
200
 
165
201
  function Body() {
@@ -167,7 +203,9 @@
167
203
 
168
204
  this._initBody = function(body) {
169
205
  this._bodyInit = body
170
- if (typeof body === 'string') {
206
+ if (!body) {
207
+ this._bodyText = ''
208
+ } else if (typeof body === 'string') {
171
209
  this._bodyText = body
172
210
  } else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
173
211
  this._bodyBlob = body
@@ -175,11 +213,12 @@
175
213
  this._bodyFormData = body
176
214
  } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
177
215
  this._bodyText = body.toString()
178
- } else if (!body) {
179
- this._bodyText = ''
180
- } else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) {
181
- // Only support ArrayBuffers for POST method.
182
- // Receiving ArrayBuffers happens via Blobs, instead.
216
+ } else if (support.arrayBuffer && support.blob && isDataView(body)) {
217
+ this._bodyArrayBuffer = bufferClone(body.buffer)
218
+ // IE 10-11 can't handle a DataView body.
219
+ this._bodyInit = new Blob([this._bodyArrayBuffer])
220
+ } else if (support.arrayBuffer && (ArrayBuffer.prototype.isPrototypeOf(body) || isArrayBufferView(body))) {
221
+ this._bodyArrayBuffer = bufferClone(body)
183
222
  } else {
184
223
  throw new Error('unsupported BodyInit type')
185
224
  }
@@ -204,6 +243,8 @@
204
243
 
205
244
  if (this._bodyBlob) {
206
245
  return Promise.resolve(this._bodyBlob)
246
+ } else if (this._bodyArrayBuffer) {
247
+ return Promise.resolve(new Blob([this._bodyArrayBuffer]))
207
248
  } else if (this._bodyFormData) {
208
249
  throw new Error('could not read FormData body as blob')
209
250
  } else {
@@ -212,27 +253,28 @@
212
253
  }
213
254
 
214
255
  this.arrayBuffer = function() {
215
- return this.blob().then(readBlobAsArrayBuffer)
216
- }
217
-
218
- this.text = function() {
219
- var rejected = consumed(this)
220
- if (rejected) {
221
- return rejected
222
- }
223
-
224
- if (this._bodyBlob) {
225
- return readBlobAsText(this._bodyBlob)
226
- } else if (this._bodyFormData) {
227
- throw new Error('could not read FormData body as text')
256
+ if (this._bodyArrayBuffer) {
257
+ return consumed(this) || Promise.resolve(this._bodyArrayBuffer)
228
258
  } else {
229
- return Promise.resolve(this._bodyText)
259
+ return this.blob().then(readBlobAsArrayBuffer)
230
260
  }
231
261
  }
232
- } else {
233
- this.text = function() {
234
- var rejected = consumed(this)
235
- return rejected ? rejected : Promise.resolve(this._bodyText)
262
+ }
263
+
264
+ this.text = function() {
265
+ var rejected = consumed(this)
266
+ if (rejected) {
267
+ return rejected
268
+ }
269
+
270
+ if (this._bodyBlob) {
271
+ return readBlobAsText(this._bodyBlob)
272
+ } else if (this._bodyArrayBuffer) {
273
+ return Promise.resolve(readArrayBufferAsText(this._bodyArrayBuffer))
274
+ } else if (this._bodyFormData) {
275
+ throw new Error('could not read FormData body as text')
276
+ } else {
277
+ return Promise.resolve(this._bodyText)
236
278
  }
237
279
  }
238
280
 
@@ -260,7 +302,10 @@
260
302
  function Request(input, options) {
261
303
  options = options || {}
262
304
  var body = options.body
263
- if (Request.prototype.isPrototypeOf(input)) {
305
+
306
+ if (typeof input === 'string') {
307
+ this.url = input
308
+ } else {
264
309
  if (input.bodyUsed) {
265
310
  throw new TypeError('Already read')
266
311
  }
@@ -271,12 +316,10 @@
271
316
  }
272
317
  this.method = input.method
273
318
  this.mode = input.mode
274
- if (!body) {
319
+ if (!body && input._bodyInit != null) {
275
320
  body = input._bodyInit
276
321
  input.bodyUsed = true
277
322
  }
278
- } else {
279
- this.url = input
280
323
  }
281
324
 
282
325
  this.credentials = options.credentials || this.credentials || 'omit'
@@ -294,7 +337,7 @@
294
337
  }
295
338
 
296
339
  Request.prototype.clone = function() {
297
- return new Request(this)
340
+ return new Request(this, { body: this._bodyInit })
298
341
  }
299
342
 
300
343
  function decode(body) {
@@ -310,16 +353,17 @@
310
353
  return form
311
354
  }
312
355
 
313
- function headers(xhr) {
314
- var head = new Headers()
315
- var pairs = (xhr.getAllResponseHeaders() || '').trim().split('\n')
316
- pairs.forEach(function(header) {
317
- var split = header.trim().split(':')
318
- var key = split.shift().trim()
319
- var value = split.join(':').trim()
320
- head.append(key, value)
356
+ function parseHeaders(rawHeaders) {
357
+ var headers = new Headers()
358
+ rawHeaders.split('\r\n').forEach(function(line) {
359
+ var parts = line.split(':')
360
+ var key = parts.shift().trim()
361
+ if (key) {
362
+ var value = parts.join(':').trim()
363
+ headers.append(key, value)
364
+ }
321
365
  })
322
- return head
366
+ return headers
323
367
  }
324
368
 
325
369
  Body.call(Request.prototype)
@@ -330,10 +374,10 @@
330
374
  }
331
375
 
332
376
  this.type = 'default'
333
- this.status = options.status
377
+ this.status = 'status' in options ? options.status : 200
334
378
  this.ok = this.status >= 200 && this.status < 300
335
- this.statusText = options.statusText
336
- this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers)
379
+ this.statusText = 'statusText' in options ? options.statusText : 'OK'
380
+ this.headers = new Headers(options.headers)
337
381
  this.url = options.url || ''
338
382
  this._initBody(bodyInit)
339
383
  }
@@ -371,35 +415,16 @@
371
415
 
372
416
  self.fetch = function(input, init) {
373
417
  return new Promise(function(resolve, reject) {
374
- var request
375
- if (Request.prototype.isPrototypeOf(input) && !init) {
376
- request = input
377
- } else {
378
- request = new Request(input, init)
379
- }
380
-
418
+ var request = new Request(input, init)
381
419
  var xhr = new XMLHttpRequest()
382
420
 
383
- function responseURL() {
384
- if ('responseURL' in xhr) {
385
- return xhr.responseURL
386
- }
387
-
388
- // Avoid security warnings on getResponseHeader when not allowed by CORS
389
- if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) {
390
- return xhr.getResponseHeader('X-Request-URL')
391
- }
392
-
393
- return
394
- }
395
-
396
421
  xhr.onload = function() {
397
422
  var options = {
398
423
  status: xhr.status,
399
424
  statusText: xhr.statusText,
400
- headers: headers(xhr),
401
- url: responseURL()
425
+ headers: parseHeaders(xhr.getAllResponseHeaders() || '')
402
426
  }
427
+ options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL')
403
428
  var body = 'response' in xhr ? xhr.response : xhr.responseText
404
429
  resolve(new Response(body, options))
405
430
  }
@@ -1,5 +1,5 @@
1
1
  module Fetch
2
2
  module Rails
3
- VERSION = "1.0.0"
3
+ VERSION = "2.0.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fetch-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vipul A M
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-29 00:00:00.000000000 Z
11
+ date: 2017-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails