-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
url: expose pathToFileURL and fileURLToPath #22506
Changes from 6 commits
4d95ca1
0d9c808
0b784b4
6479897
09bc85a
c073384
d36d1f9
47d9efa
44de7c0
ee7b01d
3bba43e
d1dfe98
a9dc0ce
3394901
11b2606
d921fef
8d95ddb
87d7a73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -921,6 +921,59 @@ console.log(url.format(myURL, { fragment: false, unicode: true, auth: false })); | |
// Prints 'https://你好你好/?abc' | ||
``` | ||
|
||
### url.pathToFileURL(path) | ||
|
||
* `path` {string} The absolute path to convert to a File URL. | ||
* Returns: {URL} The file URL object. | ||
|
||
This function ensures the correct encodings of URL control characters in file | ||
paths when converting into File URLs. | ||
|
||
For example, the following errors can occur when converting from paths to URLs: | ||
|
||
```js | ||
// throws for missing schema (posix) | ||
// (in Windows the drive letter is detected as the protocol) | ||
new URL(__filename); | ||
|
||
// 'file:///foo' instead of the correct 'file:///foo%231' (posix) | ||
new URL('./foo#1', 'file:///'); | ||
|
||
// 'file:///nas/foo.txt' instead of the correct 'file:///foo.txt' (posix) | ||
new URL(`file://${'//nas/foo.txt'}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this and the next example, template strings with string interpolation seem confusingly redundant. Maybe interpolate previously defined variables? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I quite like having these be scannable as single-line cases, initially I wrote |
||
|
||
// 'file:///some/path%' instead of the correct 'file:///some/path%25' (posix) | ||
new URL(`sfile:${'/some/path%.js'}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks |
||
``` | ||
|
||
where using `pathToFileURL` we can get the correct results above. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
### url.fileURLToPath(url) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section needs to go before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, a shame we can't keep these together though. |
||
|
||
* `url` {URL} | {string} The file URL string or URL object to convert to a path. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* Returns: {URL} 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. | ||
|
||
When converting from URL to path, the following common errors can occur: | ||
|
||
```js | ||
// '/C:/path/' instead of 'C:\path\' (Windows) | ||
new URL('file:///C:/path/').pathname; | ||
|
||
// '/foo.txt' instead of '\\nas\foo.txt' (Windows) | ||
new URL('file://nas/foo.txt').pathname; | ||
|
||
// '/%E4%BD%A0%E5%A5%BD.txt' instead of '/你好.txt' (posix) | ||
new URL('file:///你好.txt').pathname; | ||
|
||
// '/hello%20world.txt' instead of '/hello world.txt' (posix) | ||
new URL('file:///hello world.txt').pathname; | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add an example that uses the new API instead? |
||
|
||
where using `fileURLToPath` we can get the correct results above. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
## Legacy URL API | ||
|
||
### Legacy `urlObject` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
posix
->POSIX
here and below?