From bb2d85f23ee7da0a488bb71507b12cb2ca043c18 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 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 | 3 ++- test/es-module/test-esm-data-urls.js | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) 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());