From 2a11e6aaf33f95ec2f154194a06894696845ea1a Mon Sep 17 00:00:00 2001 From: ZYSzys <17367077526@163.com> Date: Thu, 6 Dec 2018 13:50:41 +0800 Subject: [PATCH] module: use validateString in modules/cjs PR-URL: https://github.com/nodejs/node/pull/24863 Refs: https://github.com/nodejs/node/pull/22101 Reviewed-By: Richard Lau Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca Reviewed-By: Trivikram Kamat --- lib/internal/modules/cjs/helpers.js | 10 +++------- lib/internal/modules/cjs/loader.js | 6 ++---- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/internal/modules/cjs/helpers.js b/lib/internal/modules/cjs/helpers.js index b08e97b29c5c8d..2c856a99c6b8fd 100644 --- a/lib/internal/modules/cjs/helpers.js +++ b/lib/internal/modules/cjs/helpers.js @@ -1,6 +1,6 @@ 'use strict'; -const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; +const { validateString } = require('internal/validators'); const { CHAR_LINE_FEED, @@ -26,18 +26,14 @@ function makeRequireFunction(mod) { } function resolve(request, options) { - if (typeof request !== 'string') { - throw new ERR_INVALID_ARG_TYPE('request', 'string', request); - } + validateString(request, 'request'); return Module._resolveFilename(request, mod, false, options); } require.resolve = resolve; function paths(request) { - if (typeof request !== 'string') { - throw new ERR_INVALID_ARG_TYPE('request', 'string', request); - } + validateString(request, 'request'); return Module._resolveLookupPaths(request, mod, true); } diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 294c94d36943d4..2ba5e0818b644e 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -47,10 +47,10 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); const experimentalModules = getOptionValue('--experimental-modules'); const { - ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_REQUIRE_ESM } = require('internal/errors').codes; +const { validateString } = require('internal/validators'); module.exports = Module; @@ -649,9 +649,7 @@ Module.prototype.load = function(filename) { // Loads a module at the given file path. Returns that module's // `exports` property. Module.prototype.require = function(id) { - if (typeof id !== 'string') { - throw new ERR_INVALID_ARG_TYPE('id', 'string', id); - } + validateString(id, 'id'); if (id === '') { throw new ERR_INVALID_ARG_VALUE('id', id, 'must be a non-empty string');