diff --git a/lib/internal/modules/esm/get_source.js b/lib/internal/modules/esm/get_source.js index 155a019802f0ba..092f61c0b00900 100644 --- a/lib/internal/modules/esm/get_source.js +++ b/lib/internal/modules/esm/get_source.js @@ -2,6 +2,7 @@ const { RegExpPrototypeExec, + decodeURIComponent, } = primordials; const { getOptionValue } = require('internal/options'); // Do not eagerly grab .manifest, it may be in TDZ @@ -32,7 +33,7 @@ async function defaultGetSource(url, { format } = {}, defaultGetSource) { throw new ERR_INVALID_URL(url); } const { 1: base64, 2: body } = match; - source = Buffer.from(body, base64 ? 'base64' : 'utf8'); + source = Buffer.from(decodeURIComponent(body), base64 ? 'base64' : 'utf8'); } else { throw new ERR_INVALID_URL_SCHEME(['file', 'data']); } diff --git a/test/es-module/test-esm-data-urls.js b/test/es-module/test-esm-data-urls.js index 61da442c9e081c..282aaf1857a0df 100644 --- a/test/es-module/test-esm-data-urls.js +++ b/test/es-module/test-esm-data-urls.js @@ -102,4 +102,9 @@ function createBase64URL(mime, body) { assert.strictEqual(e.code, 'ERR_INVALID_RETURN_PROPERTY_VALUE'); } } + { + const plainESMURL = 'data:text/javascript,export%20default%202'; + const module = await import(plainESMURL); + assert.strictEqual(module.default, 2); + } })().then(common.mustCall());