pmcf 1.24.1 → 1.25.1

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.
@@ -2,7 +2,11 @@
2
2
 
3
3
  import { join } from "node:path";
4
4
  import { createHmac } from "node:crypto";
5
- import { writeLines, isIPv4Address } from "../src/utils.mjs";
5
+ import {
6
+ writeLines,
7
+ isIPv4Address,
8
+ normalizeIPAddress
9
+ } from "../src/utils.mjs";
6
10
  import { prepare } from "../src/cmd.mjs";
7
11
 
8
12
  const { world, args, options } = prepare();
@@ -41,7 +45,10 @@ async function generateNamedDefs(owner, targetDir) {
41
45
  type,
42
46
  value,
43
47
  toString: () =>
44
- `${key.padEnd(maxKeyLength, " ")} ${ttl} IN ${type.padEnd(5, " ")} ${value}`
48
+ `${key.padEnd(maxKeyLength, " ")} ${ttl} IN ${type.padEnd(
49
+ 5,
50
+ " "
51
+ )} ${value}`
45
52
  };
46
53
  };
47
54
 
@@ -201,16 +208,3 @@ export function reverseArpaAddress(address) {
201
208
  (isIPv4Address(address) ? ".in-addr.arpa" : ".ip6.arpa")
202
209
  );
203
210
  }
204
-
205
- export function normalizeIPAddress(address) {
206
- if (isIPv4Address(address)) {
207
- return address;
208
- }
209
- address = address.replace(/\/\d+$/, "");
210
- const parts = address.split(":");
211
- const i = parts.indexOf("");
212
- if (i >= 0) {
213
- parts.splice(i, 1, ..."0".repeat(9 - parts.length));
214
- }
215
- return parts.map(s => s.padStart(4, "0")).join(":");
216
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pmcf",
3
- "version": "1.24.1",
3
+ "version": "1.25.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/base.mjs CHANGED
@@ -122,8 +122,14 @@ export class Base {
122
122
  }
123
123
 
124
124
  execFinalize() {
125
+ this.traverse(object => {
126
+ //console.log(object.toString());
127
+ object._finalize();
128
+ });
129
+ }
130
+
131
+ _finalize() {
125
132
  if (this.#finalize) {
126
- //this.info("finalize");
127
133
  let i = 0;
128
134
  for (const action of this.#finalize) {
129
135
  if (action) {
@@ -135,6 +141,23 @@ export class Base {
135
141
  }
136
142
  }
137
143
 
144
+ traverse(visitor, ...args) {
145
+ const visited = new Set();
146
+ this._traverse(visited, visitor, ...args);
147
+ }
148
+
149
+ _traverse(visited, visitor, ...args) {
150
+ if (visited.has(this)) {
151
+ return false;
152
+ }
153
+
154
+ visited.add(this);
155
+
156
+ visitor(this, ...args);
157
+
158
+ return true;
159
+ }
160
+
138
161
  error(...args) {
139
162
  console.error(`${this.toString()}:`, ...args);
140
163
  }
package/src/model.mjs CHANGED
@@ -4,7 +4,8 @@ import {
4
4
  asArray,
5
5
  bridgeToJSON,
6
6
  isIPv4Address,
7
- isIPv6Address
7
+ isIPv6Address,
8
+ normalizeIPAddress
8
9
  } from "./utils.mjs";
9
10
  import { Base } from "./base.mjs";
10
11
  import { Service } from "./service.mjs";
@@ -41,18 +42,22 @@ export class Owner extends Base {
41
42
  }
42
43
  }
43
44
  Object.assign(this, data);
45
+ }
44
46
 
45
- this.finalize(() => {
47
+ _traverse(...args) {
48
+ if (super._traverse(...args)) {
46
49
  for (const network of this.#networks.values()) {
47
- network.execFinalize();
50
+ network._traverse(...args);
48
51
  }
49
- });
50
52
 
51
- this.finalize(() => {
52
53
  for (const host of this.#hosts.values()) {
53
- host.execFinalize();
54
+ host._traverse(...args);
54
55
  }
55
- });
56
+
57
+ return true;
58
+ }
59
+
60
+ return false;
56
61
  }
57
62
 
58
63
  get dns() {
@@ -219,6 +224,18 @@ export class World extends Owner {
219
224
  this.addObject(this);
220
225
  }
221
226
 
227
+ _traverse(...args) {
228
+ if (super._traverse(...args)) {
229
+ for (const object of this.#byName.values()) {
230
+ object._traverse(...args);
231
+ }
232
+
233
+ return true;
234
+ }
235
+
236
+ return false;
237
+ }
238
+
222
239
  get fullName() {
223
240
  return "";
224
241
  }
@@ -429,7 +446,6 @@ export class Host extends Base {
429
446
  #deployment;
430
447
  #chassis;
431
448
  #vendor;
432
- #location;
433
449
 
434
450
  static get typeName() {
435
451
  return "host";
@@ -452,11 +468,6 @@ export class Host extends Base {
452
468
  constructor(owner, data) {
453
469
  super(owner, data);
454
470
 
455
- if (data.location !== undefined) {
456
- this.#location = data.location;
457
- delete data.location;
458
- }
459
-
460
471
  if (data.deployment !== undefined) {
461
472
  this.#deployment = data.deployment;
462
473
  delete data.deployment;
@@ -520,12 +531,20 @@ export class Host extends Base {
520
531
  }
521
532
 
522
533
  owner.addHost(this);
534
+ }
523
535
 
524
- this.finalize(() => {
536
+ _traverse(...args) {
537
+ if (super._traverse(...args)) {
525
538
  for (const ni of Object.values(this.networkInterfaces)) {
526
- ni.execFinalize();
539
+ ni._traverse(...args);
540
+ }
541
+ for (const service of this.services()) {
542
+ service._traverse(...args);
527
543
  }
528
- });
544
+
545
+ return true;
546
+ }
547
+ return false;
529
548
  }
530
549
 
531
550
  get deployment() {
@@ -552,10 +571,6 @@ export class Host extends Base {
552
571
  return this.#extends.map(e => this.expand(e));
553
572
  }
554
573
 
555
- get location() {
556
- return this.#location || super.location;
557
- }
558
-
559
574
  get provides() {
560
575
  let provides = new Set(this.#provides);
561
576
  this.extends.forEach(h => (provides = provides.union(h.provides)));
@@ -645,13 +660,11 @@ export class Host extends Base {
645
660
  }
646
661
 
647
662
  get ipAddresses() {
648
- return [...this.networkAddresses()].map(na => na.address);
663
+ return [...this.networkAddresses()].map(na => normalizeIPAddress(na.address));
649
664
  }
650
665
 
651
666
  get ipAddress() {
652
- for (const a of this.networkAddresses()) {
653
- return a.address;
654
- }
667
+ return this.ipAddresses[0];
655
668
  }
656
669
 
657
670
  async publicKey(type = "ed25519") {
@@ -857,6 +870,5 @@ export class Subnet extends Base {
857
870
  }
858
871
  }
859
872
 
860
-
861
873
  const _types = [Location, Network, Subnet, Host, Service, DNSService];
862
874
  const _typesByName = Object.fromEntries(_types.map(t => [t.typeName, t]));
package/src/utils.mjs CHANGED
@@ -39,3 +39,16 @@ export function isIPv4Address(address) {
39
39
  export function isIPv6Address(address) {
40
40
  return address.indexOf(":") >= 0;
41
41
  }
42
+
43
+ export function normalizeIPAddress(address) {
44
+ address = address.replace(/\/\d+$/, "");
45
+ if (isIPv4Address(address)) {
46
+ return address;
47
+ }
48
+ const parts = address.split(":");
49
+ const i = parts.indexOf("");
50
+ if (i >= 0) {
51
+ parts.splice(i, 1, ..."0".repeat(9 - parts.length));
52
+ }
53
+ return parts.map(s => s.padStart(4, "0")).join(":");
54
+ }
package/types/base.d.mts CHANGED
@@ -22,6 +22,9 @@ export class Base {
22
22
  expand(object: any): any;
23
23
  finalize(action: any): void;
24
24
  execFinalize(): void;
25
+ _finalize(): void;
26
+ traverse(visitor: any, ...args: any[]): void;
27
+ _traverse(visited: any, visitor: any, ...args: any[]): boolean;
25
28
  error(...args: any[]): void;
26
29
  info(...args: any[]): void;
27
30
  toString(): string;
package/types/model.d.mts CHANGED
@@ -3,6 +3,7 @@ export class Owner extends Base {
3
3
  ntp: {
4
4
  servers: any[];
5
5
  };
6
+ _traverse(...args: any[]): boolean;
6
7
  get dns(): DNSService;
7
8
  hosts(): AsyncGenerator<any, void, unknown>;
8
9
  addObject(object: any): void;
@@ -58,6 +59,7 @@ export class Host extends Base {
58
59
  static prepareData(world: any, data: any): Promise<typeof Host>;
59
60
  networkInterfaces: {};
60
61
  postinstall: any[];
62
+ _traverse(...args: any[]): boolean;
61
63
  get deployment(): any;
62
64
  get chassis(): any;
63
65
  get vendor(): any;
package/types/utils.d.mts CHANGED
@@ -4,3 +4,4 @@ export function bridgeToJSON(bridge: any): any[];
4
4
  export function asArray(value: any): any[];
5
5
  export function isIPv4Address(address: any): boolean;
6
6
  export function isIPv6Address(address: any): boolean;
7
+ export function normalizeIPAddress(address: any): any;