Skip to content

Commit 2a11e6a

Browse files
ZYSzysBethGriggs
authored andcommitted
module: use validateString in modules/cjs
PR-URL: #24863 Refs: #22101 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 79c52a9 commit 2a11e6a

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

lib/internal/modules/cjs/helpers.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
3+
const { validateString } = require('internal/validators');
44

55
const {
66
CHAR_LINE_FEED,
@@ -26,18 +26,14 @@ function makeRequireFunction(mod) {
2626
}
2727

2828
function resolve(request, options) {
29-
if (typeof request !== 'string') {
30-
throw new ERR_INVALID_ARG_TYPE('request', 'string', request);
31-
}
29+
validateString(request, 'request');
3230
return Module._resolveFilename(request, mod, false, options);
3331
}
3432

3533
require.resolve = resolve;
3634

3735
function paths(request) {
38-
if (typeof request !== 'string') {
39-
throw new ERR_INVALID_ARG_TYPE('request', 'string', request);
40-
}
36+
validateString(request, 'request');
4137
return Module._resolveLookupPaths(request, mod, true);
4238
}
4339

lib/internal/modules/cjs/loader.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
4747
const experimentalModules = getOptionValue('--experimental-modules');
4848

4949
const {
50-
ERR_INVALID_ARG_TYPE,
5150
ERR_INVALID_ARG_VALUE,
5251
ERR_REQUIRE_ESM
5352
} = require('internal/errors').codes;
53+
const { validateString } = require('internal/validators');
5454

5555
module.exports = Module;
5656

@@ -649,9 +649,7 @@ Module.prototype.load = function(filename) {
649649
// Loads a module at the given file path. Returns that module's
650650
// `exports` property.
651651
Module.prototype.require = function(id) {
652-
if (typeof id !== 'string') {
653-
throw new ERR_INVALID_ARG_TYPE('id', 'string', id);
654-
}
652+
validateString(id, 'id');
655653
if (id === '') {
656654
throw new ERR_INVALID_ARG_VALUE('id', id,
657655
'must be a non-empty string');

0 commit comments

Comments
 (0)