Skip to content

Commit 4ec2d92

Browse files
anonrigRafaelGSS
authored andcommitted
module: reduce url invocations in esm/load.js
PR-URL: #48337 Refs: nodejs/performance#92 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent e10a4cd commit 4ec2d92

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

‎lib/internal/modules/esm/load.js‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,42 @@ const {
2929

3030
constDATA_URL_PATTERN=/^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/;
3131

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+
*/
3237
asyncfunctiongetSource(url,context){
33-
constparsed=newURL(url);
34-
letresponseURL=url;
38+
const{ protocol, href }=url;
39+
letresponseURL=href;
3540
letsource;
36-
if(parsed.protocol==='file:'){
41+
if(protocol==='file:'){
3742
const{readFile: readFileAsync}=require('internal/fs/promises').exports;
38-
source=awaitreadFileAsync(parsed);
39-
}elseif(parsed.protocol==='data:'){
40-
constmatch=RegExpPrototypeExec(DATA_URL_PATTERN,parsed.pathname);
43+
source=awaitreadFileAsync(url);
44+
}elseif(protocol==='data:'){
45+
constmatch=RegExpPrototypeExec(DATA_URL_PATTERN,url.pathname);
4146
if(!match){
42-
thrownewERR_INVALID_URL(url);
47+
thrownewERR_INVALID_URL(responseURL);
4348
}
4449
const{1: base64,2: body}=match;
4550
source=BufferFrom(decodeURIComponent(body),base64 ? 'base64' : 'utf8');
4651
}elseif(experimentalNetworkImports&&(
47-
parsed.protocol==='https:'||
48-
parsed.protocol==='http:'
52+
protocol==='https:'||
53+
protocol==='http:'
4954
)){
5055
const{ fetchModule }=require('internal/modules/esm/fetch_module');
51-
constres=awaitfetchModule(parsed,context);
56+
constres=awaitfetchModule(url,context);
5257
source=awaitres.body;
5358
responseURL=res.resolvedHREF;
5459
}else{
5560
constsupportedSchemes=['file','data'];
5661
if(experimentalNetworkImports){
5762
ArrayPrototypePush(supportedSchemes,'http','https');
5863
}
59-
thrownewERR_UNSUPPORTED_ESM_URL_SCHEME(parsed,supportedSchemes);
64+
thrownewERR_UNSUPPORTED_ESM_URL_SCHEME(url,supportedSchemes);
6065
}
6166
if(policy?.manifest){
62-
policy.manifest.assertIntegrity(parsed,source);
67+
policy.manifest.assertIntegrity(href,source);
6368
}
6469
return{__proto__: null, responseURL, source };
6570
}
@@ -93,7 +98,7 @@ async function defaultLoad(url, context = kEmptyObject) {
9398
){
9499
source=null;
95100
}elseif(source==null){
96-
({ responseURL, source }=awaitgetSource(url,context));
101+
({ responseURL, source }=awaitgetSource(urlInstance,context));
97102
}
98103

99104
return{

0 commit comments

Comments
 (0)