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

Parse undefined / null => isValid:true ? #131

Open
lqzhgood opened this issue Feb 7, 2019 · 3 comments · May be fixed by #132
Open

Parse undefined / null => isValid:true ? #131

lqzhgood opened this issue Feb 7, 2019 · 3 comments · May be fixed by #132

Comments

@lqzhgood
Copy link

lqzhgood commented Feb 7, 2019

tldjs.parse(undefined)
isValid:true

tldjs.parse(null)
isValid:true

@remusao
Copy link
Collaborator

remusao commented Feb 7, 2019

Hi @lqzhgood, thanks for taking the time to open an issue. Unfortunately this is "expected" behavior. For backward compatibility reasons any value passed to tld.js which is not a string, will be converted to a string. That happens there: https://github.com/oncletom/tld.js/blob/master/lib/clean-host.js#L76

If the values you are manipulated are already hostnames, you can override this step completely using a custom extractHostname function:

const tldjs = require('tldjs');

tldjs.parse(undefined);
// { hostname: 'undefined',
//   isValid: true,
//   isIp: false,
//   tldExists: false,
//   publicSuffix: null,
//   domain: null,
//   subdomain: null }

const custom = tldjs.fromUserSettings({ extractHostname: (hostname) => hostname });
custom.parse(undefined)
// { hostname: undefined,
//   isValid: false,
//   isIp: false,
//   tldExists: false,
//   publicSuffix: null,
//   domain: null,
//   subdomain: null }

Note that you could also use this customization to implement your own URL parsing (e.g.: using the URL API from Node.js):

const URL = require('url').URL;
const tldjs = require('tldjs').fromUserSettings({ extractHostname: (url) => new URL(url).hostname });

tldjs.parse(undefined);
// Thrown:
// { TypeError [ERR_INVALID_URL]: Invalid URL: undefined
//     at onParseError (internal/url.js:241:17)
//     at new URL (internal/url.js:319:5)
//     at extractHostname (repl:1:60)
//     at Object.parse (/home/remi/dev/repositories/public/tld.js/index.js:52:17) input: 'undefined' }

tldjs.parse('https://foo.com/bar')
// { hostname: 'foo.com',
//   isValid: true,
//   isIp: false,
//   tldExists: true,
//   publicSuffix: 'com',
//   domain: 'foo.com',
//   subdomain: '' }

I hope this helps!

@lqzhgood
Copy link
Author

lqzhgood commented Feb 7, 2019

in https://github.com/oncletom/tld.js/blob/master/lib/clean-host.js#L68

extractHostname Why not first check if the url is correct? like this

module.exports = function extractHostname(value) {
  if (!isNaN(value)) {
    return null;
  }
  if (typeof value !== 'string') {
    return null;
  }

  // First check if `value` is already a valid hostname.
  if (isValid(value)) {
    return trimTrailingDots(value);
  }

  var url = value;

  var needsTrimming = checkTrimmingNeeded(url);
......
tld.parse(null)
tld.parse(undefined)
tld.parse(() => {})
{
    "hostname": null,
    "isValid": false,
    "isIp": false,
    "tldExists": false,
    "publicSuffix": null,
    "domain": null,
    "subdomain": null
}

That looks fine. And there's nothing wrong with that run npm run test

And I have one more question that I'm not sure about。
1234 Are pure Numbers is domain names in RFC?
if not . https://github.com/oncletom/tld.js/blob/master/lib/is-valid.js#L40 add judge.

module.exports = function isValid(hostname) {
  if (!isNaN(hostname)) {
    return false;
  }
  if (typeof hostname !== 'string') {
    return false;
  }
...

@remusao
Copy link
Collaborator

remusao commented Feb 7, 2019

@lqzhgood All good suggestions. Feel free to make a PR to the repository with improvements, but there was not much activity around lately. I currently maintain my own fork of this project.

lqzhgood added a commit to lqzhgood/tld.js that referenced this issue Feb 7, 2019
@lqzhgood lqzhgood linked a pull request Feb 7, 2019 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants