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

Commit e436b84

Browse files
committed
support xhr authorization
1 parent 08349ae commit e436b84

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/system-fetch.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var fetchTextFromURL;
22
if (typeof XMLHttpRequest != 'undefined') {
3-
fetchTextFromURL = function(url, fulfill, reject) {
3+
fetchTextFromURL = function(url, authorization, fulfill, reject) {
44
var xhr = new XMLHttpRequest();
55
var sameDomain = true;
66
var doTimeout = false;
@@ -40,8 +40,15 @@
4040
};
4141
xhr.open("GET", url, true);
4242

43-
if (xhr.setRequestHeader)
43+
if (xhr.setRequestHeader) {
4444
xhr.setRequestHeader('Accept', 'application/x-es-module */*');
45+
// can set "authorization: true" to enable withCredentials only
46+
if (authorization) {
47+
if (typeof authorization == 'string')
48+
xhr.setRequestHeader('Authorization', authorization);
49+
xhr.withCredentials = true;
50+
}
51+
}
4552

4653
if (doTimeout)
4754
setTimeout(function() {
@@ -53,7 +60,7 @@
5360
}
5461
else if (typeof require != 'undefined') {
5562
var fs;
56-
fetchTextFromURL = function(url, fulfill, reject) {
63+
fetchTextFromURL = function(url, authorization, fulfill, reject) {
5764
if (url.substr(0, 8) != 'file:///')
5865
throw new Error('Unable to fetch "' + url + '". Only file URLs of the form file:/// allowed running in Node.');
5966
fs = fs || require('fs');
@@ -82,6 +89,6 @@
8289

8390
SystemLoader.prototype.fetch = function(load) {
8491
return new Promise(function(resolve, reject) {
85-
fetchTextFromURL(load.address, resolve, reject);
92+
fetchTextFromURL(load.address, undefined, resolve, reject);
8693
});
8794
};

0 commit comments

Comments
 (0)