Gathering detailed insights and metrics for psl
Gathering detailed insights and metrics for psl
Gathering detailed insights and metrics for psl
Gathering detailed insights and metrics for psl
JavaScript domain name parser based on the Public Suffix List
npm install psl
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
388 Stars
287 Commits
80 Forks
4 Watching
3 Branches
9 Contributors
Updated on 28 Nov 2024
JavaScript (99.83%)
TypeScript (0.17%)
Cumulative downloads
Total Downloads
Last day
-7.3%
6,066,137
Compared to previous day
Last week
4.3%
37,074,137
Compared to previous week
Last month
18.9%
143,592,244
Compared to previous month
Last year
1.1%
1,432,872,071
Compared to previous year
1
psl
is a JavaScript
domain name parser based on the
Public Suffix List.
This implementation is tested against the test data hosted by Mozilla and kindly provided by Comodo.
Cross browser testing provided by
The Public Suffix List is a cross-vendor initiative to provide an accurate list of domain name suffixes.
The Public Suffix List is an initiative of the Mozilla Project, but is maintained as a community resource. It is available for use in any software, but was originally created to meet the needs of browser manufacturers.
A "public suffix" is one under which Internet users can directly register names. Some examples of public suffixes are ".com", ".co.uk" and "pvt.k12.wy.us". The Public Suffix List is a list of all known public suffixes.
Source: http://publicsuffix.org
This module is available both for Node.js and the browser. See below for more details.
This module is tested on Node.js v8, v10, v12, v14, v16, v18, v20 and v22. See
.github/workflows/node.js.yml
.
1npm install psl
From version v1.13.0
you can now import psl
as ESM.
1import psl from 'psl';
If your project still uses CommonJS, you can continue importing the module like in previous versions.
1const psl = require('psl');
If you are using a bundler to build your app, you should be able to import
and/or require
the module just like in Node.js.
In modern browsers you can also import the ESM directly from a CDN
. For
example:
1import psl from 'https://unpkg.com/psl@latest/dist/psl.mjs';
Finally, you can still download dist/psl.umd.cjs
and include it in a script tag.
1<script src="psl.umd.cjs"></script>
This script is bundled and wrapped in a umd wrapper so you should be able to use it standalone or together with a module loader.
The script is also available on most popular CDNs. For example:
psl.parse(domain)
Parse domain based on Public Suffix List. Returns an Object
with the following
properties:
tld
: Top level domain (this is the public suffix).sld
: Second level domain (the first private part of the domain name).domain
: The domain name is the sld
+ tld
.subdomain
: Optional parts left of the domain.Parse domain without subdomain:
1import psl from 'psl';
2
3const parsed = psl.parse('google.com');
4console.log(parsed.tld); // 'com'
5console.log(parsed.sld); // 'google'
6console.log(parsed.domain); // 'google.com'
7console.log(parsed.subdomain); // null
Parse domain with subdomain:
1import psl from 'psl';
2
3const parsed = psl.parse('www.google.com');
4console.log(parsed.tld); // 'com'
5console.log(parsed.sld); // 'google'
6console.log(parsed.domain); // 'google.com'
7console.log(parsed.subdomain); // 'www'
Parse domain with nested subdomains:
1import psl from 'psl';
2
3const parsed = psl.parse('a.b.c.d.foo.com');
4console.log(parsed.tld); // 'com'
5console.log(parsed.sld); // 'foo'
6console.log(parsed.domain); // 'foo.com'
7console.log(parsed.subdomain); // 'a.b.c.d'
psl.get(domain)
Get domain name, sld
+ tld
. Returns null
if not valid.
1import psl from 'psl'; 2 3// null input. 4psl.get(null); // null 5 6// Mixed case. 7psl.get('COM'); // null 8psl.get('example.COM'); // 'example.com' 9psl.get('WwW.example.COM'); // 'example.com' 10 11// Unlisted TLD. 12psl.get('example'); // null 13psl.get('example.example'); // 'example.example' 14psl.get('b.example.example'); // 'example.example' 15psl.get('a.b.example.example'); // 'example.example' 16 17// TLD with only 1 rule. 18psl.get('biz'); // null 19psl.get('domain.biz'); // 'domain.biz' 20psl.get('b.domain.biz'); // 'domain.biz' 21psl.get('a.b.domain.biz'); // 'domain.biz' 22 23// TLD with some 2-level rules. 24psl.get('uk.com'); // null); 25psl.get('example.uk.com'); // 'example.uk.com'); 26psl.get('b.example.uk.com'); // 'example.uk.com'); 27 28// More complex TLD. 29psl.get('c.kobe.jp'); // null 30psl.get('b.c.kobe.jp'); // 'b.c.kobe.jp' 31psl.get('a.b.c.kobe.jp'); // 'b.c.kobe.jp' 32psl.get('city.kobe.jp'); // 'city.kobe.jp' 33psl.get('www.city.kobe.jp'); // 'city.kobe.jp' 34 35// IDN labels. 36psl.get('食狮.com.cn'); // '食狮.com.cn' 37psl.get('食狮.公司.cn'); // '食狮.公司.cn' 38psl.get('www.食狮.公司.cn'); // '食狮.公司.cn' 39 40// Same as above, but punycoded. 41psl.get('xn--85x722f.com.cn'); // 'xn--85x722f.com.cn' 42psl.get('xn--85x722f.xn--55qx5d.cn'); // 'xn--85x722f.xn--55qx5d.cn' 43psl.get('www.xn--85x722f.xn--55qx5d.cn'); // 'xn--85x722f.xn--55qx5d.cn'
psl.isValid(domain)
Check whether a domain has a valid Public Suffix. Returns a Boolean
indicating
whether the domain has a valid Public Suffix.
1import psl from 'psl';
2
3psl.isValid('google.com'); // true
4psl.isValid('www.google.com'); // true
5psl.isValid('x.yz'); // false
There are tests both for Node.js and the browser (using Playwright and BrowserStack).
1# Run tests in node. 2npm test 3# Run tests in browserstack. 4npm run test:browserstack 5 6# Update rules from publicsuffix.org 7npm run update-rules 8 9# Build ESM, CJS and UMD and create dist files 10npm run build
Feel free to fork if you see possible improvements!
The MIT License (MIT)
Copyright (c) 2014-2024 Lupo Montero lupomontero@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
30 commit(s) and 19 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
4 existing vulnerabilities detected
Details
Reason
Found 2/12 approved changesets -- score normalized to 1
Reason
dependency not pinned by hash detected -- score normalized to 1
Details
Reason
detected GitHub workflow tokens with excessive permissions
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