Skip to content

Commit

Permalink
[Fix] sync/async Do not leak Object.prototype when checking for c…
Browse files Browse the repository at this point in the history
…ore modules

[New] add `is-core` export

See #203.
  • Loading branch information
nicolo-ribaudo authored and ljharb committed Nov 25, 2019
1 parent 1aedb83 commit e5da77e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var core = require('./lib/core');
var async = require('./lib/async');
async.core = core;
async.isCore = function isCore(x) { return core[x]; };
async.core = require('./lib/core');
async.isCore = require('./lib/is-core');
async.sync = require('./lib/sync');

exports = async;
Expand Down
4 changes: 2 additions & 2 deletions lib/async.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var core = require('./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 isCore = require('./is-core');

var defaultIsFile = function isFile(file, cb) {
fs.stat(file, function (err, stat) {
Expand Down Expand Up @@ -84,7 +84,7 @@ module.exports = function resolve(x, options, callback) {
} else loadAsFile(res, opts.package, onfile);
} else loadNodeModules(x, basedir, function (err, n, pkg) {
if (err) cb(err);
else if (core[x]) return cb(null, x);
else if (isCore(x)) return cb(null, x);
else if (n) {
return maybeUnwrapSymlink(n, opts, function (err, realN) {
if (err) {
Expand Down
5 changes: 5 additions & 0 deletions lib/is-core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var core = require('./core');

module.exports = function isCore(x) {
return Object.prototype.hasOwnProperty.call(core, x);
};
6 changes: 3 additions & 3 deletions lib/sync.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var core = require('./core');
var isCore = require('./is-core');
var fs = require('fs');
var path = require('path');
var caller = require('./caller.js');
Expand Down Expand Up @@ -62,14 +62,14 @@ module.exports = function (x, options) {
if (x === '..' || x.slice(-1) === '/') res += '/';
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
if (m) return maybeUnwrapSymlink(m, opts);
} else if (core[x]) {
} else if (isCore(x)) {
return x;
} else {
var n = loadNodeModulesSync(x, absoluteStart);
if (n) return maybeUnwrapSymlink(n, opts);
}

if (core[x]) return x;
if (isCore(x)) return x;

var err = new Error("Cannot find module '" + x + "' from '" + parent + "'");
err.code = 'MODULE_NOT_FOUND';
Expand Down
3 changes: 3 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ test('core modules', function (t) {

st.ok(!resolve.isCore('seq'));
st.ok(!resolve.isCore('../'));

st.ok(!resolve.isCore('toString'));

st.end();
});

Expand Down

0 comments on commit e5da77e

Please sign in to comment.