summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/fs.js100
-rw-r--r--lib/internal/fs/promises.js46
-rw-r--r--lib/internal/fs/streams.js6
-rw-r--r--lib/internal/fs/watchers.js6
-rw-r--r--lib/internal/modules/cjs/loader.js8
-rw-r--r--lib/internal/modules/esm/default_resolve.js8
-rw-r--r--lib/internal/modules/esm/translators.js6
-rw-r--r--lib/internal/process/esm_loader.js6
-rw-r--r--lib/internal/url.js53
-rw-r--r--lib/url.js8
10 files changed, 138 insertions, 109 deletions
diff --git a/lib/fs.js b/lib/fs.js
index e1fb2c022f..3302cfe0bf 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -54,7 +54,7 @@ const {
const { FSReqCallback, statValues } = binding;
const internalFS = require('internal/fs/utils');
-const { getPathFromURL } = require('internal/url');
+const { toPathIfFileURL } = require('internal/url');
const internalUtil = require('internal/util');
const {
copyObject,
@@ -177,7 +177,7 @@ function access(path, mode, callback) {
mode = F_OK;
}
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
mode = mode | 0;
@@ -187,7 +187,7 @@ function access(path, mode, callback) {
}
function accessSync(path, mode) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
if (mode === undefined)
@@ -297,7 +297,7 @@ function readFile(path, options, callback) {
return;
}
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
binding.open(pathModule.toNamespacedPath(path),
stringToFlags(options.flag || 'r'),
@@ -409,7 +409,7 @@ function closeSync(fd) {
}
function open(path, flags, mode, callback) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const flagsNumber = stringToFlags(flags);
if (arguments.length < 4) {
@@ -431,7 +431,7 @@ function open(path, flags, mode, callback) {
function openSync(path, flags, mode) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const flagsNumber = stringToFlags(flags);
mode = validateMode(mode, 'mode', 0o666);
@@ -587,9 +587,9 @@ function writeSync(fd, buffer, offset, length, position) {
function rename(oldPath, newPath, callback) {
callback = makeCallback(callback);
- oldPath = getPathFromURL(oldPath);
+ oldPath = toPathIfFileURL(oldPath);
validatePath(oldPath, 'oldPath');
- newPath = getPathFromURL(newPath);
+ newPath = toPathIfFileURL(newPath);
validatePath(newPath, 'newPath');
const req = new FSReqCallback();
req.oncomplete = callback;
@@ -599,9 +599,9 @@ function rename(oldPath, newPath, callback) {
}
function renameSync(oldPath, newPath) {
- oldPath = getPathFromURL(oldPath);
+ oldPath = toPathIfFileURL(oldPath);
validatePath(oldPath, 'oldPath');
- newPath = getPathFromURL(newPath);
+ newPath = toPathIfFileURL(newPath);
validatePath(newPath, 'newPath');
const ctx = { path: oldPath, dest: newPath };
binding.rename(pathModule.toNamespacedPath(oldPath),
@@ -680,7 +680,7 @@ function ftruncateSync(fd, len = 0) {
function rmdir(path, callback) {
callback = makeCallback(callback);
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const req = new FSReqCallback();
req.oncomplete = callback;
@@ -688,7 +688,7 @@ function rmdir(path, callback) {
}
function rmdirSync(path) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const ctx = { path };
binding.rmdir(pathModule.toNamespacedPath(path), undefined, ctx);
@@ -735,7 +735,7 @@ function mkdir(path, options, callback) {
mode = 0o777
} = options || {};
callback = makeCallback(callback);
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
if (typeof recursive !== 'boolean')
@@ -751,7 +751,7 @@ function mkdirSync(path, options) {
if (typeof options === 'number' || typeof options === 'string') {
options = { mode: options };
}
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
const {
recursive = false,
mode = 0o777
@@ -771,7 +771,7 @@ function mkdirSync(path, options) {
function readdir(path, options, callback) {
callback = makeCallback(typeof options === 'function' ? options : callback);
options = getOptions(options, {});
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const req = new FSReqCallback();
@@ -792,7 +792,7 @@ function readdir(path, options, callback) {
function readdirSync(path, options) {
options = getOptions(options, {});
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const ctx = { path };
const result = binding.readdir(pathModule.toNamespacedPath(path),
@@ -819,7 +819,7 @@ function lstat(path, options, callback) {
options = {};
}
callback = makeStatsCallback(callback);
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const req = new FSReqCallback(options.bigint);
req.oncomplete = callback;
@@ -832,7 +832,7 @@ function stat(path, options, callback) {
options = {};
}
callback = makeStatsCallback(callback);
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const req = new FSReqCallback(options.bigint);
req.oncomplete = callback;
@@ -848,7 +848,7 @@ function fstatSync(fd, options = {}) {
}
function lstatSync(path, options = {}) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const ctx = { path };
const stats = binding.lstat(pathModule.toNamespacedPath(path),
@@ -858,7 +858,7 @@ function lstatSync(path, options = {}) {
}
function statSync(path, options = {}) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const ctx = { path };
const stats = binding.stat(pathModule.toNamespacedPath(path),
@@ -870,7 +870,7 @@ function statSync(path, options = {}) {
function readlink(path, options, callback) {
callback = makeCallback(typeof options === 'function' ? options : callback);
options = getOptions(options, {});
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path, 'oldPath');
const req = new FSReqCallback();
req.oncomplete = callback;
@@ -879,7 +879,7 @@ function readlink(path, options, callback) {
function readlinkSync(path, options) {
options = getOptions(options, {});
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path, 'oldPath');
const ctx = { path };
const result = binding.readlink(pathModule.toNamespacedPath(path),
@@ -892,8 +892,8 @@ function symlink(target, path, type_, callback_) {
const type = (typeof type_ === 'string' ? type_ : null);
const callback = makeCallback(arguments[arguments.length - 1]);
- target = getPathFromURL(target);
- path = getPathFromURL(path);
+ target = toPathIfFileURL(target);
+ path = toPathIfFileURL(path);
validatePath(target, 'target');
validatePath(path);
@@ -907,8 +907,8 @@ function symlink(target, path, type_, callback_) {
function symlinkSync(target, path, type) {
type = (typeof type === 'string' ? type : null);
- target = getPathFromURL(target);
- path = getPathFromURL(path);
+ target = toPathIfFileURL(target);
+ path = toPathIfFileURL(path);
validatePath(target, 'target');
validatePath(path);
const flags = stringToSymlinkType(type);
@@ -923,8 +923,8 @@ function symlinkSync(target, path, type) {
function link(existingPath, newPath, callback) {
callback = makeCallback(callback);
- existingPath = getPathFromURL(existingPath);
- newPath = getPathFromURL(newPath);
+ existingPath = toPathIfFileURL(existingPath);
+ newPath = toPathIfFileURL(newPath);
validatePath(existingPath, 'existingPath');
validatePath(newPath, 'newPath');
@@ -937,8 +937,8 @@ function link(existingPath, newPath, callback) {
}
function linkSync(existingPath, newPath) {
- existingPath = getPathFromURL(existingPath);
- newPath = getPathFromURL(newPath);
+ existingPath = toPathIfFileURL(existingPath);
+ newPath = toPathIfFileURL(newPath);
validatePath(existingPath, 'existingPath');
validatePath(newPath, 'newPath');
@@ -952,7 +952,7 @@ function linkSync(existingPath, newPath) {
function unlink(path, callback) {
callback = makeCallback(callback);
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const req = new FSReqCallback();
req.oncomplete = callback;
@@ -960,7 +960,7 @@ function unlink(path, callback) {
}
function unlinkSync(path) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const ctx = { path };
binding.unlink(pathModule.toNamespacedPath(path), undefined, ctx);
@@ -1018,7 +1018,7 @@ function lchmodSync(path, mode) {
function chmod(path, mode, callback) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
mode = validateMode(mode, 'mode');
callback = makeCallback(callback);
@@ -1029,7 +1029,7 @@ function chmod(path, mode, callback) {
}
function chmodSync(path, mode) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
mode = validateMode(mode, 'mode');
@@ -1040,7 +1040,7 @@ function chmodSync(path, mode) {
function lchown(path, uid, gid, callback) {
callback = makeCallback(callback);
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
@@ -1050,7 +1050,7 @@ function lchown(path, uid, gid, callback) {
}
function lchownSync(path, uid, gid) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
@@ -1081,7 +1081,7 @@ function fchownSync(fd, uid, gid) {
function chown(path, uid, gid, callback) {
callback = makeCallback(callback);
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
@@ -1092,7 +1092,7 @@ function chown(path, uid, gid, callback) {
}
function chownSync(path, uid, gid) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
@@ -1103,7 +1103,7 @@ function chownSync(path, uid, gid) {
function utimes(path, atime, mtime, callback) {
callback = makeCallback(callback);
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const req = new FSReqCallback();
@@ -1115,7 +1115,7 @@ function utimes(path, atime, mtime, callback) {
}
function utimesSync(path, atime, mtime) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const ctx = { path };
binding.utimes(pathModule.toNamespacedPath(path),
@@ -1282,7 +1282,7 @@ function watch(filename, options, listener) {
const statWatchers = new Map();
function watchFile(filename, options, listener) {
- filename = getPathFromURL(filename);
+ filename = toPathIfFileURL(filename);
validatePath(filename);
filename = pathModule.resolve(filename);
let stat;
@@ -1321,7 +1321,7 @@ function watchFile(filename, options, listener) {
}
function unwatchFile(filename, listener) {
- filename = getPathFromURL(filename);
+ filename = toPathIfFileURL(filename);
validatePath(filename);
filename = pathModule.resolve(filename);
const stat = statWatchers.get(filename);
@@ -1393,7 +1393,7 @@ function realpathSync(p, options) {
options = emptyObj;
else
options = getOptions(options, emptyObj);
- p = getPathFromURL(p);
+ p = toPathIfFileURL(p);
if (typeof p !== 'string') {
p += '';
}
@@ -1525,7 +1525,7 @@ function realpathSync(p, options) {
realpathSync.native = function(path, options) {
options = getOptions(options, {});
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const ctx = { path };
const result = binding.realpath(path, options.encoding, undefined, ctx);
@@ -1540,7 +1540,7 @@ function realpath(p, options, callback) {
options = emptyObj;
else
options = getOptions(options, emptyObj);
- p = getPathFromURL(p);
+ p = toPathIfFileURL(p);
if (typeof p !== 'string') {
p += '';
}
@@ -1667,7 +1667,7 @@ function realpath(p, options, callback) {
realpath.native = function(path, options, callback) {
callback = makeCallback(callback || options);
options = getOptions(options, {});
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const req = new FSReqCallback();
req.oncomplete = callback;
@@ -1710,8 +1710,8 @@ function copyFile(src, dest, flags, callback) {
throw new ERR_INVALID_CALLBACK();
}
- src = getPathFromURL(src);
- dest = getPathFromURL(dest);
+ src = toPathIfFileURL(src);
+ dest = toPathIfFileURL(dest);
validatePath(src, 'src');
validatePath(dest, 'dest');
@@ -1725,8 +1725,8 @@ function copyFile(src, dest, flags, callback) {
function copyFileSync(src, dest, flags) {
- src = getPathFromURL(src);
- dest = getPathFromURL(dest);
+ src = toPathIfFileURL(src);
+ dest = toPathIfFileURL(dest);
validatePath(src, 'src');
validatePath(dest, 'dest');
diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js
index e0ddee4c7f..0aa26ba2f9 100644
--- a/lib/internal/fs/promises.js
+++ b/lib/internal/fs/promises.js
@@ -15,7 +15,7 @@ const {
ERR_INVALID_ARG_VALUE,
ERR_METHOD_NOT_IMPLEMENTED
} = require('internal/errors').codes;
-const { getPathFromURL } = require('internal/url');
+const { toPathIfFileURL } = require('internal/url');
const { isUint8Array } = require('internal/util/types');
const {
copyObject,
@@ -172,7 +172,7 @@ async function readFileHandle(filehandle, options) {
// All of the functions are defined as async in order to ensure that errors
// thrown cause promise rejections rather than being thrown synchronously.
async function access(path, mode = F_OK) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
mode = mode | 0;
@@ -181,8 +181,8 @@ async function access(path, mode = F_OK) {
}
async function copyFile(src, dest, flags) {
- src = getPathFromURL(src);
- dest = getPathFromURL(dest);
+ src = toPathIfFileURL(src);
+ dest = toPathIfFileURL(dest);
validatePath(src, 'src');
validatePath(dest, 'dest');
flags = flags | 0;
@@ -194,7 +194,7 @@ async function copyFile(src, dest, flags) {
// Note that unlike fs.open() which uses numeric file descriptors,
// fsPromises.open() uses the fs.FileHandle class.
async function open(path, flags, mode) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
mode = validateMode(mode, 'mode', 0o666);
return new FileHandle(
@@ -257,8 +257,8 @@ async function write(handle, buffer, offset, length, position) {
}
async function rename(oldPath, newPath) {
- oldPath = getPathFromURL(oldPath);
- newPath = getPathFromURL(newPath);
+ oldPath = toPathIfFileURL(oldPath);
+ newPath = toPathIfFileURL(newPath);
validatePath(oldPath, 'oldPath');
validatePath(newPath, 'newPath');
return binding.rename(pathModule.toNamespacedPath(oldPath),
@@ -278,7 +278,7 @@ async function ftruncate(handle, len = 0) {
}
async function rmdir(path) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
return binding.rmdir(pathModule.toNamespacedPath(path), kUsePromises);
}
@@ -301,7 +301,7 @@ async function mkdir(path, options) {
recursive = false,
mode = 0o777
} = options || {};
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
if (typeof recursive !== 'boolean')
@@ -314,7 +314,7 @@ async function mkdir(path, options) {
async function readdir(path, options) {
options = getOptions(options, {});
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const result = await binding.readdir(pathModule.toNamespacedPath(path),
options.encoding, !!options.withTypes,
@@ -326,7 +326,7 @@ async function readdir(path, options) {
async function readlink(path, options) {
options = getOptions(options, {});
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path, 'oldPath');
return binding.readlink(pathModule.toNamespacedPath(path),
options.encoding, kUsePromises);
@@ -334,8 +334,8 @@ async function readlink(path, options) {
async function symlink(target, path, type_) {
const type = (typeof type_ === 'string' ? type_ : null);
- target = getPathFromURL(target);
- path = getPathFromURL(path);
+ target = toPathIfFileURL(target);
+ path = toPathIfFileURL(path);
validatePath(target, 'target');
validatePath(path);
return binding.symlink(preprocessSymlinkDestination(target, type, path),
@@ -351,7 +351,7 @@ async function fstat(handle, options = { bigint: false }) {
}
async function lstat(path, options = { bigint: false }) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const result = await binding.lstat(pathModule.toNamespacedPath(path),
options.bigint, kUsePromises);
@@ -359,7 +359,7 @@ async function lstat(path, options = { bigint: false }) {
}
async function stat(path, options = { bigint: false }) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
const result = await binding.stat(pathModule.toNamespacedPath(path),
options.bigint, kUsePromises);
@@ -367,8 +367,8 @@ async function stat(path, options = { bigint: false }) {
}
async function link(existingPath, newPath) {
- existingPath = getPathFromURL(existingPath);
- newPath = getPathFromURL(newPath);
+ existingPath = toPathIfFileURL(existingPath);
+ newPath = toPathIfFileURL(newPath);
validatePath(existingPath, 'existingPath');
validatePath(newPath, 'newPath');
return binding.link(pathModule.toNamespacedPath(existingPath),
@@ -377,7 +377,7 @@ async function link(existingPath, newPath) {
}
async function unlink(path) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
return binding.unlink(pathModule.toNamespacedPath(path), kUsePromises);
}
@@ -389,7 +389,7 @@ async function fchmod(handle, mode) {
}
async function chmod(path, mode) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
mode = validateMode(mode, 'mode');
return binding.chmod(pathModule.toNamespacedPath(path), mode, kUsePromises);
@@ -404,7 +404,7 @@ async function lchmod(path, mode) {
}
async function lchown(path, uid, gid) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
@@ -420,7 +420,7 @@ async function fchown(handle, uid, gid) {
}
async function chown(path, uid, gid) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
@@ -429,7 +429,7 @@ async function chown(path, uid, gid) {
}
async function utimes(path, atime, mtime) {
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
return binding.utimes(pathModule.toNamespacedPath(path),
toUnixTimestamp(atime),
@@ -446,7 +446,7 @@ async function futimes(handle, atime, mtime) {
async function realpath(path, options) {
options = getOptions(options, {});
- path = getPathFromURL(path);
+ path = toPathIfFileURL(path);
validatePath(path);
return binding.realpath(path, options.encoding, kUsePromises);
}
diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js
index 502b3c1430..a2ae1c9787 100644
--- a/lib/internal/fs/streams.js
+++ b/lib/internal/fs/streams.js
@@ -16,7 +16,7 @@ const {
getOptions,
} = require('internal/fs/utils');
const { Readable, Writable } = require('stream');
-const { getPathFromURL } = require('internal/url');
+const { toPathIfFileURL } = require('internal/url');
const util = require('util');
const kMinPoolSpace = 128;
@@ -52,7 +52,7 @@ function ReadStream(path, options) {
Readable.call(this, options);
// path will be ignored when fd is specified, so it can be falsy
- this.path = getPathFromURL(path);
+ this.path = toPathIfFileURL(path);
this.fd = options.fd === undefined ? null : options.fd;
this.flags = options.flags === undefined ? 'r' : options.flags;
this.mode = options.mode === undefined ? 0o666 : options.mode;
@@ -240,7 +240,7 @@ function WriteStream(path, options) {
Writable.call(this, options);
// path will be ignored when fd is specified, so it can be falsy
- this.path = getPathFromURL(path);
+ this.path = toPathIfFileURL(path);
this.fd = options.fd === undefined ? null : options.fd;
this.flags = options.flags === undefined ? 'w' : options.flags;
this.mode = options.mode === undefined ? 0o666 : options.mode;
diff --git a/lib/internal/fs/watchers.js b/lib/internal/fs/watchers.js
index 8f8cb5249b..3db3a5cc7c 100644
--- a/lib/internal/fs/watchers.js
+++ b/lib/internal/fs/watchers.js
@@ -18,7 +18,7 @@ const {
} = require('internal/async_hooks');
const { toNamespacedPath } = require('path');
const { validateUint32 } = require('internal/validators');
-const { getPathFromURL } = require('internal/url');
+const { toPathIfFileURL } = require('internal/url');
const util = require('util');
const assert = require('assert');
@@ -71,7 +71,7 @@ StatWatcher.prototype.start = function(filename, persistent, interval) {
// the sake of backwards compatibility
this[kOldStatus] = -1;
- filename = getPathFromURL(filename);
+ filename = toPathIfFileURL(filename);
validatePath(filename, 'filename');
validateUint32(interval, 'interval');
const err = this._handle.start(toNamespacedPath(filename), interval);
@@ -154,7 +154,7 @@ FSWatcher.prototype.start = function(filename,
return;
}
- filename = getPathFromURL(filename);
+ filename = toPathIfFileURL(filename);
validatePath(filename, 'filename');
const err = this._handle.start(toNamespacedPath(filename),
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index c218dc2cac..1e93791237 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -54,7 +54,7 @@ module.exports = Module;
let asyncESM;
let ModuleJob;
let createDynamicModule;
-let getURLFromFilePath;
+let pathToFileURL;
let decorateErrorStack;
function lazyLoadESM() {
@@ -63,7 +63,7 @@ function lazyLoadESM() {
createDynamicModule = require(
'internal/modules/esm/create_dynamic_module');
decorateErrorStack = require('internal/util').decorateErrorStack;
- getURLFromFilePath = require('internal/url').getURLFromFilePath;
+ pathToFileURL = require('internal/url').pathToFileURL;
}
const {
@@ -602,7 +602,7 @@ Module.prototype.load = function(filename) {
if (experimentalModules) {
if (asyncESM === undefined) lazyLoadESM();
const ESMLoader = asyncESM.ESMLoader;
- const url = getURLFromFilePath(filename);
+ const url = pathToFileURL(filename);
const urlString = `${url}`;
const exports = this.exports;
if (ESMLoader.moduleMap.has(urlString) !== true) {
@@ -731,7 +731,7 @@ Module.runMain = function() {
if (experimentalModules) {
if (asyncESM === undefined) lazyLoadESM();
asyncESM.loaderPromise.then((loader) => {
- return loader.import(getURLFromFilePath(process.argv[1]).pathname);
+ return loader.import(pathToFileURL(process.argv[1]).pathname);
})
.catch((e) => {
decorateErrorStack(e);
diff --git a/lib/internal/modules/esm/default_resolve.js b/lib/internal/modules/esm/default_resolve.js
index b573ce4e8d..875c560cb1 100644
--- a/lib/internal/modules/esm/default_resolve.js
+++ b/lib/internal/modules/esm/default_resolve.js
@@ -15,7 +15,7 @@ const {
} = require('internal/errors').codes;
const { resolve: moduleWrapResolve } = internalBinding('module_wrap');
const StringStartsWith = Function.call.bind(String.prototype.startsWith);
-const { getURLFromFilePath, getPathFromURL } = require('internal/url');
+const { pathToFileURL, fileURLToPath } = require('internal/url');
const realpathCache = new Map();
@@ -62,7 +62,7 @@ function resolve(specifier, parentURL) {
let url;
try {
url = search(specifier,
- parentURL || getURLFromFilePath(`${process.cwd()}/`).href);
+ parentURL || pathToFileURL(`${process.cwd()}/`).href);
} catch (e) {
if (typeof e.message === 'string' &&
StringStartsWith(e.message, 'Cannot find module'))
@@ -73,11 +73,11 @@ function resolve(specifier, parentURL) {
const isMain = parentURL === undefined;
if (isMain ? !preserveSymlinksMain : !preserveSymlinks) {
- const real = realpathSync(getPathFromURL(url), {
+ const real = realpathSync(fileURLToPath(url), {
[internalFS.realpathCacheKey]: realpathCache
});
const old = url;
- url = getURLFromFilePath(real);
+ url = pathToFileURL(real);
url.search = old.search;
url.hash = old.hash;
}
diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js
index 618f1adac8..aaf35ed461 100644
--- a/lib/internal/modules/esm/translators.js
+++ b/lib/internal/modules/esm/translators.js
@@ -40,7 +40,7 @@ const isWindows = process.platform === 'win32';
const winSepRegEx = /\//g;
translators.set('cjs', async (url, isMain) => {
debug(`Translating CJSModule ${url}`);
- const pathname = internalURLModule.getPathFromURL(new URL(url));
+ const pathname = internalURLModule.fileURLToPath(new URL(url));
const module = CJSModule._cache[
isWindows ? StringReplace(pathname, winSepRegEx, '\\') : pathname];
if (module && module.loaded) {
@@ -81,7 +81,7 @@ translators.set('addon', async (url) => {
return createDynamicModule(['default'], url, (reflect) => {
debug(`Loading NativeModule ${url}`);
const module = { exports: {} };
- const pathname = internalURLModule.getPathFromURL(new URL(url));
+ const pathname = internalURLModule.fileURLToPath(new URL(url));
process.dlopen(module, _makeLong(pathname));
reflect.exports.default.set(module.exports);
});
@@ -92,7 +92,7 @@ translators.set('json', async (url) => {
debug(`Translating JSONModule ${url}`);
return createDynamicModule(['default'], url, (reflect) => {
debug(`Loading JSONModule ${url}`);
- const pathname = internalURLModule.getPathFromURL(new URL(url));
+ const pathname = internalURLModule.fileURLToPath(new URL(url));
const content = readFileSync(pathname, 'utf8');
try {
const exports = JsonParse(stripBOM(content));
diff --git a/lib/internal/process/esm_loader.js b/lib/internal/process/esm_loader.js
index c622084415..6f3ac729f8 100644
--- a/lib/internal/process/esm_loader.js
+++ b/lib/internal/process/esm_loader.js
@@ -6,7 +6,7 @@ const {
setInitializeImportMetaObjectCallback
} = internalBinding('module_wrap');
-const { getURLFromFilePath } = require('internal/url');
+const { pathToFileURL } = require('internal/url');
const Loader = require('internal/modules/esm/loader');
const path = require('path');
const { URL } = require('url');
@@ -17,7 +17,7 @@ const {
function normalizeReferrerURL(referrer) {
if (typeof referrer === 'string' && path.isAbsolute(referrer)) {
- return getURLFromFilePath(referrer).href;
+ return pathToFileURL(referrer).href;
}
return new URL(referrer).href;
}
@@ -52,7 +52,7 @@ exports.setup = function() {
const userLoader = process.binding('config').userLoader;
if (userLoader) {
const hooks = await ESMLoader.import(
- userLoader, getURLFromFilePath(`${process.cwd()}/`).href);
+ userLoader, pathToFileURL(`${process.cwd()}/`).href);
ESMLoader = new Loader();
ESMLoader.hook(hooks);
exports.ESMLoader = ESMLoader;
diff --git a/lib/internal/url.js b/lib/internal/url.js
index ffd8f10eda..2afd2db9b7 100644
--- a/lib/internal/url.js
+++ b/lib/internal/url.js
@@ -20,13 +20,16 @@ const {
ERR_MISSING_ARGS
} = require('internal/errors').codes;
const {
- CHAR_PERCENT,
- CHAR_PLUS,
CHAR_AMPERSAND,
+ CHAR_BACKWARD_SLASH,
CHAR_EQUAL,
+ CHAR_FORWARD_SLASH,
CHAR_LOWERCASE_A,
CHAR_LOWERCASE_Z,
+ CHAR_PERCENT,
+ CHAR_PLUS
} = require('internal/constants');
+const path = require('path');
// Lazy loaded for startup performance.
let querystring;
@@ -1392,11 +1395,12 @@ function getPathFromURLPosix(url) {
return decodeURIComponent(pathname);
}
-function getPathFromURL(path) {
- if (path == null || !path[searchParams] ||
- !path[searchParams][searchParams]) {
- return path;
- }
+function fileURLToPath(path) {
+ if (typeof path === 'string')
+ path = new URL(path);
+ else if (path == null || !path[searchParams] ||
+ !path[searchParams][searchParams])
+ throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);
if (path.protocol !== 'file:')
throw new ERR_INVALID_URL_SCHEME('file');
return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);
@@ -1406,12 +1410,30 @@ function getPathFromURL(path) {
// 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;
+const backslashRegEx = /\\/g;
+function pathToFileURL(filepath) {
+ let resolved = path.resolve(filepath);
+ // path.resolve strips trailing slashes so we must add them back
+ const filePathLast = filepath.charCodeAt(filepath.length - 1);
+ if ((filePathLast === CHAR_FORWARD_SLASH ||
+ isWindows && filePathLast === CHAR_BACKWARD_SLASH) &&
+ resolved[resolved.length - 1] !== path.sep)
+ resolved += '/';
+ const outURL = new URL('file://');
+ if (resolved.includes('%'))
+ resolved = resolved.replace(percentRegEx, '%25');
+ // in posix, "/" is a valid character in paths
+ if (!isWindows && resolved.includes('\\'))
+ resolved = resolved.replace(backslashRegEx, '%5C');
+ outURL.pathname = resolved;
+ return outURL;
+}
+
+function toPathIfFileURL(fileURLOrPath) {
+ if (fileURLOrPath == null || !fileURLOrPath[searchParams] ||
+ !fileURLOrPath[searchParams][searchParams])
+ return fileURLOrPath;
+ return fileURLToPath(fileURLOrPath);
}
function NativeURL(ctx) {
@@ -1441,8 +1463,9 @@ setURLConstructor(constructUrl);
module.exports = {
toUSVString,
- getPathFromURL,
- getURLFromFilePath,
+ fileURLToPath,
+ pathToFileURL,
+ toPathIfFileURL,
URL,
URLSearchParams,
domainToASCII,
diff --git a/lib/url.js b/lib/url.js
index 3a2c3c01ea..ba2033b4e5 100644
--- a/lib/url.js
+++ b/lib/url.js
@@ -42,6 +42,8 @@ const {
domainToUnicode,
formatSymbol,
encodeStr,
+ pathToFileURL,
+ fileURLToPath
} = require('internal/url');
// Original url.parse() API
@@ -966,5 +968,9 @@ module.exports = {
URL,
URLSearchParams,
domainToASCII,
- domainToUnicode
+ domainToUnicode,
+
+ // Utilities
+ pathToFileURL,
+ fileURLToPath
};