Permalink
Browse files

Lessen unnecessary throws, handle require errors properly, count modu…

…le loads properly on multiple module ensures to fix reloading problems
  • Loading branch information...
1 parent c55d0c6 commit b47105fb2228a27e2e2ff4f3ff3ebdb4a1f123b1 @kriszyp committed Aug 25, 2010
Showing with 79 additions and 77 deletions.
  1. +79 −77 lib/nodules.js
View
@@ -20,8 +20,9 @@ var Unzip = require("./nodules-utils/unzip").Unzip,
main = null,
defaultMap = {
"http://github.com/([^/]+)/}([^/]+)/raw/([^/]+)/(.*)": "zip:http://github.com/$1/$2/zipball/$3!/$4"
- };
-
+ },
+ allKnownOverlays = {npm: true, narwhal: true, rhino: true, node: true};
+
if(typeof process === "undefined"){
var request = require("./nodules-utils/rhino-http-client").request,
schedule = require("./nodules-utils/rhino-delay").schedule,
@@ -64,6 +65,9 @@ function EnginePackage(engine){
e.message += " trying to parse local package.json";
throw e;
}
+ if(path.charAt(path.length - 1) == '\\' || path.charAt(path.length - 1) == '/'){
+ path = path.substring(0, path.length - 1);
+ }
return enginePackage.usePackage(parsed, "file://" + path);
};
this.usePackage= function(packageData, path){
@@ -249,7 +253,10 @@ function makeRequire(currentId){
try{
return moduleExports[id] || defaultRequire(id);
}catch(e){
- if(e.message.substring(0,19) == "Can not find module"){
+ if(e.message.substring(0, 19) == "Can not find module"){
+ throw new Error("Can not find module " + uri);
+ }
+ if(e.message.substring(0, 28) == "require error: couldn't find"){
throw new Error("Can not find module " + uri);
}
throw e;
@@ -292,101 +299,96 @@ function makeRequire(currentId){
var ensure = require.ensure = function(id, callback){
var require = makeRequire(uri);
if(id instanceof Array){
+ var uri = resolveUri(currentId, id[0], getPackage(currentId).mappings),
+ require = makeRequire(uri);
waitingOn++;
+ if(callback){
+ callbacks.unshift(callback);
+ }
try{
var results = id.map(ensure);
}finally{
- waitingOn--;
- }
- if(waitingOn){
- if(callback){
- callbacks.unshift(callback);
- }
- }else{
- if(callback){
- var uri = resolveUri(currentId, id[0], getPackage(currentId).mappings),
- require = makeRequire(uri);
- callback(require);
- }
+ decrementWaiting();
}
return results;
}
var uri = resolveUri(currentId, id, getPackage(currentId).mappings),
require = makeRequire(uri),
i = 0;
if(factories[uri]){
- if(callback){
+ if(typeof callback == "function"){
callback(require);
}
return;
}
- if(callback){
+ if(typeof callback == "function"){
callbacks.unshift(callback);
}
- if(uri.indexOf(':') > 0 && !inFlight[uri]){
- function onError(error){
- if(uri.indexOf(":") === -1){
- id = uri;
- if(id.substring(id.length - 3) == ".js"){
- id = id.substring(0, id.length - 3);
- }
+ if(uri.indexOf(':') < 0 || inFlight[uri]){
+ return;
+ }
+ function onError(error){
+ if(uri.indexOf(":") === -1){
+ id = uri;
+ if(id.substring(id.length - 3) == ".js"){
+ id = id.substring(0, id.length - 3);
}
- try{
- //check to see if it is a system module
- moduleExports[id] || defaultRequire(id);
- }catch(e){
- factories[uri] = function(){
- throw new Error(error.message + " failed to load " + uri);
- };
- }
- decrementWaiting();
}
- function decrementWaiting(){
- waitingOn--;
- if(waitingOn === 0){
- var calling = callbacks;
- callbacks = [];
- inFlight = {};
- calling.forEach(function(callback){
- enqueue(function(){
- callback(require);
- });
+ try{
+ //check to see if it is a system module
+ moduleExports[id] || defaultRequire(id);
+ }catch(e){
+ factories[uri] = function(){
+ throw new Error(error.message + " failed to load " + uri);
+ };
+ }
+ decrementWaiting();
+ }
+ function decrementWaiting(){
+ waitingOn--;
+ if(waitingOn === 0){
+ var calling = callbacks;
+ callbacks = [];
+ inFlight = {};
+ calling.forEach(function(callback){
+ enqueue(function(){
+ callback(require);
});
- }
+ });
}
- waitingOn++;
- inFlight[uri] = true;
- try{
- var source = exports.load(uri, require);
- return when(source, function(source){
- try{
- if(source !== undefined){
- var packageData = getPackage(uri);
- if(packageData && packageData.compiler){
- var deferred = promiseModule.defer();
- require.ensure(packageData.compiler.module, function(){
- try{
- var rewrittenSource = require(packageData.compiler.module)[packageData.compiler["function"] || "compile"](source);
- createFactory(uri, rewrittenSource);
- deferred.resolve();
- }catch(e){
- e.message += " compiling " + uri;
- deferred.reject(e);
- }
- });
- return deferred.promise;
- }
- createFactory(uri, source);
- return exports;
+ }
+ waitingOn++;
+ inFlight[uri] = true;
+ try{
+ var source = exports.load(uri, require);
+ return when(source, function(source){
+ try{
+ if(source !== undefined){
+ var packageData = getPackage(uri);
+ if(packageData && packageData.compiler){
+ var deferred = promiseModule.defer();
+ require.ensure(packageData.compiler.module, function(){
+ try{
+ var rewrittenSource = require(packageData.compiler.module)[packageData.compiler["function"] || "compile"](source);
+ createFactory(uri, rewrittenSource);
+ deferred.resolve();
+ }catch(e){
+ e.message += " compiling " + uri;
+ deferred.reject(e);
+ }
+ });
+ return deferred.promise;
}
- }finally{
- decrementWaiting();
+ createFactory(uri, source);
+ return exports;
}
- }, onError);
- }
- catch(e){
- onError(e);
- }
+ }finally{
+ decrementWaiting();
+ }
+ }, onError);
+ }
+ catch(e){
+ onError(e);
}
};
return require;
@@ -435,7 +437,7 @@ function processPackage(packageUri, packageData, engine){
if(packageData.overlay){
Object.keys(packageData.overlay).forEach(function(condition){
try{
- var matches = (engine == condition) || eval(condition);
+ var matches = (engine == condition) || !(condition in allKnownOverlays) && eval(condition);
}catch(e){}
if(matches){
addMappings(packageData.overlay[condition].mappings);
@@ -711,7 +713,7 @@ function readModuleFile(path, uri){
return source;
}
catch(e){
- if(path.match(/\.js$/)){
+ if(path.match(/\.js$/) && typeof process != "undefined"){
path = path.replace(/\.js$/,".node");
try{
file.read(path);

0 comments on commit b47105f

Please sign in to comment.