@@ -130,7 +130,7 @@ function emitTrailingSlashPatternDeprecation(match, pjsonUrl, isExports, base) {
130
130
* @returns
131
131
*/
132
132
function emitLegacyIndexDeprecation ( url , packageJSONUrl , base , main ) {
133
- const format = defaultGetFormat ( url ) ;
133
+ const format = defaultGetFormatWithoutErrors ( url ) ;
134
134
if ( format !== 'module' )
135
135
return ;
136
136
const path = fileURLToPath ( url ) ;
@@ -474,22 +474,6 @@ const patternRegEx = /\*/g;
474
474
function resolvePackageTargetString (
475
475
target , subpath , match , packageJSONUrl , base , pattern , internal , conditions ) {
476
476
477
- const composeResult = ( resolved ) => {
478
- let format ;
479
- try {
480
- format = getPackageType ( resolved ) ;
481
- } catch ( err ) {
482
- if ( err . code === 'ERR_INVALID_FILE_URL_PATH' ) {
483
- const invalidModuleErr = new ERR_INVALID_MODULE_SPECIFIER (
484
- resolved , 'must not include encoded "/" or "\\" characters' , base ) ;
485
- invalidModuleErr . cause = err ;
486
- throw invalidModuleErr ;
487
- }
488
- throw err ;
489
- }
490
- return { resolved, ...( format !== 'none' ) && { format } } ;
491
- } ;
492
-
493
477
if ( subpath !== '' && ! pattern && target [ target . length - 1 ] !== '/' )
494
478
throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
495
479
@@ -522,18 +506,22 @@ function resolvePackageTargetString(
522
506
if ( ! StringPrototypeStartsWith ( resolvedPath , packagePath ) )
523
507
throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
524
508
525
- if ( subpath === '' ) return composeResult ( resolved ) ;
509
+ if ( subpath === '' ) return resolved ;
526
510
527
511
if ( RegExpPrototypeTest ( invalidSegmentRegEx , subpath ) )
528
512
throwInvalidSubpath ( match + subpath , packageJSONUrl , internal , base ) ;
529
513
530
514
if ( pattern ) {
531
- return composeResult ( new URL ( RegExpPrototypeSymbolReplace ( patternRegEx ,
532
- resolved . href ,
533
- ( ) => subpath ) ) ) ;
515
+ return new URL (
516
+ RegExpPrototypeSymbolReplace (
517
+ patternRegEx ,
518
+ resolved . href ,
519
+ ( ) => subpath
520
+ )
521
+ ) ;
534
522
}
535
523
536
- return composeResult ( new URL ( subpath , resolved ) ) ;
524
+ return new URL ( subpath , resolved ) ;
537
525
}
538
526
539
527
/**
@@ -659,15 +647,15 @@ function packageExportsResolve(
659
647
! StringPrototypeIncludes ( packageSubpath , '*' ) &&
660
648
! StringPrototypeEndsWith ( packageSubpath , '/' ) ) {
661
649
const target = exports [ packageSubpath ] ;
662
- const resolveResult = resolvePackageTarget (
650
+ const resolved = resolvePackageTarget (
663
651
packageJSONUrl , target , '' , packageSubpath , base , false , false , conditions
664
652
) ;
665
653
666
- if ( resolveResult == null ) {
654
+ if ( resolved == null ) {
667
655
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
668
656
}
669
657
670
- return { ... resolveResult , exact : true } ;
658
+ return { resolved , exact : true } ;
671
659
}
672
660
673
661
let bestMatch = '' ;
@@ -703,7 +691,7 @@ function packageExportsResolve(
703
691
if ( bestMatch ) {
704
692
const target = exports [ bestMatch ] ;
705
693
const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
706
- const resolveResult = resolvePackageTarget (
694
+ const resolved = resolvePackageTarget (
707
695
packageJSONUrl ,
708
696
target ,
709
697
bestMatchSubpath ,
@@ -713,15 +701,15 @@ function packageExportsResolve(
713
701
false ,
714
702
conditions ) ;
715
703
716
- if ( resolveResult == null ) {
704
+ if ( resolved == null ) {
717
705
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
718
706
}
719
707
720
708
if ( ! pattern ) {
721
709
emitFolderMapDeprecation ( bestMatch , packageJSONUrl , true , base ) ;
722
710
}
723
711
724
- return { ... resolveResult , exact : pattern } ;
712
+ return { resolved , exact : pattern } ;
725
713
}
726
714
727
715
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
@@ -761,11 +749,11 @@ function packageImportsResolve(name, base, conditions) {
761
749
if ( ObjectPrototypeHasOwnProperty ( imports , name ) &&
762
750
! StringPrototypeIncludes ( name , '*' ) &&
763
751
! StringPrototypeEndsWith ( name , '/' ) ) {
764
- const resolveResult = resolvePackageTarget (
752
+ const resolved = resolvePackageTarget (
765
753
packageJSONUrl , imports [ name ] , '' , name , base , false , true , conditions
766
754
) ;
767
- if ( resolveResult != null ) {
768
- return { resolved : resolveResult . resolved , exact : true } ;
755
+ if ( resolved != null ) {
756
+ return { resolved, exact : true } ;
769
757
}
770
758
} else {
771
759
let bestMatch = '' ;
@@ -798,15 +786,15 @@ function packageImportsResolve(name, base, conditions) {
798
786
if ( bestMatch ) {
799
787
const target = imports [ bestMatch ] ;
800
788
const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
801
- const resolveResult = resolvePackageTarget (
789
+ const resolved = resolvePackageTarget (
802
790
packageJSONUrl , target ,
803
791
bestMatchSubpath , bestMatch ,
804
792
base , pattern , true ,
805
793
conditions ) ;
806
- if ( resolveResult !== null ) {
794
+ if ( resolved !== null ) {
807
795
if ( ! pattern )
808
796
emitFolderMapDeprecation ( bestMatch , packageJSONUrl , false , base ) ;
809
- return { resolved : resolveResult . resolved , exact : pattern } ;
797
+ return { resolved, exact : pattern } ;
810
798
}
811
799
}
812
800
}
@@ -883,7 +871,8 @@ function packageResolve(specifier, base, conditions) {
883
871
if ( packageConfig . name === packageName &&
884
872
packageConfig . exports !== undefined && packageConfig . exports !== null ) {
885
873
return packageExportsResolve (
886
- packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
874
+ packageJSONUrl , packageSubpath , packageConfig , base , conditions
875
+ ) . resolved ;
887
876
}
888
877
}
889
878
@@ -907,24 +896,19 @@ function packageResolve(specifier, base, conditions) {
907
896
const packageConfig = getPackageConfig ( packageJSONPath , specifier , base ) ;
908
897
if ( packageConfig . exports !== undefined && packageConfig . exports !== null ) {
909
898
return packageExportsResolve (
910
- packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
899
+ packageJSONUrl , packageSubpath , packageConfig , base , conditions
900
+ ) . resolved ;
911
901
}
912
902
913
903
if ( packageSubpath === '.' ) {
914
- return {
915
- resolved : legacyMainResolve (
916
- packageJSONUrl ,
917
- packageConfig ,
918
- base ) ,
919
- ...( packageConfig . type !== 'none' ) && { format : packageConfig . type }
920
- } ;
904
+ return legacyMainResolve (
905
+ packageJSONUrl ,
906
+ packageConfig ,
907
+ base
908
+ ) ;
921
909
}
922
910
923
- return {
924
- resolved : new URL ( packageSubpath , packageJSONUrl ) ,
925
- ...( packageConfig . type !== 'none' ) && { format : packageConfig . type }
926
- } ;
927
-
911
+ return new URL ( packageSubpath , packageJSONUrl ) ;
928
912
// Cross-platform root check.
929
913
} while ( packageJSONPath . length !== lastPath . length ) ;
930
914
@@ -967,7 +951,6 @@ function moduleResolve(specifier, base, conditions) {
967
951
// Order swapped from spec for minor perf gain.
968
952
// Ok since relative URLs cannot parse as URLs.
969
953
let resolved ;
970
- let format ;
971
954
if ( shouldBeTreatedAsRelativeOrAbsolutePath ( specifier ) ) {
972
955
resolved = new URL ( specifier , base ) ;
973
956
} else if ( specifier [ 0 ] === '#' ) {
@@ -976,13 +959,10 @@ function moduleResolve(specifier, base, conditions) {
976
959
try {
977
960
resolved = new URL ( specifier ) ;
978
961
} catch {
979
- ( { resolved, format } = packageResolve ( specifier , base , conditions ) ) ;
962
+ resolved = packageResolve ( specifier , base , conditions ) ;
980
963
}
981
964
}
982
- return {
983
- url : finalizeResolution ( resolved , base ) ,
984
- ...( format != null ) && { format }
985
- } ;
965
+ return finalizeResolution ( resolved , base ) ;
986
966
}
987
967
988
968
/**
@@ -1031,6 +1011,13 @@ function resolveAsCommonJS(specifier, parentURL) {
1031
1011
}
1032
1012
}
1033
1013
1014
+ function throwIfUnsupportedURLProtocol ( url ) {
1015
+ if ( url . protocol !== 'file:' && url . protocol !== 'data:' &&
1016
+ url . protocol !== 'node:' ) {
1017
+ throw new ERR_UNSUPPORTED_ESM_URL_SCHEME ( url ) ;
1018
+ }
1019
+ }
1020
+
1034
1021
function defaultResolve ( specifier , context = { } , defaultResolveUnused ) {
1035
1022
let { parentURL, conditions } = context ;
1036
1023
if ( parentURL && policy ?. manifest ) {
@@ -1093,14 +1080,8 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
1093
1080
1094
1081
conditions = getConditionsSet ( conditions ) ;
1095
1082
let url ;
1096
- let format ;
1097
1083
try {
1098
- ( { url, format } =
1099
- moduleResolve (
1100
- specifier ,
1101
- parentURL ,
1102
- conditions
1103
- ) ) ;
1084
+ url = moduleResolve ( specifier , parentURL , conditions ) ;
1104
1085
} catch ( error ) {
1105
1086
// Try to give the user a hint of what would have been the
1106
1087
// resolved CommonJS module
@@ -1136,9 +1117,11 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
1136
1117
url . hash = old . hash ;
1137
1118
}
1138
1119
1120
+ throwIfUnsupportedURLProtocol ( url ) ;
1121
+
1139
1122
return {
1140
1123
url : `${ url } ` ,
1141
- ... ( format != null ) && { format }
1124
+ format : defaultGetFormatWithoutErrors ( url ) ,
1142
1125
} ;
1143
1126
}
1144
1127
@@ -1153,4 +1136,6 @@ module.exports = {
1153
1136
} ;
1154
1137
1155
1138
// cycle
1156
- const { defaultGetFormat } = require ( 'internal/modules/esm/get_format' ) ;
1139
+ const {
1140
+ defaultGetFormatWithoutErrors,
1141
+ } = require ( 'internal/modules/esm/get_format' ) ;
0 commit comments