Comparing version
@@ -174,15 +174,17 @@ var core = require('./core'); | ||
var pkgfile = path.join(dir, 'package.json'); | ||
isFile(pkgfile, function (err, ex) { | ||
// on err, ex is false | ||
if (!ex) return loadpkg(path.dirname(dir), cb); | ||
maybeUnwrapSymlink(path.join(dir, 'package.json'), opts, function (unwrapErr, pkgfile) { | ||
if (unwrapErr) return loadpkg(path.dirname(dir), cb); | ||
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) {} | ||
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); | ||
if (pkg && opts.packageFilter) { | ||
pkg = opts.packageFilter(pkg, pkgfile); | ||
} | ||
cb(null, pkg, dir); | ||
}); | ||
}); | ||
@@ -200,42 +202,44 @@ }); | ||
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'), fpkg, cb); | ||
readFile(pkgfile, function (err, body) { | ||
maybeUnwrapSymlink(path.join(x, 'package.json'), opts, function (unwrapErr, pkgfile) { | ||
if (unwrapErr) return cb(unwrapErr); | ||
isFile(pkgfile, function (err, ex) { | ||
if (err) return cb(err); | ||
try { | ||
var pkg = JSON.parse(body); | ||
} catch (jsonErr) {} | ||
if (!ex) return loadAsFile(path.join(x, 'index'), fpkg, cb); | ||
if (opts.packageFilter) { | ||
pkg = opts.packageFilter(pkg, pkgfile); | ||
} | ||
readFile(pkgfile, function (err, body) { | ||
if (err) return cb(err); | ||
try { | ||
var pkg = JSON.parse(body); | ||
} catch (jsonErr) {} | ||
if (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 && opts.packageFilter) { | ||
pkg = opts.packageFilter(pkg, pkgfile); | ||
} | ||
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 (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 (n) return cb(null, n, pkg); | ||
loadAsFile(path.join(x, 'index'), pkg, cb); | ||
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; | ||
} | ||
return; | ||
} | ||
loadAsFile(path.join(x, '/index'), pkg, cb); | ||
loadAsFile(path.join(x, '/index'), pkg, cb); | ||
}); | ||
}); | ||
@@ -242,0 +246,0 @@ }); |
@@ -108,3 +108,3 @@ var core = require('./core'); | ||
var pkgfile = path.join(dir, 'package.json'); | ||
var pkgfile = maybeUnwrapSymlink(path.join(dir, 'package.json'), opts); | ||
@@ -122,3 +122,3 @@ if (!isFile(pkgfile)) { | ||
if (pkg && opts.packageFilter) { | ||
pkg = opts.packageFilter(pkg, dir); | ||
pkg = opts.packageFilter(pkg, pkgfile, dir); | ||
} | ||
@@ -130,3 +130,3 @@ | ||
function loadAsDirectorySync(x) { | ||
var pkgfile = path.join(x, '/package.json'); | ||
var pkgfile = maybeUnwrapSymlink(path.join(x, '/package.json'), opts); | ||
if (isFile(pkgfile)) { | ||
@@ -138,7 +138,7 @@ try { | ||
if (opts.packageFilter) { | ||
pkg = opts.packageFilter(pkg, x); | ||
if (pkg && opts.packageFilter) { | ||
pkg = opts.packageFilter(pkg, pkgfile, x); | ||
} | ||
if (pkg.main) { | ||
if (pkg && pkg.main) { | ||
if (typeof pkg.main !== 'string') { | ||
@@ -145,0 +145,0 @@ var mainError = new TypeError('package “' + pkg.name + '” `main` must be a string'); |
{ | ||
"name": "resolve", | ||
"description": "resolve like require.resolve() on behalf of files asynchronously and synchronously", | ||
"version": "1.12.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/browserify/resolve.git" | ||
}, | ||
"main": "index.js", | ||
"keywords": [ | ||
"resolve", | ||
"require", | ||
"node", | ||
"module" | ||
], | ||
"scripts": { | ||
"prepublish": "safe-publish-latest", | ||
"lint": "eslint .", | ||
"pretests-only": "cd ./test/resolver/nested_symlinks && node mylib/sync && node mylib/async", | ||
"tests-only": "tape test/*.js", | ||
"pretest": "npm run lint", | ||
"test": "npm run --silent tests-only", | ||
"posttest": "npm run test:multirepo", | ||
"test:multirepo": "cd ./test/resolver/multirepo && npm install && npm test" | ||
}, | ||
"devDependencies": { | ||
"@ljharb/eslint-config": "^13.1.1", | ||
"eslint": "^5.16.0", | ||
"object-keys": "^1.1.1", | ||
"safe-publish-latest": "^1.1.2", | ||
"tap": "0.4.13", | ||
"tape": "^4.11.0" | ||
}, | ||
"license": "MIT", | ||
"author": { | ||
"name": "James Halliday", | ||
"email": "mail@substack.net", | ||
"url": "http://substack.net" | ||
}, | ||
"dependencies": { | ||
"path-parse": "^1.0.6" | ||
} | ||
"name": "resolve", | ||
"description": "resolve like require.resolve() on behalf of files asynchronously and synchronously", | ||
"version": "1.12.1", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/browserify/resolve.git" | ||
}, | ||
"main": "index.js", | ||
"keywords": [ | ||
"resolve", | ||
"require", | ||
"node", | ||
"module" | ||
], | ||
"scripts": { | ||
"prepublish": "safe-publish-latest", | ||
"lint": "eslint .", | ||
"pretests-only": "cd ./test/resolver/nested_symlinks && node mylib/sync && node mylib/async", | ||
"tests-only": "tape test/*.js", | ||
"pretest": "npm run lint", | ||
"test": "npm run --silent tests-only", | ||
"posttest": "npm run test:multirepo", | ||
"test:multirepo": "cd ./test/resolver/multirepo && npm install && npm test" | ||
}, | ||
"devDependencies": { | ||
"@ljharb/eslint-config": "^15.0.2", | ||
"array.prototype.map": "^1.0.1", | ||
"eslint": "^6.6.0", | ||
"object-keys": "^1.1.1", | ||
"safe-publish-latest": "^1.1.4", | ||
"tap": "0.4.13", | ||
"tape": "^4.11.0" | ||
}, | ||
"license": "MIT", | ||
"author": { | ||
"name": "James Halliday", | ||
"email": "mail@substack.net", | ||
"url": "http://substack.net" | ||
}, | ||
"funding": { | ||
"url": "https://github.com/sponsors/ljharb" | ||
}, | ||
"dependencies": { | ||
"path-parse": "^1.0.6" | ||
} | ||
} |
@@ -10,5 +10,5 @@ var path = require('path'); | ||
basedir: dir, | ||
packageFilter: function (pkg, dir) { | ||
pkg.main = 'doom'; | ||
packageFilterArgs = [pkg, dir]; | ||
packageFilter: function (pkg, pkgfile, dir) { | ||
pkg.main = 'doom'; // eslint-disable-line no-param-reassign | ||
packageFilterArgs = [pkg, pkgfile, dir]; | ||
return pkg; | ||
@@ -24,5 +24,8 @@ } | ||
var packageFile = packageFilterArgs[1]; | ||
t.equal(packageFile, path.join(dir, 'baz'), 'second packageFilter argument is "dir"'); | ||
t.equal(packageFile, path.join(dir, 'baz', 'package.json'), 'package.json path is correct'); | ||
var packageDir = packageFilterArgs[2]; | ||
t.equal(packageDir, path.join(dir, 'baz'), 'third packageFilter argument is "dir"'); | ||
t.end(); | ||
}); |
@@ -12,3 +12,3 @@ var path = require('path'); | ||
packageFilter: function (pkg, pkgfile) { | ||
pkg.main = 'doom'; | ||
pkg.main = 'doom'; // eslint-disable-line no-param-reassign | ||
packageFilterArgs = [pkg, pkgfile]; | ||
@@ -15,0 +15,0 @@ return pkg; |
@@ -321,4 +321,4 @@ var path = require('path'); | ||
if (pkg.browser) { | ||
pkg.main = pkg.browser; | ||
delete pkg.browser; | ||
pkg.main = pkg.browser; // eslint-disable-line no-param-reassign | ||
delete pkg.browser; // eslint-disable-line no-param-reassign | ||
} | ||
@@ -325,0 +325,0 @@ return pkg; |
@@ -405,4 +405,4 @@ var path = require('path'); | ||
if (pkg.browser) { | ||
pkg.main = pkg.browser; | ||
delete pkg.browser; | ||
pkg.main = pkg.browser; // eslint-disable-line no-param-reassign | ||
delete pkg.browser; // eslint-disable-line no-param-reassign | ||
} | ||
@@ -409,0 +409,0 @@ return pkg; |
var path = require('path'); | ||
var fs = require('fs'); | ||
var test = require('tape'); | ||
var map = require('array.prototype.map'); | ||
var resolve = require('../'); | ||
@@ -8,2 +9,4 @@ | ||
var packageDir = path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'package'); | ||
var modADir = path.join(__dirname, 'symlinks', 'source', 'node_modules', 'mod-a'); | ||
var symlinkModADir = path.join(__dirname, 'symlinks', 'dest', 'node_modules', 'mod-a'); | ||
try { | ||
@@ -16,2 +19,9 @@ fs.unlinkSync(symlinkDir); | ||
try { | ||
fs.unlinkSync(modADir); | ||
} catch (err) {} | ||
try { | ||
fs.unlinkSync(symlinkModADir); | ||
} catch (err) {} | ||
try { | ||
fs.symlinkSync('./_/symlink_target', symlinkDir, 'dir'); | ||
@@ -28,2 +38,8 @@ } catch (err) { | ||
} | ||
try { | ||
fs.symlinkSync('../../source/node_modules/mod-a', symlinkModADir, 'dir'); | ||
} catch (err) { | ||
// if fails then it is probably on Windows and lets try to create a junction | ||
fs.symlinkSync(path.join(__dirname, '..', '..', 'source', 'node_modules', 'mod-a') + '\\', symlinkModADir, 'junction'); | ||
} | ||
@@ -85,4 +101,67 @@ test('symlink', function (t) { | ||
t.equal(result, path.resolve(__dirname, 'resolver/symlinked/package/bar.js')); | ||
t.end(); | ||
}); | ||
}); | ||
test('packageFilter', function (t) { | ||
function testPackageFilter(preserveSymlinks) { | ||
return function (st) { | ||
st.plan(5); | ||
var destMain = 'symlinks/dest/node_modules/mod-a/index.js'; | ||
var destPkg = 'symlinks/dest/node_modules/mod-a/package.json'; | ||
var sourceMain = 'symlinks/source/node_modules/mod-a/index.js'; | ||
var sourcePkg = 'symlinks/source/node_modules/mod-a/package.json'; | ||
var destDir = path.join(__dirname, 'symlinks', 'dest'); | ||
var packageFilterPath = []; | ||
var actualPath = resolve.sync('mod-a', { | ||
basedir: destDir, | ||
preserveSymlinks: preserveSymlinks, | ||
packageFilter: function (pkg, pkgfile) { | ||
packageFilterPath.push(pkgfile); | ||
} | ||
}); | ||
st.equal( | ||
actualPath.replace(__dirname + '/', ''), | ||
preserveSymlinks ? destMain : sourceMain, | ||
'sync: actual path is correct' | ||
); | ||
st.deepEqual( | ||
map(packageFilterPath, function (x) { return x.replace(__dirname + '/', ''); }), | ||
preserveSymlinks ? [destPkg, destPkg] : [sourcePkg, sourcePkg], | ||
'sync: packageFilter pkgfile arg is correct' | ||
); | ||
var asyncPackageFilterPath = []; | ||
resolve( | ||
'mod-a', | ||
{ | ||
basedir: destDir, | ||
preserveSymlinks: preserveSymlinks, | ||
packageFilter: function (pkg, pkgfile) { | ||
asyncPackageFilterPath.push(pkgfile); | ||
} | ||
}, | ||
function (err, actualPath) { | ||
st.error(err, 'no error'); | ||
st.equal( | ||
actualPath.replace(__dirname + '/', ''), | ||
preserveSymlinks ? destMain : sourceMain, | ||
'async: actual path is correct' | ||
); | ||
st.deepEqual( | ||
map(asyncPackageFilterPath, function (x) { return x.replace(__dirname + '/', ''); }), | ||
preserveSymlinks ? [destPkg, destPkg, destPkg] : [sourcePkg, sourcePkg, sourcePkg], | ||
'async: packageFilter pkgfile arg is correct' | ||
); | ||
} | ||
); | ||
}; | ||
} | ||
t.test('preserveSymlinks: false', testPackageFilter(false)); | ||
t.test('preserveSymlinks: true', testPackageFilter(true)); | ||
t.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
2172
3.68%94215
-5.31%7
16.67%89
-1.11%