Extract the TLD from a URL against the public suffix list.
Getting started
Install the package using your preferred package manager:
npm install extract-tld
You can now use the parser:
import { parseUrl } from 'extract-tld';
parseUrl('https://google.com');
Private TLDs
Private TLDs are supported:
import { parseUrl } from 'extract-tld';
parseUrl('test.compute.amazonaws.com', { allowPrivateTLD: true });
Unknown TLDs
You can allow unknown TLDs by specifying the configuration option:
import { parseUrl } from 'extract-tld';
parseUrl('https://somewhere.local');
parseUrl('https://somewhere.local', { allowUnknownTLD: true });
Development
Ensure you have pnpm installed
- Clone this repository
- Run
pnpm install
Contributing
All contributions are welcome - feel free to open a PR or issue :)
List maintenance
There is a script that will fetch the latest public suffix list and transform it into a format usable by this library. You can run the script by running pnmpm updateList
.
Ideally, this script would run once a day and update tlds.json accordingly, but the work for that hasn't been done yet.
Credits
- tld-extract
- This is the main basis of inspiration for this library. I've basically taken this library and modernised it (and added some nice things like TypeScript support) as the author seems to be unreachable.