UNPKG

6.47 kBMarkdownView Raw
1# psl (Public Suffix List)
2
3[![Node.js CI](https://github.com/lupomontero/psl/actions/workflows/node.js.yml/badge.svg)](https://github.com/lupomontero/psl/actions/workflows/node.js.yml)
4
5`psl` is a `JavaScript` domain name parser based on the
6[Public Suffix List](https://publicsuffix.org/).
7
8This implementation is tested against the
9[test data hosted by Mozilla](http://mxr.mozilla.org/mozilla-central/source/netwerk/test/unit/data/test_psl.txt?raw=1)
10and kindly provided by [Comodo](https://www.comodo.com/).
11
12Cross browser testing provided by
13[<img alt="BrowserStack" width="160" src="./browserstack-logo.svg" />](https://www.browserstack.com/)
14
15## What is the Public Suffix List?
16
17The Public Suffix List is a cross-vendor initiative to provide an accurate list
18of domain name suffixes.
19
20The Public Suffix List is an initiative of the Mozilla Project, but is
21maintained as a community resource. It is available for use in any software,
22but was originally created to meet the needs of browser manufacturers.
23
24A "public suffix" is one under which Internet users can directly register names.
25Some examples of public suffixes are ".com", ".co.uk" and "pvt.k12.wy.us". The
26Public Suffix List is a list of all known public suffixes.
27
28Source: http://publicsuffix.org
29
30
31## Installation
32
33### Node.js
34
35```sh
36npm install --save psl
37```
38
39### Browser
40
41Download [psl.min.js](https://raw.githubusercontent.com/lupomontero/psl/master/dist/psl.min.js)
42and include it in a script tag.
43
44```html
45<script src="psl.min.js"></script>
46```
47
48This script is browserified and wrapped in a [umd](https://github.com/umdjs/umd)
49wrapper so you should be able to use it standalone or together with a module
50loader.
51
52## API
53
54### `psl.parse(domain)`
55
56Parse domain based on Public Suffix List. Returns an `Object` with the following
57properties:
58
59* `tld`: Top level domain (this is the _public suffix_).
60* `sld`: Second level domain (the first private part of the domain name).
61* `domain`: The domain name is the `sld` + `tld`.
62* `subdomain`: Optional parts left of the domain.
63
64#### Example:
65
66```js
67var psl = require('psl');
68
69// Parse domain without subdomain
70var parsed = psl.parse('google.com');
71console.log(parsed.tld); // 'com'
72console.log(parsed.sld); // 'google'
73console.log(parsed.domain); // 'google.com'
74console.log(parsed.subdomain); // null
75
76// Parse domain with subdomain
77var parsed = psl.parse('www.google.com');
78console.log(parsed.tld); // 'com'
79console.log(parsed.sld); // 'google'
80console.log(parsed.domain); // 'google.com'
81console.log(parsed.subdomain); // 'www'
82
83// Parse domain with nested subdomains
84var parsed = psl.parse('a.b.c.d.foo.com');
85console.log(parsed.tld); // 'com'
86console.log(parsed.sld); // 'foo'
87console.log(parsed.domain); // 'foo.com'
88console.log(parsed.subdomain); // 'a.b.c.d'
89```
90
91### `psl.get(domain)`
92
93Get domain name, `sld` + `tld`. Returns `null` if not valid.
94
95#### Example:
96
97```js
98var psl = require('psl');
99
100// null input.
101psl.get(null); // null
102
103// Mixed case.
104psl.get('COM'); // null
105psl.get('example.COM'); // 'example.com'
106psl.get('WwW.example.COM'); // 'example.com'
107
108// Unlisted TLD.
109psl.get('example'); // null
110psl.get('example.example'); // 'example.example'
111psl.get('b.example.example'); // 'example.example'
112psl.get('a.b.example.example'); // 'example.example'
113
114// TLD with only 1 rule.
115psl.get('biz'); // null
116psl.get('domain.biz'); // 'domain.biz'
117psl.get('b.domain.biz'); // 'domain.biz'
118psl.get('a.b.domain.biz'); // 'domain.biz'
119
120// TLD with some 2-level rules.
121psl.get('uk.com'); // null);
122psl.get('example.uk.com'); // 'example.uk.com');
123psl.get('b.example.uk.com'); // 'example.uk.com');
124
125// More complex TLD.
126psl.get('c.kobe.jp'); // null
127psl.get('b.c.kobe.jp'); // 'b.c.kobe.jp'
128psl.get('a.b.c.kobe.jp'); // 'b.c.kobe.jp'
129psl.get('city.kobe.jp'); // 'city.kobe.jp'
130psl.get('www.city.kobe.jp'); // 'city.kobe.jp'
131
132// IDN labels.
133psl.get('食狮.com.cn'); // '食狮.com.cn'
134psl.get('食狮.公司.cn'); // '食狮.公司.cn'
135psl.get('www.食狮.公司.cn'); // '食狮.公司.cn'
136
137// Same as above, but punycoded.
138psl.get('xn--85x722f.com.cn'); // 'xn--85x722f.com.cn'
139psl.get('xn--85x722f.xn--55qx5d.cn'); // 'xn--85x722f.xn--55qx5d.cn'
140psl.get('www.xn--85x722f.xn--55qx5d.cn'); // 'xn--85x722f.xn--55qx5d.cn'
141```
142
143### `psl.isValid(domain)`
144
145Check whether a domain has a valid Public Suffix. Returns a `Boolean` indicating
146whether the domain has a valid Public Suffix.
147
148#### Example
149
150```js
151var psl = require('psl');
152
153psl.isValid('google.com'); // true
154psl.isValid('www.google.com'); // true
155psl.isValid('x.yz'); // false
156```
157
158
159## Testing and Building
160
161Test are written using [`mocha`](https://mochajs.org/) and can be
162run in two different environments: `node` and `phantomjs`.
163
164```sh
165# This will run `eslint`, `mocha` and `karma`.
166npm test
167
168# Individual test environments
169# Run tests in node only.
170./node_modules/.bin/mocha test
171# Run tests in phantomjs only.
172./node_modules/.bin/karma start ./karma.conf.js --single-run
173
174# Build data (parse raw list) and create dist files
175npm run build
176```
177
178Feel free to fork if you see possible improvements!
179
180
181## Acknowledgements
182
183* Mozilla Foundation's [Public Suffix List](https://publicsuffix.org/)
184* Thanks to Rob Stradling of [Comodo](https://www.comodo.com/) for providing
185 test data.
186* Inspired by [weppos/publicsuffix-ruby](https://github.com/weppos/publicsuffix-ruby)
187
188
189## License
190
191The MIT License (MIT)
192
193Copyright (c) 2017 Lupo Montero <lupomontero@gmail.com>
194
195Permission is hereby granted, free of charge, to any person obtaining a copy
196of this software and associated documentation files (the "Software"), to deal
197in the Software without restriction, including without limitation the rights
198to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
199copies of the Software, and to permit persons to whom the Software is
200furnished to do so, subject to the following conditions:
201
202The above copyright notice and this permission notice shall be included in
203all copies or substantial portions of the Software.
204
205THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
206IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
207FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
208AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
209LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
210OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
211THE SOFTWARE.