Skip to content

Commit c448d08

Browse files
nicolo-ribaudoljharb
authored andcommitted
[Fix] sync/async Do not leak Object.prototype when checking for core modules
[New] add `is-core` export
1 parent f6f2a46 commit c448d08

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
var core = require('./lib/core');
21
var async = require('./lib/async');
3-
async.core = core;
4-
async.isCore = function isCore(x) { return core[x]; };
2+
async.core = require('./lib/core');
3+
async.isCore = require('./lib/is-core');
54
async.sync = require('./lib/sync');
65

76
exports = async;

lib/async.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var core = require('./core');
21
var fs = require('fs');
32
var path = require('path');
43
var caller = require('./caller.js');
54
var nodeModulesPaths = require('./node-modules-paths.js');
65
var normalizeOptions = require('./normalize-options.js');
6+
var isCore = require('./is-core');
77

88
var defaultIsFile = function isFile(file, cb) {
99
fs.stat(file, function (err, stat) {
@@ -98,7 +98,7 @@ module.exports = function resolve(x, options, callback) {
9898
} else loadAsFile(res, opts.package, onfile);
9999
} else loadNodeModules(x, basedir, function (err, n, pkg) {
100100
if (err) cb(err);
101-
else if (core[x]) return cb(null, x);
101+
else if (isCore(x)) return cb(null, x);
102102
else if (n) {
103103
return maybeUnwrapSymlink(n, opts, function (err, realN) {
104104
if (err) {

lib/is-core.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var core = require('./core');
2+
3+
module.exports = function isCore(x) {
4+
return Object.prototype.hasOwnProperty.call(core, x);
5+
};

lib/sync.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var core = require('./core');
1+
var isCore = require('./is-core');
22
var fs = require('fs');
33
var path = require('path');
44
var caller = require('./caller.js');
@@ -68,14 +68,14 @@ module.exports = function (x, options) {
6868
if (x === '..' || x.slice(-1) === '/') res += '/';
6969
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
7070
if (m) return maybeUnwrapSymlink(m, opts);
71-
} else if (core[x]) {
71+
} else if (isCore(x)) {
7272
return x;
7373
} else {
7474
var n = loadNodeModulesSync(x, absoluteStart);
7575
if (n) return maybeUnwrapSymlink(n, opts);
7676
}
7777

78-
if (core[x]) return x;
78+
if (isCore(x)) return x;
7979

8080
var err = new Error("Cannot find module '" + x + "' from '" + parent + "'");
8181
err.code = 'MODULE_NOT_FOUND';

test/core.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ test('core modules', function (t) {
1010

1111
st.ok(!resolve.isCore('seq'));
1212
st.ok(!resolve.isCore('../'));
13+
14+
st.ok(!resolve.isCore('toString'));
15+
1316
st.end();
1417
});
1518

0 commit comments

Comments
 (0)