@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,172 @@
1
+ /**
2
+ * The Cache is a key/value store and used to store commonly accessed Redis metadata
3
+ * (mainly the execution rules for the app) to save time accessing them as they
4
+ * are immutable per verison. The only time the rules are ejected are when
5
+ * a new version is deployed to the quorum and the cache is invalidated/cleared.
6
+ */
7
+
8
+ import { ActivityType } from "../../types/activity";
9
+ import { HookRule } from "../../types/hook";
10
+ import { HotMeshApp, HotMeshSettings } from "../../types/hotmesh";
11
+ import { Symbols } from "../../types/serializer";
12
+ import { Transitions } from "../../types/transition";
13
+
14
+ class Cache {
15
+ settings: HotMeshSettings;
16
+ appId: string;
17
+ apps: Record<string, HotMeshApp>;
18
+ schemas: Record<string, ActivityType>;
19
+ subscriptions: Record<string, Record<string, string>>;
20
+ symbols: Record<string, Symbols>;
21
+ symvals: Record<string, Symbols>;
22
+ transitions: Record<string, Record<string, unknown>>;
23
+ hookRules: Record<string, Record<string, HookRule[]>>;
24
+ workItems: Record<string, string>;
25
+
26
+ /**
27
+ * The cache is ALWAYS initialized with HotMeshSettings. The other parameters are optional.
28
+ * @param settings
29
+ * @param apps
30
+ * @param schemas
31
+ * @param subscriptions
32
+ * @param transitions
33
+ * @param hookRules
34
+ */
35
+ constructor(appId: string, settings: HotMeshSettings, apps: Record<string, HotMeshApp> = {}, schemas: Record<string, ActivityType> = {}, subscriptions: Record<string, Record<string, string>> = {}, symbols: Record<string, Symbols> = {}, symvals: Record<string, Symbols> = {}, transitions: Record<string, Record<string, unknown>> = {}, hookRules: Record<string, Record<string, HookRule[]>> = {}, workItems: Record<string, string> = {}) {
36
+ this.appId = appId;
37
+ this.settings = settings;
38
+ this.apps = apps;
39
+ this.schemas = schemas;
40
+ this.subscriptions = subscriptions;
41
+ this.symbols = symbols;
42
+ this.symvals = symvals;
43
+ this.transitions = transitions;
44
+ this.hookRules = hookRules;
45
+ this.workItems = workItems;
46
+ }
47
+
48
+ /**
49
+ * invalidate the cache; settings are not invalidated!
50
+ */
51
+ invalidate(): void {
52
+ this.apps = {} as Record<string, HotMeshApp>;
53
+ this.schemas = {};
54
+ this.subscriptions = {};
55
+ this.transitions = {};
56
+ this.hookRules = {};
57
+ }
58
+
59
+ getSettings(): HotMeshSettings {
60
+ return this.settings;
61
+ }
62
+
63
+ setSettings(settings: HotMeshSettings): void {
64
+ this.settings = settings;
65
+ }
66
+
67
+ getApps(): Record<string, HotMeshApp> {
68
+ return this.apps;
69
+ }
70
+
71
+ getApp(appId: string): HotMeshApp {
72
+ return this.apps[appId] as HotMeshApp;
73
+ }
74
+
75
+ setApps(apps: Record<string, HotMeshApp>): void {
76
+ this.apps = apps;
77
+ }
78
+
79
+ setApp(appId: string, app: HotMeshApp): void {
80
+ this.apps[appId] = app;
81
+ }
82
+
83
+ getSchemas(appId: string, version: string): Record<string, ActivityType> {
84
+ return this.schemas[`${appId}/${version}`] as unknown as Record<string, ActivityType>;
85
+ }
86
+
87
+ getSchema(appId: string, version: string, activityId: string): ActivityType {
88
+ return this.schemas?.[`${appId}/${version}`]?.[activityId] as ActivityType;
89
+ }
90
+
91
+ setSchemas(appId: string, version: string, schemas: Record<string, ActivityType>): void {
92
+ this.schemas[`${appId}/${version}`] = schemas as unknown as Record<string, ActivityType>;
93
+ }
94
+
95
+ setSchema(appId: string, version: string, activityId: string, schema: ActivityType): void {
96
+ this.schemas[`${appId}/${version}`][activityId] = schema;
97
+ }
98
+
99
+ getSubscriptions(appId: string, version: string): Record<string, string> {
100
+ return this.subscriptions[`${appId}/${version}`];
101
+ }
102
+
103
+ getSubscription(appId: string, version: string, topic: string): unknown {
104
+ return this.subscriptions?.[`${appId}/${version}`]?.[topic];
105
+ }
106
+
107
+ setSubscriptions(appId: string, version: string, subscriptions: Record<string, string>): void {
108
+ this.subscriptions[`${appId}/${version}`] = subscriptions;
109
+ }
110
+
111
+ getSymbols(appId: string, targetEntityId: string): Symbols {
112
+ return this.symbols[`${appId}/${targetEntityId}`] as Symbols;
113
+ }
114
+
115
+ setSymbols(appId: string, targetEntityId: string, symbols: Symbols): void {
116
+ this.symbols[`${appId}/${targetEntityId}`] = symbols;
117
+ }
118
+
119
+ deleteSymbols(appId: string, targetEntityId: string): void {
120
+ delete this.symbols[`${appId}/${targetEntityId}`];
121
+ }
122
+
123
+ getSymbolValues(appId: string): Symbols {
124
+ return this.symvals[`${appId}`] as Symbols;
125
+ }
126
+
127
+ setSymbolValues(appId: string, symvals: Symbols): void {
128
+ this.symvals[`${appId}`] = symvals;
129
+ }
130
+
131
+ deleteSymbolValues(appId: string): void {
132
+ delete this.symvals[`${appId}`];
133
+ }
134
+
135
+ getTransitions(appId: string, version: string): Transitions {
136
+ return this.transitions[`${appId}/${version}`] as Transitions;
137
+ }
138
+
139
+ setTransitions(appId: string, version: string, transitions: Transitions): void {
140
+ this.transitions[`${appId}/${version}`] = transitions;
141
+ }
142
+
143
+ getHookRules(appId: string): Record<string, HookRule[]> {
144
+ return this.hookRules[`${appId}`];
145
+ }
146
+
147
+ setHookRules(appId: string, hookRules: Record<string, HookRule[]>): void {
148
+ this.hookRules[`${appId}`] = hookRules;
149
+ }
150
+
151
+ getSignals(appId: string, version: string): Record<string, unknown> {
152
+ throw new Error("SIGNAL (getHooks) is not supported");
153
+ }
154
+
155
+ setSignals(appId: string, version: string): Record<string, unknown> {
156
+ throw new Error("SIGNAL (setHook) is not supported");
157
+ }
158
+
159
+ getActiveTaskQueue(appId: string): string {
160
+ return this.workItems[appId];
161
+ }
162
+
163
+ setWorkItem(appId: string, workItem: string): void {
164
+ this.workItems[appId] = workItem;
165
+ }
166
+
167
+ removeWorkItem(appId: string): void {
168
+ delete this.workItems[appId];
169
+ }
170
+ }
171
+
172
+ export { Cache };
@@ -0,0 +1,123 @@
1
+ import { KeyType } from '../../../modules/key';
2
+ import { ILogger } from '../../logger';
3
+ import { SerializerService as Serializer } from '../../serializer';
4
+ import { Cache } from '../cache';
5
+ import { StoreService } from '../index';
6
+ import { RedisClientType, RedisMultiType } from '../../../types/ioredisclient';
7
+ import { ReclaimedMessageType } from '../../../types/stream';
8
+
9
+ class IORedisStoreService extends StoreService<RedisClientType, RedisMultiType> {
10
+ redisClient: RedisClientType;
11
+ cache: Cache;
12
+ namespace: string;
13
+ appId: string;
14
+ logger: ILogger;
15
+ serializer: Serializer;
16
+
17
+ constructor(redisClient: RedisClientType) {
18
+ super(redisClient);
19
+ }
20
+
21
+ getMulti(): RedisMultiType {
22
+ return this.redisClient.multi();
23
+ }
24
+
25
+ hGetAllResult(result: any) {
26
+ //ioredis response signature is [null, {}] or [null, null]
27
+ return result[1];
28
+ }
29
+
30
+ async addTaskQueues(keys: string[]): Promise<void> {
31
+ const multi = this.redisClient.multi();
32
+ const zsetKey = this.mintKey(KeyType.WORK_ITEMS, { appId: this.appId });
33
+ for (const key of keys) {
34
+ multi.zadd(zsetKey, 'NX', Date.now(), key);
35
+ }
36
+ await multi.exec();
37
+ }
38
+
39
+ async publish(keyType: KeyType.QUORUM, message: Record<string, any>, appId: string, engineId?: string): Promise<boolean> {
40
+ const topic = this.mintKey(keyType, { appId, engineId });
41
+ const status: number = await this.redisClient.publish(topic, JSON.stringify(message));
42
+ return status === 1;
43
+ }
44
+
45
+ async xgroup(command: 'CREATE', key: string, groupName: string, id: string, mkStream?: 'MKSTREAM'): Promise<boolean> {
46
+ if (mkStream === 'MKSTREAM') {
47
+ try {
48
+ return (await this.redisClient.xgroup(command, key, groupName, id, mkStream)) === 'OK';
49
+ } catch (err) {
50
+ this.logger.info(`Consumer group not created with MKSTREAM for key: ${key} and group: ${groupName}`);
51
+ throw err;
52
+ }
53
+ } else {
54
+ try {
55
+ return (await this.redisClient.xgroup(command, key, groupName, id)) === 'OK';
56
+ } catch (err) {
57
+ this.logger.info(`Consumer group not created for key: ${key} and group: ${groupName}`);
58
+ throw err;
59
+ }
60
+ }
61
+ }
62
+
63
+ async xadd(key: string, id: string, messageId: string, messageValue: string, multi?: RedisMultiType): Promise<string | RedisMultiType> {
64
+ try {
65
+ return await (multi || this.redisClient).xadd(key, id, messageId, messageValue);
66
+ } catch (err) {
67
+ this.logger.error(`Error publishing 'xadd'; key: ${key}`, err);
68
+ throw err;
69
+ }
70
+ }
71
+
72
+ async xpending(
73
+ key: string,
74
+ group: string,
75
+ start?: string,
76
+ end?: string,
77
+ count?: number,
78
+ consumer?: string
79
+ ): Promise<[string, string, number, [string, number][]][] | [string, string, number, number] | unknown[]> {
80
+ try {
81
+ return await this.redisClient.xpending(key, group, start, end, count, consumer);
82
+ } catch (err) {
83
+ this.logger.error(`Error in retrieving pending messages for [stream ${key}], [group ${group}]`, err);
84
+ throw err;
85
+ }
86
+ }
87
+
88
+ async xclaim(
89
+ key: string,
90
+ group: string,
91
+ consumer: string,
92
+ minIdleTime: number,
93
+ id: string,
94
+ ...args: string[]
95
+ ): Promise<ReclaimedMessageType> {
96
+ try {
97
+ return await this.redisClient.xclaim(key, group, consumer, minIdleTime, id, ...args) as unknown as ReclaimedMessageType;
98
+ } catch (err) {
99
+ this.logger.error(`Error in claiming message with id: ${id} in group: ${group} for key: ${key}`, err);
100
+ throw err;
101
+ }
102
+ }
103
+
104
+ async xack(key: string, group: string, id: string, multi? : RedisMultiType): Promise<number|RedisMultiType> {
105
+ try {
106
+ return await (multi || this.redisClient).xack(key, group, id);
107
+ } catch (err) {
108
+ this.logger.error(`Error in acknowledging messages in group: ${group} for key: ${key}`, err);
109
+ throw err;
110
+ }
111
+ }
112
+
113
+ async xdel(key: string, id: string, multi? : RedisMultiType): Promise<number|RedisMultiType> {
114
+ try {
115
+ return await (multi || this.redisClient).xdel(key, id);
116
+ } catch (err) {
117
+ this.logger.error(`Error in deleting messages with id: ${id} for key: ${key}`, err);
118
+ throw err;
119
+ }
120
+ }
121
+ }
122
+
123
+ export { IORedisStoreService };
@@ -0,0 +1,169 @@
1
+ import { KeyType } from '../../../modules/key';
2
+ import { ILogger } from '../../logger';
3
+ import { SerializerService as Serializer } from '../../serializer';
4
+ import { Cache } from '../cache';
5
+ import { StoreService } from '../index';
6
+ import { RedisClientType, RedisMultiType } from '../../../types/redisclient';
7
+ import { ReclaimedMessageType } from '../../../types/stream';
8
+
9
+ class RedisStoreService extends StoreService<RedisClientType, RedisMultiType> {
10
+ redisClient: RedisClientType;
11
+ cache: Cache;
12
+ namespace: string;
13
+ appId: string;
14
+ logger: ILogger;
15
+ serializer: Serializer;
16
+ commands: Record<string, string>;
17
+
18
+ constructor(redisClient: RedisClientType) {
19
+ super(redisClient);
20
+ this.commands = {
21
+ setnx: 'SETNX',
22
+ del: 'DEL',
23
+ expire: 'EXPIRE',
24
+ hset: 'HSET',
25
+ hsetnx: 'HSETNX',
26
+ hincrby: 'HINCRBY',
27
+ hdel: 'HDEL',
28
+ hget: 'HGET',
29
+ hmget: 'HMGET',
30
+ hgetall: 'HGETALL',
31
+ hincrbyfloat: 'HINCRBYFLOAT',
32
+ zrange: 'ZRANGE',
33
+ zrangebyscore_withscores: 'ZRANGEBYSCORE_WITHSCORES',
34
+ zrangebyscore: 'ZRANGEBYSCORE',
35
+ zrem: 'ZREM',
36
+ zadd: 'ZADD',
37
+ lmove: 'LMOVE',
38
+ lrange: 'LRANGE',
39
+ llen: 'LLEN',
40
+ lpop: 'LPOP',
41
+ rename: 'RENAME',
42
+ rpush: 'RPUSH',
43
+ xack: 'XACK',
44
+ xdel: 'XDEL',
45
+ };
46
+ }
47
+
48
+ getMulti(): RedisMultiType {
49
+ const multi = this.redisClient.MULTI();
50
+ return multi as unknown as RedisMultiType;
51
+ }
52
+
53
+ async publish(keyType: KeyType.QUORUM, message: Record<string, any>, appId: string, engineId?: string): Promise<boolean> {
54
+ const topic = this.mintKey(keyType, { appId, engineId });
55
+ const status: number = await this.redisClient.publish(topic, JSON.stringify(message));
56
+ return this.isSuccessful(status);
57
+ }
58
+
59
+ async zAdd(key: string, score: number | string, value: string | number, redisMulti?: RedisMultiType): Promise<any> {
60
+ return await (redisMulti || this.redisClient)[this.commands.zadd](key, { score: score, value: value.toString() } as any);
61
+ }
62
+
63
+ async zRangeByScoreWithScores(key: string, score: number | string, value: string | number): Promise<string | null> {
64
+ const result = await this.redisClient[this.commands.zrangebyscore_withscores](key, score, value);
65
+ if (result?.length > 0) {
66
+ return result[0];
67
+ }
68
+ return null;
69
+ }
70
+
71
+ async zRangeByScore(key: string, score: number | string, value: string | number): Promise<string | null> {
72
+ const result = await this.redisClient[this.commands.zrangebyscore](key, score, value);
73
+ if (result?.length > 0) {
74
+ return result[0];
75
+ }
76
+ return null;
77
+ }
78
+
79
+ async xgroup(command: 'CREATE', key: string, groupName: string, id: string, mkStream?: 'MKSTREAM'): Promise<boolean> {
80
+ const args = mkStream === 'MKSTREAM' ? ['MKSTREAM'] : [];
81
+ try {
82
+ return (await this.redisClient.sendCommand(['XGROUP', 'CREATE', key, groupName, id, ...args])) === 1;
83
+ } catch (err) {
84
+ const streamType = mkStream === 'MKSTREAM' ? 'with MKSTREAM' : 'without MKSTREAM';
85
+ this.logger.warn(`x-group-error ${streamType} for key: ${key} and group: ${groupName}`, err);
86
+ throw err;
87
+ }
88
+ }
89
+
90
+ async xadd(key: string, id: string, ...args: any[]): Promise<string | RedisMultiType> {
91
+ let multi: RedisMultiType;
92
+ if (typeof args[args.length - 1] !== 'string') {
93
+ multi = args.pop() as RedisMultiType;
94
+ }
95
+ try {
96
+ return await (multi || this.redisClient).XADD(key, id, { [args[0]]: args[1] });
97
+ } catch (err) {
98
+ this.logger.error(`Error publishing 'xadd'; key: ${key}`, err);
99
+ throw err;
100
+ }
101
+ }
102
+
103
+ async xpending(
104
+ key: string,
105
+ group: string,
106
+ start?: string,
107
+ end?: string,
108
+ count?: number,
109
+ consumer?: string
110
+ ): Promise<[string, string, number, [string, number][]][] | [string, string, number, number]> {
111
+ try {
112
+ const args = [key, group];
113
+ if (start) args.push(start);
114
+ if (end) args.push(end);
115
+ if (count !== undefined) args.push(count.toString());
116
+ if (consumer) args.push(consumer);
117
+ return await this.redisClient.sendCommand(['XPENDING', ...args]);
118
+ } catch (err) {
119
+ this.logger.error(`Error in retrieving pending messages for group: ${group} in key: ${key}`, err);
120
+ throw err;
121
+ }
122
+ }
123
+
124
+ async xclaim(
125
+ key: string,
126
+ group: string,
127
+ consumer: string,
128
+ minIdleTime: number,
129
+ id: string,
130
+ ...args: string[]
131
+ ): Promise<ReclaimedMessageType> {
132
+ try {
133
+ return await this.redisClient.sendCommand(['XCLAIM', key, group, consumer, minIdleTime.toString(), id, ...args]) as unknown as ReclaimedMessageType;
134
+ } catch (err) {
135
+ this.logger.error(`Error in claiming message with id: ${id} in group: ${group} for key: ${key}`, err);
136
+ throw err;
137
+ }
138
+ }
139
+
140
+ async xack(key: string, group: string, id: string, multi? : RedisMultiType): Promise<number|RedisMultiType> {
141
+ try {
142
+ if (multi) {
143
+ multi[this.commands.xack](key, group, id);
144
+ return multi;
145
+ } else {
146
+ return await this.redisClient[this.commands.xack](key, group, id);
147
+ }
148
+ } catch (err) {
149
+ this.logger.error(`Error in acknowledging messages in group: ${group} for key: ${key}`, err);
150
+ throw err;
151
+ }
152
+ }
153
+
154
+ async xdel(key: string, id: string, multi? : RedisMultiType): Promise<number|RedisMultiType> {
155
+ try {
156
+ if (multi) {
157
+ multi[this.commands.xdel](key, id);
158
+ return multi;
159
+ } else {
160
+ return await this.redisClient[this.commands.xdel](key, id);
161
+ }
162
+ } catch (err) {
163
+ this.logger.error(`Error in deleting messages with ids: ${id} for key: ${key}`, err);
164
+ throw err;
165
+ }
166
+ }
167
+ }
168
+
169
+ export { RedisStoreService };