Refactor normalizeOptions function; simplify argument handling and improve boolean conversion logic for better clarity and performance

This commit is contained in:
2026-01-05 19:58:15 +03:00
parent 2fce3a192a
commit 3dafd087cd

View File

@@ -24,52 +24,10 @@ const AMZ_DEFAULTS = {
* - dns=false => remote-dns-resolve: false (вне зависимости от входа)
* - ipv6=false => удалить IPv6 из allowed-ips (и вообще не добавлять ipv6-части)
**********************/
function normalizeOptions(rawOptions) {
const opts = rawOptions ?? {};
function normalizeOptions() {
const args = (typeof $arguments !== "undefined" && $arguments) ? $arguments : {};
// попробуем найти URL в разных местах (Sub Store версии гуляют)
const urlStr =
(typeof opts === "string" ? opts : "") ||
opts.url ||
(opts.meta && opts.meta.url) ||
(opts.subscription && opts.subscription.url) ||
(opts.profile && opts.profile.url) ||
(opts.ctx && opts.ctx.url) ||
(opts.request && opts.request.url) ||
"";
// вытащить hash часть (#...)
let hash = "";
if (typeof urlStr === "string") {
const idx = urlStr.indexOf("#");
if (idx >= 0) hash = urlStr.slice(idx + 1);
}
// иногда Sub Store может передать query прямо как объект
// пробуем дополнительно распарсить opts.query/opts.params, если они строки
const extra =
(typeof opts.query === "string" ? opts.query : "") ||
(typeof opts.params === "string" ? opts.params : "") ||
(typeof opts.hash === "string" ? opts.hash : "") ||
"";
const qstr = (hash || extra || "").trim();
const params = {};
if (qstr.includes("=")) {
for (const part of qstr.split("&")) {
if (!part) continue;
const [k, v] = part.split("=");
if (!k) continue;
params[k.trim().toLowerCase()] = (v ?? "").trim();
}
}
// ещё фоллбек: если opts.dns/opts.ipv6 передали напрямую
const directDns = opts.dns;
const directIpv6 = opts.ipv6;
const asBool = (v, def) => {
const asBool = (v, def = true) => {
if (v === undefined || v === null || v === "") return def;
if (typeof v === "boolean") return v;
const s = String(v).toLowerCase().trim();
@@ -79,13 +37,12 @@ function normalizeOptions(rawOptions) {
};
return {
dns: asBool(params.dns ?? directDns, true),
ipv6: asBool(params.ipv6 ?? directIpv6, true),
dns: asBool(args.dns, true),
ipv6: asBool(args.ipv6, true),
};
}
/**********************
* Parsing WG INI blocks
**********************/
@@ -249,7 +206,7 @@ function buildProxy(blockName, wg, options) {
/**********************
* ENTRYPOINT
**********************/
const opts = normalizeOptions($options);
const opts = normalizeOptions();
// Вход: чаще всего $content, но на всякий пожарный берём $files[0]
const input = String($content ?? ($files && $files[0]) ?? "");