UNPKG

5.77 kBPlain TextView Raw
1import { readdirSync } from 'fs';
2import { getLogger } from './logging';
3import {
4 Action,
5 ActionDefinition,
6 isValidActionDefinition,
7} from './model/Action';
8import { ActionType } from './model/ActionType';
9import { MqttAction } from './model/MqttAction';
10import { MqttPublishAction } from './model/MqttPublishAction';
11import {
12 isRestActionDefinition,
13 RestAction,
14 RestActionDefinition,
15} from './model/RestAction';
16import { TimerAction } from './model/TimerAction';
17import {
18 isWebSocketActionDefinition,
19 WebSocketAction,
20 WebSocketActionDefinition,
21} from './model/WebSocketAction';
22import { loadYamlConfiguration, nameFromYamlConfig } from './yamlParsing';
23import {
24 isValidAMQPListenActionDefinition,
25 AMQPListenAction,
26 AMQPListenActionDefinition,
27} from './model/AMQPListenAction';
28import {
29 isValidNodeJSActionDefinition,
30 NodeJSAction,
31} from './model/NodeJSAction';
32
33const isTimerAction = (actionDef: ActionDefinition): boolean =>
34 actionDef && actionDef.type === ActionType[ActionType.TIMER];
35
36const isMqttAction = (actionDef: ActionDefinition): boolean =>
37 actionDef && actionDef.type === ActionType[ActionType.MQTT];
38
39const isMqttPublishAction = (actionDef: ActionDefinition): boolean =>
40 actionDef && actionDef.type === ActionType[ActionType.MQTT_PUBLISH];
41
42/* TODO */
43export const loadAllActions = (actionDir: string, envConfig: any): Action[] => {
44 const loadedActions: Action[] = [];
45
46 readdirSync(actionDir).forEach(file => {
47 const actionDef = loadYamlConfiguration(`${actionDir}/${file}`);
48
49 if (!isValidActionDefinition(actionDef)) {
50 getLogger().error(
51 `Invalid action definition: ${JSON.stringify(actionDef)}`,
52 );
53 return;
54 }
55
56 if (isRestActionDefinition(actionDef)) {
57 const host = getURL(actionDef, envConfig);
58 loadedActions.push(
59 new RestAction(
60 nameFromYamlConfig(file),
61 undefined,
62 actionDef,
63 host + actionDef.endpoint,
64 actionDef.service,
65 ),
66 );
67 } else if (isTimerAction(actionDef)) {
68 loadedActions.push(
69 new TimerAction(nameFromYamlConfig(file), undefined, actionDef),
70 );
71 } else if (isWebSocketActionDefinition(actionDef)) {
72 const host = getURL(actionDef, envConfig);
73 loadedActions.push(
74 new WebSocketAction(
75 nameFromYamlConfig(file),
76 undefined,
77 actionDef,
78 actionDef.service,
79 host + actionDef.endpoint,
80 ),
81 );
82 } else if (isMqttAction(actionDef)) {
83 loadedActions.push(
84 new MqttAction(nameFromYamlConfig(file), undefined, actionDef),
85 );
86 } else if (isMqttPublishAction(actionDef)) {
87 loadedActions.push(
88 new MqttPublishAction(
89 nameFromYamlConfig(file),
90 undefined,
91 actionDef,
92 ),
93 );
94 } else if (isValidAMQPListenActionDefinition(actionDef)) {
95 loadedActions.push(
96 new AMQPListenAction(
97 nameFromYamlConfig(file),
98 getURL(actionDef, envConfig),
99 actionDef,
100 ),
101 );
102 } else if (isValidNodeJSActionDefinition(actionDef)) {
103 loadedActions.push(
104 new NodeJSAction(nameFromYamlConfig(file), actionDef),
105 );
106 } else {
107 getLogger().error(
108 `Action definition ${nameFromYamlConfig(file)} of type ${
109 actionDef.type
110 } does not contain a valid action definition for that type.`,
111 );
112 }
113 });
114
115 return loadedActions;
116};
117
118/**
119 * Extracts the URL from the action definition if present or otherwise from the
120 * environment configuration. In case of REST actions, if the http protocol is
121 * not specified explicitly, https is assumed. In case of WebSocket actions, if
122 * the ws protocol is not specified explicitly, wss is assumed. In case of
123 * AMQPListenActions if the amqp protocol is not specified explicitly, amqps is
124 * assumed.
125 * @param actionDef The action definition
126 * @param envConfig The environment configuration
127 */
128const getURL = (
129 actionDef:
130 | AMQPListenActionDefinition
131 | RestActionDefinition
132 | WebSocketActionDefinition,
133 envConfig: any,
134): string => {
135 if (actionDef.type === 'REST') {
136 if (actionDef.service.startsWith('http')) {
137 return actionDef.service;
138 }
139 if (envConfig[actionDef.service].startsWith('http')) {
140 return envConfig[actionDef.service];
141 }
142 return `https://${envConfig[actionDef.service]}`;
143 }
144 if (actionDef.type === 'WEBSOCKET') {
145 if (actionDef.service.startsWith('ws')) {
146 return actionDef.service;
147 }
148 if (envConfig[actionDef.service].startsWith('ws')) {
149 return envConfig[actionDef.service];
150 }
151 return `wss://${envConfig[actionDef.service]}`;
152 }
153 if (actionDef.type === 'AMQP_LISTEN') {
154 if (actionDef.broker.startsWith('amqp')) {
155 return actionDef.broker;
156 }
157 if (envConfig[actionDef.broker].startsWith('amqp')) {
158 return envConfig[actionDef.broker];
159 }
160 return `amqps://${envConfig[actionDef.broker]}`;
161 }
162 throw new Error(
163 `Cannot get URL for action type ${
164 (actionDef as ActionDefinition).type
165 }`,
166 );
167};