Skip to content

Commit

Permalink
module: use validateString in modules/esm
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ZYSzys authored and BethGriggs committed Dec 17, 2018
1 parent 1f61c89 commit 25dae6c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
6 changes: 3 additions & 3 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_RETURN_PROPERTY,
ERR_INVALID_RETURN_PROPERTY_VALUE,
ERR_INVALID_RETURN_VALUE,
ERR_MISSING_DYNAMIC_INSTANTIATE_HOOK,
ERR_UNKNOWN_MODULE_FORMAT
} = require('internal/errors').codes;
const { URL } = require('url');
const { validateString } = require('internal/validators');
const ModuleMap = require('internal/modules/esm/module_map');
const ModuleJob = require('internal/modules/esm/module_job');
const defaultResolve = require('internal/modules/esm/default_resolve');
Expand Down Expand Up @@ -52,8 +52,8 @@ class Loader {

async resolve(specifier, parentURL) {
const isMain = parentURL === undefined;
if (!isMain && typeof parentURL !== 'string')
throw new ERR_INVALID_ARG_TYPE('parentURL', 'string', parentURL);
if (!isMain)
validateString(parentURL, 'parentURL');

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

Expand Down
13 changes: 4 additions & 9 deletions lib/internal/modules/esm/module_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,24 @@ const ModuleJob = require('internal/modules/esm/module_job');
const { SafeMap } = require('internal/safe_globals');
const debug = require('util').debuglog('esm');
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
const { validateString } = require('internal/validators');

// Tracks the state of the loader-level module cache
class ModuleMap extends SafeMap {
get(url) {
if (typeof url !== 'string') {
throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
}
validateString(url, 'url');
return super.get(url);
}
set(url, job) {
if (typeof url !== 'string') {
throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
}
validateString(url, 'url');
if (job instanceof ModuleJob !== true) {
throw new ERR_INVALID_ARG_TYPE('job', 'ModuleJob', job);
}
debug(`Storing ${url} in ModuleMap`);
return super.set(url, job);
}
has(url) {
if (typeof url !== 'string') {
throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
}
validateString(url, 'url');
return super.has(url);
}
}
Expand Down

0 comments on commit 25dae6c

Please sign in to comment.