File size: 3,972 Bytes
ea775bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | import { apiRequest } from "../../lib/api-client";
import type { EnvConfig, RuntimeConfig, RuntimeConfigPatch } from "./types";
const path = "/api/v1/system/config";
const DEFAULT_CONFIG: RuntimeConfig = {
request_log_enabled: true,
reverse_proxy_log_detail_enabled: false,
reverse_proxy_log_req_headers_max_bytes: 0,
reverse_proxy_log_req_body_max_bytes: 0,
reverse_proxy_log_resp_headers_max_bytes: 0,
reverse_proxy_log_resp_body_max_bytes: 0,
max_consecutive_failures: 0,
max_latency_test_interval: "",
max_authority_latency_test_interval: "",
max_egress_test_interval: "",
latency_test_url: "",
latency_authorities: [],
p2c_latency_window: "",
latency_decay_window: "",
cache_flush_interval: "",
cache_flush_dirty_threshold: 0,
};
function asNumber(raw: unknown, fallback: number): number {
const value = Number(raw);
if (!Number.isFinite(value)) {
return fallback;
}
return value;
}
function asString(raw: unknown, fallback: string): string {
if (typeof raw !== "string") {
return fallback;
}
return raw;
}
function normalizeRuntimeConfig(raw: Partial<RuntimeConfig> | null | undefined): RuntimeConfig {
if (!raw) {
return DEFAULT_CONFIG;
}
return {
request_log_enabled: Boolean(raw.request_log_enabled),
reverse_proxy_log_detail_enabled: Boolean(raw.reverse_proxy_log_detail_enabled),
reverse_proxy_log_req_headers_max_bytes: asNumber(
raw.reverse_proxy_log_req_headers_max_bytes,
DEFAULT_CONFIG.reverse_proxy_log_req_headers_max_bytes,
),
reverse_proxy_log_req_body_max_bytes: asNumber(
raw.reverse_proxy_log_req_body_max_bytes,
DEFAULT_CONFIG.reverse_proxy_log_req_body_max_bytes,
),
reverse_proxy_log_resp_headers_max_bytes: asNumber(
raw.reverse_proxy_log_resp_headers_max_bytes,
DEFAULT_CONFIG.reverse_proxy_log_resp_headers_max_bytes,
),
reverse_proxy_log_resp_body_max_bytes: asNumber(
raw.reverse_proxy_log_resp_body_max_bytes,
DEFAULT_CONFIG.reverse_proxy_log_resp_body_max_bytes,
),
max_consecutive_failures: asNumber(raw.max_consecutive_failures, DEFAULT_CONFIG.max_consecutive_failures),
max_latency_test_interval: asString(raw.max_latency_test_interval, DEFAULT_CONFIG.max_latency_test_interval),
max_authority_latency_test_interval: asString(
raw.max_authority_latency_test_interval,
DEFAULT_CONFIG.max_authority_latency_test_interval,
),
max_egress_test_interval: asString(raw.max_egress_test_interval, DEFAULT_CONFIG.max_egress_test_interval),
latency_test_url: asString(raw.latency_test_url, DEFAULT_CONFIG.latency_test_url),
latency_authorities: Array.isArray(raw.latency_authorities)
? raw.latency_authorities.filter((item): item is string => typeof item === "string")
: DEFAULT_CONFIG.latency_authorities,
p2c_latency_window: asString(raw.p2c_latency_window, DEFAULT_CONFIG.p2c_latency_window),
latency_decay_window: asString(raw.latency_decay_window, DEFAULT_CONFIG.latency_decay_window),
cache_flush_interval: asString(raw.cache_flush_interval, DEFAULT_CONFIG.cache_flush_interval),
cache_flush_dirty_threshold: asNumber(
raw.cache_flush_dirty_threshold,
DEFAULT_CONFIG.cache_flush_dirty_threshold,
),
};
}
export async function getSystemConfig(): Promise<RuntimeConfig> {
const data = await apiRequest<RuntimeConfig>(path);
return normalizeRuntimeConfig(data);
}
export async function getDefaultSystemConfig(): Promise<RuntimeConfig> {
const data = await apiRequest<RuntimeConfig>(path + "/default");
return normalizeRuntimeConfig(data);
}
export async function patchSystemConfig(patch: RuntimeConfigPatch): Promise<RuntimeConfig> {
const data = await apiRequest<RuntimeConfig>(path, {
method: "PATCH",
body: patch,
});
return normalizeRuntimeConfig(data);
}
export async function getEnvConfig(): Promise<EnvConfig> {
return await apiRequest<EnvConfig>(path + "/env");
}
|