Gathering detailed insights and metrics for tldts
Gathering detailed insights and metrics for tldts
Gathering detailed insights and metrics for tldts
Gathering detailed insights and metrics for tldts
tldts-core
tldts core primitives (internal module)
tldts-icann
Library to work against complex domain names, subdomains and URIs. Only contains ICANN section.
tldts-experimental
Library to work against complex domain names, subdomains and URIs.
parse-domains
Domain parser that automatically updates its suffix lists from Mozilla's publicsuffix.org
JavaScript Library to extract domains, subdomains and public suffixes from complex URIs.
npm install tldts
99.6
Supply Chain
100
Quality
95.3
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
489 Stars
2,774 Commits
19 Forks
3 Watching
25 Branches
15 Contributors
Updated on 26 Nov 2024
Minified
Minified + Gzipped
TypeScript (95.54%)
JavaScript (4.31%)
Shell (0.11%)
Makefile (0.04%)
Cumulative downloads
Total Downloads
Last day
-13%
640,071
Compared to previous day
Last week
4.7%
4,051,932
Compared to previous week
Last month
97.9%
15,153,655
Compared to previous month
Last year
592.1%
32,045,968
Compared to previous year
tldts
is a JavaScript library to extract hostnames, domains, public suffixes, top-level domains and subdomains from URLs.
Features:
umd
, esm
, cjs
bundles and type definitions⚠️ If you are migrating to tldts
from another library like psl
, make sure to check the migration section.
1npm install --save tldts
Using the command-line interface:
1$ npx tldts 'http://www.writethedocs.org/conf/eu/2017/' 2{ 3 "domain": "writethedocs.org", 4 "domainWithoutSuffix": "writethedocs", 5 "hostname": "www.writethedocs.org", 6 "isIcann": true, 7 "isIp": false, 8 "isPrivate": false, 9 "publicSuffix": "org", 10 "subdomain": "www" 11}
Or from the command-line in batch:
1$ echo "http://www.writethedocs.org/\nhttps://example.com" | npx tldts 2{ 3 "domain": "writethedocs.org", 4 "domainWithoutSuffix": "writethedocs", 5 "hostname": "www.writethedocs.org", 6 "isIcann": true, 7 "isIp": false, 8 "isPrivate": false, 9 "publicSuffix": "org", 10 "subdomain": "www" 11} 12{ 13 "domain": "example.com", 14 "domainWithoutSuffix": "example", 15 "hostname": "example.com", 16 "isIcann": true, 17 "isIp": false, 18 "isPrivate": false, 19 "publicSuffix": "com", 20 "subdomain": "" 21}
Programmatically:
1const { parse } = require('tldts'); 2 3// Retrieving hostname related informations of a given URL 4parse('http://www.writethedocs.org/conf/eu/2017/'); 5// { domain: 'writethedocs.org', 6// domainWithoutSuffix: 'writethedocs', 7// hostname: 'www.writethedocs.org', 8// isIcann: true, 9// isIp: false, 10// isPrivate: false, 11// publicSuffix: 'org', 12// subdomain: 'www' }
Modern ES6 modules import is also supported:
1import { parse } from 'tldts';
Alternatively, you can try it directly in your browser here: https://npm.runkit.com/tldts
Check README.md for more details about the API.
TL;DR—here is a quick overview of how to use tldts
to match the default behavior of the psl
library. Skip to after the tables for a more detailed explanation.
Parsing a hostname | |
---|---|
tldts | tldts.parse('spark-public.s3.amazonaws.com', { allowPrivateDomains: true }) |
psl | psl.parse('spark-public.s3.amazonaws.com') |
Note | Make sure to include { allowPrivateDomains: true } to consider private suffixes |
Parsing a URL | |
---|---|
tldts | tldts.parse('https://spark-public.s3.amazonaws.com/data', { allowPrivateDomains: true }) |
psl | psl.parse(new URL('https://spark-public.s3.amazonaws.com/data').hostname) |
Note | No need to extract hostnames from URLs, tldts can do that for you |
Getting the domain | |
---|---|
tldts | tldts.getDomain('spark-public.s3.amazonaws.com', { allowPrivateDomains: true }) |
psl | psl.get('spark-public.s3.amazonaws.com') |
Note | Using specific functions like getDomain are more efficient then relying on parse |
Getting the Public Suffix | |
---|---|
tldts | tldts.getPublicSuffix('spark-public.s3.amazonaws.com', { allowPrivateDomains: true }) |
psl | psl.parse('spark-public.s3.amazonaws.com').tld |
Explanation. There are multiple libraries which can be used to parse URIs
based on the Public Suffix List. Not all these libraries offer the same
behavior by default and depending on your particular use-case, this can matter.
When migrating from another library to tldts
, make sure to read this section
to preserve the same behavior.
The biggest difference between tldts
's default behavior and some other
libraries like psl
has to do with which suffixes are considered by default.
The default for tldts
is to only consider the ICANN section and ignore the
Private section.
Consider this example using the unmaintained psl
library:
1const psl = require('psl'); 2 3psl.parse('https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv') 4// { 5// input: 'spark-public.s3.amazonaws.com', 6// tld: 's3.amazonaws.com', <<< Public Suffix is from Private section 7// sld: 'spark-public', 8// domain: 'spark-public.s3.amazonaws.com', 9// subdomain: null, 10// listed: true 11// }
And now with tldts
:
1const { parse } = require('tldts'); 2 3parse('spark-public.s3.amazonaws.com'); 4// { 5// domain: 'amazonaws.com', 6// domainWithoutSuffix: 'amazonaws', 7// hostname: 'spark-public.s3.amazonaws.com', 8// isIcann: true, 9// isIp: false, 10// isPrivate: false, 11// publicSuffix: 'com', <<< By default, use Public Suffix from ICANN section 12// subdomain: 'spark-public.s3' 13// }
To get the same behavior, you need to pass the { allowPrivateDomains: true }
option:
1const { parse } = require('tldts'); 2 3parse('spark-public.s3.amazonaws.com', { allowPrivateDomains: true }); 4// { 5// domain: 'spark-public.s3.amazonaws.com', 6// domainWithoutSuffix: 'spark-public', 7// hostname: 'spark-public.s3.amazonaws.com', 8// isIcann: false, 9// isIp: false, 10// isPrivate: true, 11// publicSuffix: 's3.amazonaws.com', <<< Private Public Suffix is used 12// subdomain: '' 13// }
Here are some other differences which can make your life easy. tldts
accepts
both hostnames and URLs as arguments, so you do not need to parse your
inputs before handing them over to tldts
:
1const { parse } = require('tldts'); 2 3// Both are fine! 4parse('spark-public.s3.amazonaws.com', { allowPrivateDomains: true }); 5parse('https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv', { allowPrivateDomains: true });
tldts
offers dedicated methods to extract the Public Suffix, domain,
subdomain, etc. without having to rely on the more generic parse
function.
This is also more efficient than calling parse
, because less work as to be
done.
1const { 2 getHostname, 3 getDomain, 4 getPublicSuffix, 5 getSubdomain, 6 getDomainWithoutSuffix, 7} = require('tldts'); 8 9const url = 'https://spark-public.s3.amazonaws.com'; 10 11console.log(getHostname(url)); // spark-public.s3.amazonaws.com 12console.log(getDomain(url, { allowPrivateDomains: true })); // spark-public.s3.amazonaws.com 13console.log(getPublicSuffix(url, { allowPrivateDomains: true })); // s3.amazonaws.com 14console.log(getSubdomain(url, { allowPrivateDomains: true })); // '' 15console.log(getDomainWithoutSuffix(url, { allowPrivateDomains: true })); // spark-public
tldts
is based upon the excellent tld.js
library and would not exist without
the many contributors who worked on the project.
This project would not be possible without the amazing Mozilla's public suffix list either. Thank you for your hard work!
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
30 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 0/17 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More