@yarkivaev/scada 2.26.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.26.0",
3
+ "version": "2.27.0",
4
4
  "description": "SCADA domain objects, state persistence, and plant monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -76,7 +76,8 @@ export function collectorDeviceAssignments(document, selection, { rules, sourceT
76
76
  const profile = profileDefinitionFor(expanded, server);
77
77
  const rows = (releases(document, server.id).scada.machines || []).filter((row) => { return accepts(row, selection, rules); });
78
78
  const processes = sourceTypes.flatMap(({ schemes, projection }) => {
79
- 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);
80
81
  });
81
82
  return compile(processes, generation);
82
83
  });
@@ -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;
@@ -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';