tld.js
tld.js
is a Node.js module written in JavaScript to work against complex domain names, subdomains and well-known TLDs.
It answers with accuracy to questions like what is mail.google.com
's domain?, what is a.b.ide.kyoto.jp
's subdomain? and is https://big.data
's TLD a well-known one?.
tld.js
runs fast, is fully tested and is safe to use in the browser (with browserify, webpack and others). Because it relies on Mozilla's public suffix list, now is a good time to say thank you Mozilla!
Install
# Regular install npm install --save tldjs # You can update the list of well-known TLD during the install npm install --save tldjs --tldjs-update-rules
The latter is useful if you significantly rely on an up-to-date list of TLDs. You can list the recent changes (changes Atom Feed) to get a better idea of what is going on in the Public Suffix world.
Using It
const parse tldExists = ; // Checking only if TLD exists in URL or hostname// First TLD exists; the second does not.console;console; // Retrieving hostname related informations of a given URL;
👋 Try it your browser to see how it works.
⬇️ Read the documentation below to find out the available functions.
tldjs.parse()
This methods returns handy properties about a URL or a hostname.
const tldjs = ; tldjs;// { hostname: 'spark-public.s3.amazonaws.com',// isValid: true,// isIp: false,// tldExists: true,// publicSuffix: 's3.amazonaws.com',// domain: 'spark-public.s3.amazonaws.com',// subdomain: ''// } tldjs;// { hostname: 'domain.unknown',// isValid: true,// isIp: false,// tldExists: false,// publicSuffix: 'unknown',// domain: 'domain.unknown',// subdomain: ''// } tldjs// { hostname: '192.168.0.0',// isValid: true,// isIp: true,// tldExists: false,// publicSuffix: null,// domain: null,// subdomain: null// }
Property Name | Type | |
---|---|---|
hostname |
String |
|
isValid |
Boolean |
Is the hostname valid according to the RFC? |
tldExists |
Boolean |
Is the TLD well-known or not? |
publicSuffix |
String |
|
domain |
String |
|
subdomain |
String |
Single purpose methods
These methods are shorthands if you want to retrieve only a single value.
tldExists()
Checks if the TLD is well-known for a given hostname — parseable with require('url').parse
.
const tldExists = tldjs; ; // returns `true`; // returns `false` (not an explicit registered TLD); // returns `true`; // returns `true`; // returns `true` (because `uk` is a valid TLD); // returns `true` (still because `uk` is a valid TLD); // returns `true` (still because `uk` is a valid TLD); // returns `true`
getDomain()
Returns the fully qualified domain from a given string — parseable with require('url').parse
.
const getDomain = tldjs; ; // returns `google.com`; // returns `google.com`; // returns `google.google`; // returns `google.co.uk`; // returns `t.co`; // returns `t.co`; // returns `example.co.uk`
getSubdomain()
Returns the complete subdomain for a given string — parseable with require('url').parse
.
const getSubdomain = tldjs; ; // returns ``; // returns `fr`; // returns ``; // returns `foo`; // returns `moar.foo`; // returns ``; // returns `fr`; // returns `secure`
getPublicSuffix()
Returns the public suffix for a given string — parseable with require('url').parse
.
const getPublicSuffix = tldjs; ; // returns `com`; // returns `com`; // returns `co.uk`; // returns `s3.amazonaws.com`; // returns `unknown`
isValid()
Checks the validity of a given string — parseable with require('url').parse
.
It does not check if the TLD is well-known.
const isValid = tldjs; ; // returns `true`; // returns `false`; // returns `true`; // returns `false`; // returns `true` // returns `true`
Troubleshooting
localhost
and custom hostnames
Retrieving subdomain of tld.js
methods getDomain
and getSubdomain
are designed to work only with known and valid TLDs.
This way, you can trust what a domain is.
localhost
is a valid hostname but not a TLD. Although you can instanciate your own flavour of tld.js
with additional valid hosts:
const tldjs = ; tldjs; // returns nulltldjs; // returns null const myTldjs = tldjs; myTldjs; // returns 'localhost'myTldjs; // returns 'vhost'
Updating the TLDs List
Many libraries offer a list of TLDs. But, are they up-to-date? And how to update them?
tld.js
bundles a list of known TLDs but this list can become outdated.
This is especially true if the package have not been updated on npm for a while.
Hopefully for you, even if I'm flying over the world, if I've lost my Internet connection or even if you do manage your own list, you can update it by yourself, painlessly.
How? By passing the --tldjs-update-rules
to your npm install
command:
# anytime you reinstall your project npm install --tldjs-update-rules # or if you add the dependency to your project npm install --save tldjs --tldjs-update-rules
Open an issue to request an update of the bundled TLDs.
Contributing
Provide a pull request (with tested code) to include your work in this main project. Issues may be awaiting for help so feel free to give a hand, with code or ideas.
Performances
tld.js
is fast, but keep in mind that it might vary depending on your own
use-case. Because the library tried to be smart, the speed can be drastically
different depending on the input (it will be faster if you provide an already
cleaned hostname, compared to a random URL).
On an Intel i7-6600U (2,60-3,40 GHz):
For already cleaned hostnames
Methods | ops/sec |
---|---|
isValid |
~8,700,000 |
extractHostname |
~8,100,000 |
tldExists |
~2,000,000 |
getPublicSuffix |
~1,130,000 |
getDomain |
~1,000,000 |
getSubdomain |
~1,000,000 |
parse |
~850,000 |
For random URLs
Methods | ops/sec |
---|---|
isValid |
~25,400,000 |
extractHostname |
~400,000 |
tldExists |
~310,000 |
getPublicSuffix |
~240,000 |
getDomain |
~240,000 |
getSubdomain |
~240,000 |
parse |
~230,000 |
You can measure the performance of tld.js
on your hardware by running the following command:
npm run benchmark
Notice: if this is not fast enough for your use-case, keep in mind that you can
provide your own extractHostname
function (which is the bottleneck in
this benchmark) to tld.js
.
Contributors
This project exists thanks to all the people who contribute. [Contribute].
Backers
Thank you to all our backers! 🙏 [Become a backer]
Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]