All Projects → oncletom → Tld.js

oncletom / Tld.js

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Tld.js

Searchwithmybrowser
Open Cortana searches with your default browser.
Stars: ✭ 285 (-28.57%)
Mutual labels:  uri, browser
Nager.publicsuffix
.NET publicsuffix domain parser
Stars: ✭ 67 (-83.21%)
Mutual labels:  validation-library, subdomain
Lynket Browser
🌐 A better browser for Android using the Custom Tab protocol. Previously called Chromer.
Stars: ✭ 364 (-8.77%)
Mutual labels:  browser
Filer
Node-like file system for browsers
Stars: ✭ 389 (-2.51%)
Mutual labels:  browser
Kotlin Result
A multiplatform Result monad for modelling success or failure operations.
Stars: ✭ 369 (-7.52%)
Mutual labels:  browser
Lulumi Browser
Lulumi-browser is a lightweight browser coded with Vue.js 2 and Electron.
Stars: ✭ 367 (-8.02%)
Mutual labels:  browser
Nightmare
A high-level browser automation library.
Stars: ✭ 19,067 (+4678.7%)
Mutual labels:  browser
Jquery.scrollto
Lightweight, cross-browser and highly customizable animated scrolling with jQuery
Stars: ✭ 3,609 (+804.51%)
Mutual labels:  browser
Pdfjs
A Portable Document Format (PDF) generation library targeting both the server- and client-side.
Stars: ✭ 395 (-1%)
Mutual labels:  browser
React Uploady
Modern file uploading - components & hooks for React
Stars: ✭ 372 (-6.77%)
Mutual labels:  browser
Js Dos
The best API for running dos programs in browser
Stars: ✭ 385 (-3.51%)
Mutual labels:  browser
Agent
👮 A PHP desktop/mobile user agent parser with support for Laravel, based on Mobiledetect
Stars: ✭ 3,891 (+875.19%)
Mutual labels:  browser
Webcatalog App
Official WebCatalog app.
Stars: ✭ 369 (-7.52%)
Mutual labels:  browser
Servo
The Servo Browser Engine
Stars: ✭ 20,469 (+5030.08%)
Mutual labels:  browser
Open Browser.vim
Open URI with your favorite browser from your most favorite editor
Stars: ✭ 364 (-8.77%)
Mutual labels:  browser
Webrunlocal
PluginOK(牛插)中间件是一个实现网页浏览器(Web Browser)与本地程序(Local App)之间进行双向调用的低成本、强兼容、安全可控、轻量级、易集成、可扩展、跨浏览器的原生小程序系统。通过此中间件可实现网页前端JS脚本无障碍操作本地电脑各种硬件、调用本地系统API及相关组件功能,可彻底解决DLL模块、ActiveX控件及自动化程序(如微软Office、金山WPS、AutoCAD等)在Chrome、Edge、360、FireFox、IE、Opera、QQ、搜狗等浏览器各版本中的嵌入使用问题,媲美原Java Applet的效果
Stars: ✭ 391 (-2.01%)
Mutual labels:  browser
Uxp
This is a GitHub mirror of the Unified XUL Platform. The main repository can be found at https://repo.palemoon.org/MoonchildProductions/UXP/
Stars: ✭ 363 (-9.02%)
Mutual labels:  browser
Undetected Chromedriver
Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)
Stars: ✭ 365 (-8.52%)
Mutual labels:  browser
Fuzzdata
Fuzzing resources for feeding various fuzzers with input. 🔧
Stars: ✭ 376 (-5.76%)
Mutual labels:  browser
Uap Php
PHP implementation of ua-parser
Stars: ✭ 397 (-0.5%)
Mutual labels:  browser

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:); // 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:); // 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:); // 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:); // 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].