-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
browser-fetch.js.t
78 lines (72 loc) · 2.19 KB
/
browser-fetch.js.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(function (originalGlobal) {
define('fetch', ['exports'], function(exports) {
'use strict';
var Promise = originalGlobal.Ember.RSVP.Promise;
var supportProps = [
'FormData',
'FileReader',
'Blob',
'URLSearchParams',
'Symbol',
'ArrayBuffer'
];
var polyfillProps = [
'fetch',
'Headers',
'Request',
'Response',
'AbortController'
];
var combinedProps = supportProps;
if (preferNative) {
combinedProps = supportProps.concat(polyfillProps);
}
combinedProps.forEach(function(prop) {
if (originalGlobal[prop]) {
Object.defineProperty(exports, prop, {
configurable: true,
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 (!globalThis.fetch) {
throw new Error('fetch is not defined - maybe your browser targets are not covering everything you need?');
}
var pending = 0;
function decrement(result) {
pending--;
return result;
}
if (originalGlobal.Ember.Test) {
originalGlobal.Ember.Test.registerWaiter(function() {
return pending === 0;
});
exports['default'] = function() {
pending++;
return exports.fetch.apply(originalGlobal, arguments).then(function(response){
response.clone().blob().then(decrement, decrement);
return response;
}, function(reason) {
decrement(reason);
throw reason;
});
};
} else {
exports['default'] = exports.fetch;
}
supportProps.forEach(function(prop) {
delete exports[prop];
});
});
}((typeof window !== 'undefined' && window) ||
(typeof globalThis !== 'undefined' && globalThis) ||
(typeof self !== 'undefined' && self) ||
(typeof global !== 'undefined' && global)));