Skip to content

Commit 7b0ed4b

Browse files
aduh95richardlau
authored andcommitted
module: improve support of data: URLs
Add support for loading modules using percent-encoded URLs. PR-URL: #37392 Backport-PR-URL: #37859 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Zijian Liu <[email protected]>
1 parent 2da24ac commit 7b0ed4b

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
const{
4+
decodeURIComponent,
5+
}=primordials;
36
const{ getOptionValue }=require('internal/options');
47
constmanifest=getOptionValue('--experimental-policy') ?
58
require('internal/process/policy').manifest :
@@ -28,8 +31,8 @@ async function defaultGetSource(url,{format } ={}, defaultGetSource){
2831
if(!match){
2932
thrownewERR_INVALID_URL(url);
3033
}
31-
const[,base64,body]=match;
32-
source=Buffer.from(body,base64 ? 'base64' : 'utf8');
34+
const{1:base64,2: body}=match;
35+
source=Buffer.from(decodeURIComponent(body),base64 ? 'base64' : 'utf8');
3336
}else{
3437
thrownewERR_INVALID_URL_SCHEME(['file','data']);
3538
}

‎test/es-module/test-esm-data-urls.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,9 @@ function createBase64URL(mime, body){
102102
assert.strictEqual(e.code,'ERR_INVALID_RETURN_PROPERTY_VALUE');
103103
}
104104
}
105+
{
106+
constplainESMURL='data:text/javascript,export%20default%202';
107+
constmodule=awaitimport(plainESMURL);
108+
assert.strictEqual(module.default,2);
109+
}
105110
})().then(common.mustCall());

0 commit comments

Comments
(0)