- chore(ui): add module file and main stub - feat(ui): add layout, pages and partials - feat(ui): add stylesheet and scripts - feat(ui): add page renderer and static file handler - test(ui): cover rendering, escaping and static serving
130 lines
4.3 KiB
JavaScript
130 lines
4.3 KiB
JavaScript
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
export const EDITABLE_TYPES = ["A", "AAAA", "CNAME", "NS", "MX", "TXT", "SRV", "CAA"];
|
|
export const KNOWN_TYPES = [...EDITABLE_TYPES, "SOA"];
|
|
|
|
export const FIELD_SPECS = {
|
|
A: [
|
|
{ key: "ip", label: "IPv4 address", placeholder: "192.0.2.10" },
|
|
],
|
|
AAAA: [
|
|
{ key: "ip", label: "IPv6 address", placeholder: "2001:db8::10" },
|
|
],
|
|
CNAME: [
|
|
{ key: "host", label: "Target host", placeholder: "target.example.org" },
|
|
],
|
|
NS: [
|
|
{ key: "host", label: "Name server host", placeholder: "ns1.example.org" },
|
|
],
|
|
MX: [
|
|
{ key: "preference", label: "Preference", placeholder: "10", numeric: true },
|
|
{ key: "host", label: "Mail host", placeholder: "mail.example.org" },
|
|
],
|
|
TXT: [
|
|
{ key: "text", label: "Text", multiline: true, placeholder: "v=spf1 -all" },
|
|
],
|
|
SRV: [
|
|
{ key: "priority", label: "Priority", placeholder: "10", numeric: true },
|
|
{ key: "weight", label: "Weight", placeholder: "5", numeric: true },
|
|
{ key: "port", label: "Port", placeholder: "443", numeric: true },
|
|
{ key: "target", label: "Target host", placeholder: "host.example.org" },
|
|
],
|
|
CAA: [
|
|
{ key: "flag", label: "Flag", options: [["0", "0 (not critical)"], ["128", "128 (critical)"]] },
|
|
{ key: "tag", label: "Tag", placeholder: "issue" },
|
|
{ key: "value", label: "Value", placeholder: "ca.example.net" },
|
|
],
|
|
};
|
|
|
|
export function presentValue(record) {
|
|
const data = record.data;
|
|
if (!data || typeof data !== "object") return record.raw === null || record.raw === undefined ? "" : String(record.raw);
|
|
switch (record.type) {
|
|
case "A":
|
|
case "AAAA":
|
|
return String(data.ip);
|
|
case "CNAME":
|
|
case "NS":
|
|
return String(data.host);
|
|
case "MX":
|
|
return data.preference + " " + data.host;
|
|
case "TXT":
|
|
return '"' + data.text + '"';
|
|
case "SRV":
|
|
return data.priority + " " + data.weight + " " + data.port + " " + data.target;
|
|
case "CAA":
|
|
return data.flag + " " + data.tag + ' "' + data.value + '"';
|
|
case "SOA":
|
|
return [data.ns, data.MBox !== undefined ? data.MBox : data.mbox, data.serial === null || data.serial === undefined ? "dynamic" : data.serial, data.refresh, data.retry, data.expire, data.minttl].join(" ");
|
|
default:
|
|
return JSON.stringify(data);
|
|
}
|
|
}
|
|
|
|
export function displayName(record) {
|
|
return record.name === "" ? "@" : record.name;
|
|
}
|
|
|
|
function nameLabels(name) {
|
|
return name === "" ? [] : name.toLowerCase().split(".").reverse();
|
|
}
|
|
|
|
function compareLabels(a, b) {
|
|
const left = nameLabels(a);
|
|
const right = nameLabels(b);
|
|
const length = Math.min(left.length, right.length);
|
|
for (let i = 0; i < length; i += 1) {
|
|
if (left[i] < right[i]) return -1;
|
|
if (left[i] > right[i]) return 1;
|
|
}
|
|
return left.length - right.length;
|
|
}
|
|
|
|
function typeRank(type) {
|
|
if (type === "SOA") return 0;
|
|
if (type === "NS") return 1;
|
|
return 2;
|
|
}
|
|
|
|
export function compareRecords(a, b) {
|
|
const byName = compareLabels(a.name, b.name);
|
|
if (byName !== 0) return byName;
|
|
const byRank = typeRank(a.type) - typeRank(b.type);
|
|
if (byRank !== 0) return byRank;
|
|
if (a.type !== b.type) return a.type < b.type ? -1 : 1;
|
|
const left = presentValue(a);
|
|
const right = presentValue(b);
|
|
return left.localeCompare(right, "en", { numeric: true });
|
|
}
|
|
|
|
export function sortRecords(records) {
|
|
return [...records].sort(compareRecords);
|
|
}
|
|
|
|
export function matchesFilter(record, type, text) {
|
|
if (type && record.type !== type) return false;
|
|
if (!text) return true;
|
|
const needle = text.toLowerCase();
|
|
const haystack = [displayName(record), record.fqdn || "", record.type, presentValue(record)].join(" ").toLowerCase();
|
|
return haystack.includes(needle);
|
|
}
|
|
|
|
export function stripDot(name) {
|
|
return name.endsWith(".") ? name.slice(0, -1) : name;
|
|
}
|
|
|
|
export function previewName(input, zone) {
|
|
const apex = zone.endsWith(".") ? zone : zone + ".";
|
|
let text = input.trim().toLowerCase();
|
|
if (text === "" || text === "@") return { name: "", fqdn: apex, error: "" };
|
|
if (text.endsWith("." + apex)) {
|
|
text = text.slice(0, -(apex.length + 1));
|
|
} else if (text === apex) {
|
|
return { name: "", fqdn: apex, error: "" };
|
|
} else if (text.endsWith(".")) {
|
|
return { name: "", fqdn: "", error: "The name ends with a dot and is outside " + apex };
|
|
}
|
|
return { name: text, fqdn: text + "." + apex, error: "" };
|
|
}
|