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 should support auth, base, pathabsolute #31874

Closed
dodtsair opened this issue Feb 19, 2020 · 1 comment
Closed

URL should support auth, base, pathabsolute #31874

dodtsair opened this issue Feb 19, 2020 · 1 comment

Comments

@dodtsair
Copy link

Is your feature request related to a problem? Please describe.
We want to use URL to manage the configuration for our connection to an http based database. In our configuration we want to be able to separately specify host, port, user, password. Some of the code is old, and it takes the URL and appends ':' and the configured port. This works fine as long as we keep the URL without a terminating '/' but the URL object does not support this. When a URL is constructed without a '/' and then exported again using url.href the '/' is always appended. You cannot correct the issue by using url.pathname = ''

URL works great and I do not want to change the above functionality, just understand the context to provide insight into my desired solution. I want to be able to take the URL object and scoop out of it the protocol, user, password, host, port. There is currently not a getter (for example base) that would enable me to scoop this out. Instead I must do the following

let stringUrl = url.protocol + '//' + url.username + ':' url.password + '@' + url.hostname + ":" + url.port;
This would work, assuming that there is a username, or password, or port. If there isn't then I have to put if checks around the : and @ etc. But that really is the job of URL.

Describe the solution you'd like
Please describe the desired behavior.
Instead of coding the above, I just want to scoop out everything but the path and beyond. Something like

    const myURL = new URL("https://abc:xyz@example.org/p/a/t/h?query=string#hash");
    console.log(url.base)
    // Prints https://abc:xyz@example.org

also
expose auth and pathabsolute (https://url.spec.whatwg.org/#relative-url-with-fragment-string)

    const myURL = new URL("https://abc:xyz@example.org/p/a/t/h?query=string#hash");
    console.log(myURL.auth);
    // Prints abc:xyz
    const myURL = new URL("https://abc:xyz@example.org/p/a/t/h?query=string#hash");
    console.log(myURL.pathabsolute);
    // Prints /p/a/t/h?query=string#hash

Describe alternatives you've considered
Coding it myself is always an option, but I would rather leave URL to do the work.

Note documentation also mentions an auth which makes one think there is a getter for it, but there is not. See documentation containing auth:

┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                              href                                              │
├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤
│ protocol │  │        auth         │          host          │           path            │ hash  │
│          │  │                     ├─────────────────┬──────┼──────────┬────────────────┤       │
│          │  │                     │    hostname     │ port │ pathname │     search     │       │
│          │  │                     │                 │      │          ├─┬──────────────┤       │
│          │  │                     │                 │      │          │ │    query     │       │
"  https:   //    user   :   pass   @ sub.example.com : 8080   /p/a/t/h  ?  query=string   #hash "
│          │  │          │          │    hostname     │ port │          │                │       │
│          │  │          │          ├─────────────────┴──────┤          │                │       │
│ protocol │  │ username │ password │          host          │          │                │       │
├──────────┴──┼──────────┴──────────┼────────────────────────┤          │                │       │
│   origin    │                     │         origin         │ pathname │     search     │ hash  │
├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤
│                                              href                                              │
└────────────────────────────────────────────────────────────────────────────────────────────────┘

I suggest updating the documentation to:

┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                              href                                              │
├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤
│ protocol │  │        auth         │          host          │           path            │ hash  │
│          │  │                     ├─────────────────┬──────┼──────────┬────────────────┤       │
│          │  │                     │    hostname     │ port │ pathname │     search     │       │
│          │  │                     │                 │      │          ├─┬──────────────┤       │
│          │  │                     │                 │      │          │ │    query     │       │
"  https:   //    user   :   pass   @ sub.example.com : 8080   /p/a/t/h  ?  query=string   #hash "
│          │  │          │          │    hostname     │ port │          │                │       │
│          │  │          │          ├─────────────────┴──────┤          │                │       │
│ protocol │  │ username │ password │          host          │          │                │       │
├──────────┴──┼──────────┴──────────┼────────────────────────┤          │                │       │
│   origin    │                     │         origin         │ pathname │     search     │ hash  │
├─────────────┴─────────────────────┴────────────────────────┼──────────┴────────────────┴───────┤
│                       base                                 │     pathabsolute                  │
├────────────────────────────────────────────────────────────┴───────────────────────────────────┤
│                                              href                                              │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
@dodtsair dodtsair changed the title URL should support auth and authority URL should support auth, base, pathabsolute Feb 19, 2020
@joyeecheung
Copy link
Member

Node.js is only an implementer of the WHATWG URL standard here, so changes like this need to be accepted in the upstream before it can be implemented in Node.js. Please open your feature request in https://github.com/whatwg/url/issues, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants