Skip to content

Commit

Permalink
Fixes Fetch error when debugging in Chrome
Browse files Browse the repository at this point in the history
Summary:
Fixes facebook#6679
Closes facebook#8512

Differential Revision: D3503958

Pulled By: davidaurelio

fbshipit-source-id: 204eaa40832c9acb455f736d0cae40385c67a82f
  • Loading branch information
bestander authored and samerce committed Aug 23, 2016
1 parent 45f0e5d commit b6bf14e
Showing 1 changed file with 73 additions and 26 deletions.
99 changes: 73 additions & 26 deletions Libraries/Fetch/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 = {}

Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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

Expand All @@ -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)) {
Expand All @@ -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')
}
}
}
Expand Down Expand Up @@ -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()
Expand All @@ -334,6 +382,7 @@ var self = {};
this.url = options.url || ''
this._initBody(bodyInit)
}

Body.call(Response.prototype)

Response.prototype.clone = function() {
Expand Down Expand Up @@ -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) {
Expand All @@ -386,30 +435,28 @@ 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))
}

xhr.onerror = function() {
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') {
Expand Down

0 comments on commit b6bf14e

Please sign in to comment.