url.parse produces incorrect pathname for file:///-based URLs under Windows, it adds a leading slash which breaks other filesystem APIs like createWriteStream(pathname). path.normalize doesn't fix it.
Example:
var url = require("url");
var path = require("path");
var fileUrl = "file:///c:/Users/avo/test.txt"
var parsed = url.parse(fileUrl)
console.log(parsed.pathname)
console.log(path.normalize(parsed.pathname))
Result:
/c:/Users/avo/test.txt \c:\Users\avo\test.txt
A workaround I'm currently using: pathname = pathname.replace(/^\\(\w:)/, "$1");
Originally posted as a question on StackOverflow.
If it is appropriate, you can use url.fileURLToPath(url) now.
@vsemozhetbyt, url.fileURLToPath(url) is exactly what I needed. Spasibo!
@avoio does this solves your issue?
@bzoz yes fileURLToPath(url) did solve my issue. This bug report is however about url.parse (or perhaps, about path.normalize - should it be removing the leading \ on Windows?)
/ping @nodejs/platform-windows @nodejs/url Is this a bug or expected behavior?
I'd say expected behavior.
Hello @avoio, and thank you for the report.
AFAICT this is expected behavior:
Appendix E of rfc8089 as Nonstandard Syntax Variations\c:\Users\avo\test.txt is a valid file path which means "An alternate stream named \Users\avo\test.txt on a file named c in the current working drive".I'm going to close this issue as a known-limitation.
Most helpful comment
@vsemozhetbyt,
url.fileURLToPath(url)is exactly what I needed. Spasibo!