Permalink
Browse files

Gracefully handle missing modules, removing them from dep lists

  • Loading branch information...
1 parent 927cca2 commit 1e3dbf51775be2d0e9fb5cbddc63c298cca05f07 @kriszyp committed May 22, 2010
Showing with 28 additions and 12 deletions.
  1. +28 −12 lib/jsgi/transporter.js
View
@@ -3,6 +3,7 @@ exports.Transporter = function(options, app) {
var options = options || {},
prefix = options.urlPrefix || "/lib/",
resolveDeps = "resolveDeps" in options ? options.resolveDeps : true,
+ excludeModules = options.excludeModules || ["require", "yabble", "transportD"],
paths = options.paths || require.paths.map(function(path){
return path.replace(/[\\\/]engines[\\\/](\w*)/,function(t, engine){
return "/engines/" + (engine === "default" ? "default" : "browser");
@@ -18,8 +19,12 @@ exports.Transporter = function(options, app) {
}
},
autoRun = "autoRun" in options ? options.autoRun : true,
+ excluded = {};
converter = options.converter || exports.CommonJS;
+ excludeModules.forEach(function(moduleId){
+ excluded[moduleId] = true;
+ });
return function(request) {
var modules, isPaused, path = request.pathInfo;
if(path.indexOf(prefix) != 0){
@@ -34,7 +39,8 @@ exports.Transporter = function(options, app) {
body: ["Not found"]
};
}
- var requestedModules = path.substring(prefix.length, path.length)
+ var errors = [],
+ requestedModules = path.substring(prefix.length, path.length)
.split(',').map(function(module){
if(module.substring(module.length - 3, module.length) == ".js"){
return module.substring(0, module.length - 3); // strip the .js
@@ -67,6 +73,7 @@ exports.Transporter = function(options, app) {
if(converter.end){
converter.end(write);
}
+ errors.forEach(write);
if(isPaused){
write("require.resume();\n");
}
@@ -97,18 +104,18 @@ exports.Transporter = function(options, app) {
write("require.pause();\n");
isPaused = true;
}
- return;
+ return true;
}
modules[moduleName] = 1; // in progress
try{
var fileContents = loader(moduleName + ".js");
}
catch(e){
- write('console.error(' + JSON.stringify(moduleName) + ' + " could not be loaded: " + ' + JSON.stringify(e.message) + ');\n');
+ errors.push('console.error(' + JSON.stringify(moduleName) + ' + " could not be loaded: " + ' + JSON.stringify(e.message) + ');\n');
return;
}
if(!fileContents && write){
- write('console.error(' + JSON.stringify(moduleName) + ' + " not found");\n');
+ errors.push('console.error(' + JSON.stringify(moduleName) + ' + " not found");\n');
return;
}
if(moduleName === "require"){
@@ -120,16 +127,17 @@ exports.Transporter = function(options, app) {
// we set the base url in the require config so that it knows where to load from
write('require={baseUrlMatch:RegExp("' + requestedModules[0] + '")};');
write(fileContents);
- return;
+ return true;
}
- if(fileContents.indexOf("require.def(") > -1){
+ if(excluded[moduleName] || fileContents.indexOf("require.def(") > -1){
// also hand-coded modules that define their require.def's don't need wrapping/resolution,
// they can be directly written
write(fileContents);
- return;
+ return true;
}
- converter(moduleName, fileContents, resolveDeps ? loadModule : function(){}, modules, write);
+ converter(moduleName, fileContents, resolveDeps ? loadModule : function(){ return true;}, modules, write);
modules[moduleName] = 2; // finished
+ return true;
}
}
}
@@ -167,7 +175,9 @@ exports.CommonJS = function(moduleName, fileContents, loadModule, modules, write
}
depNames[moduleId] = true;
deps.push(moduleId);
- loadModule(moduleId, write);
+ if(!loadModule(moduleId, write)){
+ deps.splice(deps.indexOf(moduleId), 1);
+ }
});
if(write){
if(first){
@@ -228,7 +238,9 @@ exports.CommonJSTransportC = function(moduleName, fileContents, loadModule, modu
}
depNames[moduleId] = true;
deps.push(moduleId);
- loadModule(moduleId, write);
+ if(!loadModule(moduleId, write)){
+ deps.splice(deps.indexOf(moduleId), 1);
+ }
});
if(write){
write('require.define("');
@@ -253,7 +265,9 @@ exports.Dojo = function(moduleName, fileContents, loadModule, modules, write){
return;
}
depNames[moduleId] = true;
- deps.push(moduleId);
+ if(!loadModule(moduleId, write)){
+ deps.splice(deps.indexOf(moduleId), 1);
+ }
loadModule(moduleId, write);
});
if(write){
@@ -273,7 +287,9 @@ exports.DojoRequireJS = function(moduleName, fileContents, loadModule, modules,
}
depNames[moduleId] = true;
deps.push(moduleId);
- loadModule(moduleId, write);
+ if(!loadModule(moduleId, write)){
+ deps.splice(deps.indexOf(moduleId), 1);
+ }
});
if(write){
write('require.def("');

0 comments on commit 1e3dbf5

Please sign in to comment.