From 87c26885a3520cbb661afcc23efb22943438e52d Mon Sep 17 00:00:00 2001 From: Laurence Bortfeld Date: Tue, 8 Mar 2016 09:47:56 -0800 Subject: [PATCH] Update fetch to latest https://github.com/github/fetch Summary:This fixes https://github.com/facebook/react-native/issues/6326 with updating https://github.com/facebook/react-native/blob/master/Libraries/Fetch/fetch.js to latest from https://github.com/github/fetch. cc skevy steveluscher Closes https://github.com/facebook/react-native/pull/6347 Differential Revision: D3024308 Pulled By: davidaurelio fb-gh-sync-id: da87827b94d683aeb66be345e255bd771fdba1be shipit-source-id: da87827b94d683aeb66be345e255bd771fdba1be --- Libraries/Fetch/fetch.js | 61 ++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/Libraries/Fetch/fetch.js b/Libraries/Fetch/fetch.js index ff5ef29bb7..373ee5df0f 100644 --- a/Libraries/Fetch/fetch.js +++ b/Libraries/Fetch/fetch.js @@ -159,13 +159,13 @@ var self = {}; return false } })(), - formData: typeof FormData === 'function' + formData: typeof FormData === 'function', + arrayBuffer: typeof ArrayBuffer === 'function' } function Body() { this.bodyUsed = false - this._initBody = function(body) { this._bodyInit = body if (typeof body === 'string') { @@ -176,9 +176,20 @@ var self = {}; this._bodyFormData = body } else if (!body) { this._bodyText = '' + } else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) { + // Only support ArrayBuffers for POST method. + // Receiving ArrayBuffers happens via Blobs, instead. } else { throw new Error('unsupported BodyInit type') } + + if (!this.headers.get('content-type')) { + if (typeof body === 'string') { + 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) + } + } } if (support.blob) { @@ -278,7 +289,7 @@ var self = {}; } this._initBody(body) } - + Request.prototype.clone = function() { return new Request(this) } @@ -315,16 +326,16 @@ var self = {}; options = {} } - this._initBody(bodyInit) this.type = 'default' - this.url = null this.status = options.status this.ok = this.status >= 200 && this.status < 300 this.statusText = options.statusText this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers) this.url = options.url || '' + this._initBody(bodyInit) } - + Body.call(Response.prototype) + Response.prototype.clone = function() { return new Response(this._bodyInit, { status: this.status, @@ -334,21 +345,35 @@ var self = {}; }) } - Body.call(Response.prototype) + Response.error = function() { + var response = new Response(null, {status: 0, statusText: ''}) + response.type = 'error' + return response + } + + var redirectStatuses = [301, 302, 303, 307, 308] + + Response.redirect = function(url, status) { + if (redirectStatuses.indexOf(status) === -1) { + throw new RangeError('Invalid status code') + } + + return new Response(null, {status: status, headers: {location: url}}) + } self.Headers = Headers; self.Request = Request; self.Response = Response; self.fetch = function(input, init) { - var request - if (Request.prototype.isPrototypeOf(input) && !init) { - request = input - } else { - request = new Request(input, init) - } - return new Promise(function(resolve, reject) { + var request + if (Request.prototype.isPrototypeOf(input) && !init) { + request = input + } else { + request = new Request(input, init) + } + var xhr = new XMLHttpRequest() function responseURL() { @@ -365,13 +390,8 @@ var self = {}; } 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() @@ -405,5 +425,4 @@ var self = {}; })(); /** End of the third-party code */ - module.exports = self;