Gathering detailed insights and metrics for @jesseditson/tldjs
Gathering detailed insights and metrics for @jesseditson/tldjs
Gathering detailed insights and metrics for @jesseditson/tldjs
Gathering detailed insights and metrics for @jesseditson/tldjs
JavaScript API to work easily with complex domain names, subdomains and well-known TLDs.
npm install @jesseditson/tldjs
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
461 Stars
364 Commits
56 Forks
11 Watching
6 Branches
15 Contributors
Updated on 31 Oct 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-40%
9
Compared to previous day
Last week
375%
266
Compared to previous week
Last month
-7.9%
480
Compared to previous month
Last year
0%
1,001
Compared to previous year
1
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!
1# Regular install 2npm install --save tldjs 3 4# You can update the list of well-known TLD during the install 5npm 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.
1const {parse, tldExists} = require('tldjs'); 2 3// Checking only if TLD exists in URL or hostname 4// First TLD exists; the second does not. 5console.log(tldExists('https://www.bbc')); 6console.log(tldExists('tld.unknown')); 7 8// Retrieving hostname related informations of a given URL 9parse('http://www.writethedocs.org/conf/eu/2017/');
👋 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.
1const tldjs = require('tldjs'); 2 3tldjs.parse('https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv'); 4// { hostname: 'spark-public.s3.amazonaws.com', 5// isValid: true, 6// isIp: false, 7// tldExists: true, 8// publicSuffix: 's3.amazonaws.com', 9// domain: 'spark-public.s3.amazonaws.com', 10// subdomain: '' 11// } 12 13tldjs.parse('gopher://domain.unknown/'); 14// { hostname: 'domain.unknown', 15// isValid: true, 16// isIp: false, 17// tldExists: false, 18// publicSuffix: 'unknown', 19// domain: 'domain.unknown', 20// subdomain: '' 21// } 22 23tldjs.parse('https://192.168.0.0') 24// { hostname: '192.168.0.0', 25// isValid: true, 26// isIp: true, 27// tldExists: false, 28// publicSuffix: null, 29// domain: null, 30// subdomain: null 31// }
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 |
These methods are shorthands if you want to retrieve only a single value.
Checks if the TLD is well-known for a given hostname — parseable with require('url').parse
.
1const { tldExists } = tldjs; 2 3tldExists('google.com'); // returns `true` 4tldExists('google.local'); // returns `false` (not an explicit registered TLD) 5tldExists('com'); // returns `true` 6tldExists('uk'); // returns `true` 7tldExists('co.uk'); // returns `true` (because `uk` is a valid TLD) 8tldExists('amazon.fancy.uk'); // returns `true` (still because `uk` is a valid TLD) 9tldExists('amazon.co.uk'); // returns `true` (still because `uk` is a valid TLD) 10tldExists('https://user:password@example.co.uk:8080/some/path?and&query#hash'); // returns `true`
Returns the fully qualified domain from a given string — parseable with require('url').parse
.
1const { getDomain } = tldjs; 2 3getDomain('google.com'); // returns `google.com` 4getDomain('fr.google.com'); // returns `google.com` 5getDomain('fr.google.google'); // returns `google.google` 6getDomain('foo.google.co.uk'); // returns `google.co.uk` 7getDomain('t.co'); // returns `t.co` 8getDomain('fr.t.co'); // returns `t.co` 9getDomain('https://user:password@example.co.uk:8080/some/path?and&query#hash'); // returns `example.co.uk`
Returns the complete subdomain for a given string — parseable with require('url').parse
.
1const { getSubdomain } = tldjs; 2 3getSubdomain('google.com'); // returns `` 4getSubdomain('fr.google.com'); // returns `fr` 5getSubdomain('google.co.uk'); // returns `` 6getSubdomain('foo.google.co.uk'); // returns `foo` 7getSubdomain('moar.foo.google.co.uk'); // returns `moar.foo` 8getSubdomain('t.co'); // returns `` 9getSubdomain('fr.t.co'); // returns `fr` 10getSubdomain('https://user:password@secure.example.co.uk:443/some/path?and&query#hash'); // returns `secure`
Returns the public suffix for a given string — parseable with require('url').parse
.
1const { getPublicSuffix } = tldjs; 2 3getPublicSuffix('google.com'); // returns `com` 4getPublicSuffix('fr.google.com'); // returns `com` 5getPublicSuffix('google.co.uk'); // returns `co.uk` 6getPublicSuffix('s3.amazonaws.com'); // returns `s3.amazonaws.com` 7getPublicSuffix('tld.is.unknown'); // returns `unknown`
Checks if the given string is a valid hostname according to RFC 1035. It does not check if the TLD is well-known.
1const { isValidHostname } = tldjs; 2 3isValidHostname('google.com'); // returns `true` 4isValidHostname('.google.com'); // returns `false` 5isValidHostname('my.fake.domain'); // returns `true` 6isValidHostname('localhost'); // returns `false` 7isValidHostname('https://user:password@example.co.uk:8080/some/path?and&query#hash'); // returns `false` 8isValidHostname('192.168.0.0') // returns `true`
localhost
and custom hostnamestld.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:
1const tldjs = require('tldjs'); 2 3tldjs.getDomain('localhost'); // returns null 4tldjs.getSubdomain('vhost.localhost'); // returns null 5 6const myTldjs = tldjs.fromUserSettings({ 7 validHosts: ['localhost'] 8}); 9 10myTldjs.getDomain('localhost'); // returns 'localhost' 11myTldjs.getSubdomain('vhost.localhost'); // returns 'vhost'
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:
1# anytime you reinstall your project 2npm install --tldjs-update-rules 3 4# or if you add the dependency to your project 5npm install --save tldjs --tldjs-update-rules
Open an issue to request an update of the bundled TLDs.
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.
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):
Methods | ops/sec |
---|---|
isValidHostname | ~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 |
Methods | ops/sec |
---|---|
isValidHostname | ~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:
1npm 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
.
This project exists thanks to all the people who contribute. [Contribute].
Thank you to all our backers! 🙏 [Become a backer]
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
No vulnerabilities found.
No security vulnerabilities found.