@@ -37,7 +37,7 @@ const experimentalNetworkImports =
37
37
getOptionValue ( '--experimental-network-imports' ) ;
38
38
const inputTypeFlag = getOptionValue ( '--input-type' ) ;
39
39
const { URL , pathToFileURL, fileURLToPath, isURL } = require ( 'internal/url' ) ;
40
- const { getCWDURL } = require ( 'internal/util' ) ;
40
+ const { getCWDURL, setOwnProperty } = require ( 'internal/util' ) ;
41
41
const { canParse : URLCanParse } = internalBinding ( 'url' ) ;
42
42
const { legacyMainResolve : FSLegacyMainResolve } = internalBinding ( 'fs' ) ;
43
43
const {
@@ -51,6 +51,7 @@ const {
51
51
ERR_PACKAGE_IMPORT_NOT_DEFINED ,
52
52
ERR_PACKAGE_PATH_NOT_EXPORTED ,
53
53
ERR_UNSUPPORTED_DIR_IMPORT ,
54
+ ERR_UNSUPPORTED_RESOLVE_REQUEST ,
54
55
ERR_NETWORK_IMPORT_DISALLOWED ,
55
56
} = require ( 'internal/errors' ) . codes ;
56
57
@@ -893,22 +894,37 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) {
893
894
* @param {boolean } preserveSymlinks - Whether to preserve symlinks in the resolved URL.
894
895
*/
895
896
function moduleResolve ( specifier , base , conditions , preserveSymlinks ) {
896
- const isRemote = base . protocol === 'http:' ||
897
- base . protocol === 'https:' ;
897
+ const protocol = typeof base === 'string' ?
898
+ StringPrototypeSlice ( base , 0 , StringPrototypeIndexOf ( base , ':' ) + 1 ) :
899
+ base . protocol ;
900
+ const isData = protocol === 'data:' ;
901
+ const isRemote =
902
+ isData ||
903
+ protocol === 'http:' ||
904
+ protocol === 'https:' ;
898
905
// Order swapped from spec for minor perf gain.
899
906
// Ok since relative URLs cannot parse as URLs.
900
907
let resolved ;
901
908
if ( shouldBeTreatedAsRelativeOrAbsolutePath ( specifier ) ) {
902
- resolved = new URL ( specifier , base ) ;
903
- } else if ( ! isRemote && specifier [ 0 ] === '#' ) {
909
+ try {
910
+ resolved = new URL ( specifier , base ) ;
911
+ } catch ( cause ) {
912
+ const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST ( specifier , base ) ;
913
+ setOwnProperty ( error , 'cause' , cause ) ;
914
+ throw error ;
915
+ }
916
+ } else if ( protocol === 'file:' && specifier [ 0 ] === '#' ) {
904
917
resolved = packageImportsResolve ( specifier , base , conditions ) ;
905
918
} else {
906
919
try {
907
920
resolved = new URL ( specifier ) ;
908
- } catch {
909
- if ( ! isRemote ) {
910
- resolved = packageResolve ( specifier , base , conditions ) ;
921
+ } catch ( cause ) {
922
+ if ( isRemote && ! BuiltinModule . canBeRequiredWithoutScheme ( specifier ) ) {
923
+ const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST ( specifier , base ) ;
924
+ setOwnProperty ( error , 'cause' , cause ) ;
925
+ throw error ;
911
926
}
927
+ resolved = packageResolve ( specifier , base , conditions ) ;
912
928
}
913
929
}
914
930
if ( resolved . protocol !== 'file:' ) {
@@ -1082,7 +1098,7 @@ function defaultResolve(specifier, context = {}) {
1082
1098
}
1083
1099
}
1084
1100
1085
- let parsed ;
1101
+ let parsed , protocol ;
1086
1102
try {
1087
1103
if ( shouldBeTreatedAsRelativeOrAbsolutePath ( specifier ) ) {
1088
1104
parsed = new URL ( specifier , parsedParentURL ) ;
@@ -1091,7 +1107,7 @@ function defaultResolve(specifier, context = {}) {
1091
1107
}
1092
1108
1093
1109
// Avoid accessing the `protocol` property due to the lazy getters.
1094
- const protocol = parsed . protocol ;
1110
+ protocol = parsed . protocol ;
1095
1111
if ( protocol === 'data:' ||
1096
1112
( experimentalNetworkImports &&
1097
1113
(
@@ -1118,7 +1134,8 @@ function defaultResolve(specifier, context = {}) {
1118
1134
if ( maybeReturn ) { return maybeReturn ; }
1119
1135
1120
1136
// This must come after checkIfDisallowedImport
1121
- if ( parsed && parsed . protocol === 'node:' ) { return { __proto__ : null , url : specifier } ; }
1137
+ protocol ??= parsed ?. protocol ;
1138
+ if ( protocol === 'node:' ) { return { __proto__ : null , url : specifier } ; }
1122
1139
1123
1140
1124
1141
const isMain = parentURL === undefined ;
0 commit comments