@@ -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
@@ -884,22 +885,37 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) {
884
885
* @param {boolean } preserveSymlinks - Whether to preserve symlinks in the resolved URL.
885
886
*/
886
887
function 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:' ;
889
896
// Order swapped from spec for minor perf gain.
890
897
// Ok since relative URLs cannot parse as URLs.
891
898
let resolved ;
892
899
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 ] === '#' ) {
895
908
resolved = packageImportsResolve ( specifier , base , conditions ) ;
896
909
} else {
897
910
try {
898
911
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 ;
902
917
}
918
+ resolved = packageResolve ( specifier , base , conditions ) ;
903
919
}
904
920
}
905
921
if ( resolved . protocol !== 'file:' ) {
@@ -1073,7 +1089,7 @@ function defaultResolve(specifier, context = {}) {
1073
1089
}
1074
1090
}
1075
1091
1076
- let parsed ;
1092
+ let parsed , protocol ;
1077
1093
try {
1078
1094
if ( shouldBeTreatedAsRelativeOrAbsolutePath ( specifier ) ) {
1079
1095
parsed = new URL ( specifier , parsedParentURL ) ;
@@ -1082,7 +1098,7 @@ function defaultResolve(specifier, context = {}) {
1082
1098
}
1083
1099
1084
1100
// Avoid accessing the `protocol` property due to the lazy getters.
1085
- const protocol = parsed . protocol ;
1101
+ protocol = parsed . protocol ;
1086
1102
if ( protocol === 'data:' ||
1087
1103
( experimentalNetworkImports &&
1088
1104
(
@@ -1109,7 +1125,8 @@ function defaultResolve(specifier, context = {}) {
1109
1125
if ( maybeReturn ) { return maybeReturn ; }
1110
1126
1111
1127
// This must come after checkIfDisallowedImport
1112
- if ( parsed && parsed . protocol === 'node:' ) { return { __proto__ : null , url : specifier } ; }
1128
+ protocol ??= parsed ?. protocol ;
1129
+ if ( protocol === 'node:' ) { return { __proto__ : null , url : specifier } ; }
1113
1130
1114
1131
1115
1132
const isMain = parentURL === undefined ;
0 commit comments