@@ -29,37 +29,42 @@ const {
29
29
30
30
const DATA_URL_PATTERN = / ^ [ ^ / ] + \/ [ ^ , ; ] + (?: [ ^ , ] * ?) ( ; b a s e 6 4 ) ? , ( [ \s \S ] * ) $ / ;
31
31
32
+ /**
33
+ * @param {URL } url URL to the module
34
+ * @param {ESModuleContext } context used to decorate error messages
35
+ * @returns {{ responseURL: string, source: string | BufferView } }
36
+ */
32
37
async function getSource ( url , context ) {
33
- const parsed = new URL ( url ) ;
34
- let responseURL = url ;
38
+ const { protocol , href } = url ;
39
+ let responseURL = href ;
35
40
let source ;
36
- if ( parsed . protocol === 'file:' ) {
41
+ if ( protocol === 'file:' ) {
37
42
const { readFile : readFileAsync } = require ( 'internal/fs/promises' ) . exports ;
38
- source = await readFileAsync ( parsed ) ;
39
- } else if ( parsed . protocol === 'data:' ) {
40
- const match = RegExpPrototypeExec ( DATA_URL_PATTERN , parsed . pathname ) ;
43
+ source = await readFileAsync ( url ) ;
44
+ } else if ( protocol === 'data:' ) {
45
+ const match = RegExpPrototypeExec ( DATA_URL_PATTERN , url . pathname ) ;
41
46
if ( ! match ) {
42
- throw new ERR_INVALID_URL ( url ) ;
47
+ throw new ERR_INVALID_URL ( responseURL ) ;
43
48
}
44
49
const { 1 : base64 , 2 : body } = match ;
45
50
source = BufferFrom ( decodeURIComponent ( body ) , base64 ? 'base64' : 'utf8' ) ;
46
51
} else if ( experimentalNetworkImports && (
47
- parsed . protocol === 'https:' ||
48
- parsed . protocol === 'http:'
52
+ protocol === 'https:' ||
53
+ protocol === 'http:'
49
54
) ) {
50
55
const { fetchModule } = require ( 'internal/modules/esm/fetch_module' ) ;
51
- const res = await fetchModule ( parsed , context ) ;
56
+ const res = await fetchModule ( url , context ) ;
52
57
source = await res . body ;
53
58
responseURL = res . resolvedHREF ;
54
59
} else {
55
60
const supportedSchemes = [ 'file' , 'data' ] ;
56
61
if ( experimentalNetworkImports ) {
57
62
ArrayPrototypePush ( supportedSchemes , 'http' , 'https' ) ;
58
63
}
59
- throw new ERR_UNSUPPORTED_ESM_URL_SCHEME ( parsed , supportedSchemes ) ;
64
+ throw new ERR_UNSUPPORTED_ESM_URL_SCHEME ( url , supportedSchemes ) ;
60
65
}
61
66
if ( policy ?. manifest ) {
62
- policy . manifest . assertIntegrity ( parsed , source ) ;
67
+ policy . manifest . assertIntegrity ( href , source ) ;
63
68
}
64
69
return { __proto__ : null , responseURL, source } ;
65
70
}
@@ -93,7 +98,7 @@ async function defaultLoad(url, context = kEmptyObject) {
93
98
) {
94
99
source = null ;
95
100
} else if ( source == null ) {
96
- ( { responseURL, source } = await getSource ( url , context ) ) ;
101
+ ( { responseURL, source } = await getSource ( urlInstance , context ) ) ;
97
102
}
98
103
99
104
return {
0 commit comments