From f59c435b3522f546c9d51769fccd8bc66e7876d5 Mon Sep 17 00:00:00 2001 From: ZYSzys <17367077526@163.com> Date: Thu, 6 Dec 2018 16:22:34 +0800 Subject: [PATCH] module: use validateString in modules/esm PR-URL: https://github.com/nodejs/node/pull/24868 Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca Reviewed-By: Trivikram Kamat --- lib/internal/modules/esm/loader.js | 6 +++--- lib/internal/modules/esm/module_map.js | 13 ++++--------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 60f1e31f94e86e..820b593446ca79 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -1,7 +1,6 @@ 'use strict'; const { - ERR_INVALID_ARG_TYPE, ERR_INVALID_RETURN_PROPERTY, ERR_INVALID_RETURN_PROPERTY_VALUE, ERR_INVALID_RETURN_VALUE, @@ -9,6 +8,7 @@ const { 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'); @@ -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); diff --git a/lib/internal/modules/esm/module_map.js b/lib/internal/modules/esm/module_map.js index f4786f8e9ef0be..a9d0c23c0e5ee9 100644 --- a/lib/internal/modules/esm/module_map.js +++ b/lib/internal/modules/esm/module_map.js @@ -4,19 +4,16 @@ 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); } @@ -24,9 +21,7 @@ class ModuleMap extends SafeMap { 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); } }