Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/domain.js

Issue 29994555: Issue 7250 - Optimize third-party request check (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Jan. 31, 2019, 10:11 a.m.
Right Patch Set: Add tests Created Feb. 2, 2019, 4:54 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | test/domain.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 const publicSuffixes = require("../data/publicSuffixList.json"); 20 const publicSuffixes = require("../data/publicSuffixList.json");
21 21
22 /**
23 * Map of public suffixes to their offsets.
24 * @type {Map.<string,number>}
25 */
22 let publicSuffixMap = buildPublicSuffixMap(); 26 let publicSuffixMap = buildPublicSuffixMap();
Manish Jethani 2019/01/31 10:14:15 Maps are faster for dynamic lookups (where the pro
23 27
28 /**
29 * Builds a map of public suffixes to their offsets.
30 * @returns {Map.<string,number>}
31 */
24 function buildPublicSuffixMap() 32 function buildPublicSuffixMap()
25 { 33 {
26 let map = new Map(); 34 let map = new Map();
27 35
28 for (let key in publicSuffixes) 36 for (let key in publicSuffixes)
29 map.set(key, publicSuffixes[key]); 37 map.set(key, publicSuffixes[key]);
30 38
31 return map; 39 return map;
32 } 40 }
33 41
42 /**
43 * Yields all suffixes for a domain. For example, given the domain
44 * <code>www.example.com</code>, this function yields
45 * <code>www.example.com</code>, <code>example.com</code>, and
46 * <code>com</code>, in that order.
47 *
48 * @param {string} domain The domain.
49 * @yields {string} The next suffix for the domain.
50 */
34 function* suffixes(domain) 51 function* suffixes(domain)
Manish Jethani 2019/01/31 10:14:15 We will reuse this function in other places (e.g.
35 { 52 {
36 while (domain != "") 53 while (domain != "")
37 { 54 {
38 yield domain; 55 yield domain;
39 56
40 let dotIndex = domain.indexOf("."); 57 let dotIndex = domain.indexOf(".");
41 domain = dotIndex == -1 ? "" : domain.substr(dotIndex + 1); 58 domain = dotIndex == -1 ? "" : domain.substr(dotIndex + 1);
42 } 59 }
43 } 60 }
44 61
(...skipping 13 matching lines...) Expand all
58 // just check whether there are any colons to exclude IPv6 addresses. 75 // just check whether there are any colons to exclude IPv6 addresses.
59 return hostname.indexOf(":") == -1; 76 return hostname.indexOf(":") == -1;
60 } 77 }
61 78
62 /** 79 /**
63 * Gets the base domain for the given hostname. 80 * Gets the base domain for the given hostname.
64 * 81 *
65 * @param {string} hostname 82 * @param {string} hostname
66 * @returns {string} 83 * @returns {string}
67 */ 84 */
68 function getDomain(hostname) 85 function getDomain(hostname)
Manish Jethani 2019/01/31 10:14:15 At least twice as fast as the previous implementat
69 { 86 {
70 let slices = []; 87 let slices = [];
71 let cutoff = null; 88 let cutoff = NaN;
72 89
73 for (let suffix of suffixes(hostname)) 90 for (let suffix of suffixes(hostname))
74 { 91 {
75 slices.push(suffix); 92 slices.push(suffix);
76 93
77 let offset = publicSuffixMap.get(suffix); 94 let offset = publicSuffixMap.get(suffix);
78 95
79 if (typeof offset != "undefined") 96 if (typeof offset != "undefined")
80 { 97 {
81 cutoff = slices.length - 1 - offset; 98 cutoff = slices.length - 1 - offset;
82 break; 99 break;
83 } 100 }
84 } 101 }
85 102
86 if (cutoff == null) 103 if (isNaN(cutoff))
87 return slices.length > 2 ? slices[slices.length - 2] : hostname; 104 return slices.length > 2 ? slices[slices.length - 2] : hostname;
88 105
89 if (cutoff <= 0) 106 if (cutoff <= 0)
90 return hostname; 107 return hostname;
91 108
92 return slices[cutoff]; 109 return slices[cutoff];
93 } 110 }
94 111
95 exports.getDomain = getDomain; 112 exports.getDomain = getDomain;
96 113
(...skipping 18 matching lines...) Expand all
115 if (requestHostname == documentHostname) 132 if (requestHostname == documentHostname)
116 return false; 133 return false;
117 134
118 if (!isDomain(requestHostname) || !isDomain(documentHostname)) 135 if (!isDomain(requestHostname) || !isDomain(documentHostname))
119 return true; 136 return true;
120 137
121 return getDomain(requestHostname) != getDomain(documentHostname); 138 return getDomain(requestHostname) != getDomain(documentHostname);
122 } 139 }
123 140
124 exports.isThirdParty = isThirdParty; 141 exports.isThirdParty = isThirdParty;
LEFTRIGHT
« no previous file | test/domain.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld