pmcf 1.0.1 → 1.0.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/model.mjs +60 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pmcf",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/model.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { readFile, writeFile, mkdir, glob } from "node:fs/promises";
2
2
  import { join } from "node:path";
3
3
 
4
- class Base {
4
+ export class Base {
5
5
  owner;
6
6
  name;
7
7
 
@@ -36,20 +36,21 @@ class Base {
36
36
  }
37
37
  }
38
38
 
39
- get typeName()
40
- {
39
+ get typeName() {
41
40
  return this.constructor.typeName;
42
41
  }
43
42
 
44
- get host()
45
- {
46
- if(this instanceof Host) { return this; }
43
+ get host() {
44
+ if (this instanceof Host) {
45
+ return this;
46
+ }
47
47
  return this.owner.host;
48
48
  }
49
49
 
50
- get network()
51
- {
52
- if(this instanceof Network) { return this; }
50
+ get network() {
51
+ if (this instanceof Network) {
52
+ return this;
53
+ }
53
54
  return this.owner.network;
54
55
  }
55
56
 
@@ -84,18 +85,48 @@ class Base {
84
85
  }
85
86
 
86
87
  export class World {
87
- baseDir;
88
+ static get types() {
89
+ return _types;
90
+ }
91
+
92
+ directory;
93
+ #byName = new Map();
94
+
88
95
  /** @typedef {Map<string,Location>} */ #locations = new Map();
89
96
  /** @typedef {Map<string,Host>} */ #hosts = new Map();
90
97
 
91
- constructor(baseDir) {
92
- this.baseDir = baseDir;
98
+ constructor(directory) {
99
+ this.directory = directory;
93
100
  }
94
101
 
95
102
  get name() {
96
103
  return "";
97
104
  }
98
105
 
106
+ async load() {
107
+ for (const type of Object.values(World.types)) {
108
+ for await (const name of glob(type.fileNameGlob, {
109
+ cwd: this.directory
110
+ })) {
111
+ const baseName = type.baseName(name);
112
+ if (!this.#byName.get(baseName)) {
113
+ const data = JSON.parse(
114
+ await readFile(join(this.directory, name), "utf8")
115
+ );
116
+
117
+ data.directory = baseName;
118
+ data.name = baseName;
119
+ const object = new type(this, data);
120
+ this.#byName.set(data.name, object);
121
+ }
122
+ }
123
+ }
124
+ }
125
+
126
+ async named(name) {
127
+ return this.#byName.get(name);
128
+ }
129
+
99
130
  async *locations() {
100
131
  if (this.#locations.size > 0) {
101
132
  for (const location of this.#locations.values()) {
@@ -104,7 +135,7 @@ export class World {
104
135
  }
105
136
 
106
137
  for await (const name of glob(Location.fileNameGlob, {
107
- cwd: this.baseDir
138
+ cwd: this.directory
108
139
  })) {
109
140
  yield this.location(name);
110
141
  }
@@ -118,7 +149,7 @@ export class World {
118
149
  }
119
150
 
120
151
  for await (const name of glob(Host.fileNameGlob, {
121
- cwd: this.baseDir
152
+ cwd: this.directory
122
153
  })) {
123
154
  yield this.host(name);
124
155
  }
@@ -141,7 +172,7 @@ export class World {
141
172
  return location;
142
173
  }
143
174
 
144
- const directory = join(this.baseDir, name);
175
+ const directory = join(this.directory, name);
145
176
  try {
146
177
  const data = JSON.parse(
147
178
  await readFile(join(directory, Location.typeFileName), "utf8")
@@ -168,7 +199,7 @@ export class World {
168
199
  return host;
169
200
  }
170
201
 
171
- const directory = join(this.baseDir, name);
202
+ const directory = join(this.directory, name);
172
203
  const data = JSON.parse(
173
204
  await readFile(join(directory, Host.typeFileName), "utf8")
174
205
  );
@@ -216,7 +247,7 @@ export class World {
216
247
  }
217
248
  }
218
249
 
219
- class Host extends Base {
250
+ export class Host extends Base {
220
251
  directory;
221
252
  networkInterfaces = {};
222
253
  services = {};
@@ -404,9 +435,9 @@ class Host extends Base {
404
435
  }
405
436
  }
406
437
 
407
- class Model extends Host {}
438
+ export class Model extends Host {}
408
439
 
409
- class Location extends Base {
440
+ export class Location extends Base {
410
441
  directory;
411
442
  domain;
412
443
  dns;
@@ -574,7 +605,7 @@ class Location extends Base {
574
605
  }
575
606
  }
576
607
 
577
- class Network extends Base {
608
+ export class Network extends Base {
578
609
  #hosts = new Map();
579
610
  kind;
580
611
  ipv4;
@@ -624,7 +655,7 @@ class Network extends Base {
624
655
  }
625
656
  }
626
657
 
627
- class Subnet extends Base {
658
+ export class Subnet extends Base {
628
659
  networks = new Set();
629
660
 
630
661
  static get typeName() {
@@ -653,7 +684,7 @@ const ServiceTypes = {
653
684
  dhcp: {}
654
685
  };
655
686
 
656
- class Service extends Base {
687
+ export class Service extends Base {
657
688
  alias;
658
689
  #weight;
659
690
  #priority;
@@ -733,6 +764,13 @@ class Service extends Base {
733
764
  }
734
765
  }
735
766
 
767
+ const _types = Object.fromEntries(
768
+ [Location, Network, Subnet, Host, /*Model,*/ Service].map(t => [
769
+ t.typeName,
770
+ t
771
+ ])
772
+ );
773
+
736
774
  export async function writeLines(dir, name, lines) {
737
775
  await mkdir(dir, { recursive: true });
738
776
  return writeFile(