-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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 1 commit
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,51 @@ console.log(url.format(myURL, { fragment: false, unicode: true, auth: false })); | |
// Prints 'https://你好你好/?abc' | ||
``` | ||
|
||
## File URL Utility Functions | ||
|
||
When working with `file:///` URLs in Node.js (eg when working with ES modules | ||
which are keyed in the registry by File URL), the utility functions | ||
`urlToFilePath` and `fileUrlToPath` are provided to convert to and from file | ||
paths. | ||
|
||
The edge cases handled by these functions include percent-encoding and decoding | ||
as well as cross-platform support. | ||
|
||
For example, instead of writing: | ||
|
||
```js | ||
// BAD: | ||
// - fails in Windows | ||
// - doesn't handle loading paths using extended non-latin characters | ||
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. not just that, also things like 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 isn't meant to be comprehensive, but we can certainly pad out the examples. I expect this will also change over time, so I don't think we want to keep the docs exactly in sync with the code here either. 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. agree, but examples seem more prudent than statements like this which could be taken as the only exceptions thereby leading people only on unix and in latin charsets to feel this API is not needed 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. non-ASCII? |
||
fs.promises.readFile(fileUrl.pathname); | ||
``` | ||
|
||
write: | ||
|
||
```js | ||
// GOOD: | ||
// - works in Windows | ||
// - handles emoji file paths | ||
const { fileUrlToPath } = require('url'); | ||
fs.promises.readFile(fileUrlToPath(fileUrl)); | ||
``` | ||
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? |
||
|
||
### 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. | ||
|
||
### 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. | ||
|
||
## 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.
I’d s/registry/module registry/ for clarity
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.
module map, not registry