Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit dba399b

Browse files
committed
separate fetch implementation
1 parent ef5521c commit dba399b

File tree

3 files changed

+83
-85
lines changed

3 files changed

+83
-85
lines changed

Gruntfile.js

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = function (grunt) {
2828
'src/url-polyfill.js',
2929
'src/system.js',
3030
'src/system-resolve.js',
31+
'src/system-fetch.js',
3132
'src/wrapper-end.js'
3233
],
3334
'dist/<%= pkg.name %>-dev.src.js': [
@@ -39,6 +40,7 @@ module.exports = function (grunt) {
3940
'src/url-polyfill.js',
4041
'src/system.js',
4142
'src/system-resolve.js',
43+
'src/system-fetch.js',
4244
'src/module-tag.js',
4345
'src/wrapper-end.js'
4446
]

src/system-fetch.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var fetchTextFromURL;
2+
if (typeof XMLHttpRequest != 'undefined') {
3+
fetchTextFromURL = function(url, fulfill, reject) {
4+
var xhr = new XMLHttpRequest();
5+
var sameDomain = true;
6+
var doTimeout = false;
7+
if (!('withCredentials' in xhr)) {
8+
// check if same domain
9+
var domainCheck = /^(\w+:)?\/\/([^\/]+)/.exec(url);
10+
if (domainCheck) {
11+
sameDomain = domainCheck[2] === window.location.host;
12+
if (domainCheck[1])
13+
sameDomain &= domainCheck[1] === window.location.protocol;
14+
}
15+
}
16+
if (!sameDomain && typeof XDomainRequest != 'undefined') {
17+
xhr = new XDomainRequest();
18+
xhr.onload = load;
19+
xhr.onerror = error;
20+
xhr.ontimeout = error;
21+
xhr.onprogress = function() {};
22+
xhr.timeout = 0;
23+
doTimeout = true;
24+
}
25+
function load() {
26+
fulfill(xhr.responseText);
27+
}
28+
function error() {
29+
reject(xhr.statusText + ': ' + url || 'XHR error');
30+
}
31+
32+
xhr.onreadystatechange = function () {
33+
if (xhr.readyState === 4) {
34+
if (xhr.status === 200 || (xhr.status == 0 && xhr.responseText)) {
35+
load();
36+
} else {
37+
error();
38+
}
39+
}
40+
};
41+
xhr.open("GET", url, true);
42+
43+
if (doTimeout)
44+
setTimeout(function() {
45+
xhr.send();
46+
}, 0);
47+
48+
xhr.send(null);
49+
};
50+
}
51+
else if (typeof require != 'undefined') {
52+
var fs;
53+
fetchTextFromURL = function(url, fulfill, reject) {
54+
if (url.substr(0, 8) != 'file:///')
55+
throw 'Only file URLs of the form file:/// allowed running in Node.';
56+
fs = fs || require('fs');
57+
if (isWindows)
58+
url = url.replace(/\//g, '\\').substr(8);
59+
else
60+
url = url.substr(7);
61+
return fs.readFile(url, function(err, data) {
62+
if (err)
63+
return reject(err);
64+
else
65+
fulfill(data + '');
66+
});
67+
};
68+
}
69+
else {
70+
throw new TypeError('No environment fetch API available.');
71+
}
72+
73+
SystemLoader.prototype.fetch = function(load) {
74+
return new Promise(function(resolve, reject) {
75+
fetchTextFromURL(load.address, resolve, reject);
76+
});
77+
};

src/system.js

+4-85
Original file line numberDiff line numberDiff line change
@@ -83,88 +83,7 @@ function applyPaths(loader, name) {
8383
return outPath;
8484
}
8585

86-
(function() {
87-
var fetchTextFromURL;
88-
if (typeof XMLHttpRequest != 'undefined') {
89-
fetchTextFromURL = function(url, fulfill, reject) {
90-
var xhr = new XMLHttpRequest();
91-
var sameDomain = true;
92-
var doTimeout = false;
93-
if (!('withCredentials' in xhr)) {
94-
// check if same domain
95-
var domainCheck = /^(\w+:)?\/\/([^\/]+)/.exec(url);
96-
if (domainCheck) {
97-
sameDomain = domainCheck[2] === window.location.host;
98-
if (domainCheck[1])
99-
sameDomain &= domainCheck[1] === window.location.protocol;
100-
}
101-
}
102-
if (!sameDomain && typeof XDomainRequest != 'undefined') {
103-
xhr = new XDomainRequest();
104-
xhr.onload = load;
105-
xhr.onerror = error;
106-
xhr.ontimeout = error;
107-
xhr.onprogress = function() {};
108-
xhr.timeout = 0;
109-
doTimeout = true;
110-
}
111-
function load() {
112-
fulfill(xhr.responseText);
113-
}
114-
function error() {
115-
reject(xhr.statusText + ': ' + url || 'XHR error');
116-
}
117-
118-
xhr.onreadystatechange = function () {
119-
if (xhr.readyState === 4) {
120-
if (xhr.status === 200 || (xhr.status == 0 && xhr.responseText)) {
121-
load();
122-
} else {
123-
error();
124-
}
125-
}
126-
};
127-
xhr.open("GET", url, true);
128-
129-
if (doTimeout)
130-
setTimeout(function() {
131-
xhr.send();
132-
}, 0);
133-
134-
xhr.send(null);
135-
};
136-
}
137-
else if (typeof require != 'undefined') {
138-
var fs;
139-
fetchTextFromURL = function(url, fulfill, reject) {
140-
if (url.substr(0, 8) != 'file:///')
141-
throw 'Only file URLs of the form file:/// allowed running in Node.';
142-
fs = fs || require('fs');
143-
if (isWindows)
144-
url = url.replace(/\//g, '\\').substr(8);
145-
else
146-
url = url.substr(7);
147-
return fs.readFile(url, function(err, data) {
148-
if (err)
149-
return reject(err);
150-
else
151-
fulfill(data + '');
152-
});
153-
};
154-
}
155-
else {
156-
throw new TypeError('No environment fetch API available.');
157-
}
158-
159-
// inline Object.create-style class extension
160-
function LoaderProto() {}
161-
LoaderProto.prototype = Loader.prototype;
162-
SystemLoader.prototype = new LoaderProto();
163-
164-
SystemLoader.prototype.fetch = function(load) {
165-
return new Promise(function(resolve, reject) {
166-
fetchTextFromURL(load.address, resolve, reject);
167-
});
168-
};
169-
170-
})();
86+
// inline Object.create-style class extension
87+
function LoaderProto() {}
88+
LoaderProto.prototype = Loader.prototype;
89+
SystemLoader.prototype = new LoaderProto();

0 commit comments

Comments
 (0)