@hotmeshio/hotmesh 0.0.1

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 (263) hide show
  1. package/LICENSE +214 -0
  2. package/README.md +241 -0
  3. package/build/index.d.ts +4 -0
  4. package/build/index.js +7 -0
  5. package/build/modules/errors.d.ts +28 -0
  6. package/build/modules/errors.js +50 -0
  7. package/build/modules/key.d.ts +75 -0
  8. package/build/modules/key.js +116 -0
  9. package/build/modules/utils.d.ts +34 -0
  10. package/build/modules/utils.js +173 -0
  11. package/build/package.json +73 -0
  12. package/build/services/activities/activity.d.ts +59 -0
  13. package/build/services/activities/activity.js +396 -0
  14. package/build/services/activities/await.d.ts +16 -0
  15. package/build/services/activities/await.js +143 -0
  16. package/build/services/activities/emit.d.ts +9 -0
  17. package/build/services/activities/emit.js +13 -0
  18. package/build/services/activities/index.d.ts +15 -0
  19. package/build/services/activities/index.js +16 -0
  20. package/build/services/activities/iterate.d.ts +9 -0
  21. package/build/services/activities/iterate.js +13 -0
  22. package/build/services/activities/trigger.d.ts +22 -0
  23. package/build/services/activities/trigger.js +161 -0
  24. package/build/services/activities/worker.d.ts +17 -0
  25. package/build/services/activities/worker.js +164 -0
  26. package/build/services/collator/index.d.ts +54 -0
  27. package/build/services/collator/index.js +171 -0
  28. package/build/services/compiler/deployer.d.ts +35 -0
  29. package/build/services/compiler/deployer.js +412 -0
  30. package/build/services/compiler/index.d.ts +30 -0
  31. package/build/services/compiler/index.js +111 -0
  32. package/build/services/compiler/validator.d.ts +32 -0
  33. package/build/services/compiler/validator.js +134 -0
  34. package/build/services/connector/clients/ioredis.d.ts +13 -0
  35. package/build/services/connector/clients/ioredis.js +50 -0
  36. package/build/services/connector/clients/redis.d.ts +13 -0
  37. package/build/services/connector/clients/redis.js +62 -0
  38. package/build/services/connector/index.d.ts +5 -0
  39. package/build/services/connector/index.js +31 -0
  40. package/build/services/dimension/index.d.ts +29 -0
  41. package/build/services/dimension/index.js +35 -0
  42. package/build/services/durable/asyncLocalStorage.d.ts +3 -0
  43. package/build/services/durable/asyncLocalStorage.js +5 -0
  44. package/build/services/durable/client.d.ts +15 -0
  45. package/build/services/durable/client.js +108 -0
  46. package/build/services/durable/connection.d.ts +4 -0
  47. package/build/services/durable/connection.js +51 -0
  48. package/build/services/durable/factory.d.ts +3 -0
  49. package/build/services/durable/factory.js +123 -0
  50. package/build/services/durable/handle.d.ts +8 -0
  51. package/build/services/durable/handle.js +38 -0
  52. package/build/services/durable/index.d.ts +57 -0
  53. package/build/services/durable/index.js +58 -0
  54. package/build/services/durable/native.d.ts +4 -0
  55. package/build/services/durable/native.js +47 -0
  56. package/build/services/durable/worker.d.ts +36 -0
  57. package/build/services/durable/worker.js +266 -0
  58. package/build/services/durable/workflow.d.ts +6 -0
  59. package/build/services/durable/workflow.js +135 -0
  60. package/build/services/engine/index.d.ts +82 -0
  61. package/build/services/engine/index.js +511 -0
  62. package/build/services/hotmesh/index.d.ts +45 -0
  63. package/build/services/hotmesh/index.js +134 -0
  64. package/build/services/logger/index.d.ts +17 -0
  65. package/build/services/logger/index.js +73 -0
  66. package/build/services/mapper/index.d.ts +24 -0
  67. package/build/services/mapper/index.js +72 -0
  68. package/build/services/pipe/functions/array.d.ts +24 -0
  69. package/build/services/pipe/functions/array.js +69 -0
  70. package/build/services/pipe/functions/bitwise.d.ts +9 -0
  71. package/build/services/pipe/functions/bitwise.js +24 -0
  72. package/build/services/pipe/functions/conditional.d.ts +10 -0
  73. package/build/services/pipe/functions/conditional.js +27 -0
  74. package/build/services/pipe/functions/date.d.ts +57 -0
  75. package/build/services/pipe/functions/date.js +167 -0
  76. package/build/services/pipe/functions/index.d.ts +25 -0
  77. package/build/services/pipe/functions/index.js +26 -0
  78. package/build/services/pipe/functions/json.d.ts +5 -0
  79. package/build/services/pipe/functions/json.js +12 -0
  80. package/build/services/pipe/functions/math.d.ts +38 -0
  81. package/build/services/pipe/functions/math.js +111 -0
  82. package/build/services/pipe/functions/number.d.ts +25 -0
  83. package/build/services/pipe/functions/number.js +133 -0
  84. package/build/services/pipe/functions/object.d.ts +22 -0
  85. package/build/services/pipe/functions/object.js +63 -0
  86. package/build/services/pipe/functions/string.d.ts +23 -0
  87. package/build/services/pipe/functions/string.js +69 -0
  88. package/build/services/pipe/functions/symbol.d.ts +12 -0
  89. package/build/services/pipe/functions/symbol.js +33 -0
  90. package/build/services/pipe/functions/unary.d.ts +7 -0
  91. package/build/services/pipe/functions/unary.js +18 -0
  92. package/build/services/pipe/index.d.ts +30 -0
  93. package/build/services/pipe/index.js +128 -0
  94. package/build/services/quorum/index.d.ts +34 -0
  95. package/build/services/quorum/index.js +147 -0
  96. package/build/services/reporter/index.d.ts +47 -0
  97. package/build/services/reporter/index.js +330 -0
  98. package/build/services/serializer/index.d.ts +36 -0
  99. package/build/services/serializer/index.js +222 -0
  100. package/build/services/signaler/store.d.ts +15 -0
  101. package/build/services/signaler/store.js +53 -0
  102. package/build/services/signaler/stream.d.ts +43 -0
  103. package/build/services/signaler/stream.js +317 -0
  104. package/build/services/store/cache.d.ts +66 -0
  105. package/build/services/store/cache.js +127 -0
  106. package/build/services/store/clients/ioredis.d.ts +27 -0
  107. package/build/services/store/clients/ioredis.js +96 -0
  108. package/build/services/store/clients/redis.d.ts +29 -0
  109. package/build/services/store/clients/redis.js +143 -0
  110. package/build/services/store/index.d.ts +88 -0
  111. package/build/services/store/index.js +657 -0
  112. package/build/services/stream/clients/ioredis.d.ts +23 -0
  113. package/build/services/stream/clients/ioredis.js +115 -0
  114. package/build/services/stream/clients/redis.d.ts +23 -0
  115. package/build/services/stream/clients/redis.js +119 -0
  116. package/build/services/stream/index.d.ts +21 -0
  117. package/build/services/stream/index.js +9 -0
  118. package/build/services/sub/clients/ioredis.d.ts +20 -0
  119. package/build/services/sub/clients/ioredis.js +72 -0
  120. package/build/services/sub/clients/redis.d.ts +20 -0
  121. package/build/services/sub/clients/redis.js +63 -0
  122. package/build/services/sub/index.d.ts +18 -0
  123. package/build/services/sub/index.js +9 -0
  124. package/build/services/task/index.d.ts +18 -0
  125. package/build/services/task/index.js +73 -0
  126. package/build/services/telemetry/index.d.ts +49 -0
  127. package/build/services/telemetry/index.js +223 -0
  128. package/build/services/worker/index.d.ts +30 -0
  129. package/build/services/worker/index.js +105 -0
  130. package/build/types/activity.d.ts +86 -0
  131. package/build/types/activity.js +2 -0
  132. package/build/types/app.d.ts +16 -0
  133. package/build/types/app.js +2 -0
  134. package/build/types/async.d.ts +5 -0
  135. package/build/types/async.js +2 -0
  136. package/build/types/cache.d.ts +1 -0
  137. package/build/types/cache.js +2 -0
  138. package/build/types/collator.d.ts +8 -0
  139. package/build/types/collator.js +11 -0
  140. package/build/types/durable.d.ts +59 -0
  141. package/build/types/durable.js +2 -0
  142. package/build/types/hook.d.ts +31 -0
  143. package/build/types/hook.js +9 -0
  144. package/build/types/hotmesh.d.ts +82 -0
  145. package/build/types/hotmesh.js +2 -0
  146. package/build/types/index.d.ts +20 -0
  147. package/build/types/index.js +21 -0
  148. package/build/types/ioredisclient.d.ts +5 -0
  149. package/build/types/ioredisclient.js +5 -0
  150. package/build/types/job.d.ts +50 -0
  151. package/build/types/job.js +2 -0
  152. package/build/types/logger.d.ts +6 -0
  153. package/build/types/logger.js +2 -0
  154. package/build/types/map.d.ts +4 -0
  155. package/build/types/map.js +2 -0
  156. package/build/types/pipe.d.ts +4 -0
  157. package/build/types/pipe.js +2 -0
  158. package/build/types/quorum.d.ts +46 -0
  159. package/build/types/quorum.js +2 -0
  160. package/build/types/redis.d.ts +8 -0
  161. package/build/types/redis.js +2 -0
  162. package/build/types/redisclient.d.ts +25 -0
  163. package/build/types/redisclient.js +2 -0
  164. package/build/types/serializer.d.ts +33 -0
  165. package/build/types/serializer.js +2 -0
  166. package/build/types/stats.d.ts +83 -0
  167. package/build/types/stats.js +2 -0
  168. package/build/types/stream.d.ts +67 -0
  169. package/build/types/stream.js +25 -0
  170. package/build/types/telemetry.d.ts +1 -0
  171. package/build/types/telemetry.js +11 -0
  172. package/build/types/transition.d.ts +17 -0
  173. package/build/types/transition.js +2 -0
  174. package/index.ts +5 -0
  175. package/modules/errors.ts +55 -0
  176. package/modules/key.ts +129 -0
  177. package/modules/utils.ts +170 -0
  178. package/package.json +73 -0
  179. package/services/activities/activity.ts +473 -0
  180. package/services/activities/await.ts +172 -0
  181. package/services/activities/emit.ts +25 -0
  182. package/services/activities/index.ts +15 -0
  183. package/services/activities/iterate.ts +26 -0
  184. package/services/activities/trigger.ts +196 -0
  185. package/services/activities/worker.ts +190 -0
  186. package/services/collator/README.md +102 -0
  187. package/services/collator/index.ts +182 -0
  188. package/services/compiler/deployer.ts +432 -0
  189. package/services/compiler/index.ts +98 -0
  190. package/services/compiler/validator.ts +154 -0
  191. package/services/connector/clients/ioredis.ts +57 -0
  192. package/services/connector/clients/redis.ts +72 -0
  193. package/services/connector/index.ts +44 -0
  194. package/services/dimension/README.md +73 -0
  195. package/services/dimension/index.ts +39 -0
  196. package/services/durable/asyncLocalStorage.ts +3 -0
  197. package/services/durable/client.ts +116 -0
  198. package/services/durable/connection.ts +50 -0
  199. package/services/durable/factory.ts +124 -0
  200. package/services/durable/handle.ts +43 -0
  201. package/services/durable/index.ts +60 -0
  202. package/services/durable/native.ts +46 -0
  203. package/services/durable/worker.ts +254 -0
  204. package/services/durable/workflow.ts +136 -0
  205. package/services/engine/index.ts +615 -0
  206. package/services/hotmesh/index.ts +182 -0
  207. package/services/logger/index.ts +79 -0
  208. package/services/mapper/index.ts +84 -0
  209. package/services/pipe/functions/array.ts +87 -0
  210. package/services/pipe/functions/bitwise.ts +27 -0
  211. package/services/pipe/functions/conditional.ts +31 -0
  212. package/services/pipe/functions/date.ts +214 -0
  213. package/services/pipe/functions/index.ts +25 -0
  214. package/services/pipe/functions/json.ts +11 -0
  215. package/services/pipe/functions/math.ts +143 -0
  216. package/services/pipe/functions/number.ts +150 -0
  217. package/services/pipe/functions/object.ts +79 -0
  218. package/services/pipe/functions/string.ts +86 -0
  219. package/services/pipe/functions/symbol.ts +39 -0
  220. package/services/pipe/functions/unary.ts +19 -0
  221. package/services/pipe/index.ts +138 -0
  222. package/services/quorum/index.ts +200 -0
  223. package/services/reporter/index.ts +379 -0
  224. package/services/serializer/README.md +10 -0
  225. package/services/serializer/index.ts +243 -0
  226. package/services/signaler/store.ts +61 -0
  227. package/services/signaler/stream.ts +354 -0
  228. package/services/store/cache.ts +172 -0
  229. package/services/store/clients/ioredis.ts +123 -0
  230. package/services/store/clients/redis.ts +169 -0
  231. package/services/store/index.ts +757 -0
  232. package/services/stream/clients/ioredis.ts +148 -0
  233. package/services/stream/clients/redis.ts +144 -0
  234. package/services/stream/index.ts +57 -0
  235. package/services/sub/clients/ioredis.ts +83 -0
  236. package/services/sub/clients/redis.ts +74 -0
  237. package/services/sub/index.ts +25 -0
  238. package/services/task/index.ts +86 -0
  239. package/services/telemetry/index.ts +267 -0
  240. package/services/worker/index.ts +165 -0
  241. package/types/activity.ts +115 -0
  242. package/types/app.ts +20 -0
  243. package/types/async.ts +7 -0
  244. package/types/cache.ts +1 -0
  245. package/types/collator.ts +9 -0
  246. package/types/durable.ts +81 -0
  247. package/types/hook.ts +32 -0
  248. package/types/hotmesh.ts +102 -0
  249. package/types/index.ts +138 -0
  250. package/types/ioredisclient.ts +10 -0
  251. package/types/job.ts +59 -0
  252. package/types/logger.ts +6 -0
  253. package/types/map.ts +5 -0
  254. package/types/ms.d.ts +7 -0
  255. package/types/pipe.ts +7 -0
  256. package/types/quorum.ts +59 -0
  257. package/types/redis.ts +27 -0
  258. package/types/redisclient.ts +29 -0
  259. package/types/serializer.ts +38 -0
  260. package/types/stats.ts +100 -0
  261. package/types/stream.ts +75 -0
  262. package/types/telemetry.ts +15 -0
  263. package/types/transition.ts +20 -0
@@ -0,0 +1,412 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Deployer = void 0;
4
+ const key_1 = require("../../modules/key");
5
+ const utils_1 = require("../../modules/utils");
6
+ const collator_1 = require("../collator");
7
+ const serializer_1 = require("../serializer");
8
+ const DEFAULT_METADATA_RANGE_SIZE = 26; //metadata is 26 slots ([a-z] * 1)
9
+ const DEFAULT_DATA_RANGE_SIZE = 260; //data is 260 slots ([a-zA-Z] * 5)
10
+ const DEFAULT_RANGE_SIZE = DEFAULT_METADATA_RANGE_SIZE + DEFAULT_DATA_RANGE_SIZE;
11
+ class Deployer {
12
+ constructor(manifest) {
13
+ this.manifest = null;
14
+ this.manifest = manifest;
15
+ }
16
+ async deploy(store) {
17
+ this.store = store;
18
+ collator_1.CollatorService.compile(this.manifest.app.graphs);
19
+ this.convertTopicsToTypes();
20
+ this.copyJobSchemas();
21
+ this.bindBackRefs();
22
+ this.bindParents();
23
+ this.resolveMappingDependencies();
24
+ this.resolveJobMapsPaths();
25
+ await this.generateSymKeys();
26
+ await this.generateSymVals();
27
+ await this.deployHookPatterns();
28
+ await this.deployActivitySchemas();
29
+ await this.deploySubscriptions();
30
+ await this.deployTransitions();
31
+ await this.deployConsumerGroups();
32
+ }
33
+ getVID() {
34
+ return {
35
+ id: this.manifest.app.id,
36
+ version: this.manifest.app.version,
37
+ };
38
+ }
39
+ async generateSymKeys() {
40
+ //note: symbol ranges are additive (per version); path assignments are immutable
41
+ const appId = this.manifest.app.id;
42
+ for (const graph of this.manifest.app.graphs) {
43
+ //generate JOB symbols
44
+ const [, trigger] = this.findTrigger(graph);
45
+ const topic = trigger.subscribes;
46
+ const [lower, upper, symbols] = await this.store.reserveSymbolRange(`$${topic}`, DEFAULT_RANGE_SIZE, 'JOB');
47
+ const prefix = ''; //job meta/data is NOT namespaced
48
+ const newSymbols = this.bindSymbols(lower, upper, symbols, prefix, trigger.PRODUCES);
49
+ if (Object.keys(newSymbols).length) {
50
+ await this.store.addSymbols(`$${topic}`, newSymbols);
51
+ }
52
+ //generate ACTIVITY symbols
53
+ for (const [activityId, activity] of Object.entries(graph.activities)) {
54
+ const [lower, upper, symbols] = await this.store.reserveSymbolRange(activityId, DEFAULT_RANGE_SIZE, 'ACTIVITY');
55
+ const prefix = `${activityId}/`; //activity meta/data is namespaced
56
+ const newSymbols = this.bindSymbols(lower, upper, symbols, prefix, activity.produces);
57
+ if (Object.keys(newSymbols).length) {
58
+ await this.store.addSymbols(activityId, newSymbols);
59
+ }
60
+ }
61
+ }
62
+ }
63
+ bindSymbols(startIndex, maxIndex, existingSymbols, prefix, produces) {
64
+ let newSymbols = {};
65
+ let currentSymbols = { ...existingSymbols };
66
+ for (let path of produces) {
67
+ const fullPath = `${prefix}${path}`;
68
+ if (!currentSymbols[fullPath]) {
69
+ if (startIndex > maxIndex) {
70
+ throw new Error('Symbol index out of bounds');
71
+ }
72
+ const symbol = (0, utils_1.getSymKey)(startIndex);
73
+ startIndex++;
74
+ newSymbols[fullPath] = symbol;
75
+ currentSymbols[fullPath] = symbol; // update the currentSymbols to include this new symbol
76
+ }
77
+ }
78
+ return newSymbols;
79
+ }
80
+ copyJobSchemas() {
81
+ const graphs = this.manifest.app.graphs;
82
+ for (const graph of graphs) {
83
+ const jobSchema = graph.output?.schema;
84
+ const outputSchema = graph.input?.schema;
85
+ if (!jobSchema && !outputSchema)
86
+ continue;
87
+ const activities = graph.activities;
88
+ // Find the trigger activity and bind the job schema to it
89
+ // at execution time, the trigger is a standin for the job
90
+ for (const activityKey in activities) {
91
+ if (activities[activityKey].type === 'trigger') {
92
+ const trigger = activities[activityKey];
93
+ if (jobSchema) {
94
+ //possible for trigger to have job mappings
95
+ if (!trigger.job) {
96
+ trigger.job = {};
97
+ }
98
+ trigger.job.schema = jobSchema;
99
+ }
100
+ if (outputSchema) {
101
+ //impossible for trigger to have output mappings.
102
+ trigger.output = { schema: outputSchema };
103
+ }
104
+ }
105
+ }
106
+ }
107
+ }
108
+ bindBackRefs() {
109
+ for (const graph of this.manifest.app.graphs) {
110
+ const activities = graph.activities;
111
+ const triggerId = this.findTrigger(graph)[0];
112
+ for (const activityKey in activities) {
113
+ activities[activityKey].trigger = triggerId;
114
+ activities[activityKey].subscribes = graph.subscribes;
115
+ if (graph.publishes) {
116
+ activities[activityKey].publishes = graph.publishes;
117
+ }
118
+ if (graph.expire) {
119
+ activities[activityKey].expire = graph.expire;
120
+ }
121
+ }
122
+ }
123
+ }
124
+ //more intuitive for SDK users to use 'topic',
125
+ //but the compiler is desiged to be generic and uses 'subtypes'
126
+ convertTopicsToTypes() {
127
+ for (const graph of this.manifest.app.graphs) {
128
+ const activities = graph.activities;
129
+ for (const activityKey in activities) {
130
+ const activity = activities[activityKey];
131
+ if (['worker', 'await'].includes(activity.type) && activity.topic && !activity.subtype) {
132
+ activity.subtype = activity.topic;
133
+ }
134
+ }
135
+ }
136
+ }
137
+ async bindParents() {
138
+ const graphs = this.manifest.app.graphs;
139
+ for (const graph of graphs) {
140
+ if (graph.transitions) {
141
+ for (const fromActivity in graph.transitions) {
142
+ const toTransitions = graph.transitions[fromActivity];
143
+ for (const transition of toTransitions) {
144
+ const to = transition.to;
145
+ //DAGs have one parent; easy to optimize for
146
+ graph.activities[to].parent = fromActivity;
147
+ }
148
+ }
149
+ }
150
+ }
151
+ }
152
+ collectValues(schema, values) {
153
+ for (const [key, value] of Object.entries(schema)) {
154
+ if (key === 'enum' || key === 'examples' || key === 'default') {
155
+ if (Array.isArray(value)) {
156
+ for (const v of value) {
157
+ if (typeof v === 'string' && v.length > 5) {
158
+ values.add(v);
159
+ }
160
+ }
161
+ }
162
+ else if (typeof value === 'string' && value.length > 5) {
163
+ values.add(value);
164
+ }
165
+ }
166
+ else if (typeof value === 'object') {
167
+ this.collectValues(value, values);
168
+ }
169
+ }
170
+ }
171
+ traverse(obj, values) {
172
+ for (const value of Object.values(obj)) {
173
+ if (typeof value === 'object') {
174
+ if ('schema' in value) {
175
+ this.collectValues(value.schema, values);
176
+ }
177
+ else {
178
+ this.traverse(value, values);
179
+ }
180
+ }
181
+ }
182
+ }
183
+ async generateSymVals() {
184
+ const uniqueStrings = new Set();
185
+ for (const graph of this.manifest.app.graphs) {
186
+ this.traverse(graph, uniqueStrings);
187
+ }
188
+ const existingSymbols = await this.store.getSymbolValues();
189
+ const startIndex = Object.keys(existingSymbols).length;
190
+ const maxIndex = Math.pow(52, 2) - 1;
191
+ const newSymbols = serializer_1.SerializerService.filterSymVals(startIndex, maxIndex, existingSymbols, uniqueStrings);
192
+ await this.store.addSymbolValues(newSymbols);
193
+ }
194
+ resolveJobMapsPaths() {
195
+ function parsePaths(obj) {
196
+ let result = [];
197
+ function traverse(obj, path = []) {
198
+ for (let key in obj) {
199
+ if (typeof obj[key] === 'object' && obj[key] !== null && !('@pipe' in obj[key])) {
200
+ let newPath = [...path, key];
201
+ traverse(obj[key], newPath);
202
+ }
203
+ else {
204
+ const finalPath = `data/${[...path, key].join('/')}`;
205
+ if (!result.includes(finalPath)) {
206
+ result.push(finalPath);
207
+ }
208
+ }
209
+ }
210
+ }
211
+ if (obj) {
212
+ traverse(obj);
213
+ }
214
+ return result;
215
+ }
216
+ for (const graph of this.manifest.app.graphs) {
217
+ let results = [];
218
+ const [, trigger] = this.findTrigger(graph);
219
+ for (const activityKey in graph.activities) {
220
+ const activity = graph.activities[activityKey];
221
+ results = results.concat(parsePaths(activity.job?.maps));
222
+ }
223
+ trigger.PRODUCES = results;
224
+ }
225
+ }
226
+ resolveMappingDependencies() {
227
+ const dynamicMappingRules = [];
228
+ //recursive function to descend into the object and find all dynamic mapping rules
229
+ function traverse(obj, consumes) {
230
+ for (const key in obj) {
231
+ if (typeof obj[key] === 'string') {
232
+ const stringValue = obj[key];
233
+ const dynamicMappingRuleMatch = stringValue.match(/^\{[^@].*}$/);
234
+ if (dynamicMappingRuleMatch) {
235
+ if (stringValue.split('.')[1] !== 'input') {
236
+ dynamicMappingRules.push(stringValue);
237
+ consumes.push(stringValue);
238
+ }
239
+ }
240
+ }
241
+ else if (typeof obj[key] === 'object' && obj[key] !== null) {
242
+ traverse(obj[key], consumes);
243
+ }
244
+ }
245
+ }
246
+ const graphs = this.manifest.app.graphs;
247
+ for (const graph of graphs) {
248
+ const activities = graph.activities;
249
+ for (const activityId in activities) {
250
+ const activity = activities[activityId];
251
+ activity.consumes = [];
252
+ traverse(activity, activity.consumes);
253
+ activity.consumes = this.groupMappingRules(activity.consumes);
254
+ }
255
+ }
256
+ const groupedRules = this.groupMappingRules(dynamicMappingRules);
257
+ // Iterate through the graph and add 'produces' field to each activity
258
+ for (const graph of graphs) {
259
+ const activities = graph.activities;
260
+ for (const activityId in activities) {
261
+ const activity = activities[activityId];
262
+ activity.produces = groupedRules[`${activityId}`] || [];
263
+ }
264
+ }
265
+ }
266
+ groupMappingRules(rules) {
267
+ rules = Array.from(new Set(rules)).sort();
268
+ // Group by the first symbol before the period (this is the activity name)
269
+ const groupedRules = {};
270
+ for (const rule of rules) {
271
+ const [group, resolved] = this.resolveMappableValue(rule);
272
+ if (!groupedRules[group]) {
273
+ groupedRules[group] = [];
274
+ }
275
+ groupedRules[group].push(resolved);
276
+ }
277
+ return groupedRules;
278
+ }
279
+ resolveMappableValue(mappable) {
280
+ mappable = mappable.substring(1, mappable.length - 1);
281
+ const parts = mappable.split('.');
282
+ if (parts[0] === '$job') {
283
+ const [group, ...path] = parts;
284
+ return [group, path.join('/')];
285
+ }
286
+ else {
287
+ //normalize paths to be relative to the activity
288
+ const [group, type, subtype, ...path] = parts;
289
+ const prefix = {
290
+ hook: 'hook/data',
291
+ input: 'input/data',
292
+ output: subtype === 'data' ? 'output/data' : 'output/metadata'
293
+ }[type];
294
+ return [group, `${prefix}/${path.join('/')}`];
295
+ }
296
+ }
297
+ async deployActivitySchemas() {
298
+ const graphs = this.manifest.app.graphs;
299
+ const activitySchemas = {};
300
+ for (const graph of graphs) {
301
+ const activities = graph.activities;
302
+ for (const activityKey in activities) {
303
+ activitySchemas[activityKey] = activities[activityKey];
304
+ }
305
+ }
306
+ await this.store.setSchemas(activitySchemas, this.getVID());
307
+ }
308
+ async deploySubscriptions() {
309
+ const graphs = this.manifest.app.graphs;
310
+ const publicSubscriptions = {};
311
+ for (const graph of graphs) {
312
+ const activities = graph.activities;
313
+ const subscribesTopic = graph.subscribes;
314
+ // Find the activity ID associated with the subscribes topic
315
+ for (const activityKey in activities) {
316
+ if (activities[activityKey].type === 'trigger') {
317
+ publicSubscriptions[subscribesTopic] = activityKey;
318
+ break;
319
+ }
320
+ }
321
+ }
322
+ await this.store.setSubscriptions(publicSubscriptions, this.getVID());
323
+ }
324
+ findTrigger(graph) {
325
+ for (const activityKey in graph.activities) {
326
+ const activity = graph.activities[activityKey];
327
+ if (activity.type === 'trigger') {
328
+ return [activityKey, activity];
329
+ }
330
+ }
331
+ return null;
332
+ }
333
+ async deployTransitions() {
334
+ const graphs = this.manifest.app.graphs;
335
+ const privateSubscriptions = {};
336
+ for (const graph of graphs) {
337
+ if (graph.subscribes && graph.subscribes.startsWith('.')) {
338
+ const [triggerId] = this.findTrigger(graph);
339
+ if (triggerId) {
340
+ privateSubscriptions[graph.subscribes] = { [triggerId]: true };
341
+ }
342
+ }
343
+ if (graph.transitions) {
344
+ for (const fromActivity in graph.transitions) {
345
+ const toTransitions = graph.transitions[fromActivity];
346
+ const toValues = {};
347
+ for (const transition of toTransitions) {
348
+ const to = transition.to;
349
+ if (transition.conditions) {
350
+ toValues[to] = transition.conditions;
351
+ }
352
+ else {
353
+ toValues[to] = true;
354
+ }
355
+ }
356
+ if (Object.keys(toValues).length > 0) {
357
+ privateSubscriptions['.' + fromActivity] = toValues;
358
+ }
359
+ }
360
+ }
361
+ }
362
+ await this.store.setTransitions(privateSubscriptions, this.getVID());
363
+ }
364
+ async deployHookPatterns() {
365
+ const graphs = this.manifest.app.graphs;
366
+ const hookRules = {};
367
+ for (const graph of graphs) {
368
+ if (graph.hooks) {
369
+ for (const topic in graph.hooks) {
370
+ hookRules[topic] = graph.hooks[topic];
371
+ const activityId = graph.hooks[topic][0].to;
372
+ const targetActivity = graph.activities[activityId];
373
+ if (targetActivity) {
374
+ if (!targetActivity.hook) {
375
+ targetActivity.hook = {};
376
+ }
377
+ //create back-reference to the hook topic
378
+ targetActivity.hook.topic = topic;
379
+ }
380
+ }
381
+ }
382
+ }
383
+ await this.store.setHookRules(hookRules);
384
+ }
385
+ async deployConsumerGroups() {
386
+ //create one engine group
387
+ const params = { appId: this.manifest.app.id };
388
+ const key = this.store.mintKey(key_1.KeyType.STREAMS, params);
389
+ await this.deployConsumerGroup(key, 'ENGINE');
390
+ for (const graph of this.manifest.app.graphs) {
391
+ const activities = graph.activities;
392
+ for (const activityKey in activities) {
393
+ const activity = activities[activityKey];
394
+ if (activity.type === 'worker') {
395
+ params.topic = activity.subtype;
396
+ const key = this.store.mintKey(key_1.KeyType.STREAMS, params);
397
+ //create one worker group per unique activity subtype (the topic)
398
+ await this.deployConsumerGroup(key, 'WORKER');
399
+ }
400
+ }
401
+ }
402
+ }
403
+ async deployConsumerGroup(stream, group) {
404
+ try {
405
+ await this.store.xgroup('CREATE', stream, group, '$', 'MKSTREAM');
406
+ }
407
+ catch (err) {
408
+ this.store.logger.info('consumer-group-exists', { stream, group });
409
+ }
410
+ }
411
+ }
412
+ exports.Deployer = Deployer;
@@ -0,0 +1,30 @@
1
+ import { ILogger } from '../logger';
2
+ import { StoreService } from '../store';
3
+ import { HotMeshManifest } from '../../types/hotmesh';
4
+ import { RedisClient, RedisMulti } from '../../types/redis';
5
+ /**
6
+ * The compiler service converts a graph into a executable program.
7
+ */
8
+ declare class CompilerService {
9
+ store: StoreService<RedisClient, RedisMulti> | null;
10
+ logger: ILogger;
11
+ constructor(store: StoreService<RedisClient, RedisMulti>, logger: ILogger);
12
+ /**
13
+ * verifies and plans the deployment of an app to Redis; the app is not deployed yet
14
+ * @param path
15
+ */
16
+ plan(mySchemaOrPath: string): Promise<HotMeshManifest>;
17
+ isPath(input: string): boolean;
18
+ /**
19
+ * deploys an app to Redis but does NOT activate it.
20
+ */
21
+ deploy(mySchemaOrPath: string): Promise<HotMeshManifest>;
22
+ /**
23
+ * activates a deployed version of an app;
24
+ * @param appId
25
+ * @param appVersion
26
+ */
27
+ activate(appId: string, appVersion: string): Promise<boolean>;
28
+ saveAsJSON(originalPath: string, schema: HotMeshManifest): Promise<void>;
29
+ }
30
+ export { CompilerService };
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.CompilerService = void 0;
30
+ const json_schema_ref_parser_1 = __importDefault(require("@apidevtools/json-schema-ref-parser"));
31
+ const js_yaml_1 = __importDefault(require("js-yaml"));
32
+ const fs = __importStar(require("fs/promises"));
33
+ const path = __importStar(require("path"));
34
+ const deployer_1 = require("./deployer");
35
+ const validator_1 = require("./validator");
36
+ /**
37
+ * The compiler service converts a graph into a executable program.
38
+ */
39
+ class CompilerService {
40
+ constructor(store, logger) {
41
+ this.store = store;
42
+ this.logger = logger;
43
+ }
44
+ /**
45
+ * verifies and plans the deployment of an app to Redis; the app is not deployed yet
46
+ * @param path
47
+ */
48
+ async plan(mySchemaOrPath) {
49
+ try {
50
+ let schema;
51
+ if (this.isPath(mySchemaOrPath)) {
52
+ schema = await json_schema_ref_parser_1.default.dereference(mySchemaOrPath);
53
+ }
54
+ else {
55
+ schema = js_yaml_1.default.load(mySchemaOrPath);
56
+ }
57
+ // 1) validate the manifest file
58
+ const validator = new validator_1.Validator(schema);
59
+ validator.validate(this.store);
60
+ // 2) todo: add a PlannerService module that will plan the deployment (what might break, drift, etc)
61
+ return schema;
62
+ }
63
+ catch (err) {
64
+ this.logger.error('compiler-plan-error', err);
65
+ }
66
+ }
67
+ isPath(input) {
68
+ return !input.trim().startsWith('app:');
69
+ }
70
+ /**
71
+ * deploys an app to Redis but does NOT activate it.
72
+ */
73
+ async deploy(mySchemaOrPath) {
74
+ try {
75
+ let schema;
76
+ if (this.isPath(mySchemaOrPath)) {
77
+ schema = await json_schema_ref_parser_1.default.dereference(mySchemaOrPath);
78
+ await this.saveAsJSON(mySchemaOrPath, schema);
79
+ }
80
+ else {
81
+ schema = js_yaml_1.default.load(mySchemaOrPath);
82
+ }
83
+ // 2) validate the manifest file (synchronous operation...no callbacks)
84
+ const validator = new validator_1.Validator(schema);
85
+ validator.validate(this.store);
86
+ // 3) deploy the schema (segment, optimize, etc; save to Redis)
87
+ const deployer = new deployer_1.Deployer(schema);
88
+ await deployer.deploy(this.store);
89
+ // 4) save the app version to Redis (so it can be activated later)
90
+ await this.store.setApp(schema.app.id, schema.app.version);
91
+ return schema;
92
+ }
93
+ catch (err) {
94
+ this.logger.error('compiler-deploy-error', err);
95
+ }
96
+ }
97
+ /**
98
+ * activates a deployed version of an app;
99
+ * @param appId
100
+ * @param appVersion
101
+ */
102
+ async activate(appId, appVersion) {
103
+ return await this.store.activateAppVersion(appId, appVersion);
104
+ }
105
+ async saveAsJSON(originalPath, schema) {
106
+ const json = JSON.stringify(schema, null, 2);
107
+ const newPath = path.join(path.dirname(originalPath), `.hotmesh.${schema.app.id}.${schema.app.version}.json`);
108
+ await fs.writeFile(newPath, json, 'utf8');
109
+ }
110
+ }
111
+ exports.CompilerService = CompilerService;
@@ -0,0 +1,32 @@
1
+ import { StoreService } from '../store';
2
+ import { MappingStatements } from "../../types/map";
3
+ import { HotMeshManifest } from "../../types/hotmesh";
4
+ import { RedisClient, RedisMulti } from "../../types/redis";
5
+ declare class Validator {
6
+ manifest: HotMeshManifest | null;
7
+ activityIds: string[];
8
+ mappingStatements: MappingStatements;
9
+ store: StoreService<RedisClient, RedisMulti> | null;
10
+ static SYS_VARS: string[];
11
+ constructor(manifest: HotMeshManifest);
12
+ /**
13
+ * validate the manifest file
14
+ */
15
+ validate(store: StoreService<RedisClient, RedisMulti>): Promise<void>;
16
+ validateActivityIds(): void;
17
+ isMappingStatement(value: string): boolean;
18
+ extractMappingStatements(obj: any, result: MappingStatements, currentActivityId: string): void;
19
+ getMappingStatements(): void;
20
+ validateReferencedActivityIds(): void;
21
+ isFunction(value: string): boolean;
22
+ validateMappingStatements(): void;
23
+ validateTransitions(): void;
24
+ validateTransitionConditions(): void;
25
+ validateStats(): void;
26
+ validateSchemas(): void;
27
+ validateUniqueHandledTopics(): void;
28
+ validateGraphPublishSubscribe(): void;
29
+ validateHooks(): void;
30
+ validateConditionalStatements(): void;
31
+ }
32
+ export { Validator };