From 844356dec82e7e213d86bdcb1ee1f6a86227bf74 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Mon, 10 Aug 2015 21:49:25 +0800 Subject: [PATCH 01/25] Change credentials value to 'include' based on Fetch specs --- fetch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fetch.js b/fetch.js index 6aff4092..dd32b843 100644 --- a/fetch.js +++ b/fetch.js @@ -294,7 +294,7 @@ return new fetch.Promise(function(resolve, reject) { var xhr = getXhr(); - if (request.credentials === 'cors') { + if (request.credentials === 'include') { xhr.withCredentials = true; } From 6d93aaa8a2736ed245a36c0bf908a3033405bd15 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Tue, 11 Aug 2015 15:24:09 +0800 Subject: [PATCH 02/25] add CommonJS support --- fetch.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/fetch.js b/fetch.js index dd32b843..073ff7e4 100644 --- a/fetch.js +++ b/fetch.js @@ -354,4 +354,9 @@ } fetch.Promise = self.Promise; // you could change it to your favorite alternative self.fetch.polyfill = true + + // Support CommonJS + if (typeof module !== 'undefined' && module.exports) { + module.exports = fetch; + } })(); From 743def7c798eb2f010b6966c3d28dd0ae05c8370 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Tue, 11 Aug 2015 15:24:27 +0800 Subject: [PATCH 03/25] release 1.0.0 --- README.md | 4 +++- bower.json | 4 ++-- package.json | 13 ++++--------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 445535ca..d9e550a8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # window.fetch polyfill -**This fork supports IE8 with es5-shim, es5-sham and es6-promise** +**This fork supports IE8 with es5-shim, es5-sham and es6-promise.** + +**If you also use JSONP, checkout [fetch-jsonp](https://github.com/camsong/fetch-jsonp).** The global `fetch` function is an easier way to make web requests and handle responses than using an XMLHttpRequest. This polyfill is written as closely as diff --git a/bower.json b/bower.json index 7a2d5b8e..6ee200d4 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { - "name": "fetch-polyfill", - "version": "0.8.1", + "name": "fetch-ie8", + "version": "1.0.0", "main": "fetch.js", "devDependencies": { "es6-promise": "2.1.0" diff --git a/package.json b/package.json index 47fd4c51..5eb6e388 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,9 @@ { - "name": "fetch-polyfill", - "version": "0.8.1", + "name": "fetch-ie8", + "version": "1.0.0", "main": "fetch.js", - "repository": "undoZen/fetch", - "licenses": [ - { - "type": "MIT", - "url": "http://mit-license.org/undozen" - } - ], + "repository": "camsong/fetch", + "license": "MIT", "devDependencies": { "browserify": "^9.0.8", "es6-promise": "^2.1.1", From 9320e680ac1c891e018aa26ea8fc3e2b58110c4b Mon Sep 17 00:00:00 2001 From: Cam Song Date: Thu, 13 Aug 2015 21:29:45 +0800 Subject: [PATCH 04/25] add window.__disableNativeFetch configuration --- README.md | 4 ++++ fetch.js | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d9e550a8..2eab3acf 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ **If you also use JSONP, checkout [fetch-jsonp](https://github.com/camsong/fetch-jsonp).** +**Fetch API is still very new and not fully supported in some browsers, so you may +need to check browser verson as well as if `window.fetch` exists. In this case, +you can set `window.__disableNativeFetch = true` to use AJAX polyfill always.** + The global `fetch` function is an easier way to make web requests and handle responses than using an XMLHttpRequest. This polyfill is written as closely as possible to the standard Fetch specification at https://fetch.spec.whatwg.org. diff --git a/fetch.js b/fetch.js index 073ff7e4..145dc769 100644 --- a/fetch.js +++ b/fetch.js @@ -1,7 +1,9 @@ (function() { 'use strict'; - if (self.fetch) { + // if __disableNativeFetch is set to true, the it will always polyfill fetch + // with Ajax. + if (!self.__disableNativeFetch && self.fetch) { return } From c9a4799fea5024a9b69b7c52e91c109b89ad59c0 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Thu, 13 Aug 2015 21:31:02 +0800 Subject: [PATCH 05/25] release 1.1.0 --- bower.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bower.json b/bower.json index 6ee200d4..38a6bf1c 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.0.0", + "version": "1.1.0", "main": "fetch.js", "devDependencies": { "es6-promise": "2.1.0" diff --git a/package.json b/package.json index 5eb6e388..e38fe990 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.0.0", + "version": "1.1.0", "main": "fetch.js", "repository": "camsong/fetch", "license": "MIT", From 408edd6e1ad3ec5cb136338bc8033384eb8fc4be Mon Sep 17 00:00:00 2001 From: Cam Song Date: Fri, 14 Aug 2015 16:46:47 +0800 Subject: [PATCH 06/25] stop using ActiveXObject in IE, since IE8 works with XMLHttpRequest well --- bower.json | 2 +- fetch.js | 16 +--------------- package.json | 2 +- 3 files changed, 3 insertions(+), 17 deletions(-) diff --git a/bower.json b/bower.json index 38a6bf1c..7a6d6a84 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.1.0", + "version": "1.2.0", "main": "fetch.js", "devDependencies": { "es6-promise": "2.1.0" diff --git a/fetch.js b/fetch.js index 145dc769..642785b5 100644 --- a/fetch.js +++ b/fetch.js @@ -248,20 +248,6 @@ return head } - var noXhrPatch = - typeof window !== 'undefined' && !!window.ActiveXObject && - !(window.XMLHttpRequest && (new XMLHttpRequest).dispatchEvent); - - function getXhr() { - // from backbone.js 1.1.2 - // https://github.com/jashkenas/backbone/blob/1.1.2/backbone.js#L1181 - if (noXhrPatch && !(/^(get|post|head|put|delete|options)$/i.test(this.method))) { - this.usingActiveXhr = true; - return new ActiveXObject("Microsoft.XMLHTTP"); - } - return new XMLHttpRequest(); - } - Body.call(Request.prototype) function Response(bodyInit, options) { @@ -295,7 +281,7 @@ } return new fetch.Promise(function(resolve, reject) { - var xhr = getXhr(); + var xhr = new XMLHttpRequest(); if (request.credentials === 'include') { xhr.withCredentials = true; } diff --git a/package.json b/package.json index e38fe990..737c8a8b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.1.0", + "version": "1.2.0", "main": "fetch.js", "repository": "camsong/fetch", "license": "MIT", From 6144fac428ad6cb3461441d5991d5361113df73f Mon Sep 17 00:00:00 2001 From: Cam Song Date: Mon, 24 Aug 2015 10:49:35 +0800 Subject: [PATCH 07/25] fix invalidStateError in IE10 when setting withCredentials --- fetch.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/fetch.js b/fetch.js index 642785b5..a3c7bf59 100644 --- a/fetch.js +++ b/fetch.js @@ -282,9 +282,6 @@ return new fetch.Promise(function(resolve, reject) { var xhr = new XMLHttpRequest(); - if (request.credentials === 'include') { - xhr.withCredentials = true; - } function responseURL() { if ('responseURL' in xhr) { @@ -327,6 +324,20 @@ xhr.open(request.method, request.url, true) + // `withCredentials` should be setted after calling `.open` in IE10 + // http://stackoverflow.com/a/19667959/1219343 + try { + if (request.credentials === 'include') { + if ('withCredentials' in xhr) { + xhr.withCredentials = true; + } else { + console.error && console.error('withCredentials not supported'); + } + } + } catch (e) { + console.error && console.error('set withCredentials error:' + e); + } + if ('responseType' in xhr && support.blob) { xhr.responseType = 'blob' } From 21506b16cc2620ebfefb6716ca042a8094be6a6d Mon Sep 17 00:00:00 2001 From: Cam Song Date: Mon, 24 Aug 2015 10:50:46 +0800 Subject: [PATCH 08/25] release 1.3.0 --- bower.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bower.json b/bower.json index 7a6d6a84..5c3656fb 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.2.0", + "version": "1.3.0", "main": "fetch.js", "devDependencies": { "es6-promise": "2.1.0" diff --git a/package.json b/package.json index 737c8a8b..36d90d94 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.2.0", + "version": "1.3.0", "main": "fetch.js", "repository": "camsong/fetch", "license": "MIT", From 9dd8c2a9c6beb7fcf5fbb5ec3e330dedd24e65b8 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Tue, 8 Sep 2015 21:49:12 +0800 Subject: [PATCH 09/25] use console.warn to replace console.error --- fetch.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fetch.js b/fetch.js index a3c7bf59..66a978c2 100644 --- a/fetch.js +++ b/fetch.js @@ -331,11 +331,11 @@ if ('withCredentials' in xhr) { xhr.withCredentials = true; } else { - console.error && console.error('withCredentials not supported'); + console && console.warn && console.warn('withCredentials is not supported, you can ignore this warning'); } } } catch (e) { - console.error && console.error('set withCredentials error:' + e); + console && console.warn && console.warn('set withCredentials error:' + e); } if ('responseType' in xhr && support.blob) { From 74742b246026253b0f3bd0deb9ce7411ab646f28 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Tue, 8 Sep 2015 21:50:40 +0800 Subject: [PATCH 10/25] release 1.3.1 --- HISTORY.md | 3 +++ bower.json | 2 +- package.json | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 HISTORY.md diff --git a/HISTORY.md b/HISTORY.md new file mode 100644 index 00000000..20e89e77 --- /dev/null +++ b/HISTORY.md @@ -0,0 +1,3 @@ +1.3.1 / 2015-09-08 +================== +* use console.warn to log messages diff --git a/bower.json b/bower.json index 5c3656fb..b402f38d 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.3.0", + "version": "1.3.1", "main": "fetch.js", "devDependencies": { "es6-promise": "2.1.0" diff --git a/package.json b/package.json index 36d90d94..2d76884c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.3.0", + "version": "1.3.1", "main": "fetch.js", "repository": "camsong/fetch", "license": "MIT", From 056755255c638003dd8230dcc5e6897b1b7523a5 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Wed, 9 Dec 2015 21:01:40 +0800 Subject: [PATCH 11/25] sync with github/fetch --- fetch.js | 149 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 95 insertions(+), 54 deletions(-) diff --git a/fetch.js b/fetch.js index 66a978c2..cb4604de 100644 --- a/fetch.js +++ b/fetch.js @@ -9,7 +9,7 @@ function normalizeName(name) { if (typeof name !== 'string') { - name = name.toString(); + name = String(name) } if (/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(name)) { throw new TypeError('Invalid character in header field name') @@ -19,7 +19,7 @@ function normalizeValue(value) { if (typeof value !== 'string') { - value = value.toString(); + value = String(value) } return value } @@ -27,18 +27,15 @@ function Headers(headers) { this.map = {} - var self = this if (headers instanceof Headers) { - headers.forEach(function(name, values) { - values.forEach(function(value) { - self.append(name, value) - }) - }) + headers.forEach(function(value, name) { + this.append(name, value) + }, this) } else if (headers) { Object.getOwnPropertyNames(headers).forEach(function(name) { - self.append(name, headers[name]) - }) + this.append(name, headers[name]) + }, this) } } @@ -74,23 +71,23 @@ this.map[normalizeName(name)] = [normalizeValue(value)] } - // Instead of iterable for now. - Headers.prototype.forEach = function(callback) { - var self = this + Headers.prototype.forEach = function(callback, thisArg) { Object.getOwnPropertyNames(this.map).forEach(function(name) { - callback(name, self.map[name]) - }) + this.map[name].forEach(function(value) { + callback.call(thisArg, value, name, this) + }, this) + }, this) } function consumed(body) { if (body.bodyUsed) { - return fetch.Promise.reject(new TypeError('Already read')) + return Promise.reject(new TypeError('Already read')) } body.bodyUsed = true } function fileReaderReady(reader) { - return new fetch.Promise(function(resolve, reject) { + return new Promise(function(resolve, reject) { reader.onload = function() { resolve(reader.result) } @@ -121,7 +118,8 @@ return false } })(), - formData: 'FormData' in self + formData: 'FormData' in self, + arrayBuffer: 'ArrayBuffer' in self } function Body() { @@ -138,6 +136,9 @@ 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') } @@ -151,11 +152,11 @@ } if (this._bodyBlob) { - return fetch.Promise.resolve(this._bodyBlob) + return Promise.resolve(this._bodyBlob) } else if (this._bodyFormData) { throw new Error('could not read FormData body as blob') } else { - return fetch.Promise.resolve(new Blob([this._bodyText])) + return Promise.resolve(new Blob([this._bodyText])) } } @@ -174,13 +175,13 @@ } else if (this._bodyFormData) { throw new Error('could not read FormData body as text') } else { - return fetch.Promise.resolve(this._bodyText) + return Promise.resolve(this._bodyText) } } } else { this.text = function() { var rejected = consumed(this) - return rejected ? rejected : fetch.Promise.resolve(this._bodyText) + return rejected ? rejected : Promise.resolve(this._bodyText) } } @@ -191,9 +192,7 @@ } this.json = function() { - return this.text().then(function (text) { - return JSON.parse(text); - }); + return this.text().then(JSON.parse) } return this @@ -207,20 +206,44 @@ return (methods.indexOf(upcased) > -1) ? upcased : method } - function Request(url, options) { + function Request(input, options) { options = options || {} - this.url = url + var body = options.body + if (Request.prototype.isPrototypeOf(input)) { + if (input.bodyUsed) { + throw new TypeError('Already read') + } + this.url = input.url + this.credentials = input.credentials + if (!options.headers) { + this.headers = new Headers(input.headers) + } + this.method = input.method + this.mode = input.mode + if (!body) { + body = input._bodyInit + input.bodyUsed = true + } + } else { + this.url = input + } - this.credentials = options.credentials || 'omit' - this.headers = new Headers(options.headers) - this.method = normalizeMethod(options.method || 'GET') - this.mode = options.mode || null + this.credentials = options.credentials || this.credentials || 'omit' + if (options.headers || !this.headers) { + this.headers = new Headers(options.headers) + } + this.method = normalizeMethod(options.method || this.method || 'GET') + this.mode = options.mode || this.mode || null this.referrer = null - if ((this.method === 'GET' || this.method === 'HEAD') && options.body) { + if ((this.method === 'GET' || this.method === 'HEAD') && body) { throw new TypeError('Body not allowed for GET or HEAD requests') } - this._initBody(options.body) + this._initBody(body) + } + + Request.prototype.clone = function() { + return new Request(this) } function decode(body) { @@ -257,7 +280,6 @@ 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 @@ -267,21 +289,45 @@ Body.call(Response.prototype) + Response.prototype.clone = function() { + return new Response(this._bodyInit, { + status: this.status, + statusText: this.statusText, + headers: new Headers(this.headers), + url: this.url + }) + } + + 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) { - // TODO: Request constructor should accept 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) + } - return new fetch.Promise(function(resolve, reject) { - var xhr = new XMLHttpRequest(); + var xhr = new XMLHttpRequest() function responseURL() { if ('responseURL' in xhr) { @@ -315,11 +361,9 @@ resolve(new Response(body, options)) } xhr.onreadystatechange = onload; - if (!self.usingActiveXhr) { - xhr.onload = onload; - xhr.onerror = function() { - reject(new TypeError('Network request failed')) - } + xhr.onload = onload; // FIX: may fire two times + xhr.onerror = function() { + reject(new TypeError('Network request failed')) } xhr.open(request.method, request.url, true) @@ -342,20 +386,17 @@ xhr.responseType = 'blob' } - request.headers.forEach(function(name, values) { - values.forEach(function(value) { - xhr.setRequestHeader(name, value) - }) + request.headers.forEach(function(value, name) { + xhr.setRequestHeader(name, value) }) xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit) }) } - fetch.Promise = self.Promise; // you could change it to your favorite alternative self.fetch.polyfill = true // Support CommonJS if (typeof module !== 'undefined' && module.exports) { module.exports = fetch; } -})(); +})(); \ No newline at end of file From 3a04845fa3eb1a61fc5e8f746c9fd1c83502c29e Mon Sep 17 00:00:00 2001 From: Cam Song Date: Wed, 9 Dec 2015 21:02:15 +0800 Subject: [PATCH 12/25] fix resolve twice bug --- fetch.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fetch.js b/fetch.js index cb4604de..79f89409 100644 --- a/fetch.js +++ b/fetch.js @@ -342,12 +342,15 @@ return; } + var __onLoadHandled = false; + function onload() { if (xhr.readyState !== 4) { return } var status = (xhr.status === 1223) ? 204 : xhr.status if (status < 100 || status > 599) { + if (__onLoadHandled) { return; } else { __onLoadHandled = true; } reject(new TypeError('Network request failed')) return } @@ -358,11 +361,14 @@ url: responseURL() } var body = 'response' in xhr ? xhr.response : xhr.responseText; + + if (__onLoadHandled) { return; } else { __onLoadHandled = true; } resolve(new Response(body, options)) } xhr.onreadystatechange = onload; - xhr.onload = onload; // FIX: may fire two times + xhr.onload = onload; xhr.onerror = function() { + if (__onLoadHandled) { return; } else { __onLoadHandled = true; } reject(new TypeError('Network request failed')) } From c629e939d8446ec5b4d66f07ab37979847654f73 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Wed, 9 Dec 2015 21:03:21 +0800 Subject: [PATCH 13/25] release 1.4.0 --- HISTORY.md | 7 ++++++- package.json | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 20e89e77..9e6c6ce6 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +1.4.0 / 2015-12-09 +================== +* Sync with github/fetch +* Fix promise resolve twice bug + 1.3.1 / 2015-09-08 ================== -* use console.warn to log messages +* Use console.warn to log messages diff --git a/package.json b/package.json index 2d76884c..ac47b7bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.3.1", + "version": "1.4.0", "main": "fetch.js", "repository": "camsong/fetch", "license": "MIT", From fb33d8fd2ae50962b1421c390f86dd81b5f62667 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Wed, 30 Dec 2015 15:13:29 +0800 Subject: [PATCH 14/25] update installation guide --- README.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2eab3acf..88398d2f 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,10 @@ possible to the standard Fetch specification at https://fetch.spec.whatwg.org. ## Installation -Available on [Bower](http://bower.io) as **fetch-polyfill**. +Available on [Bower](http://bower.io) as **fetch-ie8**. ```sh -$ bower install fetch-polyfill +$ bower install fetch-ie8 ``` You'll also need a Promise polyfill for older browsers. @@ -26,10 +26,16 @@ You'll also need a Promise polyfill for older browsers. $ bower install es6-promise ``` -This can also be installed with `npm`. +Install with `npm`. ```sh -$ npm install fetch-polyfill --save +$ npm install fetch-ie8 --save +``` + +You'll also need a Promise polyfill for older browsers. + +```sh +$ npm install es6-promise ``` (For a node.js implementation, try [node-fetch](https://github.com/bitinn/node-fetch)) From 85e39b2b3052a06233bce387217a2659939b9c44 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Wed, 30 Dec 2015 15:14:38 +0800 Subject: [PATCH 15/25] Update README.md --- README.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/README.md b/README.md index 88398d2f..ff50eec0 100644 --- a/README.md +++ b/README.md @@ -14,20 +14,6 @@ possible to the standard Fetch specification at https://fetch.spec.whatwg.org. ## Installation -Available on [Bower](http://bower.io) as **fetch-ie8**. - -```sh -$ bower install fetch-ie8 -``` - -You'll also need a Promise polyfill for older browsers. - -```sh -$ bower install es6-promise -``` - -Install with `npm`. - ```sh $ npm install fetch-ie8 --save ``` From 8ff8534264d545addf877ed3a0cc294d6403884b Mon Sep 17 00:00:00 2001 From: Isken Huang Date: Wed, 20 Jan 2016 12:06:52 +0800 Subject: [PATCH 16/25] fixed blob charset for different encoding --- fetch.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fetch.js b/fetch.js index 79f89409..e81b546e 100644 --- a/fetch.js +++ b/fetch.js @@ -105,7 +105,13 @@ function readBlobAsText(blob) { var reader = new FileReader() - reader.readAsText(blob) + var _charset = blob.type.match(/charset\=[0-9a-zA-Z\-\_]*;?/) + var args = [blob] + if(_charset) { + args.push(_charset[0].replace(/^charset\=/, '').replace(/;$/, '')) + } + + reader.readAsText.apply(reader, args) return fileReaderReady(reader) } From 1460852f5bf0fbe9a5134e67a211fa9307abed32 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Wed, 20 Jan 2016 21:44:26 +0800 Subject: [PATCH 17/25] bumb 1.4.1, fixed #1 --- HISTORY.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 9e6c6ce6..fa95fc84 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,7 @@ +1.4.1 / 2016-01-20 +================== +* Make blob supporting different encoding, fixed #1 + 1.4.0 / 2015-12-09 ================== * Sync with github/fetch diff --git a/package.json b/package.json index ac47b7bf..26686d90 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.4.0", + "version": "1.4.1", "main": "fetch.js", "repository": "camsong/fetch", "license": "MIT", From 8a5da1ac0140018dfc72a8067296fc2b8956e4bb Mon Sep 17 00:00:00 2001 From: Isken Huang Date: Sun, 13 Mar 2016 16:31:15 +0800 Subject: [PATCH 18/25] fixed request encode is utf8 but response different. When encodeing different can be using response content-type --- fetch.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/fetch.js b/fetch.js index e81b546e..b338ff8d 100644 --- a/fetch.js +++ b/fetch.js @@ -103,10 +103,13 @@ return fileReaderReady(reader) } - function readBlobAsText(blob) { + function readBlobAsText(blob, options) { var reader = new FileReader() - var _charset = blob.type.match(/charset\=[0-9a-zA-Z\-\_]*;?/) + var contentType = options.headers.map['content-type'] ? options.headers.map['content-type'].toString() : '' + var regex = /charset\=[0-9a-zA-Z\-\_]*;?/ + var _charset = blob.type.match(regex) || contentType.match(regex) var args = [blob] + if(_charset) { args.push(_charset[0].replace(/^charset\=/, '').replace(/;$/, '')) } @@ -132,12 +135,13 @@ this.bodyUsed = false - this._initBody = function(body) { + this._initBody = function(body, options) { this._bodyInit = body if (typeof body === 'string') { this._bodyText = body } else if (support.blob && Blob.prototype.isPrototypeOf(body)) { this._bodyBlob = body + this._options = options } else if (support.formData && FormData.prototype.isPrototypeOf(body)) { this._bodyFormData = body } else if (!body) { @@ -177,7 +181,7 @@ } if (this._bodyBlob) { - return readBlobAsText(this._bodyBlob) + return readBlobAsText(this._bodyBlob, this._options) } else if (this._bodyFormData) { throw new Error('could not read FormData body as text') } else { @@ -245,7 +249,7 @@ if ((this.method === 'GET' || this.method === 'HEAD') && body) { throw new TypeError('Body not allowed for GET or HEAD requests') } - this._initBody(body) + this._initBody(body, options) } Request.prototype.clone = function() { @@ -284,7 +288,7 @@ options = {} } - this._initBody(bodyInit) + this._initBody(bodyInit, options) this.type = 'default' this.status = options.status this.ok = this.status >= 200 && this.status < 300 From 582588faa0d12e9f7c276dcb8dcafc98cc8f01d2 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Sun, 13 Mar 2016 22:55:40 +0800 Subject: [PATCH 19/25] release 1.4.2, fix #4 --- HISTORY.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index fa95fc84..64f67018 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,7 @@ +1.4.2 / 2016-03-13 +================== +* Fix request and response content-type different bug + 1.4.1 / 2016-01-20 ================== * Make blob supporting different encoding, fixed #1 diff --git a/package.json b/package.json index 26686d90..f9053dcf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.4.1", + "version": "1.4.2", "main": "fetch.js", "repository": "camsong/fetch", "license": "MIT", From 59b9e83f4db18e742d2a50843f22ae46bf7c15e1 Mon Sep 17 00:00:00 2001 From: Marzio Superina Date: Tue, 21 Jun 2016 11:53:11 +0100 Subject: [PATCH 20/25] Compatibility when defining the global object --- fetch.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fetch.js b/fetch.js index b338ff8d..10ec9c6f 100644 --- a/fetch.js +++ b/fetch.js @@ -413,6 +413,6 @@ // Support CommonJS if (typeof module !== 'undefined' && module.exports) { - module.exports = fetch; + module.exports = self.fetch; } -})(); \ No newline at end of file +})(); From 63d1f42fcd3266b1a505f15b1e6f9031a8e179b9 Mon Sep 17 00:00:00 2001 From: Cam Song Date: Wed, 22 Jun 2016 15:08:29 +0800 Subject: [PATCH 21/25] release 1.4.3 --- HISTORY.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 64f67018..36b2c403 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,7 @@ +1.4.3 / 2016-06-22 +================== +* Compatibility when defining the global object, merged #5 + 1.4.2 / 2016-03-13 ================== * Fix request and response content-type different bug diff --git a/package.json b/package.json index f9053dcf..9f6172af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.4.2", + "version": "1.4.3", "main": "fetch.js", "repository": "camsong/fetch", "license": "MIT", From 90816c3ceb2dd4cfbff13182758da18e6b28843d Mon Sep 17 00:00:00 2001 From: Cam Song Date: Tue, 27 Sep 2016 13:53:33 +0800 Subject: [PATCH 22/25] add es6-promise --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index ff50eec0..1de9fb81 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,11 @@ You'll also need a Promise polyfill for older browsers. $ npm install es6-promise ``` +Run this to polyfill the global environment at the beginning of your application. +```js +require('es6-promise').polyfill(); +``` + (For a node.js implementation, try [node-fetch](https://github.com/bitinn/node-fetch)) ## Usage From e19e6854d23b44965e1ee5209944975b2278ca4a Mon Sep 17 00:00:00 2001 From: Cam Song Date: Tue, 7 Feb 2017 17:16:36 +0800 Subject: [PATCH 23/25] update browser icons --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1de9fb81..e5ac6dc9 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,6 @@ header hack. ## Browser Support -![Chrome](https://raw.github.com/alrra/browser-logos/master/chrome/chrome_48x48.png) | ![Firefox](https://raw.github.com/alrra/browser-logos/master/firefox/firefox_48x48.png) | ![IE](https://raw.github.com/alrra/browser-logos/master/internet-explorer/internet-explorer_48x48.png) | ![Opera](https://raw.github.com/alrra/browser-logos/master/opera/opera_48x48.png) | ![Safari](https://raw.github.com/alrra/browser-logos/master/safari/safari_48x48.png) +![Chrome](https://raw.github.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png) | ![Firefox](https://raw.github.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png) | ![IE](https://raw.github.com/alrra/browser-logos/master/src/archive/internet-explorer_7-8/internet-explorer_7-8_48x48.png) | ![Opera](https://raw.github.com/alrra/browser-logos/master/src/opera/opera_48x48.png) | ![Safari](https://raw.github.com/alrra/browser-logos/master/src/safari/safari_48x48.png) --- | --- | --- | --- | --- | Latest ✔ | Latest ✔ | 8+ ✔ | Latest ✔ | 6.1+ ✔ | From 5312af45581e2be9405f9e8c9018c1c48e12dfbd Mon Sep 17 00:00:00 2001 From: Cam Song Date: Tue, 28 Feb 2017 11:40:46 +0800 Subject: [PATCH 24/25] update self --- fetch.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fetch.js b/fetch.js index 10ec9c6f..3791e480 100644 --- a/fetch.js +++ b/fetch.js @@ -1,4 +1,4 @@ -(function() { +(function(self) { 'use strict'; // if __disableNativeFetch is set to true, the it will always polyfill fetch @@ -415,4 +415,4 @@ if (typeof module !== 'undefined' && module.exports) { module.exports = self.fetch; } -})(); +})(typeof self !== 'undefined' ? self : this); From 44ce416c7bf110812b8fe73ca35ad9af89e9b67c Mon Sep 17 00:00:00 2001 From: Cam Song Date: Tue, 28 Feb 2017 11:41:47 +0800 Subject: [PATCH 25/25] release 1.5.0 --- HISTORY.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 36b2c403..dbd6bd3c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,7 @@ +1.5.0 / 2017-02-28 +================== +* Fix self + 1.4.3 / 2016-06-22 ================== * Compatibility when defining the global object, merged #5 diff --git a/package.json b/package.json index 9f6172af..203b141d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fetch-ie8", - "version": "1.4.3", + "version": "1.5.0", "main": "fetch.js", "repository": "camsong/fetch", "license": "MIT",