@yarkivaev/scada-server 1.0.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.
@@ -0,0 +1,39 @@
1
+ import errorResponse from '../objects/errorResponse.js';
2
+
3
+ /**
4
+ * Route collection that dispatches requests to matching routes.
5
+ * Accepts a list of route objects, each with matches() and handle() methods.
6
+ *
7
+ * @param {Array} routeList - array of route objects
8
+ * @returns {object} routes with list() and handle() methods
9
+ *
10
+ * @example
11
+ * const api = routes([...machineRoute(basePath, machines), ...alertRoute(basePath, alerts)]);
12
+ * http.createServer((req, res) => api.handle(req, res)).listen(3000);
13
+ */
14
+ export default function routes(routeList) {
15
+ return {
16
+ list() {
17
+ return routeList;
18
+ },
19
+ async handle(req, res) {
20
+ if (req.method === 'OPTIONS') {
21
+ res.writeHead(200, {
22
+ 'Access-Control-Allow-Origin': '*',
23
+ 'Access-Control-Allow-Methods': 'GET, PATCH, OPTIONS',
24
+ 'Access-Control-Allow-Headers': 'Content-Type'
25
+ });
26
+ res.end();
27
+ return;
28
+ }
29
+ const matching = routeList.find((rt) => {
30
+ return rt.matches(req);
31
+ });
32
+ if (matching) {
33
+ await matching.handle(req, res);
34
+ } else {
35
+ errorResponse('NOT_FOUND', 'Route not found', 404).send(res);
36
+ }
37
+ }
38
+ };
39
+ }
@@ -0,0 +1,41 @@
1
+ import alertRoute from './alertRoute.js';
2
+ import alertStream from './alertStream.js';
3
+ import machineRoute from './machineRoute.js';
4
+ import measurementRoute from './measurementRoute.js';
5
+ import measurementStream from './measurementStream.js';
6
+ import meltingRoute from './meltingRoute.js';
7
+ import meltingStream from './meltingStream.js';
8
+ import routes from './routes.js';
9
+
10
+ /**
11
+ * SCADA server factory that creates routes from a plant domain object.
12
+ * Assembles all route handlers for the Supervisor API.
13
+ *
14
+ * @param {string} basePath - base URL path for all routes
15
+ * @param {object} plant - plant domain object from scada package
16
+ * @param {function} clock - time provider function (optional)
17
+ * @returns {object} routes object with list() and handle() methods
18
+ *
19
+ * @example
20
+ * import { scadaServer } from 'scada-server';
21
+ * import { plant, meltingShop, meltingMachine } from 'scada';
22
+ * const p = createPlant(meltingMachine); // your plant factory
23
+ * const server = scadaServer('/api/v1', p);
24
+ * http.createServer((req, res) => server.handle(req, res)).listen(3000);
25
+ */
26
+ export default function scadaServer(basePath, plant, clock) {
27
+ plant.init();
28
+ const time = clock || (() => {
29
+ return new Date();
30
+ });
31
+ const routeList = [
32
+ ...machineRoute(basePath, plant),
33
+ ...measurementStream(basePath, plant, time),
34
+ ...measurementRoute(basePath, plant, time),
35
+ ...alertStream(basePath, plant, time),
36
+ ...alertRoute(basePath, plant),
37
+ ...meltingStream(basePath, plant, time),
38
+ ...meltingRoute(basePath, plant)
39
+ ];
40
+ return routes(routeList);
41
+ }