mirror of
https://github.com/Nezumi-2711/cloudflare-gateway-pihole-scripts.git
synced 2026-09-22 13:38:37 +00:00
implement memoization for domain normalization
This commit is contained in:
+4
-2
@@ -17,6 +17,7 @@ import {
|
|||||||
extractDomain,
|
extractDomain,
|
||||||
isComment,
|
isComment,
|
||||||
isValidDomain,
|
isValidDomain,
|
||||||
|
memoize,
|
||||||
readFile,
|
readFile,
|
||||||
} from "./lib/utils.js";
|
} from "./lib/utils.js";
|
||||||
|
|
||||||
@@ -33,6 +34,7 @@ let processedDomainCount = 0;
|
|||||||
let unnecessaryDomainCount = 0;
|
let unnecessaryDomainCount = 0;
|
||||||
let duplicateDomainCount = 0;
|
let duplicateDomainCount = 0;
|
||||||
let allowedDomainCount = 0;
|
let allowedDomainCount = 0;
|
||||||
|
const memoizedNormalizeDomain = memoize(normalizeDomain);
|
||||||
|
|
||||||
// Read allowlist
|
// Read allowlist
|
||||||
console.log(`Processing ${allowlistFilename}`);
|
console.log(`Processing ${allowlistFilename}`);
|
||||||
@@ -43,7 +45,7 @@ await readFile(resolve(`./${allowlistFilename}`), (line) => {
|
|||||||
|
|
||||||
if (isComment(_line)) return;
|
if (isComment(_line)) return;
|
||||||
|
|
||||||
const domain = normalizeDomain(_line, true);
|
const domain = memoizedNormalizeDomain(_line, true);
|
||||||
|
|
||||||
if (!isValidDomain(domain)) return;
|
if (!isValidDomain(domain)) return;
|
||||||
|
|
||||||
@@ -65,7 +67,7 @@ await readFile(resolve(`./${blocklistFilename}`), (line, rl) => {
|
|||||||
if (isComment(_line)) return;
|
if (isComment(_line)) return;
|
||||||
|
|
||||||
// Remove prefixes and suffixes in hosts, wildcard or adblock format
|
// Remove prefixes and suffixes in hosts, wildcard or adblock format
|
||||||
const domain = normalizeDomain(_line);
|
const domain = memoizedNormalizeDomain(_line);
|
||||||
|
|
||||||
// Check if it is a valid domain which is not a URL or does not contain
|
// Check if it is a valid domain which is not a URL or does not contain
|
||||||
// characters like * in the middle of the domain
|
// characters like * in the middle of the domain
|
||||||
|
|||||||
@@ -90,3 +90,24 @@ export const readFile = async (filePath, onLine) => {
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Memoizes a function
|
||||||
|
* @template T The argument type of the function.
|
||||||
|
* @template R The return type of the function.
|
||||||
|
* @param {(...fnArgs: T[]) => R} fn The function to be memoized.
|
||||||
|
*/
|
||||||
|
export const memoize = (fn) => {
|
||||||
|
const cache = new Map();
|
||||||
|
|
||||||
|
return (...args) => {
|
||||||
|
const key = args.join("-");
|
||||||
|
|
||||||
|
if (cache.has(key)) return cache.get(key);
|
||||||
|
|
||||||
|
const result = fn(...args);
|
||||||
|
|
||||||
|
cache.set(key, result);
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user