3712da5c8aba0ae8537cfea511d459f420f815c0c56b4ebcd76b2ca73b08968956252cd81a5ac68999e654e6ff1e3fb5fd6747883c1691a41424d91a4891a4 7.3 KB

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