module: allow loading files with % in the name

PR-URL: https://github.com/nodejs/node/pull/16128
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Bradley Meck <bradley.meck@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
This commit is contained in:
Guy Bedford 2017-10-10 20:44:00 +02:00 committed by Anna Henningsen
parent 2b0145d47c
commit d90dca6620
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
3 changed files with 19 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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';