Add build step to CDN domainset

This commit is contained in:
SukkaW
2023-07-13 22:18:53 +08:00
parent 9fde4a3866
commit b43c1628d6
7 changed files with 128 additions and 48 deletions

View File

@@ -0,0 +1,27 @@
const Trie = require('./trie');
/**
* @param {string[]} inputDomains
*/
const domainDeduper = (inputDomains) => {
const trie = Trie.from(inputDomains);
const sets = new Set(inputDomains);
for (let j = 0, len = inputDomains.length; j < len; j++) {
const d = inputDomains[j];
if (d[0] !== '.') {
continue;
}
trie.find(d, false).forEach(f => sets.delete(f));
const a = d.slice(1);
if (trie.has(a)) {
sets.delete(a);
}
}
return Array.from(sets);
};
module.exports.domainDeduper = domainDeduper;

View File

@@ -0,0 +1,29 @@
/* eslint-disable camelcase -- cache index access */
/**
* @param {string} line
*/
module.exports.shouldIgnoreLine = (line) => {
if (line === '') {
return null;
}
const line_0 = line[0];
if (
line_0 === '#'
|| line_0 === ' '
|| line_0 === '\r'
|| line_0 === '\n'
|| line_0 === '!'
) {
return null;
}
const trimmed = line.trim();
if (trimmed === '') {
return null;
}
return trimmed;
};

View File

@@ -81,6 +81,7 @@ class Trie {
$suffix = suffixStack.pop();
node = nodeStack.pop();
// eslint-disable-next-line guard-for-in -- plain object
for (k in node) {
if (k === SENTINEL) {
if (includeEqualWithSuffix) {
@@ -89,7 +90,6 @@ class Trie {
matches.push($suffix);
}
continue;
}
@@ -161,8 +161,9 @@ class Trie {
node = node[token];
// Prefix does not exist
if (typeof node === 'undefined')
if (typeof node === 'undefined') {
return false;
}
// Keeping track of a potential branch to prune
if (toPrune !== null) {
@@ -170,12 +171,9 @@ class Trie {
toPrune = null;
tokenToPrune = null;
}
}
else {
if (Object.keys(node).length < 2) {
toPrune = parent;
tokenToPrune = token;
}
} else if (Object.keys(node).length < 2) {
toPrune = parent;
tokenToPrune = token;
}
}
@@ -206,8 +204,9 @@ class Trie {
token = suffix[i];
node = node[token];
if (typeof node === 'undefined')
if (typeof node === 'undefined') {
return false;
}
}
return SENTINEL in node;
@@ -217,7 +216,7 @@ class Trie {
* @return {string[]}
*/
dump() {
let node = this.root;
const node = this.root;
const nodeStack = [];
const prefixStack = [];
// Resolving initial prefix
@@ -238,6 +237,7 @@ class Trie {
currentNode = nodeStack.pop();
currentPrefix = prefixStack.pop();
// eslint-disable-next-line guard-for-in -- plain object
for (k in currentNode) {
if (k === SENTINEL) {
hasValue = true;