Skip to content

Commit

Permalink
module: use validateString in modules/cjs
Browse files Browse the repository at this point in the history
PR-URL: #24863
Refs: #22101
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
ZYSzys authored and BethGriggs committed Feb 12, 2019
1 parent 8d4d396 commit 229f901
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
10 changes: 3 additions & 7 deletions 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,
Expand All @@ -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);
}

Expand Down
6 changes: 2 additions & 4 deletions lib/internal/modules/cjs/loader.js
Expand Up @@ -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;

Expand Down Expand Up @@ -629,9 +629,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');
Expand Down

0 comments on commit 229f901

Please sign in to comment.