module: define functions only once.
This commit is contained in:
parent
8ee9c53c77
commit
2f1f22ab26
@ -51,6 +51,38 @@ var debug = Module._debug;
|
|||||||
// -> a
|
// -> a
|
||||||
// -> a.<ext>
|
// -> a.<ext>
|
||||||
// -> a/index.<ext>
|
// -> a/index.<ext>
|
||||||
|
|
||||||
|
function statPath(path) {
|
||||||
|
var fs = NativeModule.require('fs');
|
||||||
|
try {
|
||||||
|
return fs.statSync(path);
|
||||||
|
} catch (ex) {}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if the file exists and is not a directory
|
||||||
|
function tryFile(requestPath) {
|
||||||
|
var fs = NativeModule.require('fs');
|
||||||
|
var stats = statPath(requestPath);
|
||||||
|
if (stats && !stats.isDirectory()) {
|
||||||
|
return fs.realpathSync(requestPath);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// given a path check a the file exists with any of the set extensions
|
||||||
|
function tryExtensions(p, exts) {
|
||||||
|
for (var i = 0, EL = exts.length; i < EL; i++) {
|
||||||
|
var filename = tryFile(p + exts[i]);
|
||||||
|
|
||||||
|
if (filename) {
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Module._findPath = function(request, paths) {
|
Module._findPath = function(request, paths) {
|
||||||
var fs = NativeModule.require('fs');
|
var fs = NativeModule.require('fs');
|
||||||
var exts = Object.keys(Module._extensions);
|
var exts = Object.keys(Module._extensions);
|
||||||
@ -61,29 +93,6 @@ Module._findPath = function(request, paths) {
|
|||||||
|
|
||||||
var trailingSlash = (request.slice(-1) === '/');
|
var trailingSlash = (request.slice(-1) === '/');
|
||||||
|
|
||||||
// check if the file exists and is not a directory
|
|
||||||
function tryFile(requestPath) {
|
|
||||||
try {
|
|
||||||
var stats = fs.statSync(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) {
|
|
||||||
for (var i = 0, EL = exts.length; i < EL; i++) {
|
|
||||||
var filename = tryFile(p + exts[i]);
|
|
||||||
|
|
||||||
if (filename) {
|
|
||||||
return filename;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
var cacheKey = JSON.stringify({request: request, paths: paths});
|
var cacheKey = JSON.stringify({request: request, paths: paths});
|
||||||
if (Module._pathCache[cacheKey]) {
|
if (Module._pathCache[cacheKey]) {
|
||||||
return Module._pathCache[cacheKey];
|
return Module._pathCache[cacheKey];
|
||||||
@ -100,13 +109,13 @@ Module._findPath = function(request, paths) {
|
|||||||
|
|
||||||
if (!filename && !trailingSlash) {
|
if (!filename && !trailingSlash) {
|
||||||
// try it with each of the extensions
|
// try it with each of the extensions
|
||||||
filename = tryExtensions(basePath);
|
filename = tryExtensions(basePath, exts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!filename) {
|
if (!filename) {
|
||||||
// try it with each of the extensions at "index"
|
// try it with each of the extensions at "index"
|
||||||
filename = tryExtensions(path.resolve(basePath, 'index'));
|
filename = tryExtensions(path.resolve(basePath, 'index'), exts);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filename) {
|
if (filename) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user