Skip to content

Commit

Permalink
Merge pull request #552 from xg-wang/fix-fetch-3-4
Browse files Browse the repository at this point in the history
  • Loading branch information
xg-wang authored Aug 10, 2020
2 parents dd0c024 + e685a4b commit 4f266b6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
script:
- yarn lint:js
- yarn test
- PREFER_NATIVE=true yarn test
- npm run test:node

- name: "Floating Dependencies"
Expand Down
39 changes: 24 additions & 15 deletions assets/browser-fetch.js.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global) {
define('fetch', ['exports'], function(self) {
(function (originalGlobal) {
define('fetch', ['exports'], function(exports) {
'use strict';
var Promise = global.Ember.RSVP.Promise;
var Promise = originalGlobal.Ember.RSVP.Promise;
var supportProps = [
'FormData',
'FileReader',
Expand All @@ -22,18 +22,24 @@
combinedProps = supportProps.concat(polyfillProps);
}
combinedProps.forEach(function(prop) {
if (global[prop]) {
Object.defineProperty(self, prop, {
if (originalGlobal[prop]) {
Object.defineProperty(exports, prop, {
configurable: true,
get: function() { return global[prop] },
set: function(v) { global[prop] = v }
get: function() { return originalGlobal[prop] },
set: function(v) { originalGlobal[prop] = v }
});
}
});

// shadow github/fetch global object
// https://github.com/github/fetch/blob/v3.4.0/fetch.js
var globalThis = exports;
// shadow mo/abortcontroller-polyfill global object
// https://github.com/mo/abortcontroller-polyfill/blob/v1.4.0/src/abortcontroller-polyfill.js
var self = exports;
<%= moduleBody %>

if (!self.fetch) {
if (!globalThis.fetch) {
throw new Error('fetch is not defined - maybe your browser targets are not covering everything you need?');
}

Expand All @@ -43,15 +49,15 @@
return result;
}

if (global.Ember.Test) {
global.Ember.Test.registerWaiter(function() {
if (originalGlobal.Ember.Test) {
originalGlobal.Ember.Test.registerWaiter(function() {
return pending === 0;
});

self['default'] = function() {
exports['default'] = function() {
pending++;

return self.fetch.apply(global, arguments).then(function(response){
return exports.fetch.apply(originalGlobal, arguments).then(function(response){
response.clone().blob().then(decrement, decrement);
return response;
}, function(reason) {
Expand All @@ -60,10 +66,13 @@
});
};
} else {
self['default'] = self.fetch;
exports['default'] = exports.fetch;
}
supportProps.forEach(function(prop) {
delete self[prop];
delete exports[prop];
});
});
}(typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this));
}((typeof window !== 'undefined' && window) ||
(typeof globalThis !== 'undefined' && globalThis) ||
(typeof self !== 'undefined' && self) ||
(typeof global !== 'undefined' && global)));
2 changes: 1 addition & 1 deletion ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function(defaults) {
let app = new EmberAddon(defaults, {
// Add options here
'ember-fetch': {
preferNative: true
preferNative: process.env.PREFER_NATIVE ? true : false
}
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"ember-cli-babel": "^7.19.0",
"ember-cli-typescript": "^3.1.3",
"node-fetch": "^2.6.0",
"whatwg-fetch": "^3.0.0"
"whatwg-fetch": "^3.4.0"
},
"devDependencies": {
"@ember/optional-features": "^1.3.0",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11120,7 +11120,7 @@ websocket-extensions@>=0.1.1:
resolved "https://registry.yarnpkg.com/websocket-extensions/-/websocket-extensions-0.1.4.tgz#7f8473bc839dfd87608adb95d7eb075211578a42"
integrity sha512-OqedPIGOfsDlo31UNwYbCFMSaO9m9G/0faIHj5/dZFDMFqPTcx6UwqyOy3COEaEOg/9VsGIpdqn62W5KhoKSpg==

whatwg-fetch@^3.0.0:
whatwg-fetch@^3.0.0, whatwg-fetch@^3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.4.0.tgz#e11de14f4878f773fbebcde8871b2c0699af8b30"
integrity sha512-rsum2ulz2iuZH08mJkT0Yi6JnKhwdw4oeyMjokgxd+mmqYSd9cPpOQf01TIWgjxG/U4+QR+AwKq6lSbXVxkyoQ==
Expand Down

0 comments on commit 4f266b6

Please sign in to comment.