From d90dca6620760f0bd9a7889e906273a60362d76d Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 10 Oct 2017 20:44:00 +0200 Subject: [PATCH] module: allow loading files with % in the name PR-URL: https://github.com/nodejs/node/pull/16128 Reviewed-By: Refael Ackermann Reviewed-By: Anna Henningsen Reviewed-By: Bradley Meck Reviewed-By: James M Snell Reviewed-By: Jeremiah Senkpiel --- lib/internal/url.js | 6 ++++++ test/es-module/test-esm-double-encoding-native%20.js | 7 +++++++ test/es-module/test-esm-double-encoding.mjs | 6 ++++++ 3 files changed, 19 insertions(+) create mode 100644 test/es-module/test-esm-double-encoding-native%20.js create mode 100644 test/es-module/test-esm-double-encoding.mjs diff --git a/lib/internal/url.js b/lib/internal/url.js index 18594a86fd1..044d8b96a79 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -1377,8 +1377,14 @@ function getPathFromURL(path) { return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path); } +// We percent-encode % character when converting from file path to URL, +// as this is the only character that won't be percent encoded by +// default URL percent encoding when pathname is set. +const percentRegEx = /%/g; function getURLFromFilePath(filepath) { const tmp = new URL('file://'); + if (filepath.includes('%')) + filepath = filepath.replace(percentRegEx, '%25'); tmp.pathname = filepath; return tmp; } diff --git a/test/es-module/test-esm-double-encoding-native%20.js b/test/es-module/test-esm-double-encoding-native%20.js new file mode 100644 index 00000000000..8780cf774d2 --- /dev/null +++ b/test/es-module/test-esm-double-encoding-native%20.js @@ -0,0 +1,7 @@ +'use strict'; +require('../common'); + +// Trivial test to assert we can load files with `%` in their pathname. +// Imported by `test-esm-double-encoding.mjs`. + +module.exports = 42; diff --git a/test/es-module/test-esm-double-encoding.mjs b/test/es-module/test-esm-double-encoding.mjs new file mode 100644 index 00000000000..4af0c33ad8d --- /dev/null +++ b/test/es-module/test-esm-double-encoding.mjs @@ -0,0 +1,6 @@ +// Flags: --experimental-modules +import '../common'; + +// Assert we can import files with `%` in their pathname. + +import './test-esm-double-encoding-native%2520.js';