From 2da24ac302167ec346965d92dfe5dea3ee5c9424 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 16 Feb 2021 14:31:29 +0100 Subject: [PATCH 1/2] lib: add URI handling functions to primordials PR-URL: https://github.com/nodejs/node/pull/37394 Backport-PR-URL: https://github.com/nodejs/node/pull/37859 Reviewed-By: Zijian Liu Reviewed-By: James M Snell --- lib/internal/per_context/primordials.js | 10 ++++++++++ lib/internal/url.js | 1 + lib/querystring.js | 1 + lib/url.js | 1 + 4 files changed, 13 insertions(+) diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 09bea0fc7fc523..6a960f36feda45 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -106,6 +106,16 @@ primordials.SafePromise = makeSafe( class SafePromise extends Promise {} ); +// Create copies of URI handling functions +[ + decodeURI, + decodeURIComponent, + encodeURI, + encodeURIComponent, +].forEach((fn) => { + primordials[fn.name] = fn; +}); + // Create copies of the namespace objects [ 'JSON', diff --git a/lib/internal/url.js b/lib/internal/url.js index 16f5043df9c641..2c0b1f518a7091 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -14,6 +14,7 @@ const { Symbol, SymbolIterator, SymbolToStringTag, + decodeURIComponent, } = primordials; const { inspect } = require('internal/util/inspect'); diff --git a/lib/querystring.js b/lib/querystring.js index 4d196633dcf2c7..7bc6684f400174 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -29,6 +29,7 @@ const { MathAbs, ObjectCreate, ObjectKeys, + decodeURIComponent, } = primordials; const { Buffer } = require('buffer'); diff --git a/lib/url.js b/lib/url.js index 735390dfd2e59b..f7dbebea5ac125 100644 --- a/lib/url.js +++ b/lib/url.js @@ -25,6 +25,7 @@ const { ObjectCreate, ObjectKeys, SafeSet, + decodeURIComponent, } = primordials; const { toASCII } = require('internal/idna'); From 7b0ed4ba9229f9f6bd2d877879b6f03f8d5dd4cc Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 16 Feb 2021 14:22:18 +0100 Subject: [PATCH 2/2] 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/37859 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 | 7 +++++-- test/es-module/test-esm-data-urls.js | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/esm/get_source.js b/lib/internal/modules/esm/get_source.js index 5e2cc3e09e7687..2f7b74fb74406c 100644 --- a/lib/internal/modules/esm/get_source.js +++ b/lib/internal/modules/esm/get_source.js @@ -1,5 +1,8 @@ 'use strict'; +const { + decodeURIComponent, +} = primordials; const { getOptionValue } = require('internal/options'); const manifest = getOptionValue('--experimental-policy') ? require('internal/process/policy').manifest : @@ -28,8 +31,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 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());