2
0
Fork
You've already forked psl
0
Domain name parser based on the Public Suffix List
  • JavaScript 92.2%
  • HTML 7.8%
2024年11月02日 15:14:31 +00:00
.github/workflows feat(ci): Removes Node v10.x from build matrix and adds v20.x 2024年01月08日 14:02:43 +01:00
data re-build and bump version 2024年11月02日 15:04:01 +00:00
dist re-build and bump version 2024年11月02日 15:04:01 +00:00
examples tweak readme and add examples 2014年10月14日 13:25:05 -05:00
scripts chore(ci): Removes obsolete Travis CI build config 2024年01月08日 12:41:23 +01:00
test fix(test): Wait for driver.get to resolve 2022年07月04日 00:05:30 -05:00
.eslintignore chore(eslint): Removes hapi eslint config 2022年07月04日 00:07:12 -05:00
.eslintrc chore(eslint): Removes hapi eslint config 2022年07月04日 00:07:12 -05:00
.gitignore test(browser): Replaces karma + phantom with selenium + browserstack 2022年07月03日 23:16:49 -05:00
.npmignore chore(pkg): Updates npmignore 2022年07月04日 01:14:34 -05:00
index.d.ts fix scope, bump version 2024年11月02日 15:14:31 +00:00
index.js chore(deps): Replaces deprecated Punycode provided by Node with punycode.js #296 2024年01月08日 06:26:48 -05:00
LICENSE Create LICENSE 2018年08月13日 08:55:20 +02:00
package-lock.json package: bump version to 2.0.1 and update dependencies 2024年11月02日 20:49:32 +06:00
package.json fix scope, bump version 2024年11月02日 15:14:31 +00:00
README.md readme: remove unrelated links 2024年11月02日 20:45:33 +06:00

psl (Public Suffix List)

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.

What is the Public Suffix List?

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: https://publicsuffix.org/

Installation

Node.js

npm install --save psl

API

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.

Example:

var psl = require('psl');
// Parse domain without subdomain
var parsed = psl.parse('google.com');
console.log(parsed.tld); // 'com'
console.log(parsed.sld); // 'google'
console.log(parsed.domain); // 'google.com'
console.log(parsed.subdomain); // null

// Parse domain with subdomain
var parsed = psl.parse('www.google.com');
console.log(parsed.tld); // 'com'
console.log(parsed.sld); // 'google'
console.log(parsed.domain); // 'google.com'
console.log(parsed.subdomain); // 'www'

// Parse domain with nested subdomains
var parsed = psl.parse('a.b.c.d.foo.com');
console.log(parsed.tld); // 'com'
console.log(parsed.sld); // 'foo'
console.log(parsed.domain); // 'foo.com'
console.log(parsed.subdomain); // 'a.b.c.d'

psl.get(domain)

Get domain name, sld + tld. Returns null if not valid.

Example:

var psl = require('psl');
// null input.
psl.get(null); // null

// Mixed case.
psl.get('COM'); // null
psl.get('example.COM'); // 'example.com'
psl.get('WwW.example.COM'); // 'example.com'

// Unlisted TLD.
psl.get('example'); // null
psl.get('example.example'); // 'example.example'
psl.get('b.example.example'); // 'example.example'
psl.get('a.b.example.example'); // 'example.example'

// TLD with only 1 rule.
psl.get('biz'); // null
psl.get('domain.biz'); // 'domain.biz'
psl.get('b.domain.biz'); // 'domain.biz'
psl.get('a.b.domain.biz'); // 'domain.biz'

// TLD with some 2-level rules.
psl.get('uk.com'); // null);
psl.get('example.uk.com'); // 'example.uk.com');
psl.get('b.example.uk.com'); // 'example.uk.com');

// More complex TLD.
psl.get('c.kobe.jp'); // null
psl.get('b.c.kobe.jp'); // 'b.c.kobe.jp'
psl.get('a.b.c.kobe.jp'); // 'b.c.kobe.jp'
psl.get('city.kobe.jp'); // 'city.kobe.jp'
psl.get('www.city.kobe.jp'); // 'city.kobe.jp'

// IDN labels.
psl.get('食狮.com.cn'); // '食狮.com.cn'
psl.get('食狮.公司.cn'); // '食狮.公司.cn'
psl.get('www.食狮.公司.cn'); // '食狮.公司.cn'

// Same as above, but punycoded.
psl.get('xn--85x722f.com.cn'); // 'xn--85x722f.com.cn'
psl.get('xn--85x722f.xn--55qx5d.cn'); // 'xn--85x722f.xn--55qx5d.cn'
psl.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.

Example

var psl = require('psl');
psl.isValid('google.com'); // true
psl.isValid('www.google.com'); // true
psl.isValid('x.yz'); // false

Testing and Building

Test are written using mocha and can be run in two different environments: node and phantomjs.

# This will run `eslint`, `mocha` and `karma`.
npm test
# Individual test environments
# Run tests in node only.
./node_modules/.bin/mocha test
# Run tests in phantomjs only.
./node_modules/.bin/karma start ./karma.conf.js --single-run
# Build data (parse raw list) and create dist files
npm run build

Feel free to fork if you see possible improvements!

Acknowledgements

License

The MIT License (MIT)

Copyright (c) 2017 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.