pmcf 1.36.1 → 1.37.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pmcf",
3
- "version": "1.36.1",
3
+ "version": "1.37.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -47,7 +47,7 @@
47
47
  "ava": "^6.2.0",
48
48
  "c8": "^10.1.3",
49
49
  "documentation": "^14.0.3",
50
- "semantic-release": "^24.2.1",
50
+ "semantic-release": "^24.2.2",
51
51
  "typescript": "^5.7.3"
52
52
  },
53
53
  "engines": {
package/src/model.mjs CHANGED
@@ -12,7 +12,29 @@ import { Service } from "./service.mjs";
12
12
  import { Cluster } from "./cluster.mjs";
13
13
  import { DNSService } from "./dns.mjs";
14
14
 
15
- export class Root extends Owner {
15
+ export class Location extends Owner {
16
+ static get typeName() {
17
+ return "location";
18
+ }
19
+
20
+ get location() {
21
+ return this;
22
+ }
23
+
24
+ locationNamed(name) {
25
+ if (this.fullName === name) {
26
+ return this;
27
+ }
28
+
29
+ return super.locationNamed(name);
30
+ }
31
+
32
+ get network() {
33
+ return [...this.typeList("network")][0] || super.network;
34
+ }
35
+ }
36
+
37
+ export class Root extends Location {
16
38
  static get types() {
17
39
  return _typesByName;
18
40
  }
@@ -27,8 +49,7 @@ export class Root extends Owner {
27
49
  this.addObject(this);
28
50
  }
29
51
 
30
- get types()
31
- {
52
+ get types() {
32
53
  return this.constructor.types;
33
54
  }
34
55
 
@@ -109,24 +130,6 @@ export class Root extends Owner {
109
130
  }
110
131
  }
111
132
 
112
- export class Location extends Owner {
113
- static get typeName() {
114
- return "location";
115
- }
116
-
117
- get location() {
118
- return this;
119
- }
120
-
121
- locationNamed(name) {
122
- if (this.fullName === name) {
123
- return this;
124
- }
125
-
126
- return super.locationNamed(name);
127
- }
128
- }
129
-
130
133
  export class Host extends Base {
131
134
  postinstall = [];
132
135
  #services = [];
@@ -326,6 +329,10 @@ export class Host extends Base {
326
329
  return this;
327
330
  }
328
331
 
332
+ get network() {
333
+ return this.owner.network;
334
+ }
335
+
329
336
  addService(service) {
330
337
  this.#services.push(service);
331
338
  }
@@ -347,7 +354,7 @@ export class Host extends Base {
347
354
  return this.#networkInterfaces;
348
355
  }
349
356
 
350
- networkInterfacesNamed(name) {
357
+ networkInterfaceNamed(name) {
351
358
  return this.#networkInterfaces.get(name);
352
359
  }
353
360
 
@@ -477,11 +484,20 @@ export class NetworkInterface extends Base {
477
484
  //this.arpbridge = owner.addARPBridge(this, data.arpbridge);
478
485
  }
479
486
 
487
+ addSubnet(address) {
488
+ if (!this.network) {
489
+ this.error("Missing network", address);
490
+ } else {
491
+ return this.network.addSubnet(address);
492
+ }
493
+ }
494
+
480
495
  set ipAddresses(value) {
481
- const networkOwner = this.owner.owner;
482
496
  for (const address of asArray(value)) {
483
- const subnet = networkOwner.createSubnet(address);
484
- this.#ipAddresses.set(normalizeIPAddress(address), subnet);
497
+ this.#ipAddresses.set(
498
+ normalizeIPAddress(address),
499
+ this.addSubnet(address)
500
+ );
485
501
  }
486
502
  }
487
503
 
@@ -501,7 +517,6 @@ export class NetworkInterface extends Base {
501
517
  }
502
518
 
503
519
  get gatewayAddress() {
504
- console.log(typeof this.gateway);
505
520
  for (const a of this.gateway.networkAddresses()) {
506
521
  if (a.networkInterface.network === this.network) {
507
522
  return a.address;
@@ -510,7 +525,7 @@ export class NetworkInterface extends Base {
510
525
  }
511
526
 
512
527
  get ipAddresses() {
513
- return this.#ipAddresses.keys();
528
+ return [...this.#ipAddresses.keys()];
514
529
  }
515
530
 
516
531
  get ipAddressesWithPrefixLength() {
@@ -531,13 +546,17 @@ export class NetworkInterface extends Base {
531
546
  return this.network?.prefixLength;
532
547
  }
533
548
 
549
+ get host() {
550
+ return this.owner;
551
+ }
552
+
534
553
  get network() {
535
- return this.#network;
554
+ return this.#network || this.host.network;
536
555
  }
537
556
 
538
557
  set network(networkOrName) {
539
558
  if (!(networkOrName instanceof Network)) {
540
- let network = this.owner.owner.networkNamed(networkOrName);
559
+ let network = this.host.owner.networkNamed(networkOrName);
541
560
 
542
561
  if (network) {
543
562
  this.#network = network;
@@ -550,10 +569,6 @@ export class NetworkInterface extends Base {
550
569
  this.#network = networkOrName;
551
570
  }
552
571
 
553
- get host() {
554
- return this.owner;
555
- }
556
-
557
572
  get scope() {
558
573
  return this.#scope || this.network?.scope || "global";
559
574
  }
package/src/owner.mjs CHANGED
@@ -166,10 +166,18 @@ export class Owner extends Base {
166
166
  }
167
167
 
168
168
  subnetNamed(name) {
169
+ if (this !== this.location) {
170
+ return this.location.subnetNamed(name);
171
+ }
172
+
169
173
  return this.typeNamed("subnet", name);
170
174
  }
171
175
 
172
176
  subnets() {
177
+ if (this !== this.location) {
178
+ return this.location.subnets();
179
+ }
180
+
173
181
  return this.typeList("subnet");
174
182
  }
175
183
 
@@ -181,18 +189,28 @@ export class Owner extends Base {
181
189
  }
182
190
  }
183
191
 
184
- createSubnet(address) {
192
+ addSubnet(address) {
193
+ if (this !== this.location) {
194
+ return this.location.addSubnet(address);
195
+ }
196
+
185
197
  if (address instanceof Subnet) {
186
198
  return address;
187
199
  }
188
200
 
189
201
  const { cidr } = normalizeCIDR(address);
190
202
 
203
+ //console.log("ADD SUBNET", this.toString(), address, cidr);
204
+
191
205
  if (cidr) {
192
206
  let subnet = this.subnetNamed(cidr);
193
207
 
194
208
  if (!subnet) {
195
209
  subnet = new Subnet(this, { name: cidr });
210
+
211
+ /*if(this.owner) {
212
+ this.owner.addSubnet(subnet);
213
+ }*/
196
214
  }
197
215
 
198
216
  return subnet;
@@ -331,6 +349,10 @@ export class Network extends Owner {
331
349
  this.bridge = owner.addBridge(this, bridge);
332
350
  }
333
351
 
352
+ get network() {
353
+ return this;
354
+ }
355
+
334
356
  networkNamed(name) {
335
357
  if (this.fullName === name) {
336
358
  return this;
@@ -340,8 +362,7 @@ export class Network extends Owner {
340
362
 
341
363
  addSubnets(value) {
342
364
  for (const address of asArray(value)) {
343
- const subnet = this.owner.createSubnet(address);
344
- this.addObject(subnet);
365
+ const subnet = this.addSubnet(address);
345
366
  subnet.networks.add(this);
346
367
  }
347
368
  }
package/src/utils.mjs CHANGED
@@ -81,6 +81,38 @@ function decodeIPv4(address, length = 32) {
81
81
  return octets.join(".");
82
82
  }
83
83
 
84
+ export function encodeIPv6(address) {
85
+ const words = [0, 0, 0, 0, 0, 0, 0, 0];
86
+
87
+ let i = 0;
88
+ for (const a of normalizeIPAddress(address).split(/\:/)) {
89
+ words[i++] = parseInt(a,16);
90
+ }
91
+
92
+ let res = 0n;
93
+ let shift = 128n;
94
+
95
+ for(const word of words) {
96
+ shift -= 16n;
97
+ res += BigInt(word) << shift;
98
+ }
99
+
100
+ return res;
101
+ }
102
+
103
+ export function decodeIPv6(address, length = 128) {
104
+
105
+ let words = [];
106
+ let shift = 128n;
107
+
108
+ for(let i = 0; i < length / 16; i++) {
109
+ shift -= 16n;
110
+ words[i] = ((address >> shift) & 0xffffn).toString(16).padStart(4,'0');
111
+ }
112
+
113
+ return words.join(":");
114
+ }
115
+
84
116
  export function normalizeCIDR(address) {
85
117
  let [prefix, prefixLength] = address.split(/\//);
86
118
 
@@ -94,9 +126,9 @@ export function normalizeCIDR(address) {
94
126
  n = n & (0xffffffff << (32 - prefixLength));
95
127
  prefix = decodeIPv4(n, prefixLength);
96
128
  } else {
97
- prefix = normalizeIPAddress(prefix);
98
- const parts = prefix.split(/\:/);
99
- prefix = parts.slice(0, prefixLength / 16).join(":");
129
+ let n = encodeIPv6(prefix);
130
+ n = n & (0xffffffffffffffffffffffffffffffffn << (128n - BigInt(prefixLength)));
131
+ prefix = decodeIPv6(n, prefixLength);
100
132
  }
101
133
  } else {
102
134
  return {};
package/types/model.d.mts CHANGED
@@ -1,4 +1,7 @@
1
- export class Root extends Owner {
1
+ export class Location extends Owner {
2
+ get location(): this;
3
+ }
4
+ export class Root extends Location {
2
5
  static get types(): {
3
6
  [k: string]: typeof DNSService | typeof Owner | typeof Subnet | typeof Service | typeof Host | typeof NetworkInterface;
4
7
  };
@@ -9,9 +12,6 @@ export class Root extends Owner {
9
12
  load(name: any, options: any): any;
10
13
  loadAll(): Promise<void>;
11
14
  }
12
- export class Location extends Owner {
13
- get location(): this;
14
- }
15
15
  export class Host extends Base {
16
16
  static prepareData(root: any, data: any): Promise<typeof Host>;
17
17
  postinstall: any[];
@@ -37,7 +37,7 @@ export class Host extends Base {
37
37
  addService(service: any): void;
38
38
  services(filter: any): Generator<any, void, unknown>;
39
39
  get networkInterfaces(): Map<any, any>;
40
- networkInterfacesNamed(name: any): any;
40
+ networkInterfaceNamed(name: any): any;
41
41
  addNetworkInterface(networkInterface: any): void;
42
42
  networkAddresses(): Generator<{
43
43
  networkInterface: any;
@@ -60,8 +60,9 @@ export class NetworkInterface extends Base {
60
60
  hwaddr: any;
61
61
  set network(networkOrName: any);
62
62
  get network(): any;
63
- set ipAddresses(value: MapIterator<any>);
64
- get ipAddresses(): MapIterator<any>;
63
+ addSubnet(address: any): any;
64
+ set ipAddresses(value: any[]);
65
+ get ipAddresses(): any[];
65
66
  subnetForAddress(address: any): any;
66
67
  addressWithPrefixLength(address: any): string;
67
68
  get gateway(): any;
package/types/owner.d.mts CHANGED
@@ -20,9 +20,9 @@ export class Owner extends Base {
20
20
  networkNamed(name: any): any;
21
21
  networks(): Generator<any, void, unknown>;
22
22
  subnetNamed(name: any): any;
23
- subnets(): Generator<any, void, unknown>;
23
+ subnets(): any;
24
24
  subnetForAddress(address: any): any;
25
- createSubnet(address: any): any;
25
+ addSubnet(address: any): any;
26
26
  clusterNamed(name: any): any;
27
27
  clusters(): Generator<any, void, unknown>;
28
28
  addBridge(network: any, destinationNetworks: any): any;
@@ -38,6 +38,7 @@ export class Network extends Owner {
38
38
  metric: any;
39
39
  bridge: any;
40
40
  gateway: any;
41
+ get network(): this;
41
42
  addSubnets(value: any): void;
42
43
  }
43
44
  export class Subnet extends Base {
package/types/utils.d.mts CHANGED
@@ -6,6 +6,8 @@ export function isIPv4Address(address: any): boolean;
6
6
  export function isIPv6Address(address: any): boolean;
7
7
  export function isLinkLocal(address: any): any;
8
8
  export function normalizeIPAddress(address: any): any;
9
+ export function encodeIPv6(address: any): bigint;
10
+ export function decodeIPv6(address: any, length?: number): string;
9
11
  export function normalizeCIDR(address: any): {
10
12
  prefix?: undefined;
11
13
  prefixLength?: undefined;