Skip to content

Commit 25dae6c

Browse files
ZYSzysBethGriggs
authored andcommitted
module: use validateString in modules/esm
PR-URL: #24868 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 1f61c89 commit 25dae6c

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

lib/internal/modules/esm/loader.js

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

33
const {
4-
ERR_INVALID_ARG_TYPE,
54
ERR_INVALID_RETURN_PROPERTY,
65
ERR_INVALID_RETURN_PROPERTY_VALUE,
76
ERR_INVALID_RETURN_VALUE,
87
ERR_MISSING_DYNAMIC_INSTANTIATE_HOOK,
98
ERR_UNKNOWN_MODULE_FORMAT
109
} = require('internal/errors').codes;
1110
const { URL } = require('url');
11+
const { validateString } = require('internal/validators');
1212
const ModuleMap = require('internal/modules/esm/module_map');
1313
const ModuleJob = require('internal/modules/esm/module_job');
1414
const defaultResolve = require('internal/modules/esm/default_resolve');
@@ -52,8 +52,8 @@ class Loader {
5252

5353
async resolve(specifier, parentURL) {
5454
const isMain = parentURL === undefined;
55-
if (!isMain && typeof parentURL !== 'string')
56-
throw new ERR_INVALID_ARG_TYPE('parentURL', 'string', parentURL);
55+
if (!isMain)
56+
validateString(parentURL, 'parentURL');
5757

5858
const resolved = await this._resolve(specifier, parentURL, defaultResolve);
5959

lib/internal/modules/esm/module_map.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,24 @@ const ModuleJob = require('internal/modules/esm/module_job');
44
const { SafeMap } = require('internal/safe_globals');
55
const debug = require('util').debuglog('esm');
66
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
7+
const { validateString } = require('internal/validators');
78

89
// Tracks the state of the loader-level module cache
910
class ModuleMap extends SafeMap {
1011
get(url) {
11-
if (typeof url !== 'string') {
12-
throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
13-
}
12+
validateString(url, 'url');
1413
return super.get(url);
1514
}
1615
set(url, job) {
17-
if (typeof url !== 'string') {
18-
throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
19-
}
16+
validateString(url, 'url');
2017
if (job instanceof ModuleJob !== true) {
2118
throw new ERR_INVALID_ARG_TYPE('job', 'ModuleJob', job);
2219
}
2320
debug(`Storing ${url} in ModuleMap`);
2421
return super.set(url, job);
2522
}
2623
has(url) {
27-
if (typeof url !== 'string') {
28-
throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
29-
}
24+
validateString(url, 'url');
3025
return super.has(url);
3126
}
3227
}

0 commit comments

Comments
 (0)