mirror of
https://github.com/Nezumi-2711/cloudflare-gateway-pihole-scripts.git
synced 2026-09-22 13:38:37 +00:00
refactored domain processing code
This commit is contained in:
@@ -40,3 +40,22 @@ const request = async (url, options) => {
|
||||
*/
|
||||
export const requestGateway = (path, options) =>
|
||||
request(`${API_HOST}/accounts/${ACCOUNT_ID}/gateway${path}`, options);
|
||||
|
||||
/**
|
||||
* Normalizes a domain.
|
||||
* @param {string} value The value to be normalized.
|
||||
* @param {boolean} isAllowlisting Whether the value is to be whitelisted.
|
||||
* @returns {string}
|
||||
*/
|
||||
export const normalizeDomain = (value, isAllowlisting) => {
|
||||
const normalized = value
|
||||
.replace(/(0\.0\.0\.0|127\.0\.0\.1|::1|::)\s+/, "")
|
||||
.replace("||", "")
|
||||
.replace("^$important", "")
|
||||
.replace("*.", "")
|
||||
.replace("^", "");
|
||||
|
||||
if (isAllowlisting) return normalized.replace("@@||", "");
|
||||
|
||||
return normalized;
|
||||
};
|
||||
|
||||
+48
-5
@@ -1,3 +1,8 @@
|
||||
import { once } from "events";
|
||||
import { createReadStream } from "fs";
|
||||
import { basename } from "path";
|
||||
import { createInterface } from "readline";
|
||||
|
||||
/**
|
||||
* Sleeps for a specified amount of time.
|
||||
* @param {number} [ms=350] The amount of time in ms.
|
||||
@@ -6,9 +11,47 @@ export const sleep = (ms = 350) =>
|
||||
new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
/**
|
||||
* Truncates an array to the specified size.
|
||||
* @param {any[]} arr The array to be truncated.
|
||||
* @param {number} size The size to which the array will be truncated.
|
||||
* @returns {any[]}
|
||||
* Checks if the value is a valid domain.
|
||||
* @param {string} value The value to be checked.
|
||||
*/
|
||||
export const truncateArray = (arr, size) => arr.slice(0, size);
|
||||
export const isValidDomain = (value) =>
|
||||
/^(?!-)[A-Za-z0-9-]+([\-\.]{1}[a-z0-9]+)*\.[A-Za-z]{2,6}$/.test(value);
|
||||
|
||||
/**
|
||||
* Checks if the value is a comment.
|
||||
* @param {string} value The value to be checked.
|
||||
*/
|
||||
export const isComment = (value) =>
|
||||
value.startsWith("#") ||
|
||||
value.startsWith("//") ||
|
||||
value.startsWith("!") ||
|
||||
value.startsWith("/*") ||
|
||||
value.startsWith("*/");
|
||||
|
||||
/**
|
||||
* @callback onLine
|
||||
* @param {string} line The current line.
|
||||
* @param {ReturnType<createInterface>} rl The readline interface.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Asynchronously reads a file line by line.
|
||||
* @param {string} filePath The path to the file.
|
||||
* @param {onLine} onLine The callback executed on each line read.
|
||||
*/
|
||||
export const readFile = async (filePath, onLine) => {
|
||||
try {
|
||||
const rl = createInterface({
|
||||
input: createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
rl.on("line", (line) => onLine(line, rl));
|
||||
|
||||
await once(rl, "close");
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`Error occurred while reading ${basename(filePath)} - ${err.toString()}`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user