fix(ui): различать нулевые и неизвестные счётчики WireGuard

This commit is contained in:
Denozordec committed 2026-10-04 11:58:10 +07:00
1 parent 53c1bfbd3b
commit 7693b32372
9 files changed
+59 -16

No files matched your search

+1 -6
View File
@@ -1,3 +1,4 @@
import { parseWireGuardBytes as parseBytes } from "./wireguard-observed-values.js"
import {boundedCompleteCount,completeCollectionCount} from "./complete-count.js"
import { eq } from "drizzle-orm"
import { db } from "../db/index.js"
@@ -53,12 +54,6 @@ interface RosIpAddress {
disabled?: string
}
function parseBytes(v: string | undefined): number | undefined {
if (v == null || v === "") return undefined
const n = Number.parseInt(v, 10)
return Number.isFinite(n) ? n : undefined
}
function mapPeer(p: RosWireGuardPeer, idx: number): WgPeerDto {
const rosId = String(p[".id"] ?? `peer-${idx}`)
const allowed = (p["allowed-address"] ?? "")
@@ -0,0 +1,9 @@
import { test } from "node:test"
import assert from "node:assert/strict"
import { parseWireGuardBytes } from "./wireguard-observed-values.js"
test("WireGuard byte counters preserve zero and reject partial, negative or unsafe values", () => {
assert.equal(parseWireGuardBytes("0"), 0)
assert.equal(parseWireGuardBytes(" 1234 "), 1234)
assert.equal(parseWireGuardBytes("9007199254740991"), Number.MAX_SAFE_INTEGER)
for (const raw of [undefined, null, 0, {}, "", " ", "12junk", "1.5", "-1", "Infinity", "1e3", "9007199254740992"]) assert.equal(parseWireGuardBytes(raw), undefined)
})
@@ -0,0 +1,6 @@
// RouterOS byte counters are whole decimal values; absent or malformed values are unknown.
export function parseWireGuardBytes(raw: unknown): number | undefined {
if (typeof raw !== "string" || !/^[0-9]+$/.test(raw.trim())) return undefined
const value = Number(raw.trim())
return Number.isSafeInteger(value) && value >= 0 ? value : undefined
}