@conduitplatform/grpc-sdk 0.16.0-alpha.20 → 0.16.0-alpha.21

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 (96) hide show
  1. package/dist/classes/ConduitAuthorizedResource.js +5 -6
  2. package/dist/classes/ConduitAuthorizedResource.js.map +1 -1
  3. package/dist/classes/ConduitModule.js +59 -169
  4. package/dist/classes/ConduitModule.js.map +1 -1
  5. package/dist/classes/ConduitSchema.js +3 -4
  6. package/dist/classes/ConduitSchema.js.map +1 -1
  7. package/dist/classes/Error.js +17 -40
  8. package/dist/classes/Error.js.map +1 -1
  9. package/dist/classes/HealthCheck.js +28 -96
  10. package/dist/classes/HealthCheck.js.map +1 -1
  11. package/dist/index.js +295 -468
  12. package/dist/index.js.map +1 -1
  13. package/dist/interceptors/grpcSignedToken.js +7 -79
  14. package/dist/interceptors/grpcSignedToken.js.map +1 -1
  15. package/dist/interceptors/moduleName.js +7 -79
  16. package/dist/interceptors/moduleName.js.map +1 -1
  17. package/dist/interfaces/ConduitRouteReturn.js +9 -18
  18. package/dist/interfaces/ConduitRouteReturn.js.map +1 -1
  19. package/dist/modules/admin/index.js +13 -31
  20. package/dist/modules/admin/index.js.map +1 -1
  21. package/dist/modules/authentication/index.js +30 -50
  22. package/dist/modules/authentication/index.js.map +1 -1
  23. package/dist/modules/authorization/index.d.ts +1 -0
  24. package/dist/modules/authorization/index.js +35 -50
  25. package/dist/modules/authorization/index.js.map +1 -1
  26. package/dist/modules/chat/index.js +16 -34
  27. package/dist/modules/chat/index.js.map +1 -1
  28. package/dist/modules/config/index.d.ts +10 -1
  29. package/dist/modules/config/index.js +82 -184
  30. package/dist/modules/config/index.js.map +1 -1
  31. package/dist/modules/core/index.js +8 -26
  32. package/dist/modules/core/index.js.map +1 -1
  33. package/dist/modules/database/index.js +139 -179
  34. package/dist/modules/database/index.js.map +1 -1
  35. package/dist/modules/email/index.js +15 -33
  36. package/dist/modules/email/index.js.map +1 -1
  37. package/dist/modules/pushNotifications/index.js +38 -56
  38. package/dist/modules/pushNotifications/index.js.map +1 -1
  39. package/dist/modules/router/index.js +15 -33
  40. package/dist/modules/router/index.js.map +1 -1
  41. package/dist/modules/sms/index.js +17 -35
  42. package/dist/modules/sms/index.js.map +1 -1
  43. package/dist/modules/storage/index.js +57 -77
  44. package/dist/modules/storage/index.js.map +1 -1
  45. package/dist/protoUtils/authentication.d.ts +1 -1
  46. package/dist/protoUtils/authentication.js +267 -235
  47. package/dist/protoUtils/authentication.js.map +1 -1
  48. package/dist/protoUtils/authorization.d.ts +36 -1
  49. package/dist/protoUtils/authorization.js +494 -448
  50. package/dist/protoUtils/authorization.js.map +1 -1
  51. package/dist/protoUtils/chat.d.ts +1 -1
  52. package/dist/protoUtils/chat.js +128 -165
  53. package/dist/protoUtils/chat.js.map +1 -1
  54. package/dist/protoUtils/core.d.ts +4 -1
  55. package/dist/protoUtils/core.js +641 -573
  56. package/dist/protoUtils/core.js.map +1 -1
  57. package/dist/protoUtils/database.d.ts +1 -1
  58. package/dist/protoUtils/database.js +766 -766
  59. package/dist/protoUtils/database.js.map +1 -1
  60. package/dist/protoUtils/email.d.ts +1 -1
  61. package/dist/protoUtils/email.js +144 -174
  62. package/dist/protoUtils/email.js.map +1 -1
  63. package/dist/protoUtils/google/protobuf/empty.d.ts +1 -1
  64. package/dist/protoUtils/google/protobuf/empty.js +14 -15
  65. package/dist/protoUtils/google/protobuf/empty.js.map +1 -1
  66. package/dist/protoUtils/grpc_health_check.d.ts +1 -1
  67. package/dist/protoUtils/grpc_health_check.js +36 -34
  68. package/dist/protoUtils/grpc_health_check.js.map +1 -1
  69. package/dist/protoUtils/module.d.ts +1 -1
  70. package/dist/protoUtils/module.js +285 -293
  71. package/dist/protoUtils/module.js.map +1 -1
  72. package/dist/protoUtils/push-notifications.d.ts +1 -1
  73. package/dist/protoUtils/push-notifications.js +226 -244
  74. package/dist/protoUtils/push-notifications.js.map +1 -1
  75. package/dist/protoUtils/router.d.ts +1 -1
  76. package/dist/protoUtils/router.js +312 -320
  77. package/dist/protoUtils/router.js.map +1 -1
  78. package/dist/protoUtils/sms.d.ts +1 -1
  79. package/dist/protoUtils/sms.js +119 -109
  80. package/dist/protoUtils/sms.js.map +1 -1
  81. package/dist/protoUtils/storage.d.ts +1 -1
  82. package/dist/protoUtils/storage.js +338 -259
  83. package/dist/protoUtils/storage.js.map +1 -1
  84. package/dist/types/index.js +6 -24
  85. package/dist/types/index.js.map +1 -1
  86. package/dist/utilities/EventBus.js +20 -37
  87. package/dist/utilities/EventBus.js.map +1 -1
  88. package/dist/utilities/RedisManager.js +11 -21
  89. package/dist/utilities/RedisManager.js.map +1 -1
  90. package/dist/utilities/StateManager.js +13 -14
  91. package/dist/utilities/StateManager.js.map +1 -1
  92. package/dist/utilities/env.js +4 -4
  93. package/dist/utilities/env.js.map +1 -1
  94. package/dist/utilities/sleep.js +1 -1
  95. package/dist/utilities/sleep.js.map +1 -1
  96. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
3
  if (k2 === undefined) k2 = k;
15
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -24,58 +13,20 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
24
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
25
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
26
15
  };
27
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
28
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
29
- return new (P || (P = Promise))(function (resolve, reject) {
30
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
31
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
32
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
33
- step((generator = generator.apply(thisArg, _arguments || [])).next());
34
- });
35
- };
36
- var __generator = (this && this.__generator) || function (thisArg, body) {
37
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
38
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
39
- function verb(n) { return function (v) { return step([n, v]); }; }
40
- function step(op) {
41
- if (f) throw new TypeError("Generator is already executing.");
42
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
43
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
44
- if (y = 0, t) op = [op[0] & 2, t.value];
45
- switch (op[0]) {
46
- case 0: case 1: t = op; break;
47
- case 4: _.label++; return { value: op[1], done: false };
48
- case 5: _.label++; y = op[1]; op = [0]; continue;
49
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
50
- default:
51
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
52
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
53
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
54
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
55
- if (t[2]) _.ops.pop();
56
- _.trys.pop(); continue;
57
- }
58
- op = body.call(thisArg, _);
59
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
60
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
61
- }
62
- };
63
16
  var __importDefault = (this && this.__importDefault) || function (mod) {
64
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
65
18
  };
66
19
  Object.defineProperty(exports, "__esModule", { value: true });
67
- var modules_1 = require("./modules");
68
- var crypto_1 = __importDefault(require("crypto"));
69
- var utilities_1 = require("./utilities");
70
- var classes_1 = require("./classes");
71
- var grpc_js_1 = require("@grpc/grpc-js");
72
- var protoUtils_1 = require("./protoUtils");
73
- var types_1 = require("./types");
74
- var fast_jwt_1 = require("fast-jwt");
75
- var ConduitGrpcSdk = /** @class */ (function () {
76
- function ConduitGrpcSdk(serverUrl, name, watchModules, logger, serviceHealthStatusGetter) {
77
- if (watchModules === void 0) { watchModules = true; }
78
- if (serviceHealthStatusGetter === void 0) { serviceHealthStatusGetter = function () { return types_1.HealthCheckStatus.SERVING; }; }
20
+ const modules_1 = require("./modules");
21
+ const crypto_1 = __importDefault(require("crypto"));
22
+ const utilities_1 = require("./utilities");
23
+ const classes_1 = require("./classes");
24
+ const grpc_js_1 = require("@grpc/grpc-js");
25
+ const protoUtils_1 = require("./protoUtils");
26
+ const types_1 = require("./types");
27
+ const fast_jwt_1 = require("fast-jwt");
28
+ class ConduitGrpcSdk {
29
+ constructor(serverUrl, name, watchModules = true, logger, serviceHealthStatusGetter = () => types_1.HealthCheckStatus.SERVING) {
79
30
  this._modules = {};
80
31
  this._availableModules = {
81
32
  router: modules_1.Router,
@@ -107,346 +58,240 @@ var ConduitGrpcSdk = /** @class */ (function () {
107
58
  this.instance = this.name.startsWith('module_')
108
59
  ? this.name.substring(8)
109
60
  : crypto_1.default.randomBytes(16).toString('hex');
110
- this.urlRemap = (0, utilities_1.getJsonEnv)('URL_REMAP', process.env.URL_REMAP, function (stringConf) { return ({
61
+ this.urlRemap = (0, utilities_1.getJsonEnv)('URL_REMAP', process.env.URL_REMAP, stringConf => ({
111
62
  '*': stringConf,
112
- }); });
63
+ }));
113
64
  this.serverUrl = serverUrl;
114
65
  this._watchModules = watchModules;
115
66
  this._serviceHealthStatusGetter = serviceHealthStatusGetter;
116
67
  if (process.env.GRPC_KEY) {
117
- var sign = (0, fast_jwt_1.createSigner)({ key: process.env.GRPC_KEY });
68
+ const sign = (0, fast_jwt_1.createSigner)({ key: process.env.GRPC_KEY });
118
69
  this._grpcToken = sign({
119
70
  moduleName: this.name,
120
71
  });
121
72
  }
122
73
  }
123
- Object.defineProperty(ConduitGrpcSdk, "Logger", {
124
- get: function () {
125
- return ConduitGrpcSdk._Logger;
126
- },
127
- enumerable: false,
128
- configurable: true
129
- });
130
- Object.defineProperty(ConduitGrpcSdk, "Metrics", {
131
- get: function () {
132
- return ConduitGrpcSdk._Metrics;
133
- },
134
- set: function (metrics) {
135
- ConduitGrpcSdk._Metrics = metrics;
136
- },
137
- enumerable: false,
138
- configurable: true
139
- });
140
- Object.defineProperty(ConduitGrpcSdk, "interceptors", {
141
- get: function () {
142
- return ConduitGrpcSdk.middleware;
143
- },
144
- enumerable: false,
145
- configurable: true
146
- });
147
- Object.defineProperty(ConduitGrpcSdk.prototype, "redisManager", {
148
- get: function () {
149
- if (this._redisManager) {
150
- return this._redisManager;
151
- }
152
- else {
153
- throw new Error('Redis not available');
154
- }
155
- },
156
- enumerable: false,
157
- configurable: true
158
- });
159
- Object.defineProperty(ConduitGrpcSdk.prototype, "redisDetails", {
160
- get: function () {
161
- if (this._redisDetails) {
162
- return this._redisDetails;
163
- }
164
- else {
165
- throw new Error('Redis not available');
166
- }
167
- },
168
- enumerable: false,
169
- configurable: true
170
- });
171
- Object.defineProperty(ConduitGrpcSdk.prototype, "bus", {
172
- get: function () {
173
- if (this._eventBus) {
174
- return this._eventBus;
175
- }
176
- else {
177
- ConduitGrpcSdk.Logger.warn('Event bus not initialized');
178
- return null;
179
- }
180
- },
181
- enumerable: false,
182
- configurable: true
183
- });
184
- Object.defineProperty(ConduitGrpcSdk.prototype, "state", {
185
- get: function () {
186
- if (this._stateManager) {
187
- return this._stateManager;
188
- }
189
- else {
190
- ConduitGrpcSdk.Logger.warn('State Manager not initialized');
191
- return null;
192
- }
193
- },
194
- enumerable: false,
195
- configurable: true
196
- });
197
- Object.defineProperty(ConduitGrpcSdk.prototype, "core", {
198
- get: function () {
199
- return this._core;
200
- },
201
- enumerable: false,
202
- configurable: true
203
- });
204
- Object.defineProperty(ConduitGrpcSdk.prototype, "config", {
205
- get: function () {
206
- return this._config;
207
- },
208
- enumerable: false,
209
- configurable: true
210
- });
211
- Object.defineProperty(ConduitGrpcSdk.prototype, "admin", {
212
- get: function () {
213
- return this._admin;
214
- },
215
- enumerable: false,
216
- configurable: true
217
- });
218
- Object.defineProperty(ConduitGrpcSdk.prototype, "router", {
219
- get: function () {
220
- if (this._modules['router']) {
221
- return this._modules['router'];
222
- }
223
- else {
224
- ConduitGrpcSdk.Logger.warn('Router not up yet!');
225
- return null;
226
- }
227
- },
228
- enumerable: false,
229
- configurable: true
230
- });
231
- Object.defineProperty(ConduitGrpcSdk.prototype, "database", {
232
- get: function () {
233
- if (this._modules['database']) {
234
- return this._modules['database'];
235
- }
236
- else {
237
- ConduitGrpcSdk.Logger.warn('Database provider not up yet!');
238
- return null;
239
- }
240
- },
241
- enumerable: false,
242
- configurable: true
243
- });
244
- Object.defineProperty(ConduitGrpcSdk.prototype, "databaseProvider", {
245
- get: function () {
246
- return this.database;
247
- },
248
- enumerable: false,
249
- configurable: true
250
- });
251
- Object.defineProperty(ConduitGrpcSdk.prototype, "storage", {
252
- get: function () {
253
- if (this._modules['storage']) {
254
- return this._modules['storage'];
255
- }
256
- else {
257
- ConduitGrpcSdk.Logger.warn('Storage module not up yet!');
258
- return null;
259
- }
260
- },
261
- enumerable: false,
262
- configurable: true
263
- });
264
- Object.defineProperty(ConduitGrpcSdk.prototype, "emailProvider", {
265
- get: function () {
266
- if (this._modules['email']) {
267
- return this._modules['email'];
268
- }
269
- else {
270
- ConduitGrpcSdk.Logger.warn('Email provider not up yet!');
271
- return null;
272
- }
273
- },
274
- enumerable: false,
275
- configurable: true
276
- });
277
- Object.defineProperty(ConduitGrpcSdk.prototype, "pushNotifications", {
278
- get: function () {
279
- if (this._modules['pushNotifications']) {
280
- return this._modules['pushNotifications'];
281
- }
282
- else {
283
- ConduitGrpcSdk.Logger.warn('Push notifications module not up yet!');
284
- return null;
285
- }
286
- },
287
- enumerable: false,
288
- configurable: true
289
- });
290
- Object.defineProperty(ConduitGrpcSdk.prototype, "authentication", {
291
- get: function () {
292
- if (this._modules['authentication']) {
293
- return this._modules['authentication'];
294
- }
295
- else {
296
- ConduitGrpcSdk.Logger.warn('Authentication module not up yet!');
297
- return null;
298
- }
299
- },
300
- enumerable: false,
301
- configurable: true
302
- });
303
- Object.defineProperty(ConduitGrpcSdk.prototype, "authorization", {
304
- get: function () {
305
- if (this._modules['authorization']) {
306
- return this._modules['authorization'];
307
- }
308
- else {
309
- ConduitGrpcSdk.Logger.warn('Authorization module not up yet!');
310
- return null;
311
- }
312
- },
313
- enumerable: false,
314
- configurable: true
315
- });
316
- Object.defineProperty(ConduitGrpcSdk.prototype, "sms", {
317
- get: function () {
318
- if (this._modules['sms']) {
319
- return this._modules['sms'];
320
- }
321
- else {
322
- ConduitGrpcSdk.Logger.warn('SMS module not up yet!');
323
- return null;
324
- }
325
- },
326
- enumerable: false,
327
- configurable: true
328
- });
329
- Object.defineProperty(ConduitGrpcSdk.prototype, "chat", {
330
- get: function () {
331
- if (this._modules['chat']) {
332
- return this._modules['chat'];
333
- }
334
- else {
335
- ConduitGrpcSdk.Logger.warn('Chat module not up yet!');
336
- return null;
337
- }
338
- },
339
- enumerable: false,
340
- configurable: true
341
- });
342
- Object.defineProperty(ConduitGrpcSdk.prototype, "grpcToken", {
343
- get: function () {
344
- return this._grpcToken;
345
- },
346
- enumerable: false,
347
- configurable: true
348
- });
349
- ConduitGrpcSdk.prototype.addMiddleware = function (middleware) {
74
+ static get Logger() {
75
+ return ConduitGrpcSdk._Logger;
76
+ }
77
+ static get Metrics() {
78
+ return ConduitGrpcSdk._Metrics;
79
+ }
80
+ static set Metrics(metrics) {
81
+ ConduitGrpcSdk._Metrics = metrics;
82
+ }
83
+ static get interceptors() {
84
+ return ConduitGrpcSdk.middleware;
85
+ }
86
+ get redisManager() {
87
+ if (this._redisManager) {
88
+ return this._redisManager;
89
+ }
90
+ else {
91
+ throw new Error('Redis not available');
92
+ }
93
+ }
94
+ get redisDetails() {
95
+ if (this._redisDetails) {
96
+ return this._redisDetails;
97
+ }
98
+ else {
99
+ throw new Error('Redis not available');
100
+ }
101
+ }
102
+ get bus() {
103
+ if (this._eventBus) {
104
+ return this._eventBus;
105
+ }
106
+ else {
107
+ ConduitGrpcSdk.Logger.warn('Event bus not initialized');
108
+ return null;
109
+ }
110
+ }
111
+ get state() {
112
+ if (this._stateManager) {
113
+ return this._stateManager;
114
+ }
115
+ else {
116
+ ConduitGrpcSdk.Logger.warn('State Manager not initialized');
117
+ return null;
118
+ }
119
+ }
120
+ get core() {
121
+ return this._core;
122
+ }
123
+ get config() {
124
+ return this._config;
125
+ }
126
+ get admin() {
127
+ return this._admin;
128
+ }
129
+ get router() {
130
+ if (this._modules['router']) {
131
+ return this._modules['router'];
132
+ }
133
+ else {
134
+ ConduitGrpcSdk.Logger.warn('Router not up yet!');
135
+ return null;
136
+ }
137
+ }
138
+ get database() {
139
+ if (this._modules['database']) {
140
+ return this._modules['database'];
141
+ }
142
+ else {
143
+ ConduitGrpcSdk.Logger.warn('Database provider not up yet!');
144
+ return null;
145
+ }
146
+ }
147
+ get databaseProvider() {
148
+ return this.database;
149
+ }
150
+ get storage() {
151
+ if (this._modules['storage']) {
152
+ return this._modules['storage'];
153
+ }
154
+ else {
155
+ ConduitGrpcSdk.Logger.warn('Storage module not up yet!');
156
+ return null;
157
+ }
158
+ }
159
+ get emailProvider() {
160
+ if (this._modules['email']) {
161
+ return this._modules['email'];
162
+ }
163
+ else {
164
+ ConduitGrpcSdk.Logger.warn('Email provider not up yet!');
165
+ return null;
166
+ }
167
+ }
168
+ get pushNotifications() {
169
+ if (this._modules['pushNotifications']) {
170
+ return this._modules['pushNotifications'];
171
+ }
172
+ else {
173
+ ConduitGrpcSdk.Logger.warn('Push notifications module not up yet!');
174
+ return null;
175
+ }
176
+ }
177
+ get authentication() {
178
+ if (this._modules['authentication']) {
179
+ return this._modules['authentication'];
180
+ }
181
+ else {
182
+ ConduitGrpcSdk.Logger.warn('Authentication module not up yet!');
183
+ return null;
184
+ }
185
+ }
186
+ get authorization() {
187
+ if (this._modules['authorization']) {
188
+ return this._modules['authorization'];
189
+ }
190
+ else {
191
+ ConduitGrpcSdk.Logger.warn('Authorization module not up yet!');
192
+ return null;
193
+ }
194
+ }
195
+ get sms() {
196
+ if (this._modules['sms']) {
197
+ return this._modules['sms'];
198
+ }
199
+ else {
200
+ ConduitGrpcSdk.Logger.warn('SMS module not up yet!');
201
+ return null;
202
+ }
203
+ }
204
+ get chat() {
205
+ if (this._modules['chat']) {
206
+ return this._modules['chat'];
207
+ }
208
+ else {
209
+ ConduitGrpcSdk.Logger.warn('Chat module not up yet!');
210
+ return null;
211
+ }
212
+ }
213
+ get grpcToken() {
214
+ return this._grpcToken;
215
+ }
216
+ addMiddleware(middleware) {
350
217
  ConduitGrpcSdk.middleware.push(middleware);
351
- };
352
- ConduitGrpcSdk.prototype.initialize = function () {
353
- return __awaiter(this, void 0, void 0, function () {
354
- var state, err_1;
355
- return __generator(this, function (_a) {
356
- switch (_a.label) {
357
- case 0:
358
- if (this.name === 'core') {
359
- return [2 /*return*/, this._initialize()];
360
- }
361
- this._core = new modules_1.Core(this.name, this.serverUrl, this._grpcToken);
362
- ConduitGrpcSdk.Logger.log('Waiting for Core...');
363
- _a.label = 1;
364
- case 1:
365
- if (!true) return [3 /*break*/, 7];
366
- _a.label = 2;
367
- case 2:
368
- _a.trys.push([2, 4, , 6]);
369
- return [4 /*yield*/, this.core.check()];
370
- case 3:
371
- state = _a.sent();
372
- if (state === types_1.HealthCheckStatus.SERVING) {
373
- ConduitGrpcSdk.Logger.log('Core connection established');
374
- this._initialize();
375
- return [3 /*break*/, 7];
376
- }
377
- return [3 /*break*/, 6];
378
- case 4:
379
- err_1 = _a.sent();
380
- if (err_1.code === grpc_js_1.status.PERMISSION_DENIED) {
381
- ConduitGrpcSdk.Logger.error(err_1);
382
- process.exit(-1);
383
- }
384
- return [4 /*yield*/, (0, utilities_1.sleep)(1000)];
385
- case 5:
386
- _a.sent();
387
- return [3 /*break*/, 6];
388
- case 6: return [3 /*break*/, 1];
389
- case 7: return [2 /*return*/];
218
+ }
219
+ async initialize() {
220
+ if (this.name === 'core') {
221
+ return this._initialize();
222
+ }
223
+ this._core = new modules_1.Core(this.name, this.serverUrl, this._grpcToken);
224
+ ConduitGrpcSdk.Logger.log('Waiting for Core...');
225
+ while (true) {
226
+ try {
227
+ const state = await this.core.check();
228
+ if (state === types_1.HealthCheckStatus.SERVING) {
229
+ ConduitGrpcSdk.Logger.log('Core connection established');
230
+ this._initialize();
231
+ break;
390
232
  }
391
- });
392
- });
393
- };
394
- ConduitGrpcSdk.prototype.watchModules = function () {
395
- var _this = this;
396
- var emitter = this.config.getModuleWatcher();
233
+ }
234
+ catch (err) {
235
+ if (err.code === grpc_js_1.status.PERMISSION_DENIED) {
236
+ ConduitGrpcSdk.Logger.error(err);
237
+ process.exit(-1);
238
+ }
239
+ await (0, utilities_1.sleep)(1000);
240
+ }
241
+ }
242
+ }
243
+ watchModules() {
244
+ const emitter = this.config.getModuleWatcher();
397
245
  this.config.watchModules().then();
398
- emitter.on('serving-modules-update', function (modules) {
399
- Object.keys(_this._modules).forEach(function (r) {
246
+ emitter.on('serving-modules-update', modules => {
247
+ Object.keys(this._modules).forEach(r => {
400
248
  var _a;
401
- if (r === _this.name)
249
+ if (r === this.name)
402
250
  return;
403
- var found = modules.filter(function (m) { return m.moduleName === r && m.serving; });
404
- if ((!found || found.length === 0) && _this._modules[r]) {
405
- (_a = _this._modules[r]) === null || _a === void 0 ? void 0 : _a.closeConnection();
406
- emitter.emit("module-connection-update:".concat(r), false);
251
+ const found = modules.filter((m) => m.moduleName === r && m.serving);
252
+ if ((!found || found.length === 0) && this._modules[r]) {
253
+ (_a = this._modules[r]) === null || _a === void 0 ? void 0 : _a.closeConnection();
254
+ emitter.emit(`module-connection-update:${r}`, false);
407
255
  }
408
256
  });
409
- modules.forEach(function (m) {
257
+ modules.forEach((m) => {
410
258
  var _a;
411
- if (m.moduleName === _this.name || !m.serving)
259
+ if (m.moduleName === this.name || !m.serving)
412
260
  return;
413
- var alreadyActive = (_a = _this._modules[m.moduleName]) === null || _a === void 0 ? void 0 : _a.active;
261
+ const alreadyActive = (_a = this._modules[m.moduleName]) === null || _a === void 0 ? void 0 : _a.active;
414
262
  if (alreadyActive)
415
263
  return;
416
- if (_this._availableModules[m.moduleName] && _this._modules[m.moduleName]) {
417
- _this._modules[m.moduleName].openConnection();
264
+ if (this._availableModules[m.moduleName] && this._modules[m.moduleName]) {
265
+ this._modules[m.moduleName].openConnection();
418
266
  }
419
267
  else {
420
- _this.createModuleClient(m.moduleName, m.url);
268
+ this.createModuleClient(m.moduleName, m.url);
421
269
  }
422
- emitter.emit("module-connection-update:".concat(m.moduleName), true);
270
+ emitter.emit(`module-connection-update:${m.moduleName}`, true);
423
271
  });
424
272
  });
425
- };
426
- ConduitGrpcSdk.prototype.monitorModule = function (moduleName, callback, wait) {
427
- var _this = this;
428
- if (wait === void 0) { wait = true; }
429
- var waitPromise = wait ? this.waitForExistence(moduleName) : Promise.resolve();
273
+ }
274
+ monitorModule(moduleName, callback, wait = true) {
275
+ const waitPromise = wait ? this.waitForExistence(moduleName) : Promise.resolve();
430
276
  waitPromise
431
- .then(function () { var _a, _b; return (_b = (_a = _this._modules[moduleName]) === null || _a === void 0 ? void 0 : _a.healthClient) === null || _b === void 0 ? void 0 : _b.check({}); })
432
- .then(function (res) {
277
+ .then(() => { var _a, _b; return (_b = (_a = this._modules[moduleName]) === null || _a === void 0 ? void 0 : _a.healthClient) === null || _b === void 0 ? void 0 : _b.check({}); })
278
+ .then(res => {
433
279
  callback((res === null || res === void 0 ? void 0 : res.status) === protoUtils_1.HealthCheckResponse_ServingStatus.SERVING);
434
280
  })
435
- .catch(function () {
281
+ .catch(() => {
436
282
  callback(false);
437
283
  })
438
- .then(function () {
439
- var emitter = _this.config.getModuleWatcher();
440
- emitter.on("module-connection-update:".concat(moduleName), callback);
284
+ .then(() => {
285
+ const emitter = this.config.getModuleWatcher();
286
+ emitter.on(`module-connection-update:${moduleName}`, callback);
441
287
  });
442
- };
443
- ConduitGrpcSdk.prototype.unmonitorModule = function (moduleName) {
444
- var emitter = this.config.getModuleWatcher();
445
- emitter.removeAllListeners("module-connection-update:".concat(moduleName));
446
- };
447
- ConduitGrpcSdk.prototype.initializeEventBus = function () {
448
- var _this = this;
449
- var promise = Promise.resolve();
288
+ }
289
+ unmonitorModule(moduleName) {
290
+ const emitter = this.config.getModuleWatcher();
291
+ emitter.removeAllListeners(`module-connection-update:${moduleName}`);
292
+ }
293
+ initializeEventBus() {
294
+ let promise = Promise.resolve();
450
295
  if (process.env.REDIS_CONFIG) {
451
296
  this._redisDetails = (0, utilities_1.getJsonEnv)('REDIS_CONFIG');
452
297
  }
@@ -454,78 +299,76 @@ var ConduitGrpcSdk = /** @class */ (function () {
454
299
  this._redisDetails = {
455
300
  host: process.env.REDIS_HOST,
456
301
  port: parseInt(process.env.REDIS_PORT, 10),
457
- username: process.env.REDIS_USERNAME,
458
- password: process.env.REDIS_PASSWORD,
302
+ db: parseInt(process.env.REDIS_DB, 10) || 0,
303
+ ...(process.env.REDIS_USERNAME ? { username: process.env.REDIS_USERNAME } : {}),
304
+ ...(process.env.REDIS_PASSWORD ? { username: process.env.REDIS_PASSWORD } : {}),
459
305
  };
460
306
  }
461
307
  else {
462
308
  promise = promise
463
- .then(function () { return _this.config.getRedisDetails(); })
464
- .then(function (r) {
465
- if (r.redisConfig) {
466
- _this._redisDetails = JSON.parse(r.redisConfig);
309
+ .then(() => this.config.getRedisDetails())
310
+ .then(r => {
311
+ if (r.standalone) {
312
+ this._redisDetails = r.standalone;
467
313
  }
468
314
  else {
469
- _this._redisDetails = {
470
- host: r.redisHost,
471
- port: r.redisPort,
472
- username: r.redisUsername,
473
- password: r.redisPassword,
474
- };
315
+ this._redisDetails = r.cluster;
475
316
  }
476
317
  });
477
318
  }
478
319
  return promise
479
- .then(function () {
480
- if (_this._redisDetails.hasOwnProperty('nodes')) {
481
- _this._redisManager = new utilities_1.RedisManager(_this._redisDetails);
320
+ .then(() => {
321
+ if (this._redisDetails.hasOwnProperty('nodes')) {
322
+ this._redisManager = new utilities_1.RedisManager(this._redisDetails);
482
323
  }
483
324
  else {
484
- var redisHost = _this._redisDetails.host;
485
- _this._redisManager = new utilities_1.RedisManager(__assign(__assign({}, _this._redisDetails), { host: redisHost }));
325
+ const redisHost = this._redisDetails.host;
326
+ this._redisManager = new utilities_1.RedisManager({
327
+ ...this._redisDetails,
328
+ host: redisHost,
329
+ });
486
330
  }
487
- _this._eventBus = new utilities_1.EventBus(_this._redisManager);
488
- _this._stateManager = new utilities_1.StateManager(_this._redisManager, _this.name);
489
- return _this._eventBus;
331
+ this._eventBus = new utilities_1.EventBus(this._redisManager);
332
+ this._stateManager = new utilities_1.StateManager(this._redisManager, this.name);
333
+ return this._eventBus;
490
334
  })
491
- .catch(function (err) {
335
+ .catch((err) => {
492
336
  ConduitGrpcSdk.Logger.error('Failed to initialize event bus');
493
337
  throw err;
494
338
  });
495
- };
339
+ }
496
340
  /**
497
341
  * Gets all the registered modules from the config and creates clients for them.
498
342
  * This will only work on known modules, since the primary usage for the sdk is internal
499
343
  */
500
- ConduitGrpcSdk.prototype.initializeModules = function () {
501
- var _this = this;
344
+ initializeModules() {
502
345
  return this._config.moduleList()
503
- .then(function (r) {
504
- var emitter = _this.config.getModuleWatcher();
505
- _this.lastSearch = Date.now();
506
- r.forEach(function (m) {
507
- _this.createModuleClient(m.moduleName, m.url);
508
- emitter.emit("module-connection-update:".concat(m.moduleName), m.serving);
346
+ .then(r => {
347
+ const emitter = this.config.getModuleWatcher();
348
+ this.lastSearch = Date.now();
349
+ r.forEach(m => {
350
+ this.createModuleClient(m.moduleName, m.url);
351
+ emitter.emit(`module-connection-update:${m.moduleName}`, m.serving);
509
352
  });
510
353
  return 'ok';
511
354
  })
512
- .catch(function (err) {
355
+ .catch(err => {
513
356
  if (err.code !== 5) {
514
357
  ConduitGrpcSdk.Logger.error(err);
515
358
  }
516
359
  });
517
- };
518
- ConduitGrpcSdk.prototype.updateModuleHealth = function (moduleName, serving) {
519
- var emitter = this.config.getModuleWatcher();
520
- emitter.emit("module-connection-update:".concat(moduleName), serving);
521
- };
522
- ConduitGrpcSdk.prototype.createModuleClient = function (moduleName, moduleUrl) {
360
+ }
361
+ updateModuleHealth(moduleName, serving) {
362
+ const emitter = this.config.getModuleWatcher();
363
+ emitter.emit(`module-connection-update:${moduleName}`, serving);
364
+ }
365
+ createModuleClient(moduleName, moduleUrl) {
523
366
  var _a, _b, _c;
524
367
  if (this._modules[moduleName])
525
368
  return;
526
369
  moduleUrl =
527
370
  (_b = (_a = this.urlRemap) === null || _a === void 0 ? void 0 : _a[moduleUrl]) !== null && _b !== void 0 ? _b : (((_c = this.urlRemap) === null || _c === void 0 ? void 0 : _c['*'])
528
- ? "".concat(this.urlRemap['*'], ":").concat(moduleUrl.split(':')[1])
371
+ ? `${this.urlRemap['*']}:${moduleUrl.split(':')[1]}`
529
372
  : moduleUrl);
530
373
  if (this._availableModules[moduleName]) {
531
374
  // ConduitGrpcSdk.Logger.log(`Creating gRPC client for ${moduleName}`);
@@ -542,82 +385,67 @@ var ConduitGrpcSdk = /** @class */ (function () {
542
385
  this._modules[moduleName] = new classes_1.ConduitModule(this.name, moduleName, moduleUrl, this._grpcToken);
543
386
  this._modules[moduleName].initializeClient(protoUtils_1.ConduitModuleDefinition);
544
387
  }
545
- };
546
- ConduitGrpcSdk.prototype.isModuleUp = function (moduleName, moduleUrl, service) {
547
- if (service === void 0) { service = ''; }
388
+ }
389
+ isModuleUp(moduleName, moduleUrl, service = '') {
548
390
  return (0, classes_1.checkModuleHealth)(moduleName, moduleUrl, service, this._grpcToken);
549
- };
550
- ConduitGrpcSdk.prototype.moduleClient = function (name, type) {
391
+ }
392
+ moduleClient(name, type) {
551
393
  this._dynamicModules[name] = type;
552
- };
553
- ConduitGrpcSdk.prototype.getModule = function (name) {
394
+ }
395
+ getModule(name) {
554
396
  return this._modules[name];
555
- };
556
- ConduitGrpcSdk.prototype.getServiceClient = function (name) {
397
+ }
398
+ getServiceClient(name) {
557
399
  var _a;
558
400
  return (_a = this._modules[name]) === null || _a === void 0 ? void 0 : _a.client;
559
- };
560
- ConduitGrpcSdk.prototype.getModuleClient = function (name) {
401
+ }
402
+ getModuleClient(name) {
561
403
  var _a;
562
404
  return (_a = this._modules[name]) === null || _a === void 0 ? void 0 : _a.moduleClient;
563
- };
564
- ConduitGrpcSdk.prototype.getHealthClient = function (name) {
405
+ }
406
+ getHealthClient(name) {
565
407
  var _a;
566
408
  return (_a = this._modules[name]) === null || _a === void 0 ? void 0 : _a.healthClient;
567
- };
568
- ConduitGrpcSdk.prototype.isAvailable = function (moduleName) {
409
+ }
410
+ isAvailable(moduleName) {
569
411
  var _a;
570
412
  return !!((_a = this._modules[moduleName]) === null || _a === void 0 ? void 0 : _a.active);
571
- };
572
- ConduitGrpcSdk.prototype.waitForExistence = function (moduleName, onlyIfServing) {
573
- if (onlyIfServing === void 0) { onlyIfServing = true; }
574
- return __awaiter(this, void 0, void 0, function () {
575
- var emitter;
576
- return __generator(this, function (_a) {
577
- if (this.isAvailable(moduleName))
578
- return [2 /*return*/, true];
579
- emitter = this.config.getModuleWatcher();
580
- return [2 /*return*/, new Promise(function (resolve) {
581
- var listener = function (serving) {
582
- if (!onlyIfServing || (onlyIfServing && serving)) {
583
- emitter.removeListener("module-connection-update:".concat(moduleName), listener);
584
- return resolve(true);
585
- }
586
- };
587
- emitter.on("module-connection-update:".concat(moduleName), listener);
588
- })];
589
- });
413
+ }
414
+ async waitForExistence(moduleName, onlyIfServing = true) {
415
+ if (this.isAvailable(moduleName))
416
+ return true;
417
+ const emitter = this.config.getModuleWatcher();
418
+ return new Promise(resolve => {
419
+ const listener = (serving) => {
420
+ if (!onlyIfServing || (onlyIfServing && serving)) {
421
+ emitter.removeListener(`module-connection-update:${moduleName}`, listener);
422
+ return resolve(true);
423
+ }
424
+ };
425
+ emitter.on(`module-connection-update:${moduleName}`, listener);
590
426
  });
591
- };
592
- ConduitGrpcSdk.prototype.onceModuleUp = function (moduleName, callback) {
427
+ }
428
+ onceModuleUp(moduleName, callback) {
593
429
  if (this.isAvailable(moduleName))
594
430
  callback();
595
- var emitter = this.config.getModuleWatcher();
596
- emitter.once("module-connection-update:".concat(moduleName), function (serving) {
597
- return serving ? callback() : null;
598
- });
599
- };
600
- ConduitGrpcSdk.prototype.onceModuleDown = function (moduleName, callback) {
601
- var emitter = this.config.getModuleWatcher();
602
- emitter.once("module-connection-update:".concat(moduleName), function (serving) {
603
- return serving ? null : callback();
604
- });
605
- };
431
+ const emitter = this.config.getModuleWatcher();
432
+ emitter.once(`module-connection-update:${moduleName}`, (serving) => serving ? callback() : null);
433
+ }
434
+ onceModuleDown(moduleName, callback) {
435
+ const emitter = this.config.getModuleWatcher();
436
+ emitter.once(`module-connection-update:${moduleName}`, (serving) => serving ? null : callback());
437
+ }
606
438
  /**
607
439
  * Used to refresh all modules to check for new registrations
608
440
  * @param force If true will check for new modules no matter the interval
609
441
  */
610
- ConduitGrpcSdk.prototype.refreshModules = function (force) {
611
- return __awaiter(this, void 0, void 0, function () {
612
- return __generator(this, function (_a) {
613
- if (this.lastSearch < Date.now() - 3000 || force) {
614
- return [2 /*return*/, this.initializeModules()];
615
- }
616
- return [2 /*return*/, 'ok'];
617
- });
618
- });
619
- };
620
- ConduitGrpcSdk.prototype._initialize = function () {
442
+ async refreshModules(force) {
443
+ if (this.lastSearch < Date.now() - 3000 || force) {
444
+ return this.initializeModules();
445
+ }
446
+ return 'ok';
447
+ }
448
+ _initialize() {
621
449
  if (this._initialized)
622
450
  throw new Error("Module's grpc-sdk has already been initialized");
623
451
  this._config = new modules_1.Config(this.name, this.serverUrl, this._serviceHealthStatusGetter, this._grpcToken);
@@ -629,11 +457,10 @@ var ConduitGrpcSdk = /** @class */ (function () {
629
457
  this.watchModules();
630
458
  }
631
459
  this._initialized = true;
632
- };
633
- ConduitGrpcSdk.middleware = [];
634
- ConduitGrpcSdk._Metrics = undefined;
635
- return ConduitGrpcSdk;
636
- }());
460
+ }
461
+ }
462
+ ConduitGrpcSdk.middleware = [];
463
+ ConduitGrpcSdk._Metrics = undefined;
637
464
  exports.default = ConduitGrpcSdk;
638
465
  __exportStar(require("./interfaces"), exports);
639
466
  __exportStar(require("./classes"), exports);