Skip to content

Commit 229f901

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 8d4d396 commit 229f901

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

@@ -629,9 +629,7 @@ Module.prototype.load = function(filename) {
629629
// Loads a module at the given file path. Returns that module's
630630
// `exports` property.
631631
Module.prototype.require = function(id) {
632-
if (typeof id !== 'string') {
633-
throw new ERR_INVALID_ARG_TYPE('id', 'string', id);
634-
}
632+
validateString(id, 'id');
635633
if (id === '') {
636634
throw new ERR_INVALID_ARG_VALUE('id', id,
637635
'must be a non-empty string');

0 commit comments

Comments
 (0)