@yarkivaev/scada 2.25.0 → 2.27.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yarkivaev/scada",
3
- "version": "2.25.0",
3
+ "version": "2.27.0",
4
4
  "description": "SCADA domain objects, state persistence, and plant monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -4,6 +4,7 @@ import compileCollectorTasks from './compileCollectorTasks.js';
4
4
  import { deploymentContext } from './deploymentPlan.js';
5
5
  import releases from './releases.js';
6
6
  import collectorProcesses from './collectorProcesses.js';
7
+ import { profileDefinitionFor } from './releaseNodes.js';
7
8
 
8
9
  function sourceScheme(from) {
9
10
  const text = String(from);
@@ -72,10 +73,11 @@ export function collectorDeviceAssignments(document, selection, { rules, sourceT
72
73
  const servers = expanded.servers.filter((server) => { return !cluster || server.id === cluster; });
73
74
  if (cluster && !servers.length) { throw new TypeError(`Unknown collector cluster ${cluster}`); }
74
75
  return servers.flatMap((server) => {
75
- const profile = expanded.profiles[server.profile.id];
76
+ const profile = profileDefinitionFor(expanded, server);
76
77
  const rows = (releases(document, server.id).scada.machines || []).filter((row) => { return accepts(row, selection, rules); });
77
78
  const processes = sourceTypes.flatMap(({ schemes, projection }) => {
78
- return collect[projection](rows.filter((row) => { return schemes.includes(sourceScheme(row.from)); }), profile.apps.scada);
79
+ return collect[projection](rows.filter((row) => { return schemes.includes(sourceScheme(row.from)); }),
80
+ profile.apps.scada, document.sources);
79
81
  });
80
82
  return compile(processes, generation);
81
83
  });
@@ -1,5 +1,6 @@
1
1
  import expand from './expand.js';
2
2
  import { versionedControlCatalog } from './controlPlan.js';
3
+ import { profileDefinitionFor } from './releaseNodes.js';
3
4
 
4
5
  function readerPolicy(config, node, task, rows, { placement, pinned, oneCopy, matchesInput }) {
5
6
  const matched = rows.filter(({ from }) => { return from.includes('://'); }).filter((row) => { return matchesInput(task, row, node); });
@@ -19,7 +20,7 @@ function readerPolicy(config, node, task, rows, { placement, pinned, oneCopy, ma
19
20
 
20
21
  function nodeTasks(document, expanded, config, node, ports) {
21
22
  const server = expanded.servers.find(({ id }) => { return id === node.id; });
22
- const profile = expanded.profiles[server.profile.id];
23
+ const profile = profileDefinitionFor(expanded, server);
23
24
  const readers = ports.readers(document, node.id);
24
25
  const readerIds = new Set(readers.map(({ taskId }) => { return taskId; }));
25
26
  const tasks = ports.assignments(document, server, profile, readers);
@@ -1,13 +1,13 @@
1
1
  import { isIP } from 'node:net';
2
2
  import expand from './expand.js';
3
3
  import { releaseFor } from './releases.js';
4
- import { serverFor } from './releaseNodes.js';
4
+ import { profileDefinitionFor, serverFor } from './releaseNodes.js';
5
5
 
6
6
  /** Resolve deployment inputs once for a plant module that builds chart values. */
7
7
  export function deploymentContext(document, cluster) {
8
8
  const expanded = expand(document);
9
9
  const server = serverFor(expanded, cluster);
10
- const profile = expanded.profiles[server.profile.id];
10
+ const profile = profileDefinitionFor(expanded, server);
11
11
  return { expanded, server, profile, ...releaseFor(expanded, server) };
12
12
  }
13
13
 
@@ -0,0 +1,79 @@
1
+ const routeFields = ['fan_in', 'fan_out', 'objects', 'ownership'];
2
+
3
+ function isMap(value) {
4
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
5
+ }
6
+
7
+ /** Resolve a catalog source. Route parameters select a stream from a shared endpoint. */
8
+ export function sourceAddress(sources, id, query) {
9
+ if (typeof id !== 'string' || !Object.hasOwn(sources ?? {}, id)) {
10
+ throw new TypeError(`Unknown source ${id}`);
11
+ }
12
+ const { address } = sources[id] ?? {};
13
+ if (typeof address !== 'string' || !address.includes('://')) {
14
+ throw new TypeError(`Source ${id} must declare an address`);
15
+ }
16
+ if (query === undefined) { return address; }
17
+ if (!isMap(query)) { throw new TypeError(`Source ${id} query must be a map`); }
18
+ const url = new URL(address);
19
+ for (const [key, value] of Object.entries(query)) {
20
+ if (!['string', 'number', 'boolean'].includes(typeof value)) {
21
+ throw new TypeError(`Invalid query value for source ${id}: ${key}`);
22
+ }
23
+ url.searchParams.set(key, String(value));
24
+ }
25
+ return url.href;
26
+ }
27
+
28
+ function resolveInput(input, sources) {
29
+ if (!Object.hasOwn(input, 'source')) {
30
+ if (Object.hasOwn(input, 'query')) { throw new TypeError('Routing query requires a source reference'); }
31
+ return input;
32
+ }
33
+ const { source, query, ...flow } = input;
34
+ if (Object.hasOwn(flow, 'from')) { throw new TypeError('Routing input cannot declare both source and from'); }
35
+ return { from: sourceAddress(sources, source, query), ...flow };
36
+ }
37
+
38
+ function apiPublications(server, publications) {
39
+ if (!isMap(publications)) { throw new TypeError(`Routing publications for ${server.id} must be a map`); }
40
+ const api = server.api ?? [];
41
+ for (const id of Object.keys(publications)) {
42
+ if (!api.some((entry) => { return entry.id === id; })) { throw new TypeError(`Unknown routing API ${id} on ${server.id}`); }
43
+ }
44
+ return api.map((entry) => {
45
+ if (!Object.hasOwn(publications, entry.id)) { return entry; }
46
+ if (Object.hasOwn(entry, 'publishes')) { throw new TypeError(`Server ${server.id} declares publishes twice for ${entry.id}`); }
47
+ return { ...entry, publishes: publications[entry.id] };
48
+ });
49
+ }
50
+
51
+ function withRoutes(server, route, sources) {
52
+ if (!isMap(route)) { throw new TypeError(`Routing for ${server.id} must be a map`); }
53
+ const result = { ...server };
54
+ for (const [key, value] of Object.entries(route)) {
55
+ if (key === 'publishes') {
56
+ result.api = apiPublications(server, value);
57
+ } else {
58
+ if (!routeFields.includes(key)) { throw new TypeError(`Unknown routing field ${key} on ${server.id}`); }
59
+ if (Object.hasOwn(server, key)) { throw new TypeError(`Server ${server.id} declares ${key} twice`); }
60
+ if (!Array.isArray(value)) { throw new TypeError(`Routing ${key} for ${server.id} must be a list`); }
61
+ result[key] = key === 'fan_in' ? value.map((input) => { return resolveInput(input, sources); }) : value;
62
+ }
63
+ }
64
+ return result;
65
+ }
66
+
67
+ /** Project separately declared routes into the existing system-document shape. */
68
+ export default function resolveDeploymentRouting(document) {
69
+ const { routes, ...rest } = document;
70
+ if (routes === undefined) { return document; }
71
+ if (!isMap(routes)) { throw new TypeError('Deployment routes must be a map'); }
72
+ const servers = document.servers ?? [];
73
+ for (const id of Object.keys(routes)) {
74
+ if (!servers.some((server) => { return server.id === id; })) { throw new TypeError(`Unknown routing server ${id}`); }
75
+ }
76
+ return { ...rest, servers: servers.map((server) => {
77
+ return Object.hasOwn(routes, server.id) ? withRoutes(server, routes[server.id], document.sources) : server;
78
+ }) };
79
+ }
@@ -1,6 +1,7 @@
1
1
  import { expandTree, treePaths } from './expandNamespace.js';
2
2
  import { expandProfiles } from './expandProfiles.js';
3
3
  import { expandServers } from './expandFlows.js';
4
+ import resolveDeploymentRouting from './deploymentRouting.js';
4
5
  import frozen from './frozen.js';
5
6
 
6
7
  /**
@@ -35,14 +36,15 @@ export default function expand(document) {
35
36
  if (!document || typeof document !== 'object' || Array.isArray(document)) {
36
37
  throw new Error('system document is empty');
37
38
  }
38
- const namespace = expandTree(document.namespace, document.templates || {});
39
+ const input = resolveDeploymentRouting(document);
40
+ const namespace = expandTree(input.namespace, input.templates || {});
39
41
  const paths = treePaths(namespace);
40
- const profiles = expandProfiles(document.profiles || {});
41
- assertProfiles(document.servers || [], profiles);
42
- const servers = expandServers(document.servers || [], paths, document.mappings || {});
42
+ const profiles = expandProfiles(input.profiles || {});
43
+ assertProfiles(input.servers || [], profiles);
44
+ const servers = expandServers(input.servers || [], paths, input.mappings || {});
43
45
  return frozen({
44
- ...(document.hmi !== undefined && { hmi: document.hmi }),
45
- mappings: document.mappings || {},
46
+ ...(input.hmi !== undefined && { hmi: input.hmi }),
47
+ mappings: input.mappings || {},
46
48
  namespace,
47
49
  profiles,
48
50
  servers
@@ -88,6 +88,12 @@ export default function mergeDocuments(base, extra) {
88
88
  if (base.mappings || extra.mappings) {
89
89
  document.mappings = mergeMaps(base.mappings, extra.mappings);
90
90
  }
91
+ if (base.sources || extra.sources) {
92
+ document.sources = mergeMaps(base.sources, extra.sources);
93
+ }
94
+ if (base.routes || extra.routes) {
95
+ document.routes = mergeMaps(base.routes, extra.routes);
96
+ }
91
97
  if (base.profiles || extra.profiles) {
92
98
  document.profiles = mergeMaps(base.profiles, extra.profiles);
93
99
  }
@@ -1,4 +1,5 @@
1
1
  import readDocumentFile, { selectDeploymentPhase } from './readDocumentFile.js';
2
+ import resolveDeploymentRouting from './deploymentRouting.js';
2
3
 
3
4
  function fileSelection(file) {
4
5
  if (file instanceof URL) {
@@ -11,7 +12,7 @@ function fileSelection(file) {
11
12
  /** A deployment document can declare its phase policy and be opened as path#phase. */
12
13
  export default function readDeploymentDocument(file, parse) {
13
14
  const { path, phase } = fileSelection(file);
14
- const { deploymentPhase: policy, ...document } = readDocumentFile(path, parse);
15
+ const { deploymentPhase: policy, ...document } = resolveDeploymentRouting(readDocumentFile(path, parse));
15
16
  if (policy === undefined) {
16
17
  if (phase !== undefined) { throw new TypeError('Deployment phase policy is missing'); }
17
18
  return document;
@@ -59,6 +59,15 @@ export function serverById(document, id) {
59
59
  return found;
60
60
  }
61
61
 
62
+ function namedProfileFor(document, server) {
63
+ const label = server.profile && server.profile.id;
64
+ const profile = document.profiles && document.profiles[label];
65
+ if (!profile) {
66
+ throw new Error(`system document has no profile ${label}`);
67
+ }
68
+ return profile;
69
+ }
70
+
62
71
  /**
63
72
  * Reads the cluster a server selects.
64
73
  *
@@ -69,11 +78,7 @@ export function serverById(document, id) {
69
78
  * clusterOf({ profiles: { cell: {} } }, { profile: { id: 'cell', variables: { cluster: 'cell' } } });
70
79
  */
71
80
  export function clusterOf(document, server) {
72
- const label = server.profile && server.profile.id;
73
- const profile = document.profiles && document.profiles[label];
74
- if (!profile) {
75
- throw new Error(`system document has no profile ${label}`);
76
- }
81
+ const profile = namedProfileFor(document, server);
77
82
  const values = { ...(profile.variables || {}), ...(server.profile.variables || {}) };
78
83
  return values.cluster;
79
84
  }
@@ -100,8 +105,17 @@ export function serverFor(document, cluster) {
100
105
  return found[0];
101
106
  }
102
107
 
108
+ /** Resolve inline profile settings, leaving server variables and top-level modules to release projection. */
109
+ export function profileDefinitionFor(document, server) {
110
+ const profile = namedProfileFor(document, server);
111
+ const overrides = { ...server.profile };
112
+ delete overrides.id;
113
+ delete overrides.variables;
114
+ return Object.keys(overrides).length ? mergeMaps(profile, overrides) : profile;
115
+ }
116
+
103
117
  /**
104
- * Loads the profile a server names, with server variables overlaid.
118
+ * Loads the effective release profile, including server variables and modules.
105
119
  *
106
120
  * @param {object} document - expanded document
107
121
  * @param {object} server - selected server
@@ -110,11 +124,7 @@ export function serverFor(document, cluster) {
110
124
  * profileFor({ profiles: { cell: {} } }, { profile: { id: 'cell' } });
111
125
  */
112
126
  export function profileFor(document, server) {
113
- const label = server.profile && server.profile.id;
114
- const profile = document.profiles && document.profiles[label];
115
- if (!profile) {
116
- throw new Error(`system document has no profile ${label}`);
117
- }
127
+ const profile = profileDefinitionFor(document, server);
118
128
  const values = { ...(profile.variables || {}), ...(server.profile.variables || {}) };
119
129
  return { ...profile, ...(server.modules && { modules: mergeMaps(profile.modules, server.modules) }), variables: values };
120
130
  }
@@ -2,6 +2,7 @@ import compileControlCatalog, { matchesDeviceInput, scopeControlSecrets } from '
2
2
  import controlReaderSpec from '../../application/deploy/controlReaderSpecs.js';
3
3
  import expand from '../../application/deploy/expand.js';
4
4
  import projectControlPlane from '../../application/deploy/projectControlPlane.js';
5
+ import { profileDefinitionFor } from '../../application/deploy/releaseNodes.js';
5
6
  import { brokerSinkOptions } from './brokerTopology.js';
6
7
  import deploymentTemplate from './deploymentTemplate.js';
7
8
  import { reconciliationBundle } from './highAvailability/reconciliationBundle.js';
@@ -11,7 +12,7 @@ function serverFor(document, name) {
11
12
  }
12
13
 
13
14
  function historyBindings(expanded, server, policy) {
14
- const app = expanded.profiles[server.profile.id].apps.scada;
15
+ const app = profileDefinitionFor(expanded, server).apps.scada;
15
16
  const contracts = app.applicationContracts.filter(({ family }) => { return family === policy.historyFamily; });
16
17
  const device = server.id.slice(policy.historyDeviceOffset);
17
18
  return contracts.flatMap(({ publisherContracts, key }) => {
@@ -41,7 +42,7 @@ function readerOptions(spec, owner, config, document, { policy, ports }) {
41
42
  sinkOptions(exchange) {
42
43
  const expanded = expand(document);
43
44
  const server = serverFor(expanded, owner);
44
- const profile = expanded.profiles[server.profile.id];
45
+ const profile = profileDefinitionFor(expanded, server);
45
46
  return brokerSinkOptions(ports.readerBroker({ ...expanded, edgeControlPlane: config }, server, profile), exchange);
46
47
  },
47
48
  historyAdapter: policy.historyAdapter,
@@ -29,6 +29,7 @@ export { default as preflightKubeStorage } from './preflightKubeStorage.js';
29
29
  export { default as applyWhenChanged } from './applyWhenChanged.js';
30
30
  export { default as prepareKubePrerequisites } from './prepareKubePrerequisites.js';
31
31
  export { default as readDeploymentDocument } from '../../application/deploy/readDeploymentDocument.js';
32
+ export { sourceAddress } from '../../application/deploy/deploymentRouting.js';
32
33
  export { appendReplicaTopology, replicaShovels, brokerReplicaPlan, appendBrokerReplicas } from './brokerReplicaTopology.js';
33
34
  export { default as compileDeploymentHooks } from './compileDeploymentHooks.js';
34
35
  export { default as stagedStoragePreflight } from './stagedStoragePreflight.js';