You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
6.6 KiB

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