Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/resolve/lib')
-rw-r--r--node_modules/resolve/lib/async.js298
-rw-r--r--node_modules/resolve/lib/caller.js8
-rw-r--r--node_modules/resolve/lib/core.js53
-rw-r--r--node_modules/resolve/lib/core.json75
-rw-r--r--node_modules/resolve/lib/is-core.js5
-rw-r--r--node_modules/resolve/lib/node-modules-paths.js42
-rw-r--r--node_modules/resolve/lib/normalize-options.js10
-rw-r--r--node_modules/resolve/lib/sync.js191
8 files changed, 682 insertions, 0 deletions
diff --git a/node_modules/resolve/lib/async.js b/node_modules/resolve/lib/async.js
new file mode 100644
index 000000000..06aa45883
--- /dev/null
+++ b/node_modules/resolve/lib/async.js
@@ -0,0 +1,298 @@
+var fs = require('fs');
+var path = require('path');
+var caller = require('./caller.js');
+var nodeModulesPaths = require('./node-modules-paths.js');
+var normalizeOptions = require('./normalize-options.js');
+var isCore = require('./is-core');
+
+var realpathFS = fs.realpath && typeof fs.realpath.native === 'function' ? fs.realpath.native : fs.realpath;
+
+var defaultIsFile = function isFile(file, cb) {
+ fs.stat(file, function (err, stat) {
+ if (!err) {
+ return cb(null, stat.isFile() || stat.isFIFO());
+ }
+ if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
+ return cb(err);
+ });
+};
+
+var defaultIsDir = function isDirectory(dir, cb) {
+ fs.stat(dir, function (err, stat) {
+ if (!err) {
+ return cb(null, stat.isDirectory());
+ }
+ if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
+ return cb(err);
+ });
+};
+
+var defaultRealpath = function realpath(x, cb) {
+ realpathFS(x, function (realpathErr, realPath) {
+ if (realpathErr && realpathErr.code !== 'ENOENT') cb(realpathErr);
+ else cb(null, realpathErr ? x : realPath);
+ });
+};
+
+var maybeRealpath = function maybeRealpath(realpath, x, opts, cb) {
+ if (opts && opts.preserveSymlinks === false) {
+ realpath(x, cb);
+ } else {
+ cb(null, x);
+ }
+};
+
+var getPackageCandidates = function getPackageCandidates(x, start, opts) {
+ var dirs = nodeModulesPaths(start, opts, x);
+ for (var i = 0; i < dirs.length; i++) {
+ dirs[i] = path.join(dirs[i], x);
+ }
+ return dirs;
+};
+
+module.exports = function resolve(x, options, callback) {
+ var cb = callback;
+ var opts = options;
+ if (typeof options === 'function') {
+ cb = opts;
+ opts = {};
+ }
+ if (typeof x !== 'string') {
+ var err = new TypeError('Path must be a string.');
+ return process.nextTick(function () {
+ cb(err);
+ });
+ }
+
+ opts = normalizeOptions(x, opts);
+
+ var isFile = opts.isFile || defaultIsFile;
+ var isDirectory = opts.isDirectory || defaultIsDir;
+ var readFile = opts.readFile || fs.readFile;
+ var realpath = opts.realpath || defaultRealpath;
+ var packageIterator = opts.packageIterator;
+
+ var extensions = opts.extensions || ['.js'];
+ var basedir = opts.basedir || path.dirname(caller());
+ var parent = opts.filename || basedir;
+
+ opts.paths = opts.paths || [];
+
+ // ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
+ var absoluteStart = path.resolve(basedir);
+
+ maybeRealpath(
+ realpath,
+ absoluteStart,
+ opts,
+ function (err, realStart) {
+ if (err) cb(err);
+ else init(realStart);
+ }
+ );
+
+ var res;
+ function init(basedir) {
+ if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
+ res = path.resolve(basedir, x);
+ if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
+ if ((/\/$/).test(x) && res === basedir) {
+ loadAsDirectory(res, opts.package, onfile);
+ } else loadAsFile(res, opts.package, onfile);
+ } else if (isCore(x)) {
+ return cb(null, x);
+ } else loadNodeModules(x, basedir, function (err, n, pkg) {
+ if (err) cb(err);
+ else if (n) {
+ return maybeRealpath(realpath, n, opts, function (err, realN) {
+ if (err) {
+ cb(err);
+ } else {
+ cb(null, realN, pkg);
+ }
+ });
+ } else {
+ var moduleError = new Error("Cannot find module '" + x + "' from '" + parent + "'");
+ moduleError.code = 'MODULE_NOT_FOUND';
+ cb(moduleError);
+ }
+ });
+ }
+
+ function onfile(err, m, pkg) {
+ if (err) cb(err);
+ else if (m) cb(null, m, pkg);
+ else loadAsDirectory(res, function (err, d, pkg) {
+ if (err) cb(err);
+ else if (d) {
+ maybeRealpath(realpath, d, opts, function (err, realD) {
+ if (err) {
+ cb(err);
+ } else {
+ cb(null, realD, pkg);
+ }
+ });
+ } else {
+ var moduleError = new Error("Cannot find module '" + x + "' from '" + parent + "'");
+ moduleError.code = 'MODULE_NOT_FOUND';
+ cb(moduleError);
+ }
+ });
+ }
+
+ function loadAsFile(x, thePackage, callback) {
+ var loadAsFilePackage = thePackage;
+ var cb = callback;
+ if (typeof loadAsFilePackage === 'function') {
+ cb = loadAsFilePackage;
+ loadAsFilePackage = undefined;
+ }
+
+ var exts = [''].concat(extensions);
+ load(exts, x, loadAsFilePackage);
+
+ function load(exts, x, loadPackage) {
+ if (exts.length === 0) return cb(null, undefined, loadPackage);
+ var file = x + exts[0];
+
+ var pkg = loadPackage;
+ if (pkg) onpkg(null, pkg);
+ else loadpkg(path.dirname(file), onpkg);
+
+ function onpkg(err, pkg_, dir) {
+ pkg = pkg_;
+ if (err) return cb(err);
+ if (dir && pkg && opts.pathFilter) {
+ var rfile = path.relative(dir, file);
+ var rel = rfile.slice(0, rfile.length - exts[0].length);
+ var r = opts.pathFilter(pkg, x, rel);
+ if (r) return load(
+ [''].concat(extensions.slice()),
+ path.resolve(dir, r),
+ pkg
+ );
+ }
+ isFile(file, onex);
+ }
+ function onex(err, ex) {
+ if (err) return cb(err);
+ if (ex) return cb(null, file, pkg);
+ load(exts.slice(1), x, pkg);
+ }
+ }
+ }
+
+ function loadpkg(dir, cb) {
+ if (dir === '' || dir === '/') return cb(null);
+ if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) {
+ return cb(null);
+ }
+ if ((/[/\\]node_modules[/\\]*$/).test(dir)) return cb(null);
+
+ maybeRealpath(realpath, dir, opts, function (unwrapErr, pkgdir) {
+ if (unwrapErr) return loadpkg(path.dirname(dir), cb);
+ var pkgfile = path.join(pkgdir, 'package.json');
+ isFile(pkgfile, function (err, ex) {
+ // on err, ex is false
+ if (!ex) return loadpkg(path.dirname(dir), cb);
+
+ readFile(pkgfile, function (err, body) {
+ if (err) cb(err);
+ try { var pkg = JSON.parse(body); } catch (jsonErr) {}
+
+ if (pkg && opts.packageFilter) {
+ pkg = opts.packageFilter(pkg, pkgfile);
+ }
+ cb(null, pkg, dir);
+ });
+ });
+ });
+ }
+
+ function loadAsDirectory(x, loadAsDirectoryPackage, callback) {
+ var cb = callback;
+ var fpkg = loadAsDirectoryPackage;
+ if (typeof fpkg === 'function') {
+ cb = fpkg;
+ fpkg = opts.package;
+ }
+
+ maybeRealpath(realpath, x, opts, function (unwrapErr, pkgdir) {
+ if (unwrapErr) return cb(unwrapErr);
+ var pkgfile = path.join(pkgdir, 'package.json');
+ isFile(pkgfile, function (err, ex) {
+ if (err) return cb(err);
+ if (!ex) return loadAsFile(path.join(x, 'index'), fpkg, cb);
+
+ readFile(pkgfile, function (err, body) {
+ if (err) return cb(err);
+ try {
+ var pkg = JSON.parse(body);
+ } catch (jsonErr) {}
+
+ if (pkg && opts.packageFilter) {
+ pkg = opts.packageFilter(pkg, pkgfile);
+ }
+
+ if (pkg && pkg.main) {
+ if (typeof pkg.main !== 'string') {
+ var mainError = new TypeError('package “' + pkg.name + '” `main` must be a string');
+ mainError.code = 'INVALID_PACKAGE_MAIN';
+ return cb(mainError);
+ }
+ if (pkg.main === '.' || pkg.main === './') {
+ pkg.main = 'index';
+ }
+ loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) {
+ if (err) return cb(err);
+ if (m) return cb(null, m, pkg);
+ if (!pkg) return loadAsFile(path.join(x, 'index'), pkg, cb);
+
+ var dir = path.resolve(x, pkg.main);
+ loadAsDirectory(dir, pkg, function (err, n, pkg) {
+ if (err) return cb(err);
+ if (n) return cb(null, n, pkg);
+ loadAsFile(path.join(x, 'index'), pkg, cb);
+ });
+ });
+ return;
+ }
+
+ loadAsFile(path.join(x, '/index'), pkg, cb);
+ });
+ });
+ });
+ }
+
+ function processDirs(cb, dirs) {
+ if (dirs.length === 0) return cb(null, undefined);
+ var dir = dirs[0];
+
+ isDirectory(path.dirname(dir), isdir);
+
+ function isdir(err, isdir) {
+ if (err) return cb(err);
+ if (!isdir) return processDirs(cb, dirs.slice(1));
+ loadAsFile(dir, opts.package, onfile);
+ }
+
+ function onfile(err, m, pkg) {
+ if (err) return cb(err);
+ if (m) return cb(null, m, pkg);
+ loadAsDirectory(dir, opts.package, ondir);
+ }
+
+ function ondir(err, n, pkg) {
+ if (err) return cb(err);
+ if (n) return cb(null, n, pkg);
+ processDirs(cb, dirs.slice(1));
+ }
+ }
+ function loadNodeModules(x, start, cb) {
+ var thunk = function () { return getPackageCandidates(x, start, opts); };
+ processDirs(
+ cb,
+ packageIterator ? packageIterator(x, start, thunk, opts) : thunk()
+ );
+ }
+};
diff --git a/node_modules/resolve/lib/caller.js b/node_modules/resolve/lib/caller.js
new file mode 100644
index 000000000..b14a2804a
--- /dev/null
+++ b/node_modules/resolve/lib/caller.js
@@ -0,0 +1,8 @@
+module.exports = function () {
+ // see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
+ var origPrepareStackTrace = Error.prepareStackTrace;
+ Error.prepareStackTrace = function (_, stack) { return stack; };
+ var stack = (new Error()).stack;
+ Error.prepareStackTrace = origPrepareStackTrace;
+ return stack[2].getFileName();
+};
diff --git a/node_modules/resolve/lib/core.js b/node_modules/resolve/lib/core.js
new file mode 100644
index 000000000..0877650cc
--- /dev/null
+++ b/node_modules/resolve/lib/core.js
@@ -0,0 +1,53 @@
+var current = (process.versions && process.versions.node && process.versions.node.split('.')) || [];
+
+function specifierIncluded(specifier) {
+ var parts = specifier.split(' ');
+ var op = parts.length > 1 ? parts[0] : '=';
+ var versionParts = (parts.length > 1 ? parts[1] : parts[0]).split('.');
+
+ for (var i = 0; i < 3; ++i) {
+ var cur = Number(current[i] || 0);
+ var ver = Number(versionParts[i] || 0);
+ if (cur === ver) {
+ continue; // eslint-disable-line no-restricted-syntax, no-continue
+ }
+ if (op === '<') {
+ return cur < ver;
+ } else if (op === '>=') {
+ return cur >= ver;
+ } else {
+ return false;
+ }
+ }
+ return op === '>=';
+}
+
+function matchesRange(range) {
+ var specifiers = range.split(/ ?&& ?/);
+ if (specifiers.length === 0) { return false; }
+ for (var i = 0; i < specifiers.length; ++i) {
+ if (!specifierIncluded(specifiers[i])) { return false; }
+ }
+ return true;
+}
+
+function versionIncluded(specifierValue) {
+ if (typeof specifierValue === 'boolean') { return specifierValue; }
+ if (specifierValue && typeof specifierValue === 'object') {
+ for (var i = 0; i < specifierValue.length; ++i) {
+ if (matchesRange(specifierValue[i])) { return true; }
+ }
+ return false;
+ }
+ return matchesRange(specifierValue);
+}
+
+var data = require('./core.json');
+
+var core = {};
+for (var mod in data) { // eslint-disable-line no-restricted-syntax
+ if (Object.prototype.hasOwnProperty.call(data, mod)) {
+ core[mod] = versionIncluded(data[mod]);
+ }
+}
+module.exports = core;
diff --git a/node_modules/resolve/lib/core.json b/node_modules/resolve/lib/core.json
new file mode 100644
index 000000000..d51b70b14
--- /dev/null
+++ b/node_modules/resolve/lib/core.json
@@ -0,0 +1,75 @@
+{
+ "assert": true,
+ "async_hooks": ">= 8",
+ "buffer_ieee754": "< 0.9.7",
+ "buffer": true,
+ "child_process": true,
+ "cluster": true,
+ "console": true,
+ "constants": true,
+ "crypto": true,
+ "_debug_agent": ">= 1 && < 8",
+ "_debugger": "< 8",
+ "dgram": true,
+ "dns": true,
+ "domain": true,
+ "events": true,
+ "freelist": "< 6",
+ "fs": true,
+ "fs/promises": [">= 10 && < 10.1", ">= 14"],
+ "_http_agent": ">= 0.11.1",
+ "_http_client": ">= 0.11.1",
+ "_http_common": ">= 0.11.1",
+ "_http_incoming": ">= 0.11.1",
+ "_http_outgoing": ">= 0.11.1",
+ "_http_server": ">= 0.11.1",
+ "http": true,
+ "http2": ">= 8.8",
+ "https": true,
+ "inspector": ">= 8.0.0",
+ "_linklist": "< 8",
+ "module": true,
+ "net": true,
+ "node-inspect/lib/_inspect": ">= 7.6.0 && < 12",
+ "node-inspect/lib/internal/inspect_client": ">= 7.6.0 && < 12",
+ "node-inspect/lib/internal/inspect_repl": ">= 7.6.0 && < 12",
+ "os": true,
+ "path": true,
+ "perf_hooks": ">= 8.5",
+ "process": ">= 1",
+ "punycode": true,
+ "querystring": true,
+ "readline": true,
+ "repl": true,
+ "smalloc": ">= 0.11.5 && < 3",
+ "_stream_duplex": ">= 0.9.4",
+ "_stream_transform": ">= 0.9.4",
+ "_stream_wrap": ">= 1.4.1",
+ "_stream_passthrough": ">= 0.9.4",
+ "_stream_readable": ">= 0.9.4",
+ "_stream_writable": ">= 0.9.4",
+ "stream": true,
+ "string_decoder": true,
+ "sys": true,
+ "timers": true,
+ "_tls_common": ">= 0.11.13",
+ "_tls_legacy": ">= 0.11.3 && < 10",
+ "_tls_wrap": ">= 0.11.3",
+ "tls": true,
+ "trace_events": ">= 10",
+ "tty": true,
+ "url": true,
+ "util": true,
+ "v8/tools/arguments": ">= 10 && < 12",
+ "v8/tools/codemap": [">= 4.4.0 && < 5", ">= 5.2.0 && < 12"],
+ "v8/tools/consarray": [">= 4.4.0 && < 5", ">= 5.2.0 && < 12"],
+ "v8/tools/csvparser": [">= 4.4.0 && < 5", ">= 5.2.0 && < 12"],
+ "v8/tools/logreader": [">= 4.4.0 && < 5", ">= 5.2.0 && < 12"],
+ "v8/tools/profile_view": [">= 4.4.0 && < 5", ">= 5.2.0 && < 12"],
+ "v8/tools/splaytree": [">= 4.4.0 && < 5", ">= 5.2.0 && < 12"],
+ "v8": ">= 1",
+ "vm": true,
+ "wasi": ">= 13.4 && < 13.5",
+ "worker_threads": ">= 11.7",
+ "zlib": true
+}
diff --git a/node_modules/resolve/lib/is-core.js b/node_modules/resolve/lib/is-core.js
new file mode 100644
index 000000000..48bc96c47
--- /dev/null
+++ b/node_modules/resolve/lib/is-core.js
@@ -0,0 +1,5 @@
+var core = require('./core');
+
+module.exports = function isCore(x) {
+ return Object.prototype.hasOwnProperty.call(core, x);
+};
diff --git a/node_modules/resolve/lib/node-modules-paths.js b/node_modules/resolve/lib/node-modules-paths.js
new file mode 100644
index 000000000..2b43813a7
--- /dev/null
+++ b/node_modules/resolve/lib/node-modules-paths.js
@@ -0,0 +1,42 @@
+var path = require('path');
+var parse = path.parse || require('path-parse');
+
+var getNodeModulesDirs = function getNodeModulesDirs(absoluteStart, modules) {
+ var prefix = '/';
+ if ((/^([A-Za-z]:)/).test(absoluteStart)) {
+ prefix = '';
+ } else if ((/^\\\\/).test(absoluteStart)) {
+ prefix = '\\\\';
+ }
+
+ var paths = [absoluteStart];
+ var parsed = parse(absoluteStart);
+ while (parsed.dir !== paths[paths.length - 1]) {
+ paths.push(parsed.dir);
+ parsed = parse(parsed.dir);
+ }
+
+ return paths.reduce(function (dirs, aPath) {
+ return dirs.concat(modules.map(function (moduleDir) {
+ return path.resolve(prefix, aPath, moduleDir);
+ }));
+ }, []);
+};
+
+module.exports = function nodeModulesPaths(start, opts, request) {
+ var modules = opts && opts.moduleDirectory
+ ? [].concat(opts.moduleDirectory)
+ : ['node_modules'];
+
+ if (opts && typeof opts.paths === 'function') {
+ return opts.paths(
+ request,
+ start,
+ function () { return getNodeModulesDirs(start, modules); },
+ opts
+ );
+ }
+
+ var dirs = getNodeModulesDirs(start, modules);
+ return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
+};
diff --git a/node_modules/resolve/lib/normalize-options.js b/node_modules/resolve/lib/normalize-options.js
new file mode 100644
index 000000000..4b56904ea
--- /dev/null
+++ b/node_modules/resolve/lib/normalize-options.js
@@ -0,0 +1,10 @@
+module.exports = function (x, opts) {
+ /**
+ * This file is purposefully a passthrough. It's expected that third-party
+ * environments will override it at runtime in order to inject special logic
+ * into `resolve` (by manipulating the options). One such example is the PnP
+ * code path in Yarn.
+ */
+
+ return opts || {};
+};
diff --git a/node_modules/resolve/lib/sync.js b/node_modules/resolve/lib/sync.js
new file mode 100644
index 000000000..da74e19d0
--- /dev/null
+++ b/node_modules/resolve/lib/sync.js
@@ -0,0 +1,191 @@
+var isCore = require('./is-core');
+var fs = require('fs');
+var path = require('path');
+var caller = require('./caller.js');
+var nodeModulesPaths = require('./node-modules-paths.js');
+var normalizeOptions = require('./normalize-options.js');
+
+var realpathFS = fs.realpathSync && typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync;
+
+var defaultIsFile = function isFile(file) {
+ try {
+ var stat = fs.statSync(file);
+ } catch (e) {
+ if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
+ throw e;
+ }
+ return stat.isFile() || stat.isFIFO();
+};
+
+var defaultIsDir = function isDirectory(dir) {
+ try {
+ var stat = fs.statSync(dir);
+ } catch (e) {
+ if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
+ throw e;
+ }
+ return stat.isDirectory();
+};
+
+var defaultRealpathSync = function realpathSync(x) {
+ try {
+ return realpathFS(x);
+ } catch (realpathErr) {
+ if (realpathErr.code !== 'ENOENT') {
+ throw realpathErr;
+ }
+ }
+ return x;
+};
+
+var maybeRealpathSync = function maybeRealpathSync(realpathSync, x, opts) {
+ if (opts && opts.preserveSymlinks === false) {
+ return realpathSync(x);
+ }
+ return x;
+};
+
+var getPackageCandidates = function getPackageCandidates(x, start, opts) {
+ var dirs = nodeModulesPaths(start, opts, x);
+ for (var i = 0; i < dirs.length; i++) {
+ dirs[i] = path.join(dirs[i], x);
+ }
+ return dirs;
+};
+
+module.exports = function resolveSync(x, options) {
+ if (typeof x !== 'string') {
+ throw new TypeError('Path must be a string.');
+ }
+ var opts = normalizeOptions(x, options);
+
+ var isFile = opts.isFile || defaultIsFile;
+ var readFileSync = opts.readFileSync || fs.readFileSync;
+ var isDirectory = opts.isDirectory || defaultIsDir;
+ var realpathSync = opts.realpathSync || defaultRealpathSync;
+ var packageIterator = opts.packageIterator;
+
+ var extensions = opts.extensions || ['.js'];
+ var basedir = opts.basedir || path.dirname(caller());
+ var parent = opts.filename || basedir;
+
+ opts.paths = opts.paths || [];
+
+ // ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
+ var absoluteStart = maybeRealpathSync(realpathSync, path.resolve(basedir), opts);
+
+ if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
+ var res = path.resolve(absoluteStart, x);
+ if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
+ var m = loadAsFileSync(res) || loadAsDirectorySync(res);
+ if (m) return maybeRealpathSync(realpathSync, m, opts);
+ } else if (isCore(x)) {
+ return x;
+ } else {
+ var n = loadNodeModulesSync(x, absoluteStart);
+ if (n) return maybeRealpathSync(realpathSync, n, opts);
+ }
+
+ var err = new Error("Cannot find module '" + x + "' from '" + parent + "'");
+ err.code = 'MODULE_NOT_FOUND';
+ throw err;
+
+ function loadAsFileSync(x) {
+ var pkg = loadpkg(path.dirname(x));
+
+ if (pkg && pkg.dir && pkg.pkg && opts.pathFilter) {
+ var rfile = path.relative(pkg.dir, x);
+ var r = opts.pathFilter(pkg.pkg, x, rfile);
+ if (r) {
+ x = path.resolve(pkg.dir, r); // eslint-disable-line no-param-reassign
+ }
+ }
+
+ if (isFile(x)) {
+ return x;
+ }
+
+ for (var i = 0; i < extensions.length; i++) {
+ var file = x + extensions[i];
+ if (isFile(file)) {
+ return file;
+ }
+ }
+ }
+
+ function loadpkg(dir) {
+ if (dir === '' || dir === '/') return;
+ if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) {
+ return;
+ }
+ if ((/[/\\]node_modules[/\\]*$/).test(dir)) return;
+
+ var pkgfile = path.join(maybeRealpathSync(realpathSync, dir, opts), 'package.json');
+
+ if (!isFile(pkgfile)) {
+ return loadpkg(path.dirname(dir));
+ }
+
+ var body = readFileSync(pkgfile);
+
+ try {
+ var pkg = JSON.parse(body);
+ } catch (jsonErr) {}
+
+ if (pkg && opts.packageFilter) {
+ // v2 will pass pkgfile
+ pkg = opts.packageFilter(pkg, /*pkgfile,*/ dir); // eslint-disable-line spaced-comment
+ }
+
+ return { pkg: pkg, dir: dir };
+ }
+
+ function loadAsDirectorySync(x) {
+ var pkgfile = path.join(maybeRealpathSync(realpathSync, x, opts), '/package.json');
+ if (isFile(pkgfile)) {
+ try {
+ var body = readFileSync(pkgfile, 'UTF8');
+ var pkg = JSON.parse(body);
+ } catch (e) {}
+
+ if (pkg && opts.packageFilter) {
+ // v2 will pass pkgfile
+ pkg = opts.packageFilter(pkg, /*pkgfile,*/ x); // eslint-disable-line spaced-comment
+ }
+
+ if (pkg && pkg.main) {
+ if (typeof pkg.main !== 'string') {
+ var mainError = new TypeError('package “' + pkg.name + '” `main` must be a string');
+ mainError.code = 'INVALID_PACKAGE_MAIN';
+ throw mainError;
+ }
+ if (pkg.main === '.' || pkg.main === './') {
+ pkg.main = 'index';
+ }
+ try {
+ var m = loadAsFileSync(path.resolve(x, pkg.main));
+ if (m) return m;
+ var n = loadAsDirectorySync(path.resolve(x, pkg.main));
+ if (n) return n;
+ } catch (e) {}
+ }
+ }
+
+ return loadAsFileSync(path.join(x, '/index'));
+ }
+
+ function loadNodeModulesSync(x, start) {
+ var thunk = function () { return getPackageCandidates(x, start, opts); };
+ var dirs = packageIterator ? packageIterator(x, start, thunk, opts) : thunk();
+
+ for (var i = 0; i < dirs.length; i++) {
+ var dir = dirs[i];
+ if (isDirectory(path.dirname(dir))) {
+ var m = loadAsFileSync(dir);
+ if (m) return m;
+ var n = loadAsDirectorySync(dir);
+ if (n) return n;
+ }
+ }
+ }
+};