Skip to content
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
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions doc/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,32 @@ 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.
Copy link
Member

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}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh... thank you!


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;
```

where using `url.fileURLToPath` we can get the correct results above.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually add parentheses to functions/methods as per the style guide, so:
`url.fileURLToPath` -> `url.fileURLToPath()`


### url.format(URL[, options])
<!-- YAML
added: v7.6.0
Expand Down Expand Up @@ -921,6 +947,33 @@ 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'}`);
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 new URL('file://' + '//nas/foo.txt') to indicate the separation, but the linter wouldn't allow that.


// 'file:///some/path%' instead of the correct 'file:///some/path%25' (POSIX)
new URL(`file:${'/some/path%.js'}`);
```
Copy link
Member

Choose a reason for hiding this comment

The 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 `url.pathToFileURL` we can get the correct results above.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto)


## Legacy URL API

### Legacy `urlObject`
Expand Down
Loading