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

path: add fromURL #44315

Closed
wants to merge 2 commits into from
Closed

path: add fromURL #44315

wants to merge 2 commits into from

Conversation

MoLow
Copy link
Member

@MoLow MoLow commented Aug 20, 2022

Fixes: #41521

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. path Issues and PRs related to the path subsystem. labels Aug 20, 2022
@MoLow
Copy link
Member Author

MoLow commented Aug 20, 2022

CC @nodejs/path

@MoLow MoLow force-pushed the add-path-from-url branch 2 times, most recently from 400a742 to 7628fdd Compare August 20, 2022 22:26
@tniessen
Copy link
Member

Is my understanding correct that this API is essentially just typeof arg === 'string' ? arg : url.fileURLToPath(arg)?

Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

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

Can we add tests for when the passed argument is not a string nor a URL instance?

doc/api/path.md Outdated Show resolved Hide resolved
doc/api/path.md Outdated Show resolved Hide resolved
lib/path.js Outdated Show resolved Hide resolved
@MoLow
Copy link
Member Author

MoLow commented Aug 21, 2022

Is my understanding correct that this API is essentially just typeof arg === 'string' ? arg : url.fileURLToPath(arg)?

correct. see #41521 (comment)

@benjamingr
Copy link
Member

cc @sindresorhus

Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

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

Can you add a test case with the empty string?

test/parallel/test-path-from-url.js Outdated Show resolved Hide resolved
lib/path.js Outdated Show resolved Hide resolved
test/parallel/test-path-from-url.js Outdated Show resolved Hide resolved
test/parallel/test-path-from-url.js Outdated Show resolved Hide resolved
doc/api/path.md Show resolved Hide resolved
@MoLow MoLow added the commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. label Aug 23, 2022
Copy link
Member

@tniessen tniessen left a comment

Choose a reason for hiding this comment

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

I don't have a strong opinion on this PR; this already seems to be a one-liner in userland.

@@ -267,6 +267,38 @@ path.format({
// Returns: 'C:\\path\\dir\\file.txt'
```

## `path.fromURL(pathOrUrl)`
Copy link
Member

Choose a reason for hiding this comment

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

Isn't the name misleading, given that the input is not necessarily a URL? In fact, URL strings of the form file:///home are ignored and not treated as URLs.

Copy link
Member Author

Choose a reason for hiding this comment

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

what would you consider to be a better name?

Copy link
Contributor

Choose a reason for hiding this comment

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

What about url.toPathIfFileURL?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or maybe path.fromURLInstance?

// Returns: 'file:///Users/node/dev'

path.fromURL(new URL('file:///c:/foo.txt'));
// Returns On Windows: 'c:\\foo.txt'
Copy link
Member

Choose a reason for hiding this comment

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

I am still unsure if I like the fact that fromURL(URL) normalizes the path in an OS-specific manner while fromURL(string) does not. I would assume that

path.fromURL(url.pathToFileURL(absolutePath)) === path.fromURL(absolutePath)

but that does not seem to be true in all cases.

On the other hand, this seems to be what @sindresorhus expects.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree this makes more sence:
path.fromURL('file:///Users/node/dev') === '/Users/node/dev' and not 'file:///Users/node/dev'
@sindresorhus WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

How do you decide if the user refers to a file: URL or to a relative path inside a directory named file:?

Copy link
Member

Choose a reason for hiding this comment

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

I agree this makes more sence:
path.fromURL('file:///Users/node/dev') === '/Users/node/dev' and not 'file:///Users/node/dev'

I think that's orthogonal to my concern regarding OS-specific path normalization, and not what this comment is about.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am actually less concerned about that since you can be explicit and use path.win32/path.posix, like we do in our tests

Copy link
Member

Choose a reason for hiding this comment

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

Again, that's also not what my comment is about. My concern is the fact that fromURL appears to sometimes -- but not always -- normalize the path.

Copy link
Member Author

Choose a reason for hiding this comment

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

@tniessen do you think we should change the behavior even if the name of the method is changed to one of the names suggested here (toPathIfFileURL/fromURLInstance)?

sep: '/',
delimiter: ':',
win32: null,
posix: null
};


Copy link
Member

Choose a reason for hiding this comment

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

Nit: unrelated change.

Copy link
Member

@targos targos left a comment

Choose a reason for hiding this comment

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

Just making sure #44315 (comment) is resolved before this can land.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. needs-ci PRs that need a full CI run. path Issues and PRs related to the path subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a utility to convert file URL or path to one of them?
6 participants