Add method to parse host, port, and protocol from URL - #100
Open
vartanbeno wants to merge 3 commits into
Open
Conversation
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
|
Based on what I see in your unit tests, I would add the flawed case you described above. My naive guess would be that it defaults to |
fvnilo
reviewed
Mar 16, 2020
| * @param {Object} output An object containing host, port, and secure properties. | ||
| * @returns {Object} The object containing valid host, port, and secure keys based on the input. | ||
| */ | ||
| const parseURL = (input) => { |
There was a problem hiding this comment.
As mentionned in the global comments, I think here I would handle the case of not specifying a protocol.
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
fvnilo
reviewed
Mar 17, 2020
| } | ||
|
|
||
| if (!input.startsWith(PROTOCOL_HTTP) && !input.startsWith(PROTOCOL_HTTPS)) { | ||
| input = PROTOCOL_HTTPS + input; |
There was a problem hiding this comment.
Shouldn't you add a // in the middle here?
Contributor
Author
There was a problem hiding this comment.
It doesn't seem to be required by the url module to parse the input. On second thought though, I'll add it since most people expect https:// instead of just https:.
fvnilo
reviewed
Mar 17, 2020
| let url; | ||
| try { | ||
| url = new URL(input); | ||
| } catch (e) { |
There was a problem hiding this comment.
Would it be useful to log a warning, you think?
benjamin-morin
removed their request for review
July 30, 2024 17:31
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Overview
Normally, when we specify the host of an API, we do so like this:
If we specify both the protocol and the port in the
hostvariable, it makes the sdk behave in unexpected ways. For example:All the requests would go to
https://http://example.com:1234:443.Using Node's
urlmodule, we parse the url, and if a protocol/port are found, we override the default ones.This change is made with backward compatibility in mind. If the host we provide is just the hostname, the sdk behaves like normal.
Flaws
If we specify a host with a port but not a protocol:
Node's url parser thinks the protocol is
example.com:and that the hostname is'', which would default tolocalhost.Todo
secureflag?