Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1725,12 +1725,17 @@ function pathToFileURL(filepath, options = kEmptyObject) {
const hostname = StringPrototypeSlice(resolved, prefixLength, hostnameEndIndex);
return new URL(StringPrototypeSlice(resolved, hostnameEndIndex), hostname, kCreateURLFromWindowsPathSymbol);
}
// path.resolve strips trailing slashes so we must add them back
// path.resolve() keeps a trailing separator only for a root, so put one
// back for other directories. Compare against the separator of the
// platform selected by options.windows. path.sep is the host separator
// and disagrees when those differ, which turns "C:\" into file:///C://
// on a POSIX host and "/" into file://// on a Windows host.
const filePathLast = StringPrototypeCharCodeAt(filepath,
filepath.length - 1);
const trailingSep = windows ? CHAR_BACKWARD_SLASH : CHAR_FORWARD_SLASH;
if ((filePathLast === CHAR_FORWARD_SLASH ||
((windows ?? isWindows) && filePathLast === CHAR_BACKWARD_SLASH)) &&
resolved[resolved.length - 1] !== path.sep)
(windows && filePathLast === CHAR_BACKWARD_SLASH)) &&
StringPrototypeCharCodeAt(resolved, resolved.length - 1) !== trailingSep)
resolved += '/';

return new URL(resolved, undefined, windows ? kCreateURLFromWindowsPathSymbol : kCreateURLFromPosixPathSymbol);
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-url-pathtofileurl-root-slash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

// path.resolve() keeps a trailing separator only for a root. pathToFileURL()
// puts that separator back for other directories. The separator has to be the
// one for the platform selected by options.windows, not the host path.sep.
// Otherwise "C:\" on a POSIX host becomes file:///C://, and "/" on a Windows
// host with { windows: false } becomes file:////.

require('../common');
const assert = require('node:assert');
const url = require('node:url');

assert.strictEqual(url.pathToFileURL('C:\\', { windows: true }).href, 'file:///C:/');
assert.strictEqual(url.pathToFileURL('C:/', { windows: true }).href, 'file:///C:/');
// win32.resolve('C:\\foo\\..\\') is 'C:\\'. The original trailing separator
// must not add a second slash on top of the root.
assert.strictEqual(url.pathToFileURL('C:\\foo\\..\\', { windows: true }).href, 'file:///C:/');

assert.strictEqual(url.pathToFileURL('/', { windows: false }).href, 'file:///');

// Non-root directories still keep exactly one trailing slash.
assert.strictEqual(url.pathToFileURL('C:\\dir\\', { windows: true }).href, 'file:///C:/dir/');
assert.strictEqual(url.pathToFileURL('/dir/', { windows: false }).href, 'file:///dir/');