Chore: simplify build infra

This commit is contained in:
SukkaW 2023-12-09 21:27:03 +08:00
parent b22393a62a
commit a558b82c08
4 changed files with 65 additions and 76 deletions

View File

@ -94,7 +94,7 @@ const PRESET_MITM_HOSTNAMES = [
); );
})); }));
let mitmDomains = new Set(PRESET_MITM_HOSTNAMES); // Special case for parsed failed const mitmDomains = new Set(PRESET_MITM_HOSTNAMES); // Special case for parsed failed
const parsedFailures = []; const parsedFailures = [];
const dedupedUrlRegexPaths = [...new Set(urlRegexPaths)]; const dedupedUrlRegexPaths = [...new Set(urlRegexPaths)];
@ -109,7 +109,9 @@ const PRESET_MITM_HOSTNAMES = [
} }
}); });
mitmDomains = [...mitmDomains].filter(i => { const mitmDomainsRegExpArray = mitmDomains
.slice()
.filter(i => {
return i.length > 3 return i.length > 3
&& !i.includes('.mp4') // Special Case && !i.includes('.mp4') // Special Case
&& i !== '(www.)' // Special Case && i !== '(www.)' // Special Case
@ -117,9 +119,8 @@ const PRESET_MITM_HOSTNAMES = [
&& !i.startsWith('.') && !i.startsWith('.')
&& !i.endsWith('.') && !i.endsWith('.')
&& !i.endsWith('*'); && !i.endsWith('*');
}); })
.map(i => {
const mitmDomainsRegExpArray = mitmDomains.map(i => {
return new RegExp( return new RegExp(
escapeRegExp(i) escapeRegExp(i)
.replaceAll('{www or not}', '(www.)?') .replaceAll('{www or not}', '(www.)?')

View File

@ -108,18 +108,18 @@ export const buildRejectDomainSet = task(import.meta.path, async () => {
console.log(`Import ${previousSize} rules from reject_sukka.conf!`); console.log(`Import ${previousSize} rules from reject_sukka.conf!`);
for await (const line of readFileByLine(path.resolve(import.meta.dir, '../Source/non_ip/reject.conf'))) { for await (const line of readFileByLine(path.resolve(import.meta.dir, '../Source/non_ip/reject.conf'))) {
if (line.startsWith('DOMAIN-KEYWORD')) { const [type, keyword] = line.split(',');
const [, ...keywords] = line.split(',');
domainKeywordsSet.add(keywords.join(',').trim()); if (type === 'DOMAIN-KEYWORD') {
} else if (line.startsWith('DOMAIN-SUFFIX')) { domainKeywordsSet.add(keyword.trim());
const [, ...keywords] = line.split(','); } else if (type === 'DOMAIN-SUFFIX') {
domainSuffixSet.add(keywords.join(',').trim()); domainSuffixSet.add(keyword.trim());
} }
} }
for await (const line of readFileByLine(path.resolve(import.meta.dir, '../List/domainset/reject_phishing.conf'))) { for await (const line of readFileByLine(path.resolve(import.meta.dir, '../List/domainset/reject_phishing.conf'))) {
const l = processLine(line); const l = processLine(line);
if (l && l[0] === '.') { if (l?.[0] === '.') {
domainSuffixSet.add(l.slice(1)); domainSuffixSet.add(l.slice(1));
} }
} }

View File

@ -23,9 +23,11 @@ import { fetchWithRetry, defaultRequestInit } from './fetch-retry';
const decoder = new TextDecoder('utf-8'); const decoder = new TextDecoder('utf-8');
export async function *readFileByLine(file: string | BunFile): AsyncGenerator<string> { export async function *readFileByLine(file: string | URL | BunFile): AsyncGenerator<string> {
if (typeof file === 'string') { if (typeof file === 'string') {
file = Bun.file(file); file = Bun.file(file);
} else if (!('writer' in file)) {
file = Bun.file(file);
} }
let buf = ''; let buf = '';

View File

@ -111,29 +111,6 @@ export async function processFilterRules(
const whitelistDomainSets = new Set<string>(); const whitelistDomainSets = new Set<string>();
const blacklistDomainSets = new Set<string>(); const blacklistDomainSets = new Set<string>();
/**
* @param {string} domainToBeAddedToBlack
* @param {boolean} isSubDomain
*/
const addToBlackList = (domainToBeAddedToBlack: string, isSubDomain: boolean) => {
if (isSubDomain && domainToBeAddedToBlack[0] !== '.') {
blacklistDomainSets.add(`.${domainToBeAddedToBlack}`);
} else {
blacklistDomainSets.add(domainToBeAddedToBlack);
}
};
/**
* @param {string} domainToBeAddedToWhite
* @param {boolean} [isSubDomain]
*/
const addToWhiteList = (domainToBeAddedToWhite: string, isSubDomain = true) => {
if (isSubDomain && domainToBeAddedToWhite[0] !== '.') {
whitelistDomainSets.add(`.${domainToBeAddedToWhite}`);
} else {
whitelistDomainSets.add(domainToBeAddedToWhite);
}
};
let downloadTime = 0; let downloadTime = 0;
const gorhill = await getGorhillPublicSuffixPromise(); const gorhill = await getGorhillPublicSuffixPromise();
@ -142,7 +119,10 @@ export async function processFilterRules(
*/ */
const lineCb = (line: string) => { const lineCb = (line: string) => {
const result = parse(line, gorhill); const result = parse(line, gorhill);
if (result) { if (!result) {
return;
}
const flag = result[1]; const flag = result[1];
const hostname = result[0]; const hostname = result[0];
@ -157,21 +137,28 @@ export async function processFilterRules(
switch (flag) { switch (flag) {
case 0: case 0:
addToWhiteList(hostname, true); if (hostname[0] !== '.') {
whitelistDomainSets.add(`.${hostname}`);
} else {
whitelistDomainSets.add(hostname);
}
break; break;
case -1: case -1:
addToWhiteList(hostname, false); whitelistDomainSets.add(hostname);
break; break;
case 1: case 1:
addToBlackList(hostname, false); blacklistDomainSets.add(hostname);
break; break;
case 2: case 2:
addToBlackList(hostname, true); if (hostname[0] !== '.') {
blacklistDomainSets.add(`.${hostname}`);
} else {
blacklistDomainSets.add(hostname);
}
break; break;
default: default:
throw new Error(`Unknown flag: ${flag as any}`); throw new Error(`Unknown flag: ${flag as any}`);
} }
}
}; };
if (!fallbackUrls || fallbackUrls.length === 0) { if (!fallbackUrls || fallbackUrls.length === 0) {
@ -302,7 +289,6 @@ function parse($line: string, gorhill: PublicSuffixList): null | [hostname: stri
) { ) {
const hostname = normalizeDomain(filter.hostname); const hostname = normalizeDomain(filter.hostname);
if (!hostname) { if (!hostname) {
console.log(' * [parse-filter E0000] invalid domain:', filter.hostname);
return null; return null;
} }