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

Adds support for native URL parsing if node API is unavailable #149

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/clean-host.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@

var URL = require('url');
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe the intent would be clearer to rather test if the global URL is set and require it otherwise?

Or we could also rely directly on it since it is available since node@10.

What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

That makes sense - you'd probably know better than me if there are folks depending on this using ancient node versions, but as long as this came in a major bump IMO it'd be simplest to just use the global URL since it is available in all maintenance+ releases of node

var NODE_URL;
try {
NODE_URL = require('url');
} catch(e) {
// no-op - we have a fallback in parseURL
}
var isValid = require('./is-valid.js');

function parseURL(urlString) {
if (NODE_URL) {
return NODE_URL.parse(urlString, null, true);
}
return new URL(urlString);
}


/**
* Utility to cleanup the base host value. Also removes url fragments.
Expand Down Expand Up @@ -97,7 +109,7 @@ module.exports = function extractHostname(value) {
url = '//' + url;
}

var parts = URL.parse(url, null, true);
var parts = parseURL(url);
Copy link
Owner

Choose a reason for hiding this comment

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

👍🏻


if (parts.hostname) {
return trimTrailingDots(parts.hostname);
Expand Down