Skip to content

Commit a1ed78c

Browse files
aduh95targos
authored andcommitted
module: improve support of data: URLs
Add support for loading modules using percent-encoded URLs. PR-URL: #37392 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 856d20b commit a1ed78c

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/internal/modules/esm/get_source.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
RegExpPrototypeExec,
5+
decodeURIComponent,
56
} = primordials;
67
const { getOptionValue } = require('internal/options');
78
// Do not eagerly grab .manifest, it may be in TDZ
@@ -32,7 +33,7 @@ async function defaultGetSource(url, { format } = {}, defaultGetSource) {
3233
throw new ERR_INVALID_URL(url);
3334
}
3435
const { 1: base64, 2: body } = match;
35-
source = Buffer.from(body, base64 ? 'base64' : 'utf8');
36+
source = Buffer.from(decodeURIComponent(body), base64 ? 'base64' : 'utf8');
3637
} else {
3738
throw new ERR_INVALID_URL_SCHEME(['file', 'data']);
3839
}

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)