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