Socket
Socket
Sign inDemoInstall

enhanced-resolve

Package Overview
Dependencies
0
Maintainers
1
Versions
128
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.2 to 0.2.3

lib/createThrottledFunction.js

99

lib/resolve.js

@@ -7,3 +7,12 @@ /*

var fs = require("fs");
var performantStat = require("./performantStat");
var createThrottledFunction = require("./createThrottledFunction");
var statCache = {}, readFileCache = {};
var statAsync = createThrottledFunction(fs.stat, 2000, Object.create(statCache));
var statSync = createThrottledFunction(function(pathname, callback) {
try { callback(null, fs.statSync(pathname)); } catch(e) { callback(e) }
}, 2000, statCache);
var readFileAsync = createThrottledFunction(fs.readFile, 2000, Object.create(readFileCache));
var readFileSync = createThrottledFunction(function(pathname, enc, callback) {
try { callback(null, fs.readFileSync(pathname, enc)); } catch(e) { callback(e) }
}, 2000, readFileCache);

@@ -13,3 +22,3 @@ // http://nodejs.org/docs/v0.4.8/api/all.html#all_Together...

function resolve(context, identifier, options, type, callback) {
function resolve(context, identifier, options, type, sync, callback) {
function finalResult(err, absoluteFilename) {

@@ -35,3 +44,3 @@ if(err) {

if(type === "context") {
performantStat(pathname, function(err, stat) {
(sync?statSync:statAsync)(pathname, function(err, stat) {
if(err) {

@@ -48,13 +57,13 @@ finalResult(err);

} else {
loadAsFileOrDirectory(pathname, options, type, finalResult);
loadAsFileOrDirectory(pathname, options, type, sync, finalResult);
}
} else {
loadNodeModules(contextArray, identArray, options, type, finalResult);
loadNodeModules(contextArray, identArray, options, type, sync, finalResult);
}
}
function doResolve(context, identifier, options, type, callback) {
function doResolve(context, identifier, options, type, sync, callback) {
var identifiers = identifier.replace(/^!|!$/g, "").replace(/!!/g, "!").split(/!/g);
var resource = identifiers.pop();
resolve(context, resource, options, type, function(err, resource) {
resolve(context, resource, options, type, sync, function(err, resource) {
if(err) return callback(err);

@@ -70,3 +79,3 @@ if(identifier.indexOf("!") === -1) {

}
resolveLoaders(context, identifiers, options, function(err, identifiers) {
resolveLoaders(context, identifiers, options, sync, function(err, identifiers) {
if(err) return callback(err);

@@ -88,3 +97,3 @@ identifiers.push(resource);

function resolveLoaders(context, identifiers, options, callback) {
function resolveLoaders(context, identifiers, options, sync, callback) {
var errors = [];

@@ -102,5 +111,5 @@ var count = identifiers.length;

}
if(count == 0) endOne(count++);
if(count == 0) return endOne(count++);
identifiers.forEach(function(ident, index) {
resolve(context, ident, options, "loader", function(err, filename) {
resolve(context, ident, options, "loader", sync, function(err, filename) {
if(err) {

@@ -147,2 +156,15 @@ errors.push(err);

function createSyncCallback() {
var err, result;
function fn(_err, _result) {
err = _err;
result = _result;
}
fn.get = function() {
if(err) throw err;
return result;
}
return fn;
}
/**

@@ -161,4 +183,12 @@ * context: absolute filename of current file

options = setupDefaultOptions(options);
return doResolve(context, identifier, options, "normal", callback);
return doResolve(context, identifier, options, "normal", false, callback);
}
module.exports.sync = function(context, identifier, options) {
if(!options) options = {};
options = setupDefaultOptions(options);
var callback = createSyncCallback();
doResolve(context, identifier, options, "normal", true, callback);
return callback.get();
}
module.exports.setupDefaultOptions = setupDefaultOptions;

@@ -171,4 +201,11 @@ module.exports.context = function(context, identifier, options, callback) {

options = setupDefaultOptions(options);
return doResolve(context, identifier, options, "context", callback);
return doResolve(context, identifier, options, "context", false, callback);
}
module.exports.context.sync = function(context, identifier, options) {
if(!options) options = {};
options = setupDefaultOptions(options);
var callback = createSyncCallback();
doResolve(context, identifier, options, "context", true, callback);
return callback.get();
}

@@ -186,3 +223,3 @@ /**

if(identifiers.length == 1 && identifiers[0] == "") return callback(null, []);
return resolveLoaders(context, identifiers, options, callback);
return resolveLoaders(context, identifiers, options, false, callback);
}

@@ -204,3 +241,3 @@

function loadAsFile(filename, options, type, callback) {
function loadAsFile(filename, options, type, sync, callback) {
var extensions = type === "loader" ? options.loaderExtensions : options.extensions;

@@ -213,3 +250,3 @@ var tries = extensions.map(function(ext) {

tries.forEach(function forEachTryFn(test, idx) {
performantStat(test, function loadAsFileTryCallback(err, stat) {
(sync?statSync:statAsync)(test, function loadAsFileTryCallback(err, stat) {
results[idx] = (err || !stat || !stat.isFile()) ? null : test;

@@ -227,4 +264,4 @@ count--;

function loadAsDirectory(dirname, options, type, callback) {
performantStat(dirname, function(err, stats) {
function loadAsDirectory(dirname, options, type, sync, callback) {
(sync?statSync:statAsync)(dirname, function(err, stats) {
if(err || !stats || !stats.isDirectory()) {

@@ -234,6 +271,6 @@ return callback(new Error(dirname + " is not a directory"));

var packageJsonFile = join(split(dirname), ["package.json"]);
performantStat(packageJsonFile, function(err, stats) {
(sync?statSync:statAsync)(packageJsonFile, function(err, stats) {
var mainModule = "index";
if(!err && stats.isFile()) {
fs.readFile(packageJsonFile, "utf-8", function(err, content) {
(sync?readFileSync:readFileAsync)(packageJsonFile, "utf-8", function(err, content) {
if(err) {

@@ -252,6 +289,6 @@ callback(err);

mainModule = content.main;
loadAsFile(join(split(dirname), [mainModule]), options, type, callback);
loadAsFile(join(split(dirname), [mainModule]), options, type, sync, callback);
});
} else
loadAsFile(join(split(dirname), [mainModule]), options, type, callback);
loadAsFile(join(split(dirname), [mainModule]), options, type, sync, callback);
});

@@ -261,3 +298,3 @@ });

function loadAsFileOrDirectory(pathname, options, type, callback) {
function loadAsFileOrDirectory(pathname, options, type, sync, callback) {
var result = null;

@@ -267,3 +304,3 @@ var counter = 0;

var fastExit = false;
loadAsFile(pathname, options, type, function loadAsFileOrDirectoryFileResultCallback(err, absoluteFilename) {
loadAsFile(pathname, options, type, sync, function loadAsFileOrDirectoryFileResultCallback(err, absoluteFilename) {
if(err)

@@ -277,3 +314,3 @@ error = err;

});
loadAsDirectory(pathname, options, type, function loadAsFileOrDirectoryDirectoryResultCallback(err, absoluteFilename) {
loadAsDirectory(pathname, options, type, sync, function loadAsFileOrDirectoryDirectoryResultCallback(err, absoluteFilename) {
if(err) {

@@ -295,6 +332,6 @@ if(!error) error = err;

function loadNodeModules(context, identifier, options, type, callback) {
function loadNodeModules(context, identifier, options, type, sync, callback) {
var moduleName = identifier.shift();
var postfixes = type === "loader" ? options.loaderPostfixes : options.postfixes;
nodeModulesPaths(context, options, function(err, paths) {
nodeModulesPaths(context, options, sync, function(err, paths) {
var dirs = [];

@@ -312,3 +349,3 @@ paths.forEach(function(path) {

if(type === "context") {
performantStat(pathname, function(err, stat) {
(sync?statSync:statAsync)(pathname, function(err, stat) {
results[idx] = (err || !stat.isDirectory()) ? null : pathname;

@@ -318,3 +355,3 @@ endOne();

} else {
loadAsFileOrDirectory(pathname, options, type, function loadAsFileOrDirectoryCallback(err, absoluteFilename) {
loadAsFileOrDirectory(pathname, options, type, sync, function loadAsFileOrDirectoryCallback(err, absoluteFilename) {
results[idx] = err ? null : absoluteFilename;

@@ -346,3 +383,3 @@ endOne();

function nodeModulesPaths(context, options, callback) {
function nodeModulesPaths(context, options, sync, callback) {
var parts = context;

@@ -369,3 +406,3 @@ var root = 0;

dirs.forEach(function(dir, idx) {
performantStat(dir, function(err, stat) {
(sync?statSync:statAsync)(dir, function(err, stat) {
if(err || !stat || !stat.isDirectory())

@@ -372,0 +409,0 @@ dirs[idx] = null;

{
"name": "enhanced-resolve",
"version": "0.2.2",
"version": "0.2.3",
"author": "Tobias Koppers @sokra",

@@ -5,0 +5,0 @@ "description": "Offers a async require.resolve function. It's highly configurable.",

@@ -9,2 +9,11 @@ # enhanced-resolve

``` javascript
var resolve = require("enhanced-resolve");
resolve(string context, string identifier, object options, function callback(err, result))
resolve.sync(string context, string identifier, object options)
resolve.context(string context, string identifier, object options, function callback(err, result))
resolve.context.sync(string context, string identifier, object options)
```
*It is used in [webpack](/webpack/webpack)*

@@ -22,4 +22,10 @@ var resolve = require("../lib/resolve");

});
it("should resolve itself sync " + pathToIt[2], function() {
var filename = resolve.sync(pathToIt[0], pathToIt[1]);
should.exist(filename);
filename.should.be.a("string");
filename.should.be.eql(path.join(__dirname, "..", "lib", "resolve.js"));
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc