<commit_before>var core = require('./core');
var fs = require('fs');
var path = require('path');

module.exports = function resolve (x, opts, cb) {
    if (core[x]) return cb(null, x);
    
    if (typeof opts === 'function') {
        cb = opts;
        opts = {};
    }
    if (!opts) opts = {};
    
    var isFile = opts.isFile || function (file, cb) {
        fs.stat(file, function (err, stat) {
            if (err && err.code === 'ENOENT') cb(null, false)
            else if (err) cb(err)
            else cb(null, stat.isFile())
        });
    };
    var readFile = opts.readFile || fs.readFile;
    
    var extensions = opts.extensions || [ '.js' ];
    var y = opts.basedir
        || path.dirname(require.cache[__filename].parent.filename)
    ;
    
    opts.paths = opts.paths || [];
    
    if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) {
        loadAsFile(path.resolve(y, x), function (err, m) {
            if (err) cb(err)
            else if (m) cb(null, m)
            else loadAsDirectory(path.resolve(y, x), function (err, d) {
                if (err) cb(err)
                else if (d) cb(null, d)
                else cb(new Error("Cannot find module '" + x + "'"))
            })
        });
    }
    else loadNodeModules(x, y, function (err, n) {
        if (err) cb(err)
        else if (n) cb(null, n)
        else cb(new Error("Cannot find module '" + x + "'"))
    });
    
    function loadAsFile (x, cb) {
        (function load (exts) {
            if (exts.length === 0) return cb(null, undefined);
            var file = x + exts[0];
            
            isFile(file, function (err, ex) {
                if (err) cb(err)
                else if (ex) cb(null, file)
                else load(exts.slice(1))
            });
        })([''].concat(extensions));
    }
    
    function loadAsDirectory (x, cb) {
        var pkgfile = path.join(x, '/package.json');
        isFile(pkgfile, function (err, ex) {
            if (err) return cb(err);
            if (!ex) return loadAsFile(path.join(x, '/index'), cb);
            
            readFile(pkgfile, 'utf8', function (err, body) {
                if (err) return cb(err);
                try {
                    var pkg = JSON.parse(body);
                }
                catch (err) {}
                
                if (opts.packageFilter) {
                    pkg = opts.packageFilter(pkg, x);
                }
                
                if (pkg.main) {
                    loadAsFile(path.resolve(x, pkg.main), function (err, m) {
                        if (err) return cb(err);
                        if (m) return cb(null, m);
                        var dir = path.resolve(x, pkg.main);
                        loadAsDirectory(dir, function (err, n) {
                            if (err) return cb(err);
                            if (n) return cb(null, n);
                            loadAsFile(path.join(x, '/index'), cb);
                        });
                    });
                    return;
                }
                
                loadAsFile(path.join(x, '/index'), cb);
            });<commit_msg>fix for async parameterized readFile<commit_after>var core = require('./core');
var fs = require('fs');
var path = require('path');

module.exports = function resolve (x, opts, cb) {
    if (core[x]) return cb(null, x);
    
    if (typeof opts === 'function') {
        cb = opts;
        opts = {};
    }
    if (!opts) opts = {};
    
    var isFile = opts.isFile || function (file, cb) {
        fs.stat(file, function (err, stat) {
            if (err && err.code === 'ENOENT') cb(null, false)
            else if (err) cb(err)
            else cb(null, stat.isFile())
        });
    };
    var readFile = opts.readFile || fs.readFile;
    
    var extensions = opts.extensions || [ '.js' ];
    var y = opts.basedir
        || path.dirname(require.cache[__filename].parent.filename)
    ;
    
    opts.paths = opts.paths || [];
    
    if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) {
        loadAsFile(path.resolve(y, x), function (err, m) {
            if (err) cb(err)
            else if (m) cb(null, m)
            else loadAsDirectory(path.resolve(y, x), function (err, d) {
                if (err) cb(err)
                else if (d) cb(null, d)
                else cb(new Error("Cannot find module '" + x + "'"))
            })
        });
    }
    else loadNodeModules(x, y, function (err, n) {
        if (err) cb(err)
        else if (n) cb(null, n)
        else cb(new Error("Cannot find module '" + x + "'"))
    });
    
    function loadAsFile (x, cb) {
        (function load (exts) {
            if (exts.length === 0) return cb(null, undefined);
            var file = x + exts[0];
            
            isFile(file, function (err, ex) {
                if (err) cb(err)
                else if (ex) cb(null, file)
                else load(exts.slice(1))
            });
        })([''].concat(extensions));
    }
    
    function loadAsDirectory (x, cb) {
        var pkgfile = path.join(x, '/package.json');
        isFile(pkgfile, function (err, ex) {
            if (err) return cb(err);
            if (!ex) return loadAsFile(path.join(x, '/index'), cb);
            
            readFile(pkgfile, function (err, body) {
                if (err) return cb(err);
                try {
                    var pkg = JSON.parse(body);
                }
                catch (err) {}
                
                if (opts.packageFilter) {
                    pkg = opts.packageFilter(pkg, x);
                }
                
                if (pkg.main) {
                    loadAsFile(path.resolve(x, pkg.main), function (err, m) {
                        if (err) return cb(err);
                        if (m) return cb(null, m);
                        var dir = path.resolve(x, pkg.main);
                        loadAsDirectory(dir, function (err, n) {
                            if (err) return cb(err);
                            if (n) return cb(null, n);
                            loadAsFile(path.join(x, '/index'), cb);
                        });
                    });
                    return;
                }
                
                loadAsFile(path.join(x, '/index'), cb);
            });
{'fraction_non_alphanumeric': 0.1507672836576491, 'fraction_numerical': 0.0011074197120708748, 'mean_word_length': 1.275737940964723, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1243937', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1834, 'n_words': 475}