-
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
Closed
Closed
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4d95ca1
url: expose pathToFileUrl and fileUrlToPath
guybedford 0d9c808
Url -> URL, pad out examples
guybedford 0b784b4
update windows examples
guybedford 6479897
correction
guybedford 09bc85a
registry -> module map
guybedford c073384
document utils in same section
guybedford d36d1f9
readme corrections
guybedford 47d9efa
style nits
guybedford 44de7c0
show correct use in examples, validations
guybedford ee7b01d
fix line length
guybedford 3bba43e
formatting
guybedford d1dfe98
fix indentation
guybedford a9dc0ce
example corrections
guybedford 3394901
fix tests, doc nit
guybedford 11b2606
use same URL check, error case args correction
guybedford d921fef
tweaks
guybedford 8d95ddb
file path cases tests
guybedford 87d7a73
encode "\" in paths in posix
guybedford File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: {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. | ||
|
||
```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]) | ||
<!-- YAML | ||
added: v7.6.0 | ||
|
@@ -906,8 +928,6 @@ string serializations of the URL. These are not, however, customizable in | |
any way. The `url.format(URL[, options])` method allows for basic customization | ||
of the output. | ||
|
||
For example: | ||
|
||
```js | ||
const myURL = new URL('https://a:b@你好你好?abc#foo'); | ||
|
||
|
@@ -921,6 +941,27 @@ console.log(url.format(myURL, { fragment: false, unicode: true, auth: false })); | |
// Prints 'https://你好你好/?abc' | ||
``` | ||
|
||
### url.pathToFileURL(path) | ||
|
||
* `path` {string} The path to convert to a File URL. | ||
* Returns: {URL} The file URL object. | ||
|
||
This function ensures that `path` is resolved absolutely, and that the URL | ||
control characters are correctly encoded when converting into a File URL. | ||
|
||
```js | ||
new URL(__filename); // Incorrect: throws (POSIX) | ||
new URL(__filename); // Incorrect: C:\... (Windows) | ||
pathToFileURL(__filename); // Correct: file:///... (POSIX) | ||
pathToFileURL(__filename); // Correct: file:///C:/... (Windows) | ||
|
||
new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1 | ||
pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX) | ||
|
||
new URL('/some/path%.js', 'file:'); // Incorrect: file:///some/path% | ||
pathToFileURL('/some/path%.js'); // Correct: file:///some/path%25 (POSIX) | ||
``` | ||
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? |
||
|
||
## Legacy URL API | ||
|
||
### Legacy `urlObject` | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
actually whoops this should be
{string}
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.
Ahhh... thank you!