@@ -51,6 +51,7 @@ const {
5151 ERR_PACKAGE_IMPORT_NOT_DEFINED ,
5252 ERR_PACKAGE_PATH_NOT_EXPORTED ,
5353 ERR_UNSUPPORTED_DIR_IMPORT ,
54+ ERR_UNSUPPORTED_RESOLVE_REQUEST ,
5455 ERR_NETWORK_IMPORT_DISALLOWED ,
5556} = require ( 'internal/errors' ) . codes ;
5657
@@ -884,22 +885,37 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) {
884885 * @param {boolean } preserveSymlinks - Whether to preserve symlinks in the resolved URL.
885886 */
886887function moduleResolve ( specifier , base , conditions , preserveSymlinks ) {
887- const isRemote = base . protocol === 'http:' ||
888- base . protocol === 'https:' ;
888+ const protocol = typeof base === 'string' ?
889+ StringPrototypeSlice ( base , 0 , StringPrototypeIndexOf ( base , ':' ) + 1 ) :
890+ base . protocol ;
891+ const isData = protocol === 'data:' ;
892+ const isRemote =
893+ isData ||
894+ protocol === 'http:' ||
895+ protocol === 'https:' ;
889896 // Order swapped from spec for minor perf gain.
890897 // Ok since relative URLs cannot parse as URLs.
891898 let resolved ;
892899 if ( shouldBeTreatedAsRelativeOrAbsolutePath ( specifier ) ) {
893- resolved = new URL ( specifier , base ) ;
894- } else if ( ! isRemote && specifier [ 0 ] === '#' ) {
900+ try {
901+ resolved = new URL ( specifier , base ) ;
902+ } catch ( cause ) {
903+ const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST ( specifier , base ) ;
904+ setOwnProperty ( error , 'cause' , cause ) ;
905+ throw error ;
906+ }
907+ } else if ( protocol === 'file:' && specifier [ 0 ] === '#' ) {
895908 resolved = packageImportsResolve ( specifier , base , conditions ) ;
896909 } else {
897910 try {
898911 resolved = new URL ( specifier ) ;
899- } catch {
900- if ( ! isRemote ) {
901- resolved = packageResolve ( specifier , base , conditions ) ;
912+ } catch ( cause ) {
913+ if ( isRemote && ! BuiltinModule . canBeRequiredWithoutScheme ( specifier ) ) {
914+ const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST ( specifier , base ) ;
915+ setOwnProperty ( error , 'cause' , cause ) ;
916+ throw error ;
902917 }
918+ resolved = packageResolve ( specifier , base , conditions ) ;
903919 }
904920 }
905921 if ( resolved . protocol !== 'file:' ) {
@@ -1063,7 +1079,7 @@ function defaultResolve(specifier, context = {}) {
10631079 }
10641080 }
10651081
1066- let parsed ;
1082+ let parsed , protocol ;
10671083 try {
10681084 if ( shouldBeTreatedAsRelativeOrAbsolutePath ( specifier ) ) {
10691085 parsed = new URL ( specifier , parsedParentURL ) ;
@@ -1072,7 +1088,7 @@ function defaultResolve(specifier, context = {}) {
10721088 }
10731089
10741090 // Avoid accessing the `protocol` property due to the lazy getters.
1075- const protocol = parsed . protocol ;
1091+ protocol = parsed . protocol ;
10761092 if ( protocol === 'data:' ||
10771093 ( experimentalNetworkImports &&
10781094 (
@@ -1099,7 +1115,8 @@ function defaultResolve(specifier, context = {}) {
10991115 if ( maybeReturn ) { return maybeReturn ; }
11001116
11011117 // This must come after checkIfDisallowedImport
1102- if ( parsed && parsed . protocol === 'node:' ) { return { __proto__ : null , url : specifier } ; }
1118+ protocol ??= parsed ?. protocol ;
1119+ if ( protocol === 'node:' ) { return { __proto__ : null , url : specifier } ; }
11031120
11041121
11051122 const isMain = parentURL === undefined ;
@@ -1182,6 +1199,7 @@ module.exports = {
11821199const {
11831200 defaultGetFormatWithoutErrors,
11841201} = require ( 'internal/modules/esm/get_format' ) ;
1202+ const { setOwnProperty } = require ( 'internal/util' ) ;
11851203
11861204if ( policy ) {
11871205 const $defaultResolve = defaultResolve ;
0 commit comments