• Stars
    star
    454
  • Rank 94,241 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 12 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript API to work easily with complex domain names, subdomains and well-known TLDs.

tld.js Backers on Open Collective Sponsors on Open Collective Build Status

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} = require('tldjs');

// Checking only if TLD exists in URL or hostname
// First TLD exists; the second does not.
console.log(tldExists('https://www.bbc'));
console.log(tldExists('tld.unknown'));

// Retrieving hostname related informations of a given URL
parse('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.

const tldjs = require('tldjs');

tldjs.parse('https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv');
// { hostname: 'spark-public.s3.amazonaws.com',
//   isValid: true,
//   isIp: false,
//   tldExists: true,
//   publicSuffix: 's3.amazonaws.com',
//   domain: 'spark-public.s3.amazonaws.com',
//   subdomain: ''
// }

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

tldjs.parse('https://192.168.0.0')
// { 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;

tldExists('google.com');      // returns `true`
tldExists('google.local');    // returns `false` (not an explicit registered TLD)
tldExists('com');             // returns `true`
tldExists('uk');              // returns `true`
tldExists('co.uk');           // returns `true` (because `uk` is a valid TLD)
tldExists('amazon.fancy.uk'); // returns `true` (still because `uk` is a valid TLD)
tldExists('amazon.co.uk');    // returns `true` (still because `uk` is a valid TLD)
tldExists('https://user:[email protected]:8080/some/path?and&query#hash'); // returns `true`

getDomain()

Returns the fully qualified domain from a given string — parseable with require('url').parse.

const { getDomain } = tldjs;

getDomain('google.com');        // returns `google.com`
getDomain('fr.google.com');     // returns `google.com`
getDomain('fr.google.google');  // returns `google.google`
getDomain('foo.google.co.uk');  // returns `google.co.uk`
getDomain('t.co');              // returns `t.co`
getDomain('fr.t.co');           // returns `t.co`
getDomain('https://user:[email protected]:8080/some/path?and&query#hash'); // returns `example.co.uk`

getSubdomain()

Returns the complete subdomain for a given string — parseable with require('url').parse.

const { getSubdomain } = tldjs;

getSubdomain('google.com');             // returns ``
getSubdomain('fr.google.com');          // returns `fr`
getSubdomain('google.co.uk');           // returns ``
getSubdomain('foo.google.co.uk');       // returns `foo`
getSubdomain('moar.foo.google.co.uk');  // returns `moar.foo`
getSubdomain('t.co');                   // returns ``
getSubdomain('fr.t.co');                // returns `fr`
getSubdomain('https://user:[email protected]:443/some/path?and&query#hash'); // returns `secure`

getPublicSuffix()

Returns the public suffix for a given string — parseable with require('url').parse.

const { getPublicSuffix } = tldjs;

getPublicSuffix('google.com');       // returns `com`
getPublicSuffix('fr.google.com');    // returns `com`
getPublicSuffix('google.co.uk');     // returns `co.uk`
getPublicSuffix('s3.amazonaws.com'); // returns `s3.amazonaws.com`
getPublicSuffix('tld.is.unknown');   // returns `unknown`

isValidHostname()

Checks if the given string is a valid hostname according to RFC 1035. It does not check if the TLD is well-known.

const { isValidHostname } = tldjs;

isValidHostname('google.com');      // returns `true`
isValidHostname('.google.com');     // returns `false`
isValidHostname('my.fake.domain');  // returns `true`
isValidHostname('localhost');       // returns `false`
isValidHostname('https://user:[email protected]:8080/some/path?and&query#hash'); // returns `false`
isValidHostname('192.168.0.0')      // returns `true`

Troubleshooting

Retrieving subdomain of localhost and custom hostnames

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 = require('tldjs');

tldjs.getDomain('localhost');           // returns null
tldjs.getSubdomain('vhost.localhost');  // returns null

const myTldjs = tldjs.fromUserSettings({
  validHosts: ['localhost']
});

myTldjs.getDomain('localhost');           // returns 'localhost'
myTldjs.getSubdomain('vhost.localhost');  // 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
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

For random URLs

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:

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]

License

MIT License.

More Repositories

1

crx

A node.js command line app for packing Google Chrome extensions.
JavaScript
508
star
2

nodebook

📖 Livre publié aux Éditions Eyrolles • Première édition : Node.js v10 et npm v6.
JavaScript
304
star
3

grunt-crx

Grunt task used to package private Chrome Extensions.
JavaScript
118
star
4

wp-less

WordPress plugin which seemlessly compiles, caches and rebuilds your LESS stylesheets.
PHP
88
star
5

hexo-algolia

Index your hexo website content to Algolia Search.
JavaScript
83
star
6

webextension-fip

Minimalistic WebExtension to listen to FIP Radio, the best eclectic radio station (for Firefox, Chrome, Opera and Edge)
JavaScript
34
star
7

Imager.jsx

A React component for responsive images in desktop and mobile browsers. Featuring Imager.js.
JavaScript
32
star
8

mailto

Transform your HTML forms in beautiful mailto: links, form submission or XHR requests.
JavaScript
14
star
9

letsencrypt-alwaysdata

Update your SSL certs on alwaysdata via their REST API
Shell
12
star
10

coursera-ml

Setup to evolve through the Machine Learning course.
Makefile
10
star
11

detour.studio

Un outil de travail multi-disciplinaire pour cultiver des rituels de pratiques en pair à pair.
HTML
9
star
12

homebrew-brew

Homebrew formula to install cpdf and Lightroom Classic CC 5.7 on macOS.
Ruby
9
star
13

mindmeister-php

PHP library to access Mindmeister REST API (http://www.mindmeister.com/services/api)
PHP
9
star
14

asciidoctor-extension-interactive-runner

Turn your Asciidoc code listings into interactive playgrounds in web browsers.
JavaScript
8
star
15

Jobs

Want to apply for a dev job? Pull request!
7
star
16

thom4.net

A work in progress of my digital mind, my digital avatars and real-world artefacts.
HTML
7
star
17

vue-waveform-template

A Vue.js component to embed and annotate an audio file, visualized as a waveform, thanks to BBC Peaks.js
Vue
7
star
18

content-edit

HTML Content Edit (jQuery) plugin. To edit content with a more complex workflow than just an inline editable.
JavaScript
7
star
19

is-webview

A JavaScript library to indicate if we are dealing with a WebView or a Web page.
JavaScript
6
star
20

asciidoctor-prism-extension

Highlight code listings with Prism.js, server side, with no front-end dependencies.
JavaScript
6
star
21

talks

Public talks provided either in English or French, about JavaScript or human-centric topics.
5
star
22

11ty-stylo

Carnet web propulsé par Eleventy, et des données hébergées sur Stylo
JavaScript
5
star
23

asciidoctor-converter-opendocument

Convert Asciidoc documents to OpenDocument (`.odt`) files.
JavaScript
5
star
24

data.emunova.net

Contient les données qui permettent de générer une version statique du site Emu Nova.
JavaScript
4
star
25

photography

My own photography website. For leaner and nicer layouts than services out there.
JavaScript
4
star
26

react-inliner

React components rendered and inlined server-side.
JavaScript
4
star
27

html-game-of-life

The Game of Life, with HTML, JavaScript and DOM.
JavaScript
3
star
28

sfRedisCachePlugin

Redis cache backend for Symfony
PHP
3
star
29

uwa-vcub

UWA Widget for VCUB Bordeaux / CUB bike service
PHP
3
star
30

elevato.rs

Learning web technologies by fixing elevators
JavaScript
3
star
31

talktimer.js

Have to keep your speaker on time? Show them a countdown! Used at @sudweb.
JavaScript
3
star
32

m2-min-2020

Module Développement Web (MIN 306) du « Master innovation & transformation numérique »
HTML
2
star
33

emunova.net

Application frontend d'un site contributif dédié au rétrogaming et à l'émulation de jeux vidéo.
HTML
2
star
34

m2-min-2018

Cours de développement web pour le Master 2 « Innovation et Transformation Numérique » de TélécomParisTech.
HTML
2
star
35

print-pad

Turn a HackMD/CodiMD/HedgeDoc pad as a printable and paginated document. Self-hostable.
CSS
2
star
36

reactzine

A mobile first magasine prototype app using React.
JavaScript
2
star
37

bbc-pid

Simple JavaScript API to validate Programme Identifier (PID) syntax and to compute BBC URLs.
JavaScript
2
star
38

cyneticmonkey.com

Fortunately, it's not written in Leetspeak.
JavaScript
2
star
39

m2-min-2019

Module Développement Web (MIN 306) du « Master innovation & transformation numérique »
HTML
2
star
40

wp-amazon-widgets-shortcodes

Keep your time and save your money with these Amazon widgets shortcodes. Standard compliants, easy to use and so on!
PHP
2
star
41

reading-time-stream

Input a Stream text in, get a reading time out. A super fast Buffer-only reading time calculation.
JavaScript
1
star
42

yeswiki-release-action

Crée un artéfact distribuable d'une extension ou thème YesWiki
Shell
1
star
43

hexo-theme

Hexo theme for my portfolio and stuff.
JavaScript
1
star
44

test-website-repo-3796

1
star
45

app.emunova.net

The inevitable Heroku app to perform OAuth and CORS requests on the behalf of the user.
JavaScript
1
star
46

pandoc-notion-subpages-filter

Assemble Notion.so Export subpages into a single document with this Pandoc filter.
Lua
1
star
47

wordpress-mindmeister-shortcode

Easy Mindmeister embeding within WordPress posts.
PHP
1
star
48

wp-canalblog-importer

Imports a whole Canalblog blog into a WordPress instance.
PHP
1
star
49

everyday-sequence

Everyday in picture, one day at a time. Since 2012.
JavaScript
1
star
50

wp-meta-view-count

Post/page view count for WordPress working through server-cache.
PHP
1
star
51

input-search

Unit tested HTML5 polyfill for input[type="search"] element.
JavaScript
1
star
52

wp-theme-toolkit

Starter files for a good and easy WordPress theme
PHP
1
star
53

grunt-testing-workshop

Learning to refactor and test Grunt Tasks within BBC News and BBC R&D.
JavaScript
1
star
54

reveal-random-colors

Pimp your Reveal.js presentations by randomising the colours and fonts of the current slide.
JavaScript
1
star
55

behind-the-code-readme-article

A selection of ideas to write well structured and nicely formatted README — Behind the Code's source code of the same name article.
1
star
56

vagrant-opencaster-it950x

Vagrant OpenCaster + it950x configuration
Shell
1
star