Reworks the two functions traverser and findModulePath into a more readable form.
This is not the supposed end of these changes, but a first change that should make future changes easier to reason about.
This commit is contained in:
parent
8c6a7b5de4
commit
e9c7195471
76
src/node.js
76
src/node.js
@ -146,46 +146,56 @@
|
|||||||
removed('require.registerExtension() removed.' +
|
removed('require.registerExtension() removed.' +
|
||||||
' Use require.extensions instead');
|
' Use require.extensions instead');
|
||||||
|
|
||||||
// Which files to traverse while finding id? Returns generator function.
|
// given a module name, and a list of paths to test, returns the first
|
||||||
function traverser(id, dirs) {
|
// matching file in the following precedence.
|
||||||
var head = [], inDir = [], dirs = dirs.slice(),
|
//
|
||||||
exts = Object.keys(extensions);
|
// require("a.<ext>")
|
||||||
return function next() {
|
// -> a.<ext>
|
||||||
var result = head.shift();
|
//
|
||||||
if (result) { return result; }
|
// require("a")
|
||||||
|
// -> a
|
||||||
var gen = inDir.shift();
|
// -> a.<ext>
|
||||||
if (gen) { head = gen(); return next(); }
|
// -> a/index.<ext>
|
||||||
|
|
||||||
var dir = dirs.shift();
|
|
||||||
if (dir !== undefined) {
|
|
||||||
function direct(ext) { return path.join(dir, id + ext); }
|
|
||||||
function index(ext) { return path.join(dir, id, 'index' + ext); }
|
|
||||||
inDir = [
|
|
||||||
function() { return exts.map(direct); },
|
|
||||||
function() { return exts.map(index); }
|
|
||||||
];
|
|
||||||
head = [path.join(dir, id)];
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function findModulePath(request, paths) {
|
function findModulePath(request, paths) {
|
||||||
var nextLoc =
|
var fs = requireNative('fs'),
|
||||||
traverser(request, request.charAt(0) === '/' ? [''] : paths);
|
exts = Object.keys(extensions);
|
||||||
|
|
||||||
var fs = requireNative('fs');
|
paths = request.charAt(0) === '/' ? [''] : paths;
|
||||||
|
|
||||||
var location, stats;
|
// check if the file exists and is not a directory
|
||||||
while (location = nextLoc()) {
|
var tryFile = function(requestPath) {
|
||||||
try { stats = fs.statSync(location); } catch (e) { continue; }
|
try {
|
||||||
if (stats && !stats.isDirectory()) return location;
|
stats = fs.statSync(requestPath);
|
||||||
|
if (stats && !stats.isDirectory()) {
|
||||||
|
return requestPath;
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
// given a path check a the file exists with any of the set extensions
|
||||||
|
var tryExtensions = function(p, extension) {
|
||||||
|
for (var i = 0, EL = exts.length; i < EL; i++) {
|
||||||
|
f = tryFile(p + exts[i]);
|
||||||
|
if (f) { return f; }
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
// For each path
|
||||||
|
for (var i = 0, PL = paths.length; i < PL; i++) {
|
||||||
|
var p = paths[i],
|
||||||
|
// try to join the request to the path
|
||||||
|
f = tryFile(path.join(p, request)) ||
|
||||||
|
// try it with each of the extensions
|
||||||
|
tryExtensions(path.join(p, request)) ||
|
||||||
|
// try it with each of the extensions at "index"
|
||||||
|
tryExtensions(path.join(p, request, 'index'));
|
||||||
|
if (f) { return f; }
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// sync - no i/o performed
|
// sync - no i/o performed
|
||||||
function resolveModuleLookupPaths(request, parent) {
|
function resolveModuleLookupPaths(request, parent) {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user