From eef072fa083f05f84fa6ca1908472eb228095a38 Mon Sep 17 00:00:00 2001 From: guybedford Date: Fri, 24 Aug 2018 18:13:32 +0200 Subject: url: provide pathToFileURL and fileURLToPath PR-URL: https://github.com/nodejs/node/pull/22506 Reviewed-By: John-David Dalton Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Matteo Collina Reviewed-By: Bradley Farias Reviewed-By: Joyee Cheung Reviewed-By: Gus Caplan --- doc/api/url.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'doc/api/url.md') diff --git a/doc/api/url.md b/doc/api/url.md index 571c3bdfdb..cd817bf304 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -880,6 +880,28 @@ console.log(url.domainToUnicode('xn--iñvalid.com')); // Prints an empty string ``` +### url.fileURLToPath(url) + +* `url` {URL | string} The file URL string or URL object to convert to a path. +* Returns: {string} The fully-resolved platform-specific Node.js file path. + +This function ensures the correct decodings of percent-encoded characters as +well as ensuring a cross-platform valid absolute path string. + +```js +new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/ +fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows) + +new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt +fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows) + +new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt +fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX) + +new URL('file:///hello world').pathname; // Incorrect: /hello%20world +fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX) +``` + ### url.format(URL[, options])