Refactor normalizeOptions function; streamline query parameter extraction and improve boolean conversion logic for better clarity and performance
This commit is contained in:
@@ -25,55 +25,42 @@ const AMZ_DEFAULTS = {
|
|||||||
* - ipv6=false => удалить IPv6 из allowed-ips (и вообще не добавлять ipv6-части)
|
* - ipv6=false => удалить IPv6 из allowed-ips (и вообще не добавлять ipv6-части)
|
||||||
**********************/
|
**********************/
|
||||||
function normalizeOptions(rawOptions) {
|
function normalizeOptions(rawOptions) {
|
||||||
// $options может быть объектом или строкой или чем-то странным.
|
|
||||||
const opts = rawOptions ?? {};
|
|
||||||
let q = "";
|
|
||||||
|
|
||||||
if (typeof opts === "string") {
|
// rawOptions может быть
|
||||||
q = opts;
|
// 1) {}
|
||||||
} else if (typeof opts === "object") {
|
// 2) { meta: { url: "..." } }
|
||||||
// часто движки кладут query в одном из полей
|
// 3) { url: "..." }
|
||||||
q =
|
const urlStr =
|
||||||
opts.query ||
|
(rawOptions && rawOptions.meta && rawOptions.meta.url) ||
|
||||||
opts.params ||
|
rawOptions.url ||
|
||||||
opts.search ||
|
"";
|
||||||
opts.hash ||
|
|
||||||
opts.raw ||
|
// Ищем part после #
|
||||||
"";
|
const hashIdx = urlStr.indexOf("#");
|
||||||
}
|
const qstr = hashIdx >= 0 ? urlStr.slice(hashIdx + 1) : "";
|
||||||
|
|
||||||
// Если q выглядит как "dns=false&ipv6=false"
|
|
||||||
const params = {};
|
const params = {};
|
||||||
if (typeof q === "string" && q.includes("=")) {
|
for (const part of qstr.split("&")) {
|
||||||
for (const part of q.split("&")) {
|
const [k, v] = part.split("=");
|
||||||
const [k, v] = part.split("=");
|
if (!k) continue;
|
||||||
if (!k) continue;
|
params[k.trim().toLowerCase()] = (v ?? "").trim();
|
||||||
params[String(k).trim()] = (v ?? "").trim();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Фоллбек: если $options уже содержит ключи dns/ipv6
|
const asBool = (v, def) => {
|
||||||
const get = (key) => {
|
if (v === undefined || v === "") return def;
|
||||||
if (params[key] !== undefined) return params[key];
|
const s = String(v).toLowerCase();
|
||||||
if (opts[key] !== undefined) return opts[key];
|
if (["1","true","yes","on"].includes(s)) return true;
|
||||||
return undefined;
|
if (["0","false","no","off"].includes(s)) return false;
|
||||||
};
|
return def;
|
||||||
|
|
||||||
const asBool = (v, defaultVal = true) => {
|
|
||||||
if (v === undefined || v === null || v === "") return defaultVal;
|
|
||||||
if (typeof v === "boolean") return v;
|
|
||||||
const s = String(v).toLowerCase().trim();
|
|
||||||
if (["1", "true", "yes", "y", "on"].includes(s)) return true;
|
|
||||||
if (["0", "false", "no", "n", "off"].includes(s)) return false;
|
|
||||||
return defaultVal;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
dns: asBool(get("dns"), true),
|
dns: asBool(params.dns, true),
|
||||||
ipv6: asBool(get("ipv6"), true),
|
ipv6: asBool(params.ipv6, true),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**********************
|
/**********************
|
||||||
* Parsing WG INI blocks
|
* Parsing WG INI blocks
|
||||||
**********************/
|
**********************/
|
||||||
|
|||||||
Reference in New Issue
Block a user