react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / tough-cookie / generate-pubsuffix.js
81145 views/*!1* Copyright (c) 2015, Salesforce.com, Inc.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions are met:6*7* 1. Redistributions of source code must retain the above copyright notice,8* this list of conditions and the following disclaimer.9*10* 2. Redistributions in binary form must reproduce the above copyright notice,11* this list of conditions and the following disclaimer in the documentation12* and/or other materials provided with the distribution.13*14* 3. Neither the name of Salesforce.com nor the names of its contributors may15* be used to endorse or promote products derived from this software without16* specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"19* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE22* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28* POSSIBILITY OF SUCH DAMAGE.29*/30'use strict';31var fs = require('fs');32var assert = require('assert');33var punycode = require('punycode');3435fs.readFile('./public-suffix.txt', 'utf8', function(err,string) {36if (err) {37throw err;38}39var lines = string.split("\n");40process.nextTick(function() {41processList(lines);42});43});4445var index = {};4647var COMMENT = new RegExp('//.+');48function processList(lines) {49while (lines.length) {50var line = lines.shift();51line = line.replace(COMMENT,'').trim();52if (!line) {53continue;54}55addToIndex(index,line);56}5758pubSufTest();5960var w = fs.createWriteStream('./lib/pubsuffix.js',{61flags: 'w',62encoding: 'utf8',63mode: parseInt('644',8)64});65w.on('end', process.exit);66w.write("/****************************************************\n");67w.write(" * AUTOMATICALLY GENERATED by generate-pubsuffix.js *\n");68w.write(" * DO NOT EDIT! *\n");69w.write(" ****************************************************/\n\n");7071w.write('"use strict";\n\n');72w.write("var punycode = require('punycode');\n\n");7374w.write("module.exports.getPublicSuffix = ");75w.write(getPublicSuffix.toString());76w.write(";\n\n");7778w.write("// The following generated structure is used under the MPL version 1.1\n");79w.write("// See public-suffix.txt for more information\n\n");80w.write("var index = module.exports.index = Object.freeze(\n");81w.write(JSON.stringify(index));82w.write(");\n\n");83w.write("// END of automatically generated file\n");8485w.end();86}8788function addToIndex(index,line) {89var prefix = '';90if (line.replace(/^(!|\*\.)/)) {91prefix = RegExp.$1;92line = line.slice(prefix.length);93}94line = prefix + punycode.toASCII(line);9596if (line.substr(0,1) == '!') {97index[line.substr(1)] = false;98} else {99index[line] = true;100}101}102103// include the licence in the function since it gets written to pubsuffix.js104function getPublicSuffix(domain) {105/*!106* Copyright (c) 2015, Salesforce.com, Inc.107* All rights reserved.108*109* Redistribution and use in source and binary forms, with or without110* modification, are permitted provided that the following conditions are met:111*112* 1. Redistributions of source code must retain the above copyright notice,113* this list of conditions and the following disclaimer.114*115* 2. Redistributions in binary form must reproduce the above copyright notice,116* this list of conditions and the following disclaimer in the documentation117* and/or other materials provided with the distribution.118*119* 3. Neither the name of Salesforce.com nor the names of its contributors may120* be used to endorse or promote products derived from this software without121* specific prior written permission.122*123* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"124* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE125* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE126* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE127* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR128* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF129* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS130* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN131* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)132* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE133* POSSIBILITY OF SUCH DAMAGE.134*/135if (!domain) {136return null;137}138if (domain.match(/^\./)) {139return null;140}141var asciiDomain = punycode.toASCII(domain);142var converted = false;143if (asciiDomain !== domain) {144domain = asciiDomain;145converted = true;146}147if (index[domain]) {148return null;149}150151domain = domain.toLowerCase();152var parts = domain.split('.').reverse();153154var suffix = '';155var suffixLen = 0;156for (var i=0; i<parts.length; i++) {157var part = parts[i];158var starstr = '*'+suffix;159var partstr = part+suffix;160161if (index[starstr]) { // star rule matches162suffixLen = i+1;163if (index[partstr] === false) { // exception rule matches (NB: false, not undefined)164suffixLen--;165}166} else if (index[partstr]) { // exact match, not exception167suffixLen = i+1;168}169170suffix = '.'+partstr;171}172173if (index['*'+suffix]) { // *.domain exists (e.g. *.kyoto.jp for domain='kyoto.jp');174return null;175}176177suffixLen = suffixLen || 1;178if (parts.length > suffixLen) {179var publicSuffix = parts.slice(0,suffixLen+1).reverse().join('.');180return converted ? punycode.toUnicode(publicSuffix) : publicSuffix;181}182183return null;184}185186function checkPublicSuffix(give,get) {187var got = getPublicSuffix(give);188assert.equal(got, get, give+' should be '+(get==null?'NULL':get)+' but got '+got);189}190191// pubSufTest() was converted to JavaScript from http://mxr.mozilla.org/mozilla-central/source/netwerk/test/unit/data/test_psl.txt?raw=1192function pubSufTest() {193// For this function-scope and this function-scope ONLY:194// Any copyright is dedicated to the Public Domain.195// http://creativecommons.org/publicdomain/zero/1.0/196197// NULL input.198checkPublicSuffix(null, null);199// Mixed case.200checkPublicSuffix('COM', null);201checkPublicSuffix('example.COM', 'example.com');202checkPublicSuffix('WwW.example.COM', 'example.com');203// Leading dot.204checkPublicSuffix('.com', null);205checkPublicSuffix('.example', null);206checkPublicSuffix('.example.com', null);207checkPublicSuffix('.example.example', null);208// Unlisted TLD.209checkPublicSuffix('example', null);210checkPublicSuffix('example.example', 'example.example');211checkPublicSuffix('b.example.example', 'example.example');212checkPublicSuffix('a.b.example.example', 'example.example');213// Listed, but non-Internet, TLD.214//checkPublicSuffix('local', null);215//checkPublicSuffix('example.local', null);216//checkPublicSuffix('b.example.local', null);217//checkPublicSuffix('a.b.example.local', null);218// TLD with only 1 rule.219checkPublicSuffix('biz', null);220checkPublicSuffix('domain.biz', 'domain.biz');221checkPublicSuffix('b.domain.biz', 'domain.biz');222checkPublicSuffix('a.b.domain.biz', 'domain.biz');223// TLD with some 2-level rules.224checkPublicSuffix('com', null);225checkPublicSuffix('example.com', 'example.com');226checkPublicSuffix('b.example.com', 'example.com');227checkPublicSuffix('a.b.example.com', 'example.com');228checkPublicSuffix('uk.com', null);229checkPublicSuffix('example.uk.com', 'example.uk.com');230checkPublicSuffix('b.example.uk.com', 'example.uk.com');231checkPublicSuffix('a.b.example.uk.com', 'example.uk.com');232checkPublicSuffix('test.ac', 'test.ac');233// TLD with only 1 (wildcard) rule.234checkPublicSuffix('cy', null);235checkPublicSuffix('c.cy', null);236checkPublicSuffix('b.c.cy', 'b.c.cy');237checkPublicSuffix('a.b.c.cy', 'b.c.cy');238// More complex TLD.239checkPublicSuffix('jp', null);240checkPublicSuffix('test.jp', 'test.jp');241checkPublicSuffix('www.test.jp', 'test.jp');242checkPublicSuffix('ac.jp', null);243checkPublicSuffix('test.ac.jp', 'test.ac.jp');244checkPublicSuffix('www.test.ac.jp', 'test.ac.jp');245checkPublicSuffix('kyoto.jp', null);246checkPublicSuffix('test.kyoto.jp', 'test.kyoto.jp');247checkPublicSuffix('ide.kyoto.jp', null);248checkPublicSuffix('b.ide.kyoto.jp', 'b.ide.kyoto.jp');249checkPublicSuffix('a.b.ide.kyoto.jp', 'b.ide.kyoto.jp');250checkPublicSuffix('c.kobe.jp', null);251checkPublicSuffix('b.c.kobe.jp', 'b.c.kobe.jp');252checkPublicSuffix('a.b.c.kobe.jp', 'b.c.kobe.jp');253checkPublicSuffix('city.kobe.jp', 'city.kobe.jp');254checkPublicSuffix('www.city.kobe.jp', 'city.kobe.jp');255// TLD with a wildcard rule and exceptions.256checkPublicSuffix('ck', null);257checkPublicSuffix('test.ck', null);258checkPublicSuffix('b.test.ck', 'b.test.ck');259checkPublicSuffix('a.b.test.ck', 'b.test.ck');260checkPublicSuffix('www.ck', 'www.ck');261checkPublicSuffix('www.www.ck', 'www.ck');262// US K12.263checkPublicSuffix('us', null);264checkPublicSuffix('test.us', 'test.us');265checkPublicSuffix('www.test.us', 'test.us');266checkPublicSuffix('ak.us', null);267checkPublicSuffix('test.ak.us', 'test.ak.us');268checkPublicSuffix('www.test.ak.us', 'test.ak.us');269checkPublicSuffix('k12.ak.us', null);270checkPublicSuffix('test.k12.ak.us', 'test.k12.ak.us');271checkPublicSuffix('www.test.k12.ak.us', 'test.k12.ak.us');272// IDN labels.273checkPublicSuffix('食狮.com.cn', '食狮.com.cn');274checkPublicSuffix('食狮.公司.cn', '食狮.公司.cn');275checkPublicSuffix('www.食狮.公司.cn', '食狮.公司.cn');276checkPublicSuffix('shishi.公司.cn', 'shishi.公司.cn');277checkPublicSuffix('公司.cn', null);278checkPublicSuffix('食狮.中国', '食狮.中国');279checkPublicSuffix('www.食狮.中国', '食狮.中国');280checkPublicSuffix('shishi.中国', 'shishi.中国');281checkPublicSuffix('中国', null);282// Same as above, but punycoded.283checkPublicSuffix('xn--85x722f.com.cn', 'xn--85x722f.com.cn');284checkPublicSuffix('xn--85x722f.xn--55qx5d.cn', 'xn--85x722f.xn--55qx5d.cn');285checkPublicSuffix('www.xn--85x722f.xn--55qx5d.cn', 'xn--85x722f.xn--55qx5d.cn');286checkPublicSuffix('shishi.xn--55qx5d.cn', 'shishi.xn--55qx5d.cn');287checkPublicSuffix('xn--55qx5d.cn', null);288checkPublicSuffix('xn--85x722f.xn--fiqs8s', 'xn--85x722f.xn--fiqs8s');289checkPublicSuffix('www.xn--85x722f.xn--fiqs8s', 'xn--85x722f.xn--fiqs8s');290checkPublicSuffix('shishi.xn--fiqs8s', 'shishi.xn--fiqs8s');291checkPublicSuffix('xn--fiqs8s', null);292}293294295