Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7b0ed4b

Browse files
aduh95richardlau
authored andcommittedMar 29, 2021
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 <targos@protonmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
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

+5-2
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
const manifest = getOptionValue('--experimental-policy') ?
58
require('internal/process/policy').manifest :
@@ -28,8 +31,8 @@ async function defaultGetSource(url, { format } = {}, defaultGetSource) {
2831
if (!match) {
2932
throw new ERR_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
throw new ERR_INVALID_URL_SCHEME(['file', 'data']);
3538
}

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

+5
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+
const plainESMURL = 'data:text/javascript,export%20default%202';
107+
const module = await import(plainESMURL);
108+
assert.strictEqual(module.default, 2);
109+
}
105110
})().then(common.mustCall());

0 commit comments

Comments
 (0)
Please sign in to comment.