Skip to content

Commit 86082a4

Browse files
guybedfordtargos
authored andcommitted
module: allow loading files with % in the name
PR-URL: #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>
1 parent f753aed commit 86082a4

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

lib/internal/url.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,8 +1378,14 @@ function getPathFromURL(path) {
13781378
return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);
13791379
}
13801380

1381+
// We percent-encode % character when converting from file path to URL,
1382+
// as this is the only character that won't be percent encoded by
1383+
// default URL percent encoding when pathname is set.
1384+
const percentRegEx = /%/g;
13811385
function getURLFromFilePath(filepath) {
13821386
const tmp = new URL('file://');
1387+
if (filepath.includes('%'))
1388+
filepath = filepath.replace(percentRegEx, '%25');
13831389
tmp.pathname = filepath;
13841390
return tmp;
13851391
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
require('../common');
3+
4+
// Trivial test to assert we can load files with `%` in their pathname.
5+
// Imported by `test-esm-double-encoding.mjs`.
6+
7+
module.exports = 42;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Flags: --experimental-modules
2+
import '../common';
3+
4+
// Assert we can import files with `%` in their pathname.
5+
6+
import './test-esm-double-encoding-native%2520.js';

0 commit comments

Comments
 (0)