nerdgram1337 1.6.1 → 1.8.5

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.
@@ -0,0 +1,959 @@
1
+ import {
2
+ CHANGE_ME,
3
+ HEADER_LINES,
4
+ assertExportShape,
5
+ cidr,
6
+ commentBlock,
7
+ exporterFail,
8
+ fileName,
9
+ lookupMapping,
10
+ mappingForTarget,
11
+ pushWarning,
12
+ renderYaml,
13
+ resolveLinks,
14
+ safeGroupName,
15
+ safeLabel,
16
+ sortWarnings,
17
+ stableUuid,
18
+ zipStore
19
+ } from "./chunk-ARL7TA3D.js";
20
+ import {
21
+ __export
22
+ } from "./chunk-MLKGABMK.js";
23
+
24
+ // server/src/exporters/run.ts
25
+ import path2 from "node:path";
26
+
27
+ // server/src/exporters/registry.ts
28
+ import { readdir } from "node:fs/promises";
29
+ import path from "node:path";
30
+ import { fileURLToPath } from "node:url";
31
+
32
+ // server/src/exporters/ansible.ts
33
+ var ansible_exports = {};
34
+ __export(ansible_exports, {
35
+ default: () => ansible_default
36
+ });
37
+ var OS_ROWS = [
38
+ { platform: "Cisco IOS", values: { ansible_network_os: "cisco.ios.ios" } },
39
+ { platform: "Cisco IOS-XE", values: { ansible_network_os: "cisco.ios.ios" } },
40
+ { platform: "Cisco NX-OS", values: { ansible_network_os: "cisco.nxos.nxos" } },
41
+ { platform: "Cisco IOS-XR", values: { ansible_network_os: "cisco.iosxr.iosxr" } },
42
+ { platform: "Arista EOS", values: { ansible_network_os: "arista.eos.eos" } },
43
+ { platform: "Juniper Junos", values: { ansible_network_os: "junipernetworks.junos.junos" } }
44
+ ];
45
+ function exportAnsible(document, context) {
46
+ const warnings = [];
47
+ const hosts = {};
48
+ const groups = /* @__PURE__ */ new Map();
49
+ const remember = (group, host) => {
50
+ const set = groups.get(group) ?? /* @__PURE__ */ new Set();
51
+ set.add(host);
52
+ groups.set(group, set);
53
+ };
54
+ for (const [index, device] of document.devices.entries()) {
55
+ const vars = {};
56
+ if (device.managementIp) vars.ansible_host = device.managementIp;
57
+ else pushWarning(warnings, `/devices/${index}/managementIp`, `${device.name} has no managementIp. ansible_host was omitted.`);
58
+ const mapped = lookupMapping(OS_ROWS, device, context.mapping);
59
+ const networkOs = mapped?.ansible_network_os;
60
+ if (networkOs && networkOs !== CHANGE_ME) {
61
+ vars.ansible_connection = mapped?.ansible_connection || "ansible.netcommon.network_cli";
62
+ vars.ansible_network_os = networkOs;
63
+ } else {
64
+ pushWarning(
65
+ warnings,
66
+ `/devices/${index}/platform`,
67
+ device.platform ? `${device.name} platform "${device.platform}" has no ansible_network_os mapping. It was omitted.` : `${device.name} has no platform. ansible_network_os was omitted.`
68
+ );
69
+ }
70
+ if (device.role) vars.nerdgram_role = device.role;
71
+ if (device.site) vars.nerdgram_site = device.site;
72
+ if (device.model) vars.nerdgram_model = device.model;
73
+ if (device.platform) vars.nerdgram_platform = device.platform;
74
+ hosts[device.name] = vars;
75
+ if (device.site) {
76
+ const group = safeGroupName(device.site);
77
+ if (group !== device.site.trim().toLowerCase()) {
78
+ pushWarning(warnings, `/devices/${index}/site`, `Site "${device.site}" was sanitized to Ansible group "${group}".`);
79
+ }
80
+ remember(group, device.name);
81
+ }
82
+ if (device.role) {
83
+ const group = safeGroupName(device.role);
84
+ if (group !== device.role.trim().toLowerCase()) {
85
+ pushWarning(warnings, `/devices/${index}/role`, `Role "${device.role}" was sanitized to Ansible group "${group}".`);
86
+ }
87
+ remember(group, device.name);
88
+ }
89
+ }
90
+ const children = {};
91
+ for (const [group, members] of [...groups.entries()].sort((a, b) => a[0] < b[0] ? -1 : 1)) {
92
+ const hostsInGroup = {};
93
+ for (const host of [...members].sort()) hostsInGroup[host] = {};
94
+ children[group] = { hosts: hostsInGroup };
95
+ }
96
+ const all = {
97
+ vars: {
98
+ ansible_user: CHANGE_ME,
99
+ ansible_password: CHANGE_ME,
100
+ ansible_become_password: CHANGE_ME
101
+ },
102
+ hosts,
103
+ ...Object.keys(children).length > 0 ? { children } : {}
104
+ };
105
+ const lines = [
106
+ ...HEADER_LINES,
107
+ "This inventory is hosts, groups, and addresses. It is not a topology.",
108
+ "Use Ansible Vault or --ask-pass / --ask-become-pass. Do not commit a real password.",
109
+ "A diagram cannot supply become secrets, jump hosts, or a verified network OS when the platform is unmapped.",
110
+ "Group names are letters, digits, and underscores, with no leading digit."
111
+ ];
112
+ const text = `${commentBlock("hash", lines)}${renderYaml({ all })}`;
113
+ return { files: [{ name: fileName(safeLabel(context.label), "yml"), text }], warnings };
114
+ }
115
+ var exporter = {
116
+ id: "ansible",
117
+ name: "Ansible inventory",
118
+ extension: "yml",
119
+ mimeType: "application/yaml",
120
+ description: "A static Ansible YAML inventory. Passwords are placeholders. NerdGram does not connect.",
121
+ export: exportAnsible
122
+ };
123
+ var ansible_default = exporter;
124
+
125
+ // server/src/exporters/cml.ts
126
+ var cml_exports = {};
127
+ __export(cml_exports, {
128
+ default: () => cml_default
129
+ });
130
+ var NODE_ROWS = [];
131
+ function isLoopback(name) {
132
+ return /^loopback/i.test(name);
133
+ }
134
+ function exportCml(document, context) {
135
+ const warnings = [];
136
+ const links = resolveLinks(document, warnings);
137
+ const title = safeLabel(context.label);
138
+ const nodeIds = /* @__PURE__ */ new Map();
139
+ const ifaceIds = /* @__PURE__ */ new Map();
140
+ const nodes = document.devices.map((device, index) => {
141
+ const id = `n${index}`;
142
+ nodeIds.set(device.name, id);
143
+ const mapped = lookupMapping(NODE_ROWS, device, context.mapping);
144
+ const nodeDefinition = mapped?.node_definition || CHANGE_ME;
145
+ if (!mapped?.node_definition) {
146
+ pushWarning(
147
+ warnings,
148
+ `/devices/${index}/platform`,
149
+ `${device.name} node_definition is ${CHANGE_ME}. IOS-XE ids such as cat8000v, csr1000v, and iosvl2 were not confirmed against a current CML export.`
150
+ );
151
+ }
152
+ if (!device.managementIp) {
153
+ pushWarning(warnings, `/devices/${index}/managementIp`, `${device.name} has no managementIp. The node is still exported.`);
154
+ }
155
+ const owned = document.interfaces.filter((item) => item.device === device.name);
156
+ let physical = 0;
157
+ const interfaces = owned.map((iface, ifaceIndex) => {
158
+ const ifaceId = `i${ifaceIndex}`;
159
+ ifaceIds.set(`${device.name}\0${iface.name}`, ifaceId);
160
+ const loopback = isLoopback(iface.name);
161
+ const entry = {
162
+ id: ifaceId,
163
+ label: iface.name,
164
+ type: loopback ? "loopback" : "physical"
165
+ };
166
+ if (!loopback) {
167
+ entry.slot = physical;
168
+ physical += 1;
169
+ }
170
+ return entry;
171
+ });
172
+ return {
173
+ id,
174
+ label: device.name,
175
+ node_definition: nodeDefinition,
176
+ x: index % 4 * 200,
177
+ y: Math.floor(index / 4) * 160,
178
+ interfaces
179
+ };
180
+ });
181
+ const cmlLinks = [];
182
+ links.forEach((link, index) => {
183
+ if (link.unresolved) {
184
+ pushWarning(warnings, `/links/${index}`, `Skipped link ${link.aDevice}:${link.aInterface} to ${link.bDevice}:${link.bInterface} because an end did not resolve.`);
185
+ return;
186
+ }
187
+ const n1 = nodeIds.get(link.aDevice);
188
+ const n2 = nodeIds.get(link.bDevice);
189
+ const i1 = ifaceIds.get(`${link.aDevice}\0${link.aInterface}`);
190
+ const i2 = ifaceIds.get(`${link.bDevice}\0${link.bInterface}`);
191
+ if (!n1 || !n2 || !i1 || !i2) {
192
+ pushWarning(warnings, `/links/${index}`, `Skipped a link whose device is not in the document.`);
193
+ return;
194
+ }
195
+ cmlLinks.push({
196
+ id: `l${cmlLinks.length}`,
197
+ n1,
198
+ i1,
199
+ n2,
200
+ i2,
201
+ label: link.label || `${link.aDevice}-${link.aInterface}<->${link.bDevice}-${link.bInterface}`
202
+ });
203
+ });
204
+ const inferred = links.filter((link) => link.inferred).map((link) => `${link.aDevice}:${link.aInterface}<->${link.bDevice}:${link.bInterface}`);
205
+ const lines = [
206
+ ...HEADER_LINES,
207
+ "NerdGram does not log in to CML. Import this YAML and attach images that already exist on the CML server.",
208
+ "Coordinates are a generated grid. Rearrange the nodes in the Workbench.",
209
+ "Loopback interfaces have no slot. Physical interfaces use a 0-based slot, matching the CiscoDevNet community lab shape.",
210
+ "node_definition stays CHANGE_ME unless a mapping row sets it.",
211
+ ...inferred.length > 0 ? [`Inferred links, not proven cables: ${inferred.join("; ")}.`] : []
212
+ ];
213
+ const lab = {
214
+ title,
215
+ version: "0.1.0",
216
+ description: "Generated from NerdGram network data. Placeholders only. No device connection.",
217
+ notes: "Import this file in CML and attach node images that are already installed."
218
+ };
219
+ const text = `${commentBlock("hash", lines)}${renderYaml({ lab, nodes, links: cmlLinks })}`;
220
+ return { files: [{ name: fileName(title, "yaml"), text }], warnings };
221
+ }
222
+ var exporter2 = {
223
+ id: "cml",
224
+ name: "CML lab",
225
+ extension: "yaml",
226
+ mimeType: "application/yaml",
227
+ description: "A CML 2.x lab YAML. Node definitions are placeholders unless you map them. NerdGram does not log in to CML.",
228
+ export: exportCml
229
+ };
230
+ var cml_default = exporter2;
231
+
232
+ // server/src/exporters/containerlab.ts
233
+ var containerlab_exports = {};
234
+ __export(containerlab_exports, {
235
+ default: () => containerlab_default
236
+ });
237
+ var KIND_ROWS = [
238
+ { platform: "Cisco IOS-XE", role: "access", values: { kind: "cisco_iol" } },
239
+ { platform: "Cisco IOS-XE", role: "distribution", values: { kind: "cisco_iol" } },
240
+ { platform: "Cisco IOS-XE", role: "core", values: { kind: "cisco_csr1000v" } },
241
+ { platform: "Cisco IOS-XE", role: "wan", values: { kind: "cisco_csr1000v" } },
242
+ { platform: "Arista EOS", values: { kind: "arista_ceos" } }
243
+ ];
244
+ function isIpv4(value) {
245
+ return /^\d{1,3}(\.\d{1,3}){3}$/.test(value);
246
+ }
247
+ function exportContainerlab(document, context) {
248
+ const warnings = [];
249
+ const links = resolveLinks(document, warnings);
250
+ const name = safeLabel(context.label);
251
+ const nodes = {};
252
+ for (const [index, device] of document.devices.entries()) {
253
+ const mapped = lookupMapping(KIND_ROWS, device, context.mapping);
254
+ const kind = mapped?.kind || CHANGE_ME;
255
+ const image = mapped?.image || CHANGE_ME;
256
+ if (!mapped?.kind) {
257
+ pushWarning(
258
+ warnings,
259
+ `/devices/${index}/platform`,
260
+ device.platform ? `${device.name} platform "${device.platform}"${device.role ? ` role "${device.role}"` : ""} has no containerlab kind. kind is ${CHANGE_ME}.` : `${device.name} has no platform. kind is ${CHANGE_ME}.`
261
+ );
262
+ }
263
+ if (image === CHANGE_ME) {
264
+ pushWarning(warnings, `/devices/${index}`, `${device.name} image is ${CHANGE_ME}. The schema does not name a default tag.`);
265
+ }
266
+ const node = { kind, image };
267
+ if (device.managementIp && isIpv4(device.managementIp)) node["mgmt-ipv4"] = device.managementIp;
268
+ else if (device.managementIp?.includes(":")) {
269
+ node["mgmt-ipv6"] = device.managementIp;
270
+ pushWarning(warnings, `/devices/${index}/managementIp`, `${device.name} management address is IPv6. It was written as mgmt-ipv6, not rewritten into an IPv4 subnet.`);
271
+ } else if (!device.managementIp) {
272
+ pushWarning(warnings, `/devices/${index}/managementIp`, `${device.name} has no managementIp. mgmt-ipv4 was omitted.`);
273
+ } else {
274
+ pushWarning(warnings, `/devices/${index}/managementIp`, `${device.name} managementIp is not an IPv4 address. mgmt-ipv4 was omitted.`);
275
+ }
276
+ const group = device.site || device.role;
277
+ if (group) node.group = group;
278
+ nodes[device.name] = node;
279
+ }
280
+ const linkItems = links.map((link) => ({
281
+ endpoints: [`${link.aDevice}:${link.aInterface}`, `${link.bDevice}:${link.bInterface}`]
282
+ }));
283
+ const inferred = links.filter((link) => link.inferred).map((link) => `${link.aDevice}:${link.aInterface} -- ${link.bDevice}:${link.bInterface}`);
284
+ const lines = [
285
+ ...HEADER_LINES,
286
+ "Management addresses are copied as written. They must sit in the lab management subnet, or set mgmt.ipv4-subnet yourself.",
287
+ "group is device.site when that is set, otherwise device.role. containerlab also uses group for graph layout.",
288
+ "Kind values come from the current containerlab schema enum. Images are placeholders.",
289
+ ...inferred.length > 0 ? [`Inferred links, not proven cables: ${inferred.join("; ")}.`] : []
290
+ ];
291
+ const text = `${commentBlock("hash", lines)}${renderYaml({ name, topology: { nodes, links: linkItems } })}`;
292
+ return { files: [{ name: fileName(name, "clab.yml"), text }], warnings };
293
+ }
294
+ var exporter3 = {
295
+ id: "containerlab",
296
+ name: "containerlab topology",
297
+ extension: "clab.yml",
298
+ mimeType: "application/yaml",
299
+ description: "A containerlab topology YAML. Images are placeholders. NerdGram does not deploy the lab.",
300
+ export: exportContainerlab
301
+ };
302
+ var containerlab_default = exporter3;
303
+
304
+ // server/src/exporters/eve-ng.ts
305
+ var eve_ng_exports = {};
306
+ __export(eve_ng_exports, {
307
+ default: () => eve_ng_default
308
+ });
309
+ var KNOWN_TYPES = /* @__PURE__ */ new Set(["iol", "dynamips", "qemu"]);
310
+ function xmlEscape(value) {
311
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
312
+ }
313
+ function exportEve(document, context) {
314
+ const warnings = [];
315
+ const links = resolveLinks(document, warnings);
316
+ const label = safeLabel(context.label);
317
+ const networkFor = /* @__PURE__ */ new Map();
318
+ let nextNetwork = 1;
319
+ for (const link of links) {
320
+ if (link.unresolved) continue;
321
+ networkFor.set(link.index, nextNetwork);
322
+ nextNetwork += 1;
323
+ }
324
+ const nodeXml = [];
325
+ document.devices.forEach((device, index) => {
326
+ const id = index + 1;
327
+ const requested = context.mapping?.find(
328
+ (row) => (!row.platform || row.platform.toLowerCase() === device.platform.toLowerCase()) && (!row.role || row.role.toLowerCase() === device.role.toLowerCase())
329
+ );
330
+ let type = requested?.type && KNOWN_TYPES.has(requested.type) ? requested.type : "qemu";
331
+ if (requested?.type && !KNOWN_TYPES.has(requested.type)) {
332
+ pushWarning(warnings, `/devices/${index}`, `${device.name} mapping type "${requested.type}" is not in the published API list (iol, dynamips, qemu). qemu was used.`);
333
+ }
334
+ const template = requested?.template || CHANGE_ME;
335
+ const image = requested?.image || CHANGE_ME;
336
+ pushWarning(
337
+ warnings,
338
+ `/devices/${index}/platform`,
339
+ `${device.name} uses type ${type}, template ${template}, image ${image}. Image names are local to your EVE-NG. They were not guessed.`
340
+ );
341
+ const owned = document.interfaces.filter((item) => item.device === device.name);
342
+ const ethernetCount = Math.max(1, owned.filter((item) => !/^loopback/i.test(item.name)).length);
343
+ const interfaces = owned.map((iface, ifaceIndex) => {
344
+ const attached = links.find(
345
+ (link) => !link.unresolved && (link.aDevice === device.name && link.aInterface === iface.name || link.bDevice === device.name && link.bInterface === iface.name)
346
+ );
347
+ const networkId = attached ? networkFor.get(attached.index) ?? 0 : 0;
348
+ return ` <interface id="${ifaceIndex}" name="${xmlEscape(iface.name)}" type="ethernet" network_id="${networkId}"/>`;
349
+ });
350
+ const left = `${10 + index % 4 * 20}%`;
351
+ const top = `${15 + Math.floor(index / 4) * 20}%`;
352
+ nodeXml.push(
353
+ [
354
+ ` <node id="${id}" name="${xmlEscape(device.name)}" type="${xmlEscape(type)}" template="${xmlEscape(template)}" image="${xmlEscape(image)}" ethernet="${ethernetCount}" ram="1024" config="Unconfigured" left="${left}" top="${top}">`,
355
+ ...interfaces,
356
+ ` </node>`
357
+ ].join("\n")
358
+ );
359
+ });
360
+ const networks = links.filter((link) => networkFor.has(link.index)).map((link) => {
361
+ const id = networkFor.get(link.index);
362
+ const name = `${link.aDevice}_${link.aInterface}__${link.bDevice}_${link.bInterface}`.replace(/[^\w.-]+/g, "_");
363
+ const mark = link.inferred ? `<!-- inferred, not a proven cable -->
364
+ ` : "";
365
+ return ` ${mark}<network id="${id}" type="bridge" name="${xmlEscape(name)}" visibility="0"/>`;
366
+ });
367
+ const inferredNote = links.some((link) => link.inferred) ? "Inferred links are marked with an XML comment and are not proven cables." : "This diagram has no inferred links.";
368
+ const header = commentBlock("xml", [
369
+ ...HEADER_LINES,
370
+ "Copy this .unl into EVE-NG and point templates at images you already have.",
371
+ "XML attribute names follow the EVE-NG API fields and were not checked against a .unl saved from a current install.",
372
+ "visibility on networks is unverified. Override templates and images.",
373
+ inferredNote
374
+ ]);
375
+ const text = [
376
+ `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
377
+ header.trimEnd(),
378
+ `<lab name="${xmlEscape(label)}" id="${stableUuid(`${label}:lab`)}" version="1" author="NerdGram" description="${xmlEscape("Generated from NerdGram network data. Placeholders only. No emulator connection.")}">`,
379
+ ` <topology>`,
380
+ ` <nodes>`,
381
+ ...nodeXml,
382
+ ` </nodes>`,
383
+ ` <networks>`,
384
+ ...networks,
385
+ ` </networks>`,
386
+ ` </topology>`,
387
+ `</lab>`,
388
+ ``
389
+ ].join("\n");
390
+ return { files: [{ name: fileName(label, "unl"), text }], warnings };
391
+ }
392
+ var exporter4 = {
393
+ id: "eve-ng",
394
+ name: "EVE-NG lab",
395
+ extension: "unl",
396
+ mimeType: "application/xml",
397
+ description: "An EVE-NG .unl lab. Templates and images are placeholders. NerdGram does not connect to EVE-NG.",
398
+ export: exportEve
399
+ };
400
+ var eve_ng_default = exporter4;
401
+
402
+ // server/src/exporters/gns3.ts
403
+ var gns3_exports = {};
404
+ __export(gns3_exports, {
405
+ default: () => gns3_default
406
+ });
407
+ var NODE_TYPES = /* @__PURE__ */ new Set([
408
+ "cloud",
409
+ "nat",
410
+ "ethernet_hub",
411
+ "ethernet_switch",
412
+ "frame_relay_switch",
413
+ "atm_switch",
414
+ "docker",
415
+ "dynamips",
416
+ "vpcs",
417
+ "traceng",
418
+ "virtualbox",
419
+ "vmware",
420
+ "iou",
421
+ "qemu"
422
+ ]);
423
+ function exportGns3(document, context) {
424
+ const warnings = [];
425
+ warnings.push({
426
+ path: "",
427
+ message: HEADER_LINES.join(" ") + " Attach a GNS3 appliance before starting the project. Port numbers are adapter 0 in interface order."
428
+ });
429
+ const links = resolveLinks(document, warnings);
430
+ const label = safeLabel(context.label);
431
+ const nodeIds = /* @__PURE__ */ new Map();
432
+ const portOf = /* @__PURE__ */ new Map();
433
+ const nodes = document.devices.map((device, index) => {
434
+ const nodeId = stableUuid(`${label}:${device.name}`);
435
+ nodeIds.set(device.name, nodeId);
436
+ const requested = context.mapping?.find(
437
+ (row) => (!row.platform || row.platform.toLowerCase() === device.platform.toLowerCase()) && (!row.role || row.role.toLowerCase() === device.role.toLowerCase())
438
+ );
439
+ let nodeType = "qemu";
440
+ if (requested?.node_type && NODE_TYPES.has(requested.node_type)) nodeType = requested.node_type;
441
+ else if (requested?.node_type) {
442
+ pushWarning(warnings, `/devices/${index}`, `${device.name} mapping node_type "${requested.node_type}" is not in the GNS3 2.2 enum. qemu was used.`);
443
+ }
444
+ pushWarning(
445
+ warnings,
446
+ `/devices/${index}/platform`,
447
+ `${device.name} node_type is ${nodeType}. NerdGram did not choose dynamips or iou. Attach the appliance that matches this node.`
448
+ );
449
+ const linked = /* @__PURE__ */ new Set();
450
+ for (const link of links) {
451
+ if (link.aDevice === device.name) linked.add(link.aInterface);
452
+ if (link.bDevice === device.name) linked.add(link.bInterface);
453
+ }
454
+ let port = 0;
455
+ for (const iface of document.interfaces.filter((item) => item.device === device.name)) {
456
+ if (!linked.has(iface.name)) continue;
457
+ portOf.set(`${device.name}\0${iface.name}`, port);
458
+ port += 1;
459
+ }
460
+ return {
461
+ compute_id: "local",
462
+ node_id: nodeId,
463
+ node_type: nodeType,
464
+ name: device.name,
465
+ x: index % 5 * 160,
466
+ y: Math.floor(index / 5) * 140,
467
+ properties: {}
468
+ };
469
+ });
470
+ const gns3Links = [];
471
+ for (const link of links) {
472
+ if (link.unresolved) {
473
+ pushWarning(warnings, `/links/${link.index}`, `Skipped a link whose interface did not resolve.`);
474
+ continue;
475
+ }
476
+ const left = nodeIds.get(link.aDevice);
477
+ const right = nodeIds.get(link.bDevice);
478
+ const leftPort = portOf.get(`${link.aDevice}\0${link.aInterface}`);
479
+ const rightPort = portOf.get(`${link.bDevice}\0${link.bInterface}`);
480
+ if (!left || !right || leftPort == null || rightPort == null) {
481
+ pushWarning(warnings, `/links/${link.index}`, `Skipped a link whose end is not on a device.`);
482
+ continue;
483
+ }
484
+ gns3Links.push({
485
+ link_id: stableUuid(`${label}:link:${link.index}`),
486
+ link_type: "ethernet",
487
+ nodes: [
488
+ { node_id: left, adapter_number: 0, port_number: leftPort },
489
+ { node_id: right, adapter_number: 0, port_number: rightPort }
490
+ ]
491
+ });
492
+ }
493
+ const project = {
494
+ project_id: stableUuid(`${label}:project`),
495
+ type: "topology",
496
+ revision: 9,
497
+ version: "2.2.0",
498
+ name: label,
499
+ topology: {
500
+ nodes,
501
+ links: gns3Links,
502
+ drawings: [],
503
+ computes: []
504
+ }
505
+ };
506
+ const text = `${JSON.stringify(project, null, 2)}
507
+ `;
508
+ return { files: [{ name: fileName(label, "gns3"), text }], warnings };
509
+ }
510
+ var exporter5 = {
511
+ id: "gns3",
512
+ name: "GNS3 project",
513
+ extension: "gns3",
514
+ mimeType: "application/json",
515
+ description: "A GNS3 2.2 .gns3 project (revision 9). Appliances are not included. NerdGram does not call a GNS3 server.",
516
+ export: exportGns3
517
+ };
518
+ var gns3_default = exporter5;
519
+
520
+ // server/src/exporters/nornir.ts
521
+ var nornir_exports = {};
522
+ __export(nornir_exports, {
523
+ default: () => nornir_default
524
+ });
525
+ var PLATFORM_ROWS = [
526
+ { platform: "Cisco IOS", values: { platform: "ios", netmiko: "cisco_ios" } },
527
+ { platform: "Cisco IOS-XE", values: { platform: "ios", netmiko: "cisco_xe" } },
528
+ { platform: "Cisco NX-OS", values: { platform: "nxos", netmiko: "cisco_nxos" } },
529
+ { platform: "Cisco IOS-XR", values: { platform: "iosxr", netmiko: "cisco_xr" } },
530
+ { platform: "Arista EOS", values: { platform: "eos", netmiko: "arista_eos" } },
531
+ { platform: "Juniper Junos", values: { platform: "junos", netmiko: "juniper_junos" } }
532
+ ];
533
+ function groupName(value) {
534
+ return value.trim().replace(/\s+/g, "_");
535
+ }
536
+ function exportNornir(document, context) {
537
+ const warnings = [];
538
+ const hosts = {};
539
+ const groups = /* @__PURE__ */ new Map();
540
+ for (const [index, device] of document.devices.entries()) {
541
+ const mapped = lookupMapping(PLATFORM_ROWS, device, context.mapping);
542
+ const data = {};
543
+ if (device.site) data.site = device.site;
544
+ if (device.role) data.role = device.role;
545
+ if (device.model) data.model = device.model;
546
+ if (device.platform) data.platform = device.platform;
547
+ const membership = [];
548
+ for (const raw of [device.site, device.role]) {
549
+ if (!raw) continue;
550
+ const name = groupName(raw);
551
+ if (name !== raw) {
552
+ pushWarning(warnings, `/devices/${index}`, `${device.name} group "${raw}" was written as "${name}".`);
553
+ }
554
+ groups.set(name, true);
555
+ membership.push(name);
556
+ }
557
+ const host = {};
558
+ if (device.managementIp) host.hostname = device.managementIp;
559
+ else pushWarning(warnings, `/devices/${index}/managementIp`, `${device.name} has no managementIp. hostname was omitted.`);
560
+ if (mapped?.platform && mapped.platform !== CHANGE_ME) {
561
+ host.platform = mapped.platform;
562
+ if (mapped.netmiko) {
563
+ host.connection_options = { netmiko: { platform: mapped.netmiko } };
564
+ }
565
+ pushWarning(
566
+ warnings,
567
+ `/devices/${index}/platform`,
568
+ `${device.name} platform "${mapped.platform}" is the NAPALM driver name. Netmiko connection_options.platform is "${mapped.netmiko ?? ""}". Confirm both, especially IOS-XE (NAPALM ios, Netmiko cisco_xe).`
569
+ );
570
+ } else {
571
+ pushWarning(
572
+ warnings,
573
+ `/devices/${index}/platform`,
574
+ device.platform ? `${device.name} platform "${device.platform}" has no Nornir driver mapping. platform was omitted.` : `${device.name} has no platform. platform was omitted.`
575
+ );
576
+ }
577
+ if (membership.length > 0) host.groups = membership;
578
+ if (Object.keys(data).length > 0) host.data = data;
579
+ hosts[device.name] = host;
580
+ }
581
+ const groupFile = {};
582
+ for (const name of [...groups.keys()].sort()) groupFile[name] = {};
583
+ const lines = [
584
+ ...HEADER_LINES,
585
+ "SimpleInventory reads hosts.yaml, groups.yaml, and defaults.yaml.",
586
+ "config.yaml points the plugin at those files. It is not required to load the inventory.",
587
+ "username and password are placeholders. Use your own secrets store."
588
+ ];
589
+ const header = commentBlock("hash", lines);
590
+ const files = [
591
+ { name: "hosts.yaml", text: `${header}${renderYaml(hosts)}` },
592
+ { name: "groups.yaml", text: `${header}${renderYaml(groupFile)}` },
593
+ {
594
+ name: "defaults.yaml",
595
+ text: `${header}${renderYaml({ username: CHANGE_ME, password: CHANGE_ME })}`
596
+ },
597
+ {
598
+ name: "config.yaml",
599
+ text: `${header}${renderYaml({
600
+ inventory: {
601
+ plugin: "SimpleInventory",
602
+ options: {
603
+ host_file: "hosts.yaml",
604
+ group_file: "groups.yaml",
605
+ defaults_file: "defaults.yaml"
606
+ }
607
+ }
608
+ })}`
609
+ }
610
+ ];
611
+ return { files, warnings };
612
+ }
613
+ var exporter6 = {
614
+ id: "nornir",
615
+ name: "Nornir inventory",
616
+ extension: "yaml",
617
+ mimeType: "application/yaml",
618
+ description: "A Nornir SimpleInventory (hosts, groups, defaults). Passwords are placeholders. NerdGram does not connect.",
619
+ export: exportNornir
620
+ };
621
+ var nornir_default = exporter6;
622
+
623
+ // server/src/exporters/pyats.ts
624
+ var pyats_exports = {};
625
+ __export(pyats_exports, {
626
+ default: () => pyats_default
627
+ });
628
+ var OS_ROWS2 = [
629
+ { platform: "Cisco IOS-XE", values: { os: "iosxe" } },
630
+ { platform: "Cisco IOS", values: { os: "ios" } },
631
+ { platform: "Cisco NX-OS", values: { os: "nxos" } },
632
+ { platform: "Cisco IOS-XR", values: { os: "iosxr" } },
633
+ { platform: "Arista EOS", values: { os: "eos" } },
634
+ { platform: "Juniper Junos", values: { os: "junos" } }
635
+ ];
636
+ var INTERFACE_TYPES = [
637
+ { prefix: "Loopback", type: "loopback" },
638
+ { prefix: "Vlan", type: "vlan" },
639
+ { prefix: "Port-channel", type: "port-channel" },
640
+ { prefix: "Po", type: "port-channel" }
641
+ ];
642
+ function interfaceType(name, context) {
643
+ const rows = context.mapping ?? [];
644
+ const custom = rows.filter((row) => row.interfacePrefix).sort((a, b) => (b.interfacePrefix ?? "").length - (a.interfacePrefix ?? "").length);
645
+ for (const row of custom) {
646
+ if (name.toLowerCase().startsWith((row.interfacePrefix ?? "").toLowerCase()) && row.type) return row.type;
647
+ }
648
+ for (const row of INTERFACE_TYPES) {
649
+ if (name.toLowerCase().startsWith(row.prefix.toLowerCase())) return row.type;
650
+ }
651
+ return "ethernet";
652
+ }
653
+ function hardwareId(model) {
654
+ if (!model.trim()) return "";
655
+ if (!/^[A-Za-z][A-Za-z0-9_-]{1,40}$/.test(model.trim())) return "";
656
+ if (/ios|nx-os|eos|junos/i.test(model)) return "";
657
+ return model.trim();
658
+ }
659
+ function exportPyats(document, context) {
660
+ const warnings = [];
661
+ const links = resolveLinks(document, warnings);
662
+ const name = safeLabel(context.label);
663
+ const devices = {};
664
+ const topology = {};
665
+ const linkName = /* @__PURE__ */ new Map();
666
+ links.forEach((link, index) => {
667
+ const raw = `${link.aDevice}_${link.aInterface}__${link.bDevice}_${link.bInterface}`.replace(/[^\w.-]+/g, "_");
668
+ linkName.set(index, raw);
669
+ });
670
+ for (const [index, device] of document.devices.entries()) {
671
+ const mapped = lookupMapping(OS_ROWS2, device, context.mapping?.filter((row) => !row.interfacePrefix) ?? null);
672
+ const os = mapped?.os;
673
+ if (!device.role) {
674
+ pushWarning(warnings, `/devices/${index}/role`, `${device.name} has no role. type is "device".`);
675
+ }
676
+ if (!os) {
677
+ pushWarning(
678
+ warnings,
679
+ `/devices/${index}/platform`,
680
+ device.platform ? `${device.name} platform "${device.platform}" has no pyATS os mapping. os was omitted.` : `${device.name} has no platform. os was omitted.`
681
+ );
682
+ }
683
+ if (!device.managementIp) {
684
+ pushWarning(warnings, `/devices/${index}/managementIp`, `${device.name} has no managementIp. The cli connection has no ip.`);
685
+ }
686
+ const platform = hardwareId(device.model);
687
+ const body = {
688
+ type: device.role || "device",
689
+ ...os ? { os } : {},
690
+ ...platform ? { platform } : {},
691
+ ...device.model ? { model: device.model } : {},
692
+ ...device.role ? { role: device.role } : {},
693
+ credentials: {
694
+ default: { username: CHANGE_ME, password: CHANGE_ME }
695
+ },
696
+ connections: {
697
+ cli: {
698
+ protocol: "ssh",
699
+ ...device.managementIp ? { ip: device.managementIp } : {}
700
+ }
701
+ }
702
+ };
703
+ devices[device.name] = body;
704
+ const owned = document.interfaces.filter((item) => item.device === device.name);
705
+ const interfaces = {};
706
+ for (const iface of owned) {
707
+ const entry = { type: interfaceType(iface.name, context) };
708
+ if (iface.ipv4[0]) entry.ipv4 = cidr(iface.ipv4[0].address, iface.ipv4[0].prefix);
709
+ if (iface.ipv4.length > 1) {
710
+ pushWarning(warnings, "", `${device.name} ${iface.name} has more than one IPv4 address. Only the first was written.`);
711
+ }
712
+ if (iface.ipv6.length === 1 && iface.ipv6[0]) entry.ipv6 = cidr(iface.ipv6[0].address, iface.ipv6[0].prefix);
713
+ else if (iface.ipv6.length > 1) entry.ipv6 = iface.ipv6.map((item) => cidr(item.address, item.prefix));
714
+ interfaces[iface.name] = entry;
715
+ }
716
+ for (const link of links) {
717
+ const onA = link.aDevice === device.name;
718
+ const onB = link.bDevice === device.name;
719
+ if (!onA && !onB) continue;
720
+ const ifaceName = onA ? link.aInterface : link.bInterface;
721
+ const current = interfaces[ifaceName];
722
+ const slot = current && typeof current === "object" ? current : { type: interfaceType(ifaceName, context) };
723
+ slot.link = linkName.get(link.index);
724
+ interfaces[ifaceName] = slot;
725
+ }
726
+ if (Object.keys(interfaces).length > 0) topology[device.name] = { interfaces };
727
+ }
728
+ const inferred = links.filter((link) => link.inferred).map((link) => linkName.get(link.index) ?? "");
729
+ const lines = [
730
+ ...HEADER_LINES,
731
+ "Passwords are placeholders. You are responsible for making names and IPs match the devices you will test.",
732
+ "The published pyATS schema still marks testbed.tacacs and testbed.passwords required. They are placeholders, not the schema default.",
733
+ "Interface type is a free string in that schema. vlan and port-channel are documented choices, not a published enum.",
734
+ ...inferred.length > 0 ? [`Inferred links, not proven cables: ${inferred.join(", ")}.`] : []
735
+ ];
736
+ const testbed = {
737
+ name,
738
+ tacacs: { username: CHANGE_ME },
739
+ passwords: { tacacs: CHANGE_ME, enable: CHANGE_ME, line: CHANGE_ME },
740
+ credentials: { default: { username: CHANGE_ME, password: CHANGE_ME } }
741
+ };
742
+ const text = `${commentBlock("hash", lines)}${renderYaml({ testbed, devices, topology })}`;
743
+ return { files: [{ name: fileName(name, "yml"), text }], warnings };
744
+ }
745
+ var exporter7 = {
746
+ id: "pyats",
747
+ name: "pyATS testbed",
748
+ extension: "yml",
749
+ mimeType: "application/yaml",
750
+ description: "A pyATS testbed YAML with placeholder credentials. NerdGram does not connect to devices.",
751
+ export: exportPyats
752
+ };
753
+ var pyats_default = exporter7;
754
+
755
+ // server/src/exporters/bundled.ts
756
+ function take(mod, file) {
757
+ const value = mod.default;
758
+ if (!value || typeof value !== "object" || !(typeof value.export === "function") || typeof value.id !== "string") {
759
+ throw new Error(file + " must default-export a module.");
760
+ }
761
+ return value;
762
+ }
763
+ var bundledExporters = [take(ansible_exports, "ansible.ts"), take(cml_exports, "cml.ts"), take(containerlab_exports, "containerlab.ts"), take(eve_ng_exports, "eve-ng.ts"), take(gns3_exports, "gns3.ts"), take(nornir_exports, "nornir.ts"), take(pyats_exports, "pyats.ts")];
764
+
765
+ // server/src/exporters/infra.ts
766
+ var EXPORTER_INFRA = [
767
+ "bundled",
768
+ "helpers",
769
+ "index",
770
+ "infra",
771
+ "limits",
772
+ "registry",
773
+ "request",
774
+ "resolve",
775
+ "run",
776
+ "types"
777
+ ];
778
+
779
+ // server/src/exporters/registry.ts
780
+ var INFRA = new Set(EXPORTER_INFRA);
781
+ var ID_RE = /^[a-z][a-z0-9-]*$/;
782
+ var here = path.dirname(fileURLToPath(import.meta.url));
783
+ function exporterBasenames(fileNames) {
784
+ const bases = /* @__PURE__ */ new Set();
785
+ for (const name of fileNames) {
786
+ const baseName = name.split(/[\\/]/).pop() ?? name;
787
+ if (!baseName || baseName.endsWith(".d.ts")) continue;
788
+ const match = /^(.*)\.(?:ts|js|mjs|cjs)$/.exec(baseName);
789
+ if (!match) continue;
790
+ const base = match[1] ?? "";
791
+ if (!base || base.endsWith(".test") || INFRA.has(base)) continue;
792
+ bases.add(base);
793
+ }
794
+ return [...bases].sort();
795
+ }
796
+ function assertExporterModule(value, label) {
797
+ if (!value || typeof value !== "object") throw new Error(`${label} must default-export an exporter.`);
798
+ const item = value;
799
+ if (typeof item.id !== "string" || !ID_RE.test(item.id)) {
800
+ throw new Error(`${label} needs an id of lowercase letters, numbers, and hyphens.`);
801
+ }
802
+ if (typeof item.name !== "string" || !item.name.trim()) throw new Error(`${label} needs a display name.`);
803
+ if (typeof item.extension !== "string") throw new Error(`${label} needs an extension.`);
804
+ if (typeof item.mimeType !== "string" || !item.mimeType.trim()) throw new Error(`${label} needs a mimeType.`);
805
+ if (typeof item.description !== "string" || !item.description.trim()) throw new Error(`${label} needs a description.`);
806
+ if (typeof item.export !== "function") throw new Error(`${label} needs an export function.`);
807
+ return {
808
+ id: item.id,
809
+ name: item.name.trim(),
810
+ extension: item.extension.trim().replace(/^\./, ""),
811
+ mimeType: item.mimeType.trim(),
812
+ description: item.description.trim(),
813
+ export: item.export
814
+ };
815
+ }
816
+ function sortModules(modules) {
817
+ const seen = /* @__PURE__ */ new Set();
818
+ const sorted = [...modules].sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
819
+ for (const item of sorted) {
820
+ if (seen.has(item.id)) throw new Error(`Exporter id "${item.id}" is registered more than once.`);
821
+ seen.add(item.id);
822
+ }
823
+ return sorted;
824
+ }
825
+ async function loadFromDirectory() {
826
+ let loaders = null;
827
+ try {
828
+ loaders = import.meta.glob("./*.ts");
829
+ } catch {
830
+ loaders = null;
831
+ }
832
+ const modules = [];
833
+ if (loaders && typeof loaders === "object") {
834
+ for (const [key, load2] of Object.entries(loaders)) {
835
+ if (exporterBasenames([key]).length === 0) continue;
836
+ const loaded = await load2();
837
+ modules.push(assertExporterModule(loaded.default, key));
838
+ }
839
+ if (modules.length > 0 || Object.keys(loaders).length > 0) return sortModules(modules);
840
+ }
841
+ const names = await readdir(here);
842
+ const load = new Function("specifier", "return import(specifier)");
843
+ for (const base of exporterBasenames(names)) {
844
+ const specifier = new URL(`./${base}.js`, import.meta.url).href;
845
+ const loaded = await load(specifier);
846
+ modules.push(assertExporterModule(loaded.default, `${base}.js`));
847
+ }
848
+ return sortModules(modules);
849
+ }
850
+ var cache = null;
851
+ var pending = null;
852
+ function loadedExporterIds() {
853
+ return cache ? cache.map((item) => item.id) : [];
854
+ }
855
+ function loadExporters() {
856
+ if (cache) return Promise.resolve(cache);
857
+ if (bundledExporters) {
858
+ cache = sortModules(bundledExporters.map((item) => assertExporterModule(item, item.id)));
859
+ return Promise.resolve(cache);
860
+ }
861
+ pending ??= loadFromDirectory().then((modules) => {
862
+ cache = modules;
863
+ return modules;
864
+ });
865
+ return pending;
866
+ }
867
+ async function exporterById(id) {
868
+ const modules = await loadExporters();
869
+ return modules.find((item) => item.id === id);
870
+ }
871
+ async function listExporterInfo() {
872
+ const modules = await loadExporters();
873
+ return modules.map((item) => ({
874
+ id: item.id,
875
+ name: item.name,
876
+ extension: item.extension,
877
+ mimeType: item.mimeType,
878
+ description: item.description
879
+ }));
880
+ }
881
+ function exporterDirectory() {
882
+ return here;
883
+ }
884
+
885
+ // server/src/exporters/run.ts
886
+ async function installedExporters() {
887
+ return listExporterInfo();
888
+ }
889
+ async function runExport(id, document, context, mappingDocument = null) {
890
+ const target = id.trim().toLowerCase();
891
+ const exporter8 = await exporterById(target);
892
+ if (!exporter8) {
893
+ const installed = (await loadExporters()).map((item) => item.id);
894
+ const known = installed.length > 0 ? installed.join(", ") : "(none)";
895
+ throw exporterFail(404, `Unknown exporter "${target}". Installed exporters: ${known}.`, "not_found");
896
+ }
897
+ assertExportShape(document);
898
+ const mapping = context.mapping ?? mappingForTarget(mappingDocument, exporter8.id);
899
+ let result;
900
+ try {
901
+ result = exporter8.export(document, { label: safeLabel(context.label), mapping });
902
+ } catch (err) {
903
+ if (err && typeof err === "object" && "status" in err) throw err;
904
+ const message = err instanceof Error ? err.message : "Export failed.";
905
+ throw exporterFail(400, message);
906
+ }
907
+ if (!result || !Array.isArray(result.files) || result.files.length === 0 || !Array.isArray(result.warnings)) {
908
+ throw exporterFail(500, `Exporter "${exporter8.id}" did not return a file.`);
909
+ }
910
+ const files = result.files.map((file) => {
911
+ if (!file || typeof file.name !== "string" || !file.name.trim() || typeof file.text !== "string") {
912
+ throw exporterFail(500, `Exporter "${exporter8.id}" returned a file without a name or text.`);
913
+ }
914
+ const name = file.name.replace(/\\/g, "/").replace(/^\/+/, "");
915
+ if (!name || name.split("/").some((part) => part === ".." || part === "")) {
916
+ throw exporterFail(500, `Exporter "${exporter8.id}" returned an unsafe file name.`);
917
+ }
918
+ return { name, text: file.text };
919
+ });
920
+ const primary = files[0];
921
+ const filename = files.length === 1 ? primary.name : fileName(context.label, "zip");
922
+ const text = primary.text;
923
+ const bytes = files.reduce((sum, file) => sum + Buffer.byteLength(file.text), 0);
924
+ const archive = files.length > 1 ? {
925
+ filename,
926
+ mimeType: "application/zip",
927
+ base64: zipStore(files).toString("base64")
928
+ } : void 0;
929
+ return {
930
+ target: exporter8.id,
931
+ filename: files.length === 1 ? primary.name : filename,
932
+ text,
933
+ files,
934
+ warnings: sortWarnings(result.warnings),
935
+ mimeType: files.length === 1 ? exporter8.mimeType : "application/zip",
936
+ extension: exporter8.extension,
937
+ bytes,
938
+ ...archive ? { archive } : {}
939
+ };
940
+ }
941
+ function outputDirectory(output, multiple) {
942
+ if (output.toLowerCase().endsWith(".zip")) return { dir: path2.dirname(output), zip: true };
943
+ if (multiple) return { dir: output, zip: false };
944
+ return { dir: path2.dirname(output), zip: false };
945
+ }
946
+
947
+ export {
948
+ EXPORTER_INFRA,
949
+ exporterBasenames,
950
+ assertExporterModule,
951
+ loadedExporterIds,
952
+ loadExporters,
953
+ exporterById,
954
+ listExporterInfo,
955
+ exporterDirectory,
956
+ installedExporters,
957
+ runExport,
958
+ outputDirectory
959
+ };