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

Commit

Permalink
support xhr authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Sep 5, 2015
1 parent 08349ae commit e436b84
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/system-fetch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var fetchTextFromURL;
if (typeof XMLHttpRequest != 'undefined') {
fetchTextFromURL = function(url, fulfill, reject) {
fetchTextFromURL = function(url, authorization, fulfill, reject) {
var xhr = new XMLHttpRequest();
var sameDomain = true;
var doTimeout = false;
Expand Down Expand Up @@ -40,8 +40,15 @@
};
xhr.open("GET", url, true);

if (xhr.setRequestHeader)
if (xhr.setRequestHeader) {
xhr.setRequestHeader('Accept', 'application/x-es-module */*');
// can set "authorization: true" to enable withCredentials only
if (authorization) {
if (typeof authorization == 'string')
xhr.setRequestHeader('Authorization', authorization);
xhr.withCredentials = true;
}
}

if (doTimeout)
setTimeout(function() {
Expand All @@ -53,7 +60,7 @@
}
else if (typeof require != 'undefined') {
var fs;
fetchTextFromURL = function(url, fulfill, reject) {
fetchTextFromURL = function(url, authorization, fulfill, reject) {
if (url.substr(0, 8) != 'file:///')
throw new Error('Unable to fetch "' + url + '". Only file URLs of the form file:/// allowed running in Node.');
fs = fs || require('fs');
Expand Down Expand Up @@ -82,6 +89,6 @@

SystemLoader.prototype.fetch = function(load) {
return new Promise(function(resolve, reject) {
fetchTextFromURL(load.address, resolve, reject);
fetchTextFromURL(load.address, undefined, resolve, reject);
});
};

0 comments on commit e436b84

Please sign in to comment.