版博士V2.0程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

README.md 6.3 KiB

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