Skip to content

Commit

Permalink
implement directory-index spec
Browse files Browse the repository at this point in the history
  • Loading branch information
hthetiot committed May 10, 2018
1 parent 70a51ce commit 272da7b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
35 changes: 24 additions & 11 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,35 @@ bootstrap("require/browser", function (require) {
} else {
xhr.onerror(event);
}
//This clears the response from memory
xhr.abort();
xhr.url = null;
xhr.module = null;
}
onload.xhrPool = xhrPool;

var jsIndexPrefix = '/index.js',
jsPreffix = '.js';
function onerror(event) {
var xhr = event.target,
url = xhr.url;
xhr.reject(new Error("Can't XHR " + JSON.stringify(url)));
onerror.xhrPool.push(xhr);
//This clears the response from memory
xhr.abort();
xhr.url = null;
xhr.module = null;

// Re-use xhr on read on .js failure if not /index.js file and
// retry on /index.js dynamically.
if (
url.indexOf(jsPreffix) !== -1 && // is .js
url.indexOf(jsIndexPrefix) === -1 // is not /index.js
) {
xhr.url = xhr.url.replace(jsPreffix, jsIndexPrefix);
xhr.module.location = xhr.url;
xhr.open(GET, xhr.url, true);
xhr.send(null);

} else {

xhr.reject(new Error("Can't XHR " + JSON.stringify(url)));
onerror.xhrPool.push(xhr);
//This clears the response from memory
xhr.abort();
xhr.url = null;
xhr.module = null;
}

}
onerror.xhrPool = xhrPool;
Expand All @@ -98,12 +111,12 @@ bootstrap("require/browser", function (require) {
xhr.reject = reject;
};
}

xhr.url = url;
xhr.module = module;

xhr.open(GET, url, true);


var response = new Promise(xhr.promiseHandler);
xhr.send(null);

Expand Down
53 changes: 28 additions & 25 deletions node.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,37 @@ Require.directoryPathToLocation = function directoryPathToLocation(path) {
return path;
};

var jsIndexPrefix = '/index.js',
jsPreffix = '.js';
Require.read = function read(location) {
var deferred = Promise.defer();
var path = Require.locationToPath(location),
indexPath = path.replace('.js', '/index.js');

// Check if file exists
FS.stat(path, function(err) {
if (!err) {
FS.readFile(path, "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
});

// use /index.js instead
} else {
FS.readFile(indexPath, "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
return new Promise(function (resolve, reject) {
var path = Require.locationToPath(location);
FS.readFile(path, "utf-8", function (error, text) {
if (error) {
// Re-use xhr on read on .js failure if not /index.js file and
// retry on /index.js dynamically.
if (
path.indexOf(jsPreffix) !== -1 && // is .js
path.indexOf(jsIndexPrefix) === -1 // is not /index.js
) {
path = path.replace(jsPreffix, jsIndexPrefix);

// Attempt to read if file exists
FS.readFile(path, "utf-8", function (error, text) {
if (error) {
reject(new Error(error));
} else {
resolve(text);
}
});
} else {
deferred.resolve(text);
reject(new Error(error));
}
});
}
});
return deferred.promise;
} else {
resolve(text);
}
});
});
};

// Compiles module text into a function.
Expand Down

0 comments on commit 272da7b

Please sign in to comment.