Browse Source

module: define functions only once.

v0.7.4-release
isaacs 14 years ago
committed by Ryan Dahl
parent
commit
2f1f22ab26
  1. 39
      lib/module.js

39
lib/module.js

@ -51,29 +51,27 @@ var debug = Module._debug;
// -> a
// -> a.<ext>
// -> a/index.<ext>
Module._findPath = function(request, paths) {
var fs = NativeModule.require('fs');
var exts = Object.keys(Module._extensions);
if (request.charAt(0) === '/') {
paths = [''];
function statPath(path) {
var fs = NativeModule.require('fs');
try {
return fs.statSync(path);
} catch (ex) {}
return false;
}
var trailingSlash = (request.slice(-1) === '/');
// check if the file exists and is not a directory
function tryFile(requestPath) {
try {
var stats = fs.statSync(requestPath);
var fs = NativeModule.require('fs');
var stats = statPath(requestPath);
if (stats && !stats.isDirectory()) {
return fs.realpathSync(requestPath);
}
} catch (e) {}
return false;
};
}
// given a path check a the file exists with any of the set extensions
function tryExtensions(p, extension) {
function tryExtensions(p, exts) {
for (var i = 0, EL = exts.length; i < EL; i++) {
var filename = tryFile(p + exts[i]);
@ -82,7 +80,18 @@ Module._findPath = function(request, paths) {
}
}
return false;
};
}
Module._findPath = function(request, paths) {
var fs = NativeModule.require('fs');
var exts = Object.keys(Module._extensions);
if (request.charAt(0) === '/') {
paths = [''];
}
var trailingSlash = (request.slice(-1) === '/');
var cacheKey = JSON.stringify({request: request, paths: paths});
if (Module._pathCache[cacheKey]) {
@ -100,13 +109,13 @@ Module._findPath = function(request, paths) {
if (!filename && !trailingSlash) {
// try it with each of the extensions
filename = tryExtensions(basePath);
filename = tryExtensions(basePath, exts);
}
}
if (!filename) {
// try it with each of the extensions at "index"
filename = tryExtensions(path.resolve(basePath, 'index'));
filename = tryExtensions(path.resolve(basePath, 'index'), exts);
}
if (filename) {

Loading…
Cancel
Save