generate-pubsuffix.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*!
  2. * Copyright (c) 2015, Salesforce.com, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of Salesforce.com nor the names of its contributors may
  16. * be used to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. 'use strict';
  32. var fs = require('fs');
  33. var assert = require('assert');
  34. var punycode = require('punycode');
  35. fs.readFile('./public-suffix.txt', 'utf8', function(err,string) {
  36. if (err) {
  37. throw err;
  38. }
  39. var lines = string.split("\n");
  40. process.nextTick(function() {
  41. processList(lines);
  42. });
  43. });
  44. var index = {};
  45. var COMMENT = new RegExp('//.+');
  46. function processList(lines) {
  47. while (lines.length) {
  48. var line = lines.shift();
  49. line = line.replace(COMMENT,'').trim();
  50. if (!line) {
  51. continue;
  52. }
  53. addToIndex(index,line);
  54. }
  55. pubSufTest();
  56. var w = fs.createWriteStream('./lib/pubsuffix.js',{
  57. flags: 'w',
  58. encoding: 'utf8',
  59. mode: parseInt('644',8)
  60. });
  61. w.on('end', process.exit);
  62. w.write("/****************************************************\n");
  63. w.write(" * AUTOMATICALLY GENERATED by generate-pubsuffix.js *\n");
  64. w.write(" * DO NOT EDIT! *\n");
  65. w.write(" ****************************************************/\n\n");
  66. w.write('"use strict";\n\n');
  67. w.write("var punycode = require('punycode');\n\n");
  68. w.write("module.exports.getPublicSuffix = ");
  69. w.write(getPublicSuffix.toString());
  70. w.write(";\n\n");
  71. w.write("// The following generated structure is used under the MPL version 1.1\n");
  72. w.write("// See public-suffix.txt for more information\n\n");
  73. w.write("var index = module.exports.index = Object.freeze(\n");
  74. w.write(JSON.stringify(index));
  75. w.write(");\n\n");
  76. w.write("// END of automatically generated file\n");
  77. w.end();
  78. }
  79. function addToIndex(index,line) {
  80. var prefix = '';
  81. if (line.replace(/^(!|\*\.)/)) {
  82. prefix = RegExp.$1;
  83. line = line.slice(prefix.length);
  84. }
  85. line = prefix + punycode.toASCII(line);
  86. if (line.substr(0,1) == '!') {
  87. index[line.substr(1)] = false;
  88. } else {
  89. index[line] = true;
  90. }
  91. }
  92. // include the licence in the function since it gets written to pubsuffix.js
  93. function getPublicSuffix(domain) {
  94. /*!
  95. * Copyright (c) 2015, Salesforce.com, Inc.
  96. * All rights reserved.
  97. *
  98. * Redistribution and use in source and binary forms, with or without
  99. * modification, are permitted provided that the following conditions are met:
  100. *
  101. * 1. Redistributions of source code must retain the above copyright notice,
  102. * this list of conditions and the following disclaimer.
  103. *
  104. * 2. Redistributions in binary form must reproduce the above copyright notice,
  105. * this list of conditions and the following disclaimer in the documentation
  106. * and/or other materials provided with the distribution.
  107. *
  108. * 3. Neither the name of Salesforce.com nor the names of its contributors may
  109. * be used to endorse or promote products derived from this software without
  110. * specific prior written permission.
  111. *
  112. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  113. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  114. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  115. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  116. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  117. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  118. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  119. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  120. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  121. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  122. * POSSIBILITY OF SUCH DAMAGE.
  123. */
  124. if (!domain) {
  125. return null;
  126. }
  127. if (domain.match(/^\./)) {
  128. return null;
  129. }
  130. var asciiDomain = punycode.toASCII(domain);
  131. var converted = false;
  132. if (asciiDomain !== domain) {
  133. domain = asciiDomain;
  134. converted = true;
  135. }
  136. if (index[domain]) {
  137. return null;
  138. }
  139. domain = domain.toLowerCase();
  140. var parts = domain.split('.').reverse();
  141. var suffix = '';
  142. var suffixLen = 0;
  143. for (var i=0; i<parts.length; i++) {
  144. var part = parts[i];
  145. var starstr = '*'+suffix;
  146. var partstr = part+suffix;
  147. if (index[starstr]) { // star rule matches
  148. suffixLen = i+1;
  149. if (index[partstr] === false) { // exception rule matches (NB: false, not undefined)
  150. suffixLen--;
  151. }
  152. } else if (index[partstr]) { // exact match, not exception
  153. suffixLen = i+1;
  154. }
  155. suffix = '.'+partstr;
  156. }
  157. if (index['*'+suffix]) { // *.domain exists (e.g. *.kyoto.jp for domain='kyoto.jp');
  158. return null;
  159. }
  160. suffixLen = suffixLen || 1;
  161. if (parts.length > suffixLen) {
  162. var publicSuffix = parts.slice(0,suffixLen+1).reverse().join('.');
  163. return converted ? punycode.toUnicode(publicSuffix) : publicSuffix;
  164. }
  165. return null;
  166. }
  167. function checkPublicSuffix(give,get) {
  168. var got = getPublicSuffix(give);
  169. assert.equal(got, get, give+' should be '+(get==null?'NULL':get)+' but got '+got);
  170. }
  171. // pubSufTest() was converted to JavaScript from http://mxr.mozilla.org/mozilla-central/source/netwerk/test/unit/data/test_psl.txt?raw=1
  172. function pubSufTest() {
  173. // For this function-scope and this function-scope ONLY:
  174. // Any copyright is dedicated to the Public Domain.
  175. // http://creativecommons.org/publicdomain/zero/1.0/
  176. // NULL input.
  177. checkPublicSuffix(null, null);
  178. // Mixed case.
  179. checkPublicSuffix('COM', null);
  180. checkPublicSuffix('example.COM', 'example.com');
  181. checkPublicSuffix('WwW.example.COM', 'example.com');
  182. // Leading dot.
  183. checkPublicSuffix('.com', null);
  184. checkPublicSuffix('.example', null);
  185. checkPublicSuffix('.example.com', null);
  186. checkPublicSuffix('.example.example', null);
  187. // Unlisted TLD.
  188. checkPublicSuffix('example', null);
  189. checkPublicSuffix('example.example', 'example.example');
  190. checkPublicSuffix('b.example.example', 'example.example');
  191. checkPublicSuffix('a.b.example.example', 'example.example');
  192. // Listed, but non-Internet, TLD.
  193. //checkPublicSuffix('local', null);
  194. //checkPublicSuffix('example.local', null);
  195. //checkPublicSuffix('b.example.local', null);
  196. //checkPublicSuffix('a.b.example.local', null);
  197. // TLD with only 1 rule.
  198. checkPublicSuffix('biz', null);
  199. checkPublicSuffix('domain.biz', 'domain.biz');
  200. checkPublicSuffix('b.domain.biz', 'domain.biz');
  201. checkPublicSuffix('a.b.domain.biz', 'domain.biz');
  202. // TLD with some 2-level rules.
  203. checkPublicSuffix('com', null);
  204. checkPublicSuffix('example.com', 'example.com');
  205. checkPublicSuffix('b.example.com', 'example.com');
  206. checkPublicSuffix('a.b.example.com', 'example.com');
  207. checkPublicSuffix('uk.com', null);
  208. checkPublicSuffix('example.uk.com', 'example.uk.com');
  209. checkPublicSuffix('b.example.uk.com', 'example.uk.com');
  210. checkPublicSuffix('a.b.example.uk.com', 'example.uk.com');
  211. checkPublicSuffix('test.ac', 'test.ac');
  212. // TLD with only 1 (wildcard) rule.
  213. checkPublicSuffix('cy', null);
  214. checkPublicSuffix('c.cy', null);
  215. checkPublicSuffix('b.c.cy', 'b.c.cy');
  216. checkPublicSuffix('a.b.c.cy', 'b.c.cy');
  217. // More complex TLD.
  218. checkPublicSuffix('jp', null);
  219. checkPublicSuffix('test.jp', 'test.jp');
  220. checkPublicSuffix('www.test.jp', 'test.jp');
  221. checkPublicSuffix('ac.jp', null);
  222. checkPublicSuffix('test.ac.jp', 'test.ac.jp');
  223. checkPublicSuffix('www.test.ac.jp', 'test.ac.jp');
  224. checkPublicSuffix('kyoto.jp', null);
  225. checkPublicSuffix('test.kyoto.jp', 'test.kyoto.jp');
  226. checkPublicSuffix('ide.kyoto.jp', null);
  227. checkPublicSuffix('b.ide.kyoto.jp', 'b.ide.kyoto.jp');
  228. checkPublicSuffix('a.b.ide.kyoto.jp', 'b.ide.kyoto.jp');
  229. checkPublicSuffix('c.kobe.jp', null);
  230. checkPublicSuffix('b.c.kobe.jp', 'b.c.kobe.jp');
  231. checkPublicSuffix('a.b.c.kobe.jp', 'b.c.kobe.jp');
  232. checkPublicSuffix('city.kobe.jp', 'city.kobe.jp');
  233. checkPublicSuffix('www.city.kobe.jp', 'city.kobe.jp');
  234. // TLD with a wildcard rule and exceptions.
  235. checkPublicSuffix('ck', null);
  236. checkPublicSuffix('test.ck', null);
  237. checkPublicSuffix('b.test.ck', 'b.test.ck');
  238. checkPublicSuffix('a.b.test.ck', 'b.test.ck');
  239. checkPublicSuffix('www.ck', 'www.ck');
  240. checkPublicSuffix('www.www.ck', 'www.ck');
  241. // US K12.
  242. checkPublicSuffix('us', null);
  243. checkPublicSuffix('test.us', 'test.us');
  244. checkPublicSuffix('www.test.us', 'test.us');
  245. checkPublicSuffix('ak.us', null);
  246. checkPublicSuffix('test.ak.us', 'test.ak.us');
  247. checkPublicSuffix('www.test.ak.us', 'test.ak.us');
  248. checkPublicSuffix('k12.ak.us', null);
  249. checkPublicSuffix('test.k12.ak.us', 'test.k12.ak.us');
  250. checkPublicSuffix('www.test.k12.ak.us', 'test.k12.ak.us');
  251. // IDN labels.
  252. checkPublicSuffix('食狮.com.cn', '食狮.com.cn');
  253. checkPublicSuffix('食狮.公司.cn', '食狮.公司.cn');
  254. checkPublicSuffix('www.食狮.公司.cn', '食狮.公司.cn');
  255. checkPublicSuffix('shishi.公司.cn', 'shishi.公司.cn');
  256. checkPublicSuffix('公司.cn', null);
  257. checkPublicSuffix('食狮.中国', '食狮.中国');
  258. checkPublicSuffix('www.食狮.中国', '食狮.中国');
  259. checkPublicSuffix('shishi.中国', 'shishi.中国');
  260. checkPublicSuffix('中国', null);
  261. // Same as above, but punycoded.
  262. checkPublicSuffix('xn--85x722f.com.cn', 'xn--85x722f.com.cn');
  263. checkPublicSuffix('xn--85x722f.xn--55qx5d.cn', 'xn--85x722f.xn--55qx5d.cn');
  264. checkPublicSuffix('www.xn--85x722f.xn--55qx5d.cn', 'xn--85x722f.xn--55qx5d.cn');
  265. checkPublicSuffix('shishi.xn--55qx5d.cn', 'shishi.xn--55qx5d.cn');
  266. checkPublicSuffix('xn--55qx5d.cn', null);
  267. checkPublicSuffix('xn--85x722f.xn--fiqs8s', 'xn--85x722f.xn--fiqs8s');
  268. checkPublicSuffix('www.xn--85x722f.xn--fiqs8s', 'xn--85x722f.xn--fiqs8s');
  269. checkPublicSuffix('shishi.xn--fiqs8s', 'shishi.xn--fiqs8s');
  270. checkPublicSuffix('xn--fiqs8s', null);
  271. }