Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1673,9 +1673,30 @@ function fileURLToPathBuffer(path, options = kEmptyObject) {
function pathToFileURL(filepath, options = kEmptyObject) {
const windows = options?.windows ?? isWindows;
const isUNC = windows && StringPrototypeStartsWith(filepath, '\\\\');
let resolved = isUNC ?
filepath :
(windows ? path.win32.resolve(filepath) : path.posix.resolve(filepath));
let resolved;
if (isUNC) {
resolved = filepath;
} else if (!windows &&
filepath.length > 0 &&
StringPrototypeCharCodeAt(filepath, 0) === CHAR_FORWARD_SLASH &&
StringPrototypeIndexOf(filepath, '.') === -1 &&
StringPrototypeIndexOf(filepath, '//') === -1) {
// Already an absolute POSIX path with no '.' / '..' or empty segments.
// posix.resolve() drops a trailing slash except for '/'. Keep that
// so the restore below does not append a second slash when path.sep
// is '\\' (Windows host, windows: false).
let end = filepath.length;
while (end > 1 &&
StringPrototypeCharCodeAt(filepath, end - 1) ===
CHAR_FORWARD_SLASH) {
end--;
}
resolved = end === filepath.length ?
filepath :
StringPrototypeSlice(filepath, 0, end);
} else {
resolved = windows ? path.win32.resolve(filepath) : path.posix.resolve(filepath);
}
if (isUNC || (windows && StringPrototypeStartsWith(resolved, '\\\\'))) {
// UNC path format: \\server\share\resource
// Handle extended UNC path and standard UNC path
Expand Down