From 98259dc527b859751984f35638b96c18924e7511 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 16 Feb 2021 14:22:18 +0100 Subject: [PATCH] module: improve support of data: URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add support for loading modules using percent-encoded URLs. PR-URL: https://github.com/nodejs/node/pull/37392 Backport-PR-URL: https://github.com/nodejs/node/pull/39669 Reviewed-By: Michaƫl Zasso Reviewed-By: Bradley Farias Reviewed-By: Myles Borins Reviewed-By: Jan Krems Reviewed-By: Guy Bedford Reviewed-By: Luigi Pinca Reviewed-By: Zijian Liu --- lib/internal/modules/esm/get_source.js | 5 +++-- test/es-module/test-esm-data-urls.js | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/esm/get_source.js b/lib/internal/modules/esm/get_source.js index b2cf0c3bd28aa2..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 @@ -31,8 +32,8 @@ async function defaultGetSource(url, { format } = {}, defaultGetSource) { if (!match) { throw new ERR_INVALID_URL(url); } - const [ , base64, body ] = match; - source = Buffer.from(body, base64 ? 'base64' : 'utf8'); + const { 1: base64, 2: body } = match; + 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 eb2b65e9fb224a..ba8bd4c95746c0 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_MODULE_SPECIFIER'); } } + { + const plainESMURL = 'data:text/javascript,export%20default%202'; + const module = await import(plainESMURL); + assert.strictEqual(module.default, 2); + } })().then(common.mustCall());