Don't attempt to load a directory.
This patch replaces the path.exists check for module loading with a call to fs.statSync (or fs.stat for require.async) which ensures that it's not trying to load a directory.
This commit is contained in:
Родитель
0b925d075d
Коммит
78520ba482
|
@ -83,17 +83,6 @@ var pathFn = process.compile("(function (exports) {" + natives.path + "\n})",
|
|||
var pathModule = createInternalModule('path', pathFn);
|
||||
var path = pathModule.exports;
|
||||
|
||||
function existsSync (path) {
|
||||
try {
|
||||
process.binding('fs').stat(path);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
var modulePaths = [path.join(process.execPath, "..", "..", "lib", "node")];
|
||||
|
||||
if (process.env["HOME"]) {
|
||||
|
@ -104,7 +93,6 @@ if (process.env["NODE_PATH"]) {
|
|||
modulePaths = process.env["NODE_PATH"].split(":").concat(modulePaths);
|
||||
}
|
||||
|
||||
|
||||
/* Sync unless callback given */
|
||||
function findModulePath (id, dirs, callback) {
|
||||
process.assert(dirs.constructor == Array);
|
||||
|
@ -154,32 +142,47 @@ function findModulePath (id, dirs, callback) {
|
|||
locations.push(path.join(dir, id, 'index' + ext));
|
||||
}
|
||||
|
||||
var fs = requireNative('fs');
|
||||
|
||||
function searchLocations () {
|
||||
var location = locations.shift();
|
||||
if (!location) {
|
||||
return findModulePath(id, rest, callback);
|
||||
}
|
||||
|
||||
// if async
|
||||
if (callback) {
|
||||
path.exists(location, function (found) {
|
||||
if (found) {
|
||||
callback(location);
|
||||
} else {
|
||||
return searchLocations();
|
||||
}
|
||||
});
|
||||
|
||||
// if sync
|
||||
if (!location && rest.length > 0) {
|
||||
return findModulePath(id, rest);
|
||||
} else if (location) {
|
||||
try {
|
||||
var stats = fs.statSync(location);
|
||||
if (stats && !stats.isDirectory()) return location;
|
||||
} catch(e) {}
|
||||
return searchLocations();
|
||||
} else {
|
||||
if (existsSync(location)) {
|
||||
return location;
|
||||
} else {
|
||||
return searchLocations();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return searchLocations();
|
||||
|
||||
function searchLocationsAsync (cb) {
|
||||
var location = locations.shift();
|
||||
|
||||
if (!location && rest.length > 0) {
|
||||
findModulePath(id, rest, cb);
|
||||
} else if (location) {
|
||||
fs.stat(location, function (err, stats) {
|
||||
if (stats && !stats.isDirectory()) {
|
||||
cb(location);
|
||||
} else {
|
||||
searchLocationsAsync(cb);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
cb(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
return searchLocationsAsync(callback);
|
||||
} else {
|
||||
return searchLocations();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -106,6 +106,23 @@ process.assert(foo.bar.expect === foo.bar.actual);
|
|||
assert.equal(require('../fixtures/foo').foo, 'ok',
|
||||
'require module with no extension');
|
||||
|
||||
// Should not attempt to load a directory
|
||||
try {
|
||||
require("../fixtures/empty");
|
||||
} catch(err) {
|
||||
assert.equal(err.message, "Cannot find module '../fixtures/empty'");
|
||||
}
|
||||
|
||||
var asyncRequireDir = false;
|
||||
require.async("../fixtures/empty", function (err, a) {
|
||||
assert.ok(err);
|
||||
|
||||
if (err) {
|
||||
asyncRequireDir = true;
|
||||
assert.equal(err.message, "Cannot find module '../fixtures/empty'");
|
||||
}
|
||||
});
|
||||
|
||||
process.addListener("exit", function () {
|
||||
assert.equal(true, a.A instanceof Function);
|
||||
assert.equal("A done", a.A());
|
||||
|
@ -128,5 +145,7 @@ process.addListener("exit", function () {
|
|||
|
||||
assert.equal(true, errorThrownAsync);
|
||||
|
||||
assert.equal(true, asyncRequireDir);
|
||||
|
||||
console.log("exit");
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче