From cc886a83bd7407b2cab3cf9f958001f5cd6c5191 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 25 Jan 2018 23:06:18 +0800 Subject: [PATCH 1/4] errors: only init colors when util is not loaded --- lib/internal/errors.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 5a3aaa04e97a09..6a52d55bb84247 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -270,12 +270,14 @@ class AssertionError extends Error { if (message != null) { super(message); } else { - const util = lazyUtil(); - if (process.stdout.isTTY && process.stdout.getColorDepth() !== 1) { + if (util_ === null && + process.stdout.isTTY && + process.stdout.getColorDepth() !== 1) { green = '\u001b[32m'; white = '\u001b[39m'; red = '\u001b[31m'; } + const util = lazyUtil(); if (actual && actual.stack && actual instanceof Error) actual = `${actual.name}: ${actual.message}`; From 6cda8056bb7064b6db78ecfdabcb410b06169e48 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 25 Jan 2018 04:26:52 +0800 Subject: [PATCH 2/4] module: use internal/errors.js in module.require --- lib/module.js | 13 ++++++++---- test/parallel/test-module-loading-error.js | 24 ++++++++++++++-------- test/sequential/test-module-loading.js | 11 ---------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/module.js b/lib/module.js index 4a8480ed1be3af..5ee537f157289b 100644 --- a/lib/module.js +++ b/lib/module.js @@ -603,10 +603,15 @@ Module.prototype.load = function(filename) { // Loads a module at the given file path. Returns that module's // `exports` property. -Module.prototype.require = function(path) { - assert(path, 'missing path'); - assert(typeof path === 'string', 'path must be a string'); - return Module._load(path, this, /* isMain */ false); +Module.prototype.require = function(id) { + if (typeof id !== 'string') { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'id', 'string', id); + } + if (id === '') { + throw new errors.Error('ERR_INVALID_ARG_VALUE', + 'id', id, 'must be a non-empty string'); + } + return Module._load(id, this, /* isMain */ false); }; diff --git a/test/parallel/test-module-loading-error.js b/test/parallel/test-module-loading-error.js index f1c457af2b2c9e..818a35eb582211 100644 --- a/test/parallel/test-module-loading-error.js +++ b/test/parallel/test-module-loading-error.js @@ -59,16 +59,22 @@ assert.throws( } ); -common.expectsError( - require, - { - code: 'ERR_ASSERTION', - message: /^missing path$/ - }); +const re = /^The "id" argument must be of type string\. Received type \w+$/; +[1, false, null, undefined, {}].forEach((value) => { + common.expectsError( + () => { require(value); }, + { + type: TypeError, + code: 'ERR_INVALID_ARG_TYPE', + message: re + }); +}); + common.expectsError( - () => { require({}); }, + () => { require(''); }, { - code: 'ERR_ASSERTION', - message: /^path must be a string$/ + type: Error, + code: 'ERR_INVALID_ARG_VALUE', + message: 'The argument \'id\' must be a non-empty string. Received \'\'' }); diff --git a/test/sequential/test-module-loading.js b/test/sequential/test-module-loading.js index d790850951b064..f0fa933a8ba2b2 100644 --- a/test/sequential/test-module-loading.js +++ b/test/sequential/test-module-loading.js @@ -297,17 +297,6 @@ try { } -// require() must take string, and must be truthy -assert.throws(function() { - console.error('require non-string'); - require({ foo: 'bar' }); -}, /path must be a string/); - -assert.throws(function() { - console.error('require empty string'); - require(''); -}, /missing path/); - process.on('exit', function() { assert.ok(a.A instanceof Function); assert.strictEqual(a.A(), 'A done'); From bd72666ae6e8e80f39b191f125cd7e558baebd16 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 25 Jan 2018 04:55:52 +0800 Subject: [PATCH 3/4] module: validate request in require.resolve --- lib/internal/module.js | 6 ++++++ test/parallel/test-require-resolve.js | 12 +++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/internal/module.js b/lib/internal/module.js index d2140411552429..5fa77a1112617f 100644 --- a/lib/internal/module.js +++ b/lib/internal/module.js @@ -1,5 +1,7 @@ 'use strict'; +const errors = require('internal/errors'); + // Invoke with makeRequireFunction(module) where |module| is the Module object // to use as the context for the require() function. function makeRequireFunction(mod) { @@ -15,6 +17,10 @@ function makeRequireFunction(mod) { } function resolve(request, options) { + if (typeof request !== 'string') { + throw new errors.Error('ERR_INVALID_ARG_TYPE', + 'request', 'string', request); + } return Module._resolveFilename(request, mod, false, options); } diff --git a/test/parallel/test-require-resolve.js b/test/parallel/test-require-resolve.js index 4fbf697faf51b6..a2bc05ada4dd8c 100644 --- a/test/parallel/test-require-resolve.js +++ b/test/parallel/test-require-resolve.js @@ -20,7 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); +const common = require('../common'); const fixtures = require('../common/fixtures'); const assert = require('assert'); @@ -38,3 +38,13 @@ assert.strictEqual('path', require.resolve('path')); // Test configurable resolve() paths. require(fixtures.path('require-resolve.js')); require(fixtures.path('resolve-paths', 'default', 'verify-paths.js')); + +const re = /^The "request" argument must be of type string\. Received type \w+$/; +[1, false, null, undefined, {}].forEach((value) => { + common.expectsError( + () => { require.resolve(value); }, + { + code: 'ERR_INVALID_ARG_TYPE', + message: re + }); +}); From 8cb4832b36251622e29ac9a5c0a3e63f9c55dc44 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 25 Jan 2018 04:56:15 +0800 Subject: [PATCH 4/4] module: validate request in require.resolve.paths --- lib/internal/module.js | 4 ++++ test/parallel/test-require-resolve.js | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/lib/internal/module.js b/lib/internal/module.js index 5fa77a1112617f..c3dede40fa3b87 100644 --- a/lib/internal/module.js +++ b/lib/internal/module.js @@ -27,6 +27,10 @@ function makeRequireFunction(mod) { require.resolve = resolve; function paths(request) { + if (typeof request !== 'string') { + throw new errors.Error('ERR_INVALID_ARG_TYPE', + 'request', 'string', request); + } return Module._resolveLookupPaths(request, mod, true); } diff --git a/test/parallel/test-require-resolve.js b/test/parallel/test-require-resolve.js index a2bc05ada4dd8c..2916f3709e3b15 100644 --- a/test/parallel/test-require-resolve.js +++ b/test/parallel/test-require-resolve.js @@ -47,4 +47,11 @@ const re = /^The "request" argument must be of type string\. Received type \w+$/ code: 'ERR_INVALID_ARG_TYPE', message: re }); + + common.expectsError( + () => { require.resolve.paths(value); }, + { + code: 'ERR_INVALID_ARG_TYPE', + message: re + }); });