@takodotid/circuit 0.18.5 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/chunks/index-jtzfw679.js +936 -0
- package/dist/chunks/{index-ex1wcst7.js → index-xm9nma2t.js} +20 -1
- package/dist/chunks/server-5shmsmse.js +246 -0
- package/dist/cli/index.js +701 -106
- package/dist/index.js +1 -1
- package/dist/presets/index.js +1 -1
- package/dist/types/adapters/devices/derive.d.ts +2 -0
- package/dist/types/core/across.d.ts +4 -0
- package/dist/types/core/addr.d.ts +10 -0
- package/dist/types/core/addresses.d.ts +46 -0
- package/dist/types/core/files.d.ts +6 -0
- package/dist/types/core/graph.d.ts +74 -0
- package/dist/types/core/schema-docs.d.ts +16 -0
- package/package.json +1 -1
- package/src/adapters/devices/derive.ts +32 -0
- package/src/adapters/devices/raisecom-ros/index.ts +23 -3
- package/src/adapters/devices/routeros/index.ts +10 -0
- package/src/adapters/devices/vrp/index.ts +10 -1
- package/src/cli/graph.ts +89 -0
- package/src/cli/index.ts +205 -2
- package/src/cli/rename.ts +129 -0
- package/src/core/across.ts +322 -0
- package/src/core/addr.ts +29 -0
- package/src/core/addresses.ts +316 -0
- package/src/core/files.ts +30 -0
- package/src/core/graph.ts +634 -0
- package/src/core/schema-docs.ts +166 -0
- package/src/core/validate.ts +2 -52
- package/src/studio/data.ts +255 -0
- package/src/studio/git.ts +119 -0
- package/src/studio/public/app.js +147 -0
- package/src/studio/public/compare.js +243 -0
- package/src/studio/public/diff.js +97 -0
- package/src/studio/public/dom.js +108 -0
- package/src/studio/public/evaluate.js +648 -0
- package/src/studio/public/export.js +122 -0
- package/src/studio/public/favicon.svg +6 -0
- package/src/studio/public/graph.js +914 -0
- package/src/studio/public/index.html +67 -0
- package/src/studio/public/net.js +128 -0
- package/src/studio/public/style.css +693 -0
- package/src/studio/public/tables.js +1223 -0
- package/src/studio/public/tools.js +578 -0
- package/src/studio/server.ts +218 -0
- package/templates/project/AGENTS.md +12 -9
- package/templates/project/README.md +1 -0
package/dist/index.js
CHANGED
package/dist/presets/index.js
CHANGED
|
@@ -3,3 +3,5 @@ import type { Device } from "../../schema";
|
|
|
3
3
|
export declare function lagIds(device: Device): Map<string, number>;
|
|
4
4
|
/** The IPv4 networks of a port's or an interface's addresses. */
|
|
5
5
|
export declare function ipv4Networks(device: Device, iface: string): string[];
|
|
6
|
+
/** Fields neither switch platform, VRP nor Raisecom ROS, expresses: a service's own sources or port, a user's description or keys, the IP stack's host settings, a point-to-point address, and offload per interface. */
|
|
7
|
+
export declare function switchUnsupported(device: Device): string[];
|
|
@@ -14,6 +14,16 @@ export declare function isCidr(text: string): boolean;
|
|
|
14
14
|
export declare function network(cidr: string): string;
|
|
15
15
|
/** Is the address or prefix `inner` inside `outer`? */
|
|
16
16
|
export declare function contains(outer: string, inner: string): boolean;
|
|
17
|
+
/** A number back to the address it stands for. */
|
|
18
|
+
export declare const formatAddress: (value: bigint, family: Family) => string;
|
|
19
|
+
/** The first and last address of a prefix, as numbers. */
|
|
20
|
+
export declare function rangeOf(cidr: string): {
|
|
21
|
+
first: bigint;
|
|
22
|
+
last: bigint;
|
|
23
|
+
family: Family;
|
|
24
|
+
};
|
|
25
|
+
/** The fewest prefixes that cover every address from `first` to `last` and nothing else. */
|
|
26
|
+
export declare function prefixesBetween(first: bigint, last: bigint, family: Family): string[];
|
|
17
27
|
/** `/25` to `255.255.255.128`. */
|
|
18
28
|
export declare const netmask: (length: number) => string;
|
|
19
29
|
/** `/24` to `0.0.0.255`, the inverted mask some ACLs take. */
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Device } from "../schema";
|
|
2
|
+
/** One address or range in a segment, and what holds it. */
|
|
3
|
+
export type Use = {
|
|
4
|
+
/** An address, or `first-last` for a range. */
|
|
5
|
+
address: string;
|
|
6
|
+
what: string;
|
|
7
|
+
};
|
|
8
|
+
/** A prefix our devices have addresses in. */
|
|
9
|
+
export type Segment = {
|
|
10
|
+
prefix: string;
|
|
11
|
+
uses: Use[];
|
|
12
|
+
/** How many addresses no one uses, as a number written out, since an IPv6 count does not fit a JavaScript number. */
|
|
13
|
+
free: string;
|
|
14
|
+
/** The lowest free address, when there is one. */
|
|
15
|
+
next?: string;
|
|
16
|
+
};
|
|
17
|
+
/** A prefix in use without being a segment: a loopback's address, or a block routed elsewhere. */
|
|
18
|
+
export type Block = {
|
|
19
|
+
prefix: string;
|
|
20
|
+
what: string;
|
|
21
|
+
};
|
|
22
|
+
/** Space the network owns: a prefix it originates in BGP, or one asked for. Everything in use inside it, and what is free. */
|
|
23
|
+
export type Space = {
|
|
24
|
+
prefix: string;
|
|
25
|
+
/** Why it is listed, such as `originated by edge-01`. */
|
|
26
|
+
owner: string;
|
|
27
|
+
segments: Segment[];
|
|
28
|
+
blocks: Block[];
|
|
29
|
+
/** The fewest prefixes that cover what nothing uses. */
|
|
30
|
+
free: string[];
|
|
31
|
+
};
|
|
32
|
+
export type AddressReport = {
|
|
33
|
+
spaces: Space[];
|
|
34
|
+
/** What is in use outside every space. Left out when a prefix is asked for. */
|
|
35
|
+
elsewhere: {
|
|
36
|
+
segments: Segment[];
|
|
37
|
+
blocks: Block[];
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
/** Every address in use, by segment, under the space the network owns. With `within`, only what falls inside that prefix, as the one space. */
|
|
41
|
+
export declare function addressesOf(devices: readonly Device[], within?: string): AddressReport;
|
|
42
|
+
/** Free prefixes of one length inside the free space, up to `limit`, and how many there are in all. */
|
|
43
|
+
export declare function freeOfLength(free: readonly string[], length: number, limit: number): {
|
|
44
|
+
prefixes: string[];
|
|
45
|
+
total: bigint;
|
|
46
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Every TypeScript file of the project, relative to its root, in name order. */
|
|
2
|
+
export declare function projectSources(root: string, directory?: string): string[];
|
|
3
|
+
/** Whether a file's text declares the device: it calls defineDevice and gives it the name, written out whole. */
|
|
4
|
+
export declare function declaresDevice(text: string, name: string): boolean;
|
|
5
|
+
/** The project files that declare the device, relative to the root. */
|
|
6
|
+
export declare function deviceFiles(root: string, name: string, sources?: string[]): string[];
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Device, Routed } from "../schema";
|
|
2
|
+
import type { Finding } from "./define";
|
|
3
|
+
/** One view of the network. Every edge belongs to one. */
|
|
4
|
+
export type Layer = "l1" | "l2" | "l3" | "bgp" | "ospf" | "tunnels";
|
|
5
|
+
export declare const LAYERS: readonly Layer[];
|
|
6
|
+
/** One of our devices, a segment several of them share, or something outside the network. */
|
|
7
|
+
export type GraphNode = {
|
|
8
|
+
/** Unique among the nodes, such as `device:edge-01` or `as:64501`. */
|
|
9
|
+
id: string;
|
|
10
|
+
kind: "device" | "segment" | "outside";
|
|
11
|
+
/** A word or two, drawn on the node. */
|
|
12
|
+
label: string;
|
|
13
|
+
/** One fact per line, for a closer look. */
|
|
14
|
+
detail: string[];
|
|
15
|
+
};
|
|
16
|
+
/** A VLAN a cable carries. */
|
|
17
|
+
export type CarriedVlan = {
|
|
18
|
+
name: string;
|
|
19
|
+
id: number;
|
|
20
|
+
/** Carried without a tag: the access VLAN, or a trunk's native VLAN. */
|
|
21
|
+
untagged: boolean;
|
|
22
|
+
};
|
|
23
|
+
export type GraphEdge = {
|
|
24
|
+
layer: Layer;
|
|
25
|
+
/** Node ids. */
|
|
26
|
+
from: string;
|
|
27
|
+
to: string;
|
|
28
|
+
/** A few words, drawn on the edge. */
|
|
29
|
+
label: string;
|
|
30
|
+
/** One fact per line, for a closer look. */
|
|
31
|
+
detail: string[];
|
|
32
|
+
/** Only one end declares it: the far device does not declare the cable, session or tunnel back. */
|
|
33
|
+
oneSided?: true;
|
|
34
|
+
/** On `l2`, the VLANs the cable carries. */
|
|
35
|
+
vlans?: CarriedVlan[];
|
|
36
|
+
};
|
|
37
|
+
export type Graph = {
|
|
38
|
+
nodes: GraphNode[];
|
|
39
|
+
edges: GraphEdge[];
|
|
40
|
+
};
|
|
41
|
+
/** How a device compares with its last snapshot, found offline as `diff` finds it. */
|
|
42
|
+
export type Drift = {
|
|
43
|
+
status: "matches" | "differs" | "no snapshot" | "cannot plan";
|
|
44
|
+
/** The plan `diff` would show, when it differs. */
|
|
45
|
+
lines: string[];
|
|
46
|
+
};
|
|
47
|
+
/** The graph with what `validate` found and how each device drawn compares with its snapshot. */
|
|
48
|
+
export type Annotated = Graph & {
|
|
49
|
+
findings: Finding[];
|
|
50
|
+
/** By device name. */
|
|
51
|
+
drift: Record<string, Drift>;
|
|
52
|
+
};
|
|
53
|
+
/** An address one of the devices holds on a port or interface. */
|
|
54
|
+
export type Holder = {
|
|
55
|
+
device: Device;
|
|
56
|
+
/** The port or interface. */
|
|
57
|
+
name: string;
|
|
58
|
+
/** With its length. */
|
|
59
|
+
address: string;
|
|
60
|
+
/** The far end of a point-to-point address. */
|
|
61
|
+
peer?: string;
|
|
62
|
+
vrrp: NonNullable<Routed<string>["vrrp"]>;
|
|
63
|
+
};
|
|
64
|
+
export declare const deviceId: (name: string) => string;
|
|
65
|
+
/** An address as a key, so two spellings of one address compare equal. Undefined when it is not an address. */
|
|
66
|
+
export declare function keyOf(address: string | undefined): string | undefined;
|
|
67
|
+
/** Ports, then interfaces, that can hold an address. */
|
|
68
|
+
export declare function routedOf(device: Device): [string, Routed<string>][];
|
|
69
|
+
/** Every valid address the devices hold. An invalid one is left for `validate` to report. */
|
|
70
|
+
export declare function holdersOf(devices: readonly Device[]): Holder[];
|
|
71
|
+
/** The network as a graph. It reads the config only, never a device, and works on a config that does not validate. */
|
|
72
|
+
export declare function graphOf(devices: readonly Device[]): Graph;
|
|
73
|
+
/** Only the edges on these layers, and among those only the ones that touch these devices when any are named. Every device named stays. */
|
|
74
|
+
export declare function only(graph: Graph, layers: readonly Layer[], deviceNames?: readonly string[]): Graph;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** The schema files in reading order, with the title each gets. */
|
|
2
|
+
export declare const SCHEMA_FILES: readonly [readonly ["device.ts", "Device"], readonly ["interfaces.ts", "Ports and interfaces"], readonly ["routing.ts", "Routing"], readonly ["policy.ts", "Route policies"], readonly ["firewall.ts", "Firewall and ACLs"], readonly ["system.ts", "System and management"], readonly ["common.ts", "Common values"]];
|
|
3
|
+
export type Field = {
|
|
4
|
+
path: string;
|
|
5
|
+
type: string;
|
|
6
|
+
description: string;
|
|
7
|
+
};
|
|
8
|
+
export type TypeDoc = {
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
definition?: string;
|
|
12
|
+
includes?: string[];
|
|
13
|
+
fields: Field[];
|
|
14
|
+
};
|
|
15
|
+
/** Every type in each schema file, in the order given. Every file is read first, so a type is known to take parameters wherever it is used. */
|
|
16
|
+
export declare function schemaTypes(sources: readonly string[]): TypeDoc[][];
|
package/package.json
CHANGED
|
@@ -25,3 +25,35 @@ export function ipv4Networks(device: Device, iface: string): string[] {
|
|
|
25
25
|
|
|
26
26
|
return addresses.filter((address) => familyOf(address) === "ipv4").map((address) => network(address));
|
|
27
27
|
}
|
|
28
|
+
|
|
29
|
+
/** Services the switch platforms run. Each takes its sources from `management.allow`, on the protocol's own port. */
|
|
30
|
+
const SWITCH_SERVICES = ["ssh", "telnet", "snmp"] as const;
|
|
31
|
+
|
|
32
|
+
/** Fields neither switch platform, VRP nor Raisecom ROS, expresses: a service's own sources or port, a user's description or keys, the IP stack's host settings, a point-to-point address, and offload per interface. */
|
|
33
|
+
export function switchUnsupported(device: Device): string[] {
|
|
34
|
+
const found: string[] = [];
|
|
35
|
+
|
|
36
|
+
for (const service of SWITCH_SERVICES) {
|
|
37
|
+
const settings = device.management?.[service];
|
|
38
|
+
if (settings?.allow) found.push(`management.${service}.allow, every service takes management.allow`);
|
|
39
|
+
if (settings?.port) found.push(`management.${service}.port`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (const [name, user] of Object.entries(device.users ?? {})) {
|
|
43
|
+
if (user.description) found.push(`users.${name}.description`);
|
|
44
|
+
if (user.ssh_keys?.length) found.push(`users.${name}.ssh_keys`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (device.system?.ip) found.push("system.ip");
|
|
48
|
+
|
|
49
|
+
for (const [name, port] of Object.entries(device.ports ?? {})) {
|
|
50
|
+
if (port?.hardware_offload !== undefined) found.push(`ports.${name}.hardware_offload`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
for (const [name, iface] of Object.entries(device.interfaces ?? {})) {
|
|
54
|
+
if (iface.addresses?.some((entry) => typeof entry !== "string")) found.push(`interfaces.${name}.addresses with a peer`);
|
|
55
|
+
if (iface.hardware_offload !== undefined) found.push(`interfaces.${name}.hardware_offload`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return found;
|
|
59
|
+
}
|
|
@@ -9,7 +9,7 @@ import { offset } from "../../../core/time";
|
|
|
9
9
|
import type { Device, Port, SnmpUser, Switched, Vrrp } from "../../../schema";
|
|
10
10
|
import { connect, readUntil, send, shell } from "../../../transport/ssh";
|
|
11
11
|
import { portsOf, spell } from "../catalog";
|
|
12
|
-
import { ipv4Networks, lagIds } from "../derive";
|
|
12
|
+
import { ipv4Networks, lagIds, switchUnsupported } from "../derive";
|
|
13
13
|
import { compare, type Dialect, entries, type Group, scrub, steps, vlanListChange } from "../lines";
|
|
14
14
|
import type { DeviceAdapter, Plan, Step } from "../types";
|
|
15
15
|
|
|
@@ -387,7 +387,14 @@ export function unsupported(device: Device): string[] {
|
|
|
387
387
|
continue;
|
|
388
388
|
}
|
|
389
389
|
|
|
390
|
-
|
|
390
|
+
for (const field of ["vrf", "description", "mtu", "ipv6_ra"] as const) {
|
|
391
|
+
if (iface[field] !== undefined) found.push(`interfaces.${name}.${field}`);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
for (const entry of iface.addresses ?? []) {
|
|
395
|
+
const address = typeof entry === "string" ? entry : entry.address;
|
|
396
|
+
if (familyOf(address) === "ipv6") found.push(`interfaces.${name}.addresses ${address}, only IPv4 here`);
|
|
397
|
+
}
|
|
391
398
|
|
|
392
399
|
for (const group of iface.vrrp ?? []) {
|
|
393
400
|
if (familyOf(group.address) === "ipv6") found.push(`interfaces.${name}.vrrp for IPv6`);
|
|
@@ -412,7 +419,15 @@ export function unsupported(device: Device): string[] {
|
|
|
412
419
|
}
|
|
413
420
|
|
|
414
421
|
for (const route of routing?.static ?? []) {
|
|
415
|
-
if (!route.via || route.vrf
|
|
422
|
+
if (!route.via || route.vrf || familyOf(route.prefix) === "ipv6") {
|
|
423
|
+
found.push(`routing.static ${route.prefix}, only an IPv4 next hop in the default table`);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (route.description || route.distance) found.push(`routing.static ${route.prefix} description and distance`);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
for (const [name, vlan] of Object.entries(device.vlans ?? {})) {
|
|
430
|
+
if (vlan.description) found.push(`vlans.${name}.description`);
|
|
416
431
|
}
|
|
417
432
|
|
|
418
433
|
const UNSUPPORTED_SECTIONS = [
|
|
@@ -443,6 +458,9 @@ export function unsupported(device: Device): string[] {
|
|
|
443
458
|
found.push(`management.snmp.users.${name}.auth ${user.auth}, the platform authenticates with SHA1 or MD5`);
|
|
444
459
|
}
|
|
445
460
|
|
|
461
|
+
if (management.snmp?.contact || management.snmp?.location) found.push("management.snmp.contact and location");
|
|
462
|
+
if (management.ssh?.weak_crypto) found.push("management.ssh.weak_crypto");
|
|
463
|
+
|
|
446
464
|
if (device.lldp && device.lldp !== true) found.push("lldp.interfaces, LLDP is per device");
|
|
447
465
|
if (device.stp?.mode && device.stp.mode !== "mstp") found.push("stp.mode other than mstp");
|
|
448
466
|
|
|
@@ -456,6 +474,8 @@ export function unsupported(device: Device): string[] {
|
|
|
456
474
|
found.push("system.dns, logging, release_channel, banner and ntp.serve");
|
|
457
475
|
}
|
|
458
476
|
|
|
477
|
+
found.push(...switchUnsupported(device));
|
|
478
|
+
|
|
459
479
|
return found;
|
|
460
480
|
}
|
|
461
481
|
|
|
@@ -99,6 +99,14 @@ export function unsupported(device: Device): string[] {
|
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
for (const [name, iface] of Object.entries(device.interfaces ?? {})) {
|
|
103
|
+
if (iface.type !== "lag") continue;
|
|
104
|
+
|
|
105
|
+
for (const field of ["storm_control", "stp"] as const) {
|
|
106
|
+
if (iface[field] !== undefined) found.push(`interfaces.${name}.${field}, on a LAG`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
102
110
|
const bgp = device.routing?.bgp;
|
|
103
111
|
|
|
104
112
|
for (const [name, neighbor] of Object.entries(bgp?.neighbors ?? {})) {
|
|
@@ -113,6 +121,8 @@ export function unsupported(device: Device): string[] {
|
|
|
113
121
|
if (management?.console) found.push("management.console, the console uses the device's users");
|
|
114
122
|
if (management?.privilege_password) found.push("management.privilege_password, the platform has no privileged mode");
|
|
115
123
|
|
|
124
|
+
if (management?.snmp?.port) found.push("management.snmp.port");
|
|
125
|
+
|
|
116
126
|
if (management?.ssh?.auth_timeout || management?.ssh?.auth_retries) {
|
|
117
127
|
found.push("management.ssh.auth_timeout and auth_retries");
|
|
118
128
|
}
|
|
@@ -9,7 +9,7 @@ import { offset } from "../../../core/time";
|
|
|
9
9
|
import type { Device, Interface, Port, Vrrp } from "../../../schema";
|
|
10
10
|
import { connect, readUntil, send, shell } from "../../../transport/ssh";
|
|
11
11
|
import { portsOf, spell } from "../catalog";
|
|
12
|
-
import { ipv4Networks, lagIds } from "../derive";
|
|
12
|
+
import { ipv4Networks, lagIds, switchUnsupported } from "../derive";
|
|
13
13
|
import { compare, type Dialect, scrub, steps, vlanListChange } from "../lines";
|
|
14
14
|
import type { DeviceAdapter, Plan } from "../types";
|
|
15
15
|
|
|
@@ -608,9 +608,14 @@ export function unsupported(device: Device): string[] {
|
|
|
608
608
|
for (const [name, iface] of Object.entries(device.interfaces ?? {})) {
|
|
609
609
|
if (iface.type !== "vlan" && iface.type !== "lag") found.push(`interfaces.${name}, a ${iface.type}`);
|
|
610
610
|
if (iface.type === "lag" && iface.addresses?.length) found.push(`interfaces.${name}.addresses, LAGs are switched`);
|
|
611
|
+
if (iface.mtu !== undefined) found.push(`interfaces.${name}.mtu`);
|
|
611
612
|
for (const group of iface.vrrp ?? []) if (familyOf(group.address) === "ipv6") found.push(`interfaces.${name}.vrrp for IPv6`);
|
|
612
613
|
}
|
|
613
614
|
|
|
615
|
+
for (const [name, vrf] of Object.entries(device.vrfs ?? {})) {
|
|
616
|
+
if (vrf.description) found.push(`vrfs.${name}.description`);
|
|
617
|
+
}
|
|
618
|
+
|
|
614
619
|
const routing = device.routing;
|
|
615
620
|
if (routing?.bgp) found.push("routing.bgp");
|
|
616
621
|
if (routing?.rpki) found.push("routing.rpki");
|
|
@@ -644,6 +649,10 @@ export function unsupported(device: Device): string[] {
|
|
|
644
649
|
found.push("system.dns, logging, release_channel and banner");
|
|
645
650
|
}
|
|
646
651
|
|
|
652
|
+
if (system.transceiver_monitoring !== undefined) found.push("system.transceiver_monitoring");
|
|
653
|
+
|
|
654
|
+
found.push(...switchUnsupported(device));
|
|
655
|
+
|
|
647
656
|
const flow = device.flow_export;
|
|
648
657
|
if (flow && flow.protocol !== "sflow") found.push(`flow_export.protocol ${flow.protocol}, the platform exports sFlow`);
|
|
649
658
|
const sampling = flow?.protocol === "sflow" ? flow.sampling : undefined;
|
package/src/cli/graph.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// What `circuit graph` prints: the graph of the network with what `validate` found and how each device compares with its last snapshot, as Mermaid, DOT or JSON.
|
|
2
|
+
|
|
3
|
+
import { type Annotated, deviceId, type GraphNode } from "../core/graph";
|
|
4
|
+
|
|
5
|
+
/** `error` when validate found an error on the device, `warning` for a warning or a difference from its snapshot. */
|
|
6
|
+
export function markOf(graph: Annotated, node: GraphNode): "error" | "warning" | undefined {
|
|
7
|
+
if (node.kind !== "device") return undefined;
|
|
8
|
+
|
|
9
|
+
const findings = graph.findings.filter((finding) => finding.device && deviceId(finding.device) === node.id);
|
|
10
|
+
if (findings.some((finding) => finding.level === "error")) return "error";
|
|
11
|
+
|
|
12
|
+
const drift = graph.drift[node.label]?.status;
|
|
13
|
+
if (findings.length || drift === "differs" || drift === "cannot plan") return "warning";
|
|
14
|
+
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Mermaid, which GitHub draws wherever Markdown is shown. */
|
|
19
|
+
export function mermaid(graph: Annotated): string {
|
|
20
|
+
const ids = new Map(graph.nodes.map((node, index) => [node.id, `n${index}`]));
|
|
21
|
+
// Lines of text, each escaped, as one quoted label.
|
|
22
|
+
const text = (...lines: string[]) =>
|
|
23
|
+
`"${lines.map((line) => line.replace(/"/g, "#quot;").replace(/</g, "#lt;").replace(/>/g, "#gt;")).join("<br/>")}"`;
|
|
24
|
+
|
|
25
|
+
const shapes: Record<GraphNode["kind"], (label: string) => string> = {
|
|
26
|
+
device: (label) => `[${label}]`,
|
|
27
|
+
segment: (label) => `([${label}])`,
|
|
28
|
+
outside: (label) => `{{${label}}}`,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const lines = ["graph LR"];
|
|
32
|
+
|
|
33
|
+
for (const node of graph.nodes) {
|
|
34
|
+
const label = node.kind === "device" ? text(node.label, node.detail[0] ?? "") : text(node.label);
|
|
35
|
+
lines.push(` ${ids.get(node.id)}${shapes[node.kind](label)}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
for (const edge of graph.edges) {
|
|
39
|
+
const arrow = edge.oneSided ? "-.-" : "---";
|
|
40
|
+
lines.push(` ${ids.get(edge.from)} ${arrow}|${text(edge.label)}| ${ids.get(edge.to)}`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
lines.push(
|
|
44
|
+
" classDef error stroke:#d1242f,stroke-width:3px",
|
|
45
|
+
" classDef warning stroke:#bf8700,stroke-width:3px",
|
|
46
|
+
" classDef outside stroke-dasharray:4 3"
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
for (const node of graph.nodes) {
|
|
50
|
+
const mark = markOf(graph, node) ?? (node.kind === "outside" ? "outside" : undefined);
|
|
51
|
+
if (mark) lines.push(` class ${ids.get(node.id)} ${mark}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return lines.join("\n") + "\n";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** DOT, which Graphviz turns into an image: `circuit graph --format=dot | dot -Tsvg > network.svg`. */
|
|
58
|
+
export function dot(graph: Annotated): string {
|
|
59
|
+
const text = (value: string) => `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
60
|
+
const colors = { error: "#d1242f", warning: "#bf8700" };
|
|
61
|
+
|
|
62
|
+
const shapes: Record<GraphNode["kind"], string> = {
|
|
63
|
+
device: "shape=box, style=rounded",
|
|
64
|
+
segment: "shape=ellipse",
|
|
65
|
+
outside: "shape=box, style=dashed",
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const lines = [
|
|
69
|
+
"graph network {",
|
|
70
|
+
" rankdir=LR;",
|
|
71
|
+
' node [fontname="Helvetica"];',
|
|
72
|
+
' edge [fontname="Helvetica", fontsize=10];',
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
for (const node of graph.nodes) {
|
|
76
|
+
const label = node.kind === "device" ? `${node.label}\n${node.detail[0] ?? ""}` : node.label;
|
|
77
|
+
const mark = markOf(graph, node);
|
|
78
|
+
const color = mark ? `, color=${text(colors[mark])}, penwidth=2` : "";
|
|
79
|
+
lines.push(` ${text(node.id)} [label=${text(label)}, ${shapes[node.kind]}${color}];`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
for (const edge of graph.edges) {
|
|
83
|
+
const style = edge.oneSided ? ", style=dashed" : "";
|
|
84
|
+
lines.push(` ${text(edge.from)} -- ${text(edge.to)} [label=${text(edge.label)}${style}];`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
lines.push("}");
|
|
88
|
+
return lines.join("\n") + "\n";
|
|
89
|
+
}
|