GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — url: expose pathToFileURL and fileURLToPath by guybedford · Pull Request #22506 · nodejs/node

url: expose pathToFileURL and fileURLToPath #22506

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.

Sign up for GitHub

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

Closed
guybedford wants to merge 18 commits into nodejs:master from guybedford:url-utils

Uh oh!

There was an error while loading. Please reload this page.

Closed

url: expose pathToFileURL and fileURLToPath #22506

Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
4d95ca1
url: expose pathToFileUrl and fileUrlToPath
guybedford Aug 24, 2018
0d9c808
Url -> URL, pad out examples
guybedford Aug 24, 2018
0b784b4
update windows examples
guybedford Aug 24, 2018
6479897
correction
guybedford Aug 24, 2018
09bc85a
registry -> module map
guybedford Aug 24, 2018
c073384
document utils in same section
guybedford Aug 24, 2018
d36d1f9
readme corrections
guybedford Aug 25, 2018
47d9efa
style nits
guybedford Aug 25, 2018
44de7c0
show correct use in examples, validations
guybedford Aug 26, 2018
ee7b01d
fix line length
guybedford Aug 26, 2018
3bba43e
formatting
guybedford Aug 26, 2018
d1dfe98
fix indentation
guybedford Aug 26, 2018
a9dc0ce
example corrections
guybedford Aug 26, 2018
3394901
fix tests, doc nit
guybedford Aug 26, 2018
11b2606
use same URL check, error case args correction
guybedford Aug 28, 2018
d921fef
tweaks
guybedford Aug 28, 2018
8d95ddb
file path cases tests
guybedford Aug 29, 2018
87d7a73
encode "\" in paths in posix
guybedford Aug 30, 2018
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
readme corrections
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@guybedford
guybedford committed Aug 25, 2018
commit d36d1f9f99d7cece4f14c6f3ad4beb7bf5e4e6db

There are no files selected for viewing

64 changes: 32 additions & 32 deletions 64 doc/api/url.md
Show comments
View file
Open in desktop
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.
Comment thread Outdated

@devsnek devsnek Aug 28, 2018

Copy link
Copy Markdown
Member

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}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@guybedford guybedford Aug 28, 2018

Copy link
Copy Markdown
Contributor Author

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

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.
Comment thread Outdated

@vsemozhetbyt vsemozhetbyt Aug 25, 2018

Copy link
Copy Markdown
Contributor

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.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

### url.format(URL[, options])
<!-- YAML
added: v7.6.0
Expand Down Expand Up @@ -932,47 +958,21 @@ 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)
// 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)
// '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)
// 'file:///nas/foo.txt' instead of the correct 'file:///foo.txt' (POSIX)
new URL(`file://${'//nas/foo.txt'}`);
Comment thread Outdated

@vsemozhetbyt vsemozhetbyt Aug 24, 2018

Copy link
Copy Markdown
Contributor

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.

In this and the next example, template strings with string interpolation seem confusingly redundant. Maybe interpolate previously defined variables?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@guybedford guybedford Aug 25, 2018

Copy link
Copy Markdown
Contributor Author

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 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vsemozhetbyt reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

// 'file:///some/path%' instead of the correct 'file:///some/path%25' (posix)
new URL(`sfile:${'/some/path%.js'}`);
```

where using `pathToFileURL` we can get the correct results above.

### 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.

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;
// 'file:///some/path%' instead of the correct 'file:///some/path%25' (POSIX)
new URL(`file:${'/some/path%.js'}`);
```
Comment thread

@mcollina mcollina Aug 25, 2018

Copy link
Copy Markdown
Member

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.

Can you please add an example that uses the new API instead?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

where using `fileURLToPath` we can get the correct results above.
where using `url.pathToFileURL` we can get the correct results above.
Comment thread Outdated

@vsemozhetbyt vsemozhetbyt Aug 25, 2018

Copy link
Copy Markdown
Contributor

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.

Ditto)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

## Legacy URL API

Expand Down
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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.