@@ -1026,6 +1026,35 @@ function throwIfInvalidParentURL(parentURL) {
10261026 }
10271027}
10281028
1029+ /**
1030+ * Process policy
1031+ */
1032+ function processPolicy ( specifier , context ) {
1033+ const { parentURL, conditions } = context ;
1034+ const redirects = policy . manifest . getDependencyMapper ( parentURL ) ;
1035+ if ( redirects ) {
1036+ const { resolve, reaction } = redirects ;
1037+ const destination = resolve ( specifier , new SafeSet ( conditions ) ) ;
1038+ let missing = true ;
1039+ if ( destination === true ) {
1040+ missing = false ;
1041+ } else if ( destination ) {
1042+ const href = destination . href ;
1043+ return { __proto__ : null , url : href } ;
1044+ }
1045+ if ( missing ) {
1046+ // Prevent network requests from firing if resolution would be banned.
1047+ // Network requests can extract data by doing things like putting
1048+ // secrets in query params
1049+ reaction ( new ERR_MANIFEST_DEPENDENCY_MISSING (
1050+ parentURL ,
1051+ specifier ,
1052+ ArrayPrototypeJoin ( [ ...conditions ] , ', ' ) ) ,
1053+ ) ;
1054+ }
1055+ }
1056+ }
1057+
10291058/**
10301059 * Resolves the given specifier using the provided context, which includes the parent URL and conditions.
10311060 * Throws an error if the parent URL is invalid or if the resolution is disallowed by the policy manifest.
@@ -1037,31 +1066,8 @@ function throwIfInvalidParentURL(parentURL) {
10371066 */
10381067function defaultResolve ( specifier , context = { } ) {
10391068 let { parentURL, conditions } = context ;
1069+ const { importMap } = context ;
10401070 throwIfInvalidParentURL ( parentURL ) ;
1041- if ( parentURL && policy ?. manifest ) {
1042- const redirects = policy . manifest . getDependencyMapper ( parentURL ) ;
1043- if ( redirects ) {
1044- const { resolve, reaction } = redirects ;
1045- const destination = resolve ( specifier , new SafeSet ( conditions ) ) ;
1046- let missing = true ;
1047- if ( destination === true ) {
1048- missing = false ;
1049- } else if ( destination ) {
1050- const href = destination . href ;
1051- return { __proto__ : null , url : href } ;
1052- }
1053- if ( missing ) {
1054- // Prevent network requests from firing if resolution would be banned.
1055- // Network requests can extract data by doing things like putting
1056- // secrets in query params
1057- reaction ( new ERR_MANIFEST_DEPENDENCY_MISSING (
1058- parentURL ,
1059- specifier ,
1060- ArrayPrototypeJoin ( [ ...conditions ] , ', ' ) ) ,
1061- ) ;
1062- }
1063- }
1064- }
10651071
10661072 let parsedParentURL ;
10671073 if ( parentURL ) {
@@ -1079,8 +1085,19 @@ function defaultResolve(specifier, context = {}) {
10791085 } else {
10801086 parsed = new URL ( specifier ) ;
10811087 }
1088+ } catch {
1089+ // Ignore exception
1090+ }
10821091
1083- // Avoid accessing the `protocol` property due to the lazy getters.
1092+ // Import maps are processed before policies and data/http handling
1093+ // so policies apply to the result of any mapping
1094+ if ( importMap ) {
1095+ // Intentionally mutating here as we don't think it is a problem
1096+ parsed = specifier = importMap . resolve ( parsed || specifier , parsedParentURL ) ;
1097+ }
1098+
1099+ // Avoid accessing the `protocol` property due to the lazy getters.
1100+ if ( parsed ) {
10841101 const protocol = parsed . protocol ;
10851102 if ( protocol === 'data:' ||
10861103 ( experimentalNetworkImports &&
@@ -1092,8 +1109,13 @@ function defaultResolve(specifier, context = {}) {
10921109 ) {
10931110 return { __proto__ : null , url : parsed . href } ;
10941111 }
1095- } catch {
1096- // Ignore exception
1112+ }
1113+
1114+ if ( parentURL && policy ?. manifest ) {
1115+ const policyResolution = processPolicy ( specifier , context ) ;
1116+ if ( policyResolution ) {
1117+ return policyResolution ;
1118+ }
10971119 }
10981120
10991121 // There are multiple deep branches that can either throw or return; instead
0 commit comments