tld.js

repository·master·Indexed 19 days ago

https://github.com/thom4parisot/tld.js

A high-performance Node.js module for parsing and analyzing domain names, subdomains, and TLDs using Mozilla's Public Suffix List. It provides a comprehensive parse() method and single-purpose utilities like getDomain(), getSubdomain(), and isValidHostname() to extract and validate URL properties.

Tokens
2.6K
Snippets
14
Records
15
Agent score
65%

What's inside tldjs

  1. How to handle localhost and custom hostnames

    master

    By default, getDomain and getSubdomain only work with known and valid TLDs. Because localhost is not a registered TLD, these methods will return null for it.

    To support custom hostnames or localhost, use tldjs.fromUserSettings() to create a new instance with validHosts defined.

    const tldjs = require('tldjs');
    
    // Default behavior
    tldjs.getDomain('localhost'); // null
    
    // Custom behavior
    const myTldjs = tldjs.fromUserSettings({
      validHosts: ['localhost']
    });
    
    myTldjs.getDomain('localhost'); // 'localhost'
    myTldjs.getSubdomain('vhost.localhost'); // 'vhost'
  2. Install tld.js

    master

    You can install tldjs via npm. If you require the most up-to-date list of well-known TLDs (Public Suffix List), use the --tldjs-update-rules flag during installation.

    # Regular install
    npm install --save tldjs
    
    # Install with updated TLD rules
    npm install --save tldjs --tldjs-update-rules
  3. Use single-purpose tldjs methods

    master

    If you only need a specific piece of information, use these shorthand methods. Most accept strings parseable by Node's require('url').parse.

    const { 
      tldExists, 
      getDomain, 
      getSubdomain, 
      getPublicSuffix, 
      isValidHostname 
    } = require('tldjs');
  4. Use tldjs.parse() to extract URL properties

    master

    The tldjs.parse() method returns an object containing various properties about a given URL or hostname. This is the most comprehensive way to inspect a domain string.

    const tldjs = require('tldjs');
    
    tldjs.parse('https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv');
    // Returns:
    // {
    //   hostname: 'spark-public.s3.amazonaws.com',
    //   isValid: true,
    //   isIp: false,
    //   tldExists: true,
    //   publicSuffix: 's3.amazonaws.com',
    //   domain: 'spark-public.s3.amazonaws.com',
    //   subdomain: ''
    // }
  5. Reference: tldjs.parse() return object properties

    master

    The object returned by tldjs.parse() contains the following properties:

    | Property Name | Type | Description |
    | --- | --- | --- |
    | `hostname` | `String` | The extracted hostname |
    | `isValid` | `Boolean` | Is the hostname valid according to the RFC? |
    | `tldExists` | `Boolean` | Is the TLD well-known or not? |
    | `publicSuffix`| `String` | The public suffix |
    | `domain` | `String` | The fully qualified domain |
    | `subdomain` | `String` | The subdomain |
  6. Use single-purpose methods for specific extraction tasks

    master

    If you only need one specific part of a URL, tldjs provides convenience wrappers that call parse() with the appropriate early-stop step. These are more efficient than calling parse() and manually accessing a property.

    • tldExists(url): Returns boolean. Checks if the TLD exists.
    • getPublicSuffix(url): Returns string. Extracts the public suffix.
    • getDomain(url): Returns string | null. Extracts the domain.
    • getSubdomain(url): Returns string. Extracts the subdomain.
    • isValidHostname(url): Returns boolean. Checks if the hostname is valid.
    • extractHostname(url): Returns string. Extracts the hostname from a URL.
    const tldjs = require('tldjs');
    
    const domain = tldjs.getDomain('https://sub.example.com');
    const suffix = tldjs.getPublicSuffix('example.co.uk');
    const exists = tldjs.tldExists('not-a-real-tld');
  7. Use `tldjs.parse()` to extract full hostname information

    master

    The parse(url, [_step]) method is the primary high-level API for analyzing a URL or hostname. It extracts the hostname and then performs a sequence of checks (IP validation, hostname validity, TLD existence, public suffix extraction, domain extraction, and subdomain extraction).

    To optimize performance, you can provide a _step argument to stop processing early once a specific piece of information is found. This simulates laziness to avoid unnecessary computation.

    Available Steps:

    • 1 (TLD_EXISTS): Stops after checking if the TLD exists.
    • 2 (PUBLIC_SUFFIX): Stops after extracting the public suffix.
    • 3 (DOMAIN): Stops after extracting the domain.
    • 4 (SUB_DOMAIN): Stops after extracting the subdomain.
    • 5 (ALL): Performs the full analysis (default).

    Returns a ParseResult object:

    • hostname: string | null
    • isValid: boolean
    • isIp: boolean
    • tldExists: boolean
    • publicSuffix: string | null
    • domain: string | null
    • subdomain: string | null
    const tldjs = require('tldjs');
    
    // Full parse
    const result = tldjs.parse('https://sub.example.co.uk/path');
    console.log(result.domain); // 'example.co.uk'
    
    // Early stop for performance (only check if TLD exists)
    const quickResult = tldjs.parse('example.com', 1);
  8. Configure `tldjs` with `fromUserSettings()`

    master

    You can create a custom instance of tldjs with specific rules or behaviors using fromUserSettings(options). This is useful if you want to provide your own TLD rules, a custom list of valid hosts, or a custom hostname extraction logic.

    FactoryOptions object:

    • rules: The suffix trie rules (from suffix-trie.js).
    • validHosts: An array of strings representing valid hosts.
    • extractHostname: A function (string) => string | null used to pull the hostname from a URL.
    const tldjs = require('tldjs');
    
    const customTldjs = tldjs.fromUserSettings({
      validHosts: ['example.com'],
      extractHostname: (url) => { /* custom logic */ }
    });
    
    const result = customTldjs.parse('https://example.com');
  9. ParseResult data structure

    master

    The ParseResult object is the standard output for the parse() method. It contains the following fields:

    FieldTypeDescription
    hostnamestring | nullThe extracted hostname from the input URL.
    isValidbooleanWhether the hostname is valid.
    isIpbooleanWhether the hostname is an IP address.
    tldExistsbooleanWhether the TLD exists in the ruleset.
    publicSuffixstring | nullThe extracted public suffix (e.g., co.uk).
    domainstring | nullThe extracted domain (e.g., example.co.uk).
    subdomainstring | nullThe extracted subdomain.
  10. Get the public suffix with getPublicSuffix()

    master

    The getPublicSuffix() method returns the public suffix for a given string.

    const { getPublicSuffix } = require('tldjs');
    
    getPublicSuffix('google.co.uk');     // 'co.uk'
    getPublicSuffix('s3.amazonaws.com'); // 's3.amazonaws.com'
  11. Get the subdomain with getSubdomain()

    master

    The getSubdomain() method returns the complete subdomain for a given string.

    const { getSubdomain } = require('tldjs');
    
    getSubdomain('fr.google.com');          // 'fr'
    getSubdomain('moar.foo.google.co.uk'); // 'moar.foo'
  12. Check if a TLD is well-known with tldExists()

    master

    The tldExists() method checks if the TLD in a given hostname is part of the well-known public suffix list.

    const { tldExists } = require('tldjs');
    
    tldExists('google.com');      // true
    tldExists('google.local');    // false
    tldExists('co.uk');           // true