Bobby L. Craig

psl (Public Suffix List)

# psl (Public Suffix List) [![NPM](]( [![Greenkeeper badge](]( [![Build Status](]( [![devDependency Status](]( `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", "" and "". The Public Suffix List is a list of all known public suffixes. Source: ## Installation ### Node.js ```sh npm install --save psl ``` ### Browser Download [psl.min.js]( and include it in a script tag. ```html ``` This script is browserified and wrapped in a [umd]( wrapper so you should be able to use it standalone or together with a module loader. ## 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: ```js var psl = require('psl'); // Parse domain without subdomain var parsed = psl.parse(''); console.log(parsed.tld); // 'com' console.log(parsed.sld); // 'google' console.log(parsed.domain); // '' console.log(parsed.subdomain); // null // Parse domain with subdomain var parsed = psl.parse(''); console.log(parsed.tld); // 'com' console.log(parsed.sld); // 'google' console.log(parsed.domain); // '' console.log(parsed.subdomain); // 'www' // Parse domain with nested subdomains var parsed = psl.parse(''); console.log(parsed.tld); // 'com' console.log(parsed.sld); // 'foo' console.log(parsed.domain); // '' console.log(parsed.subdomain); // 'a.b.c.d' ``` ### `psl.get(domain)` Get domain name, `sld` + `tld`. Returns `null` if not valid. #### Example: ```js var psl = require('psl'); // null input. psl.get(null); // null // Mixed case. psl.get('COM'); // null psl.get('example.COM'); // '' psl.get('WwW.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(''); // '' psl.get(''); // '' psl.get(''); // '' // TLD with some 2-level rules. psl.get(''); // null); psl.get(''); // ''); psl.get(''); // ''); // More complex TLD. psl.get(''); // null psl.get(''); // '' psl.get(''); // '' psl.get(''); // '' psl.get(''); // '' // IDN labels. psl.get('食狮'); // '食狮' psl.get('食狮.公司.cn'); // '食狮.公司.cn' psl.get('www.食狮.公司.cn'); // '食狮.公司.cn' // Same as above, but punycoded. psl.get(''); // '' psl.get(''); // '' psl.get(''); // '' ``` ### `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 ```js var psl = require('psl'); psl.isValid(''); // true psl.isValid(''); // 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`. ```sh # 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 * Mozilla Foundation's [Public Suffix List]( * Thanks to Rob Stradling of [Comodo]( for providing test data. * Inspired by [weppos/publicsuffix-ruby]( ## License The MIT License (MIT) Copyright (c) 2017 Lupo Montero 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.