This commit is contained in:
1 parent
65d16c2fc6
commit
32a0cf6801
6 files changed
+207
-39
No files matched your search
+112
-13
@@ -2,8 +2,26 @@
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
import { $, api, busy, formatTime, h, handleError, icon } from "./common.js";
|
||||
import { FIELD_SPECS } from "./records.js";
|
||||
|
||||
const state = { nextBeforeId: null, count: 0, filters: {}, counter: 0 };
|
||||
const SOA_FIELDS = [
|
||||
{ key: "ns", label: "Primary name server" },
|
||||
{ key: "MBox", label: "Hostmaster" },
|
||||
{ key: "serial", label: "Serial" },
|
||||
{ key: "refresh", label: "Refresh" },
|
||||
{ key: "retry", label: "Retry" },
|
||||
{ key: "expire", label: "Expire" },
|
||||
{ key: "minttl", label: "Minimum TTL" },
|
||||
];
|
||||
|
||||
function isObject(value) {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function isRecord(value) {
|
||||
return isObject(value) && Object.hasOwn(value, "id") && typeof value.record_type === "string";
|
||||
}
|
||||
|
||||
function flatten(value, prefix, out) {
|
||||
if (Array.isArray(value)) {
|
||||
@@ -17,12 +35,16 @@ function flatten(value, prefix, out) {
|
||||
}
|
||||
|
||||
function show(value) {
|
||||
return value === undefined ? "(none)" : JSON.stringify(value);
|
||||
if (value === undefined) return "—";
|
||||
if (typeof value === "string" && value !== "") {
|
||||
return value.replaceAll("\r", "\\r").replaceAll("\n", "\\n").replaceAll("\t", "\\t");
|
||||
}
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
function itemLabel(item, index) {
|
||||
if (item !== null && typeof item === "object" && !Array.isArray(item)) {
|
||||
if ("record_type" in item && "id" in item) return "record " + item.id;
|
||||
if (isRecord(item)) return "record " + item.id;
|
||||
if (isObject(item)) {
|
||||
if ("zone" in item && "username" in item && !("id" in item)) return "delegation " + item.zone + " " + item.username;
|
||||
if ("username" in item && "id" in item) return "user " + item.id;
|
||||
}
|
||||
@@ -36,10 +58,8 @@ function keyed(value) {
|
||||
return out;
|
||||
}
|
||||
|
||||
function diffValues(before, after, prefix) {
|
||||
const left = before === undefined ? new Map() : flatten(before, prefix, new Map());
|
||||
const right = after === undefined ? new Map() : flatten(after, prefix, new Map());
|
||||
const paths = [...new Set([...left.keys(), ...right.keys()])].filter((path) => path !== "");
|
||||
function compareFields(left, right) {
|
||||
const paths = [...new Set([...left.keys(), ...right.keys()])];
|
||||
return paths.map((path) => {
|
||||
const old = left.get(path);
|
||||
const now = right.get(path);
|
||||
@@ -47,13 +67,74 @@ function diffValues(before, after, prefix) {
|
||||
if (!left.has(path)) kind = "added";
|
||||
else if (!right.has(path)) kind = "removed";
|
||||
else if (JSON.stringify(old) !== JSON.stringify(now)) kind = "changed";
|
||||
return { path, kind, before: left.has(path) ? old : undefined, after: right.has(path) ? now : undefined };
|
||||
return { path, kind, before: old, after: now };
|
||||
});
|
||||
}
|
||||
|
||||
function recordFields(value) {
|
||||
if (!isRecord(value)) return value;
|
||||
const { record_type, content, ...fields } = value;
|
||||
return { ...fields, type: record_type };
|
||||
}
|
||||
|
||||
function contentFields(record) {
|
||||
const fields = new Map();
|
||||
const labels = new Map();
|
||||
if (!isRecord(record) || !Object.hasOwn(record, "content")) return { fields, labels };
|
||||
let content = record.content;
|
||||
if (typeof content === "string") {
|
||||
try {
|
||||
content = JSON.parse(content);
|
||||
} catch {
|
||||
// Unparseable records keep their original text in the audit.
|
||||
}
|
||||
}
|
||||
if (!isObject(content) || Object.keys(content).length === 0) {
|
||||
fields.set("", content);
|
||||
return { fields, labels };
|
||||
}
|
||||
const type = record.record_type.toUpperCase();
|
||||
if (type === "SOA" && !Object.hasOwn(content, "MBox") && Object.hasOwn(content, "mbox")) {
|
||||
const { mbox, ...rest } = content;
|
||||
content = { ...rest, MBox: mbox };
|
||||
}
|
||||
const specs = type === "SOA" ? SOA_FIELDS : Object.hasOwn(FIELD_SPECS, type) ? FIELD_SPECS[type] : [];
|
||||
for (const { key, label } of specs) {
|
||||
labels.set(key, label);
|
||||
if (Object.hasOwn(content, key)) fields.set(key, content[key]);
|
||||
}
|
||||
for (const key of Object.keys(content).sort()) {
|
||||
if (!fields.has(key)) fields.set(key, content[key]);
|
||||
}
|
||||
return { fields, labels };
|
||||
}
|
||||
|
||||
function diffValues(before, after, prefix) {
|
||||
const left = before == null ? new Map() : flatten(recordFields(before), prefix, new Map());
|
||||
const right = after == null ? new Map() : flatten(recordFields(after), prefix, new Map());
|
||||
const rows = compareFields(left, right).filter(({ path }) => path !== "");
|
||||
if (isRecord(before) || isRecord(after)) {
|
||||
const oldContent = contentFields(before);
|
||||
const newContent = contentFields(after);
|
||||
const fields = compareFields(oldContent.fields, newContent.fields);
|
||||
if (fields.length !== 0) {
|
||||
// Put changed fields first so they are visible before horizontal scrolling.
|
||||
fields.sort((a, b) => Number(a.kind === "same") - Number(b.kind === "same"));
|
||||
rows.push({
|
||||
path: prefix ? prefix + ".content" : "content",
|
||||
kind: fields[0].kind === "same" ? "same" : "changed",
|
||||
fields,
|
||||
beforeLabels: oldContent.labels,
|
||||
afterLabels: newContent.labels,
|
||||
});
|
||||
}
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
export function diffRows(before, after) {
|
||||
if (!Array.isArray(before) && !Array.isArray(after)) {
|
||||
return diffValues(before === undefined ? null : before, after === undefined ? null : after, "");
|
||||
return diffValues(before, after, "");
|
||||
}
|
||||
const left = keyed(before);
|
||||
const right = keyed(after);
|
||||
@@ -61,15 +142,33 @@ export function diffRows(before, after) {
|
||||
return labels.flatMap((label) => diffValues(left.has(label) ? left.get(label) : undefined, right.has(label) ? right.get(label) : undefined, label));
|
||||
}
|
||||
|
||||
function diffValue(value, kind, side, label = "") {
|
||||
const changed = value !== undefined && kind !== "same";
|
||||
const color = changed ? " diff-" + side : "";
|
||||
return h("code", { class: "diff-value" + color },
|
||||
changed ? h("span", { "aria-hidden": "true", text: side === "before" ? "− " : "+ " }) : null,
|
||||
label ? h("strong", { text: label + ": " }) : null,
|
||||
show(value));
|
||||
}
|
||||
|
||||
function diffCell(row, side) {
|
||||
if (!row.fields) return h("td", null, diffValue(row[side], row.kind, side));
|
||||
const labels = side === "before" ? row.beforeLabels : row.afterLabels;
|
||||
const parts = row.fields.filter((field) => field[side] !== undefined);
|
||||
return h("td", null,
|
||||
h("div", { class: "diff-content", tabindex: "0", role: "group", "aria-label": side === "before" ? "Before content" : "After content" },
|
||||
parts.length ? parts.map((field) => diffValue(field[side], field.kind, side, labels.get(field.path) ?? field.path)) : "—"));
|
||||
}
|
||||
|
||||
function diffTable(entry) {
|
||||
const rows = diffRows(entry.before, entry.after);
|
||||
if (rows.length === 0) return h("p", { class: "muted", text: "No field-level data." });
|
||||
return h("table", { class: "table diff" },
|
||||
return h("div", { class: "table-wrap" }, h("table", { class: "table diff" },
|
||||
h("thead", null, h("tr", null, h("th", { scope: "col", text: "Field" }), h("th", { scope: "col", text: "Before" }), h("th", { scope: "col", text: "After" }))),
|
||||
h("tbody", null, rows.map((row) => h("tr", { class: "diff-" + row.kind },
|
||||
h("tbody", null, rows.map((row) => h("tr", { class: "diff-" + row.kind + (row.fields ? " diff-content-row" : "") },
|
||||
h("th", { scope: "row", class: "mono", text: row.path }),
|
||||
h("td", { class: "mono", text: show(row.before) }),
|
||||
h("td", { class: "mono", text: show(row.after) })))));
|
||||
diffCell(row, "before"),
|
||||
diffCell(row, "after"))))));
|
||||
}
|
||||
|
||||
function entryRows(entry) {
|
||||
|
||||
Reference in new issue
Block a user