-
Notifications
You must be signed in to change notification settings - Fork 0
Add method to parse host, port, and protocol from URL #100
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| const { URL } = require('url'); | ||
|
|
||
| const PROTOCOL_HTTP = 'http:'; | ||
| const PROTOCOL_HTTPS = 'https:'; | ||
|
|
||
| /** | ||
| * Parse the input (URL string) to get its hostname, port, and protocol. | ||
| * In the case that it's not parsable and it throws an error, return an empty object. | ||
| * We do this to preserve backward compatibility. | ||
| * Note: this method assumes you are using either HTTP or HTTPS protocols. It assumes HTTPS if protocol isn't provided. | ||
| * @param {String} input The URL. | ||
| * @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) => { | ||
| const output = {}; | ||
|
|
||
| if (typeof(input) !== 'string') { | ||
| return output; | ||
| } | ||
|
|
||
| input = input.trim(); | ||
| if (input === '') { | ||
| return output; | ||
| } | ||
|
|
||
| if (!input.startsWith(PROTOCOL_HTTP) && !input.startsWith(PROTOCOL_HTTPS)) { | ||
| input = PROTOCOL_HTTPS + input; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't you add a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't seem to be required by the |
||
| } | ||
|
|
||
| let url; | ||
| try { | ||
| url = new URL(input); | ||
| } catch (e) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be useful to log a warning, you think? |
||
| return output; | ||
| } | ||
|
|
||
| const { hostname, port, protocol } = url; | ||
|
|
||
| output.host = hostname; | ||
| output.port = port; | ||
| output.secure = protocol === PROTOCOL_HTTPS; | ||
|
|
||
| return output; | ||
| }; | ||
|
|
||
| module.exports = { | ||
| parseURL | ||
| }; | ||
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.
As mentionned in the global comments, I think here I would handle the case of not specifying a protocol.