Fixes Fetch error when debugging in Chrome
Summary: Fixes https://github.com/facebook/react-native/issues/6679 Closes https://github.com/facebook/react-native/pull/8512 Differential Revision: D3503958 Pulled By: davidaurelio fbshipit-source-id: 204eaa40832c9acb455f736d0cae40385c67a82f
This commit is contained in:
parent
98f06c8876
commit
034b64509f
|
@ -8,6 +8,8 @@
|
|||
*
|
||||
* This is a third-party polyfill grabbed from:
|
||||
* https://github.com/github/fetch
|
||||
* copied from v1.0.0
|
||||
* https://github.com/github/fetch/commit/4bde61788365cfd3d6155eee8aed3c7673d299d5
|
||||
*
|
||||
* @providesModule fetch
|
||||
* @nolint
|
||||
|
@ -48,6 +50,21 @@ var self = {};
|
|||
return
|
||||
}
|
||||
|
||||
var support = {
|
||||
searchParams: 'URLSearchParams' in self,
|
||||
iterable: 'Symbol' in self && 'iterator' in Symbol,
|
||||
blob: 'FileReader' in self && 'Blob' in self && (function() {
|
||||
try {
|
||||
new Blob()
|
||||
return true
|
||||
} catch(e) {
|
||||
return false
|
||||
}
|
||||
})(),
|
||||
formData: 'FormData' in self,
|
||||
arrayBuffer: 'ArrayBuffer' in self
|
||||
}
|
||||
|
||||
function normalizeName(name) {
|
||||
if (typeof name !== 'string') {
|
||||
name = String(name)
|
||||
|
@ -65,6 +82,24 @@ var self = {};
|
|||
return value
|
||||
}
|
||||
|
||||
// Build a destructive iterator for the value list
|
||||
function iteratorFor(items) {
|
||||
var iterator = {
|
||||
next: function() {
|
||||
var value = items.shift()
|
||||
return {done: value === undefined, value: value}
|
||||
}
|
||||
}
|
||||
|
||||
if (support.iterable) {
|
||||
iterator[Symbol.iterator] = function() {
|
||||
return iterator
|
||||
}
|
||||
}
|
||||
|
||||
return iterator
|
||||
}
|
||||
|
||||
function Headers(headers) {
|
||||
this.map = {}
|
||||
|
||||
|
@ -120,6 +155,28 @@ var self = {};
|
|||
}, this)
|
||||
}
|
||||
|
||||
Headers.prototype.keys = function() {
|
||||
var items = []
|
||||
this.forEach(function(value, name) { items.push(name) })
|
||||
return iteratorFor(items)
|
||||
}
|
||||
|
||||
Headers.prototype.values = function() {
|
||||
var items = []
|
||||
this.forEach(function(value) { items.push(value) })
|
||||
return iteratorFor(items)
|
||||
}
|
||||
|
||||
Headers.prototype.entries = function() {
|
||||
var items = []
|
||||
this.forEach(function(value, name) { items.push([name, value]) })
|
||||
return iteratorFor(items)
|
||||
}
|
||||
|
||||
if (support.iterable) {
|
||||
Headers.prototype[Symbol.iterator] = Headers.prototype.entries
|
||||
}
|
||||
|
||||
function consumed(body) {
|
||||
if (body.bodyUsed) {
|
||||
return Promise.reject(new TypeError('Already read'))
|
||||
|
@ -150,19 +207,6 @@ var self = {};
|
|||
return fileReaderReady(reader)
|
||||
}
|
||||
|
||||
var support = {
|
||||
blob: typeof FileReader === 'function' && typeof Blob === 'function' && (function() {
|
||||
try {
|
||||
new Blob();
|
||||
return true
|
||||
} catch(e) {
|
||||
return false
|
||||
}
|
||||
})(),
|
||||
formData: typeof FormData === 'function',
|
||||
arrayBuffer: typeof ArrayBuffer === 'function'
|
||||
}
|
||||
|
||||
function Body() {
|
||||
this.bodyUsed = false
|
||||
|
||||
|
@ -174,6 +218,8 @@ var self = {};
|
|||
this._bodyBlob = body
|
||||
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
|
||||
this._bodyFormData = body
|
||||
} else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
|
||||
this._bodyText = body.toString()
|
||||
} else if (!body) {
|
||||
this._bodyText = ''
|
||||
} else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) {
|
||||
|
@ -188,6 +234,8 @@ var self = {};
|
|||
this.headers.set('content-type', 'text/plain;charset=UTF-8')
|
||||
} else if (this._bodyBlob && this._bodyBlob.type) {
|
||||
this.headers.set('content-type', this._bodyBlob.type)
|
||||
} else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
|
||||
this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -309,7 +357,7 @@ var self = {};
|
|||
|
||||
function headers(xhr) {
|
||||
var head = new Headers()
|
||||
var pairs = xhr.getAllResponseHeaders().trim().split('\n')
|
||||
var pairs = (xhr.getAllResponseHeaders() || '').trim().split('\n')
|
||||
pairs.forEach(function(header) {
|
||||
var split = header.trim().split(':')
|
||||
var key = split.shift().trim()
|
||||
|
@ -334,6 +382,7 @@ var self = {};
|
|||
this.url = options.url || ''
|
||||
this._initBody(bodyInit)
|
||||
}
|
||||
|
||||
Body.call(Response.prototype)
|
||||
|
||||
Response.prototype.clone = function() {
|
||||
|
@ -361,9 +410,9 @@ var self = {};
|
|||
return new Response(null, {status: status, headers: {location: url}})
|
||||
}
|
||||
|
||||
self.Headers = Headers;
|
||||
self.Request = Request;
|
||||
self.Response = Response;
|
||||
self.Headers = Headers
|
||||
self.Request = Request
|
||||
self.Response = Response
|
||||
|
||||
self.fetch = function(input, init) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
|
@ -386,23 +435,17 @@ var self = {};
|
|||
return xhr.getResponseHeader('X-Request-URL')
|
||||
}
|
||||
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
xhr.onload = function() {
|
||||
var status = (xhr.status === 1223) ? 204 : xhr.status
|
||||
if (status < 100 || status > 599) {
|
||||
reject(new TypeError('Network request failed'))
|
||||
return
|
||||
}
|
||||
|
||||
var options = {
|
||||
status: status,
|
||||
status: xhr.status,
|
||||
statusText: xhr.statusText,
|
||||
headers: headers(xhr),
|
||||
url: responseURL()
|
||||
}
|
||||
var body = 'response' in xhr ? xhr.response : xhr.responseText;
|
||||
var body = 'response' in xhr ? xhr.response : xhr.responseText
|
||||
resolve(new Response(body, options))
|
||||
}
|
||||
|
||||
|
@ -410,6 +453,10 @@ var self = {};
|
|||
reject(new TypeError('Network request failed'))
|
||||
}
|
||||
|
||||
xhr.ontimeout = function() {
|
||||
reject(new TypeError('Network request failed'))
|
||||
}
|
||||
|
||||
xhr.open(request.method, request.url, true)
|
||||
|
||||
if (request.credentials === 'include') {
|
||||
|
|
Loading…
Reference in New Issue