@conduitplatform/module-tools 0.16.26 → 0.17.0-alpha.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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/dist/index.cjs +2059 -1775
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +490 -0
  5. package/dist/index.d.ts +490 -7
  6. package/dist/index.esm.js +2033 -1746
  7. package/dist/index.esm.js.map +1 -1
  8. package/dist/module.proto +25 -0
  9. package/package.json +27 -31
  10. package/dist/ManagedModule.d.ts +0 -80
  11. package/dist/classes/ConduitActiveSchema.d.ts +0 -19
  12. package/dist/classes/ConduitServiceModule.d.ts +0 -27
  13. package/dist/classes/ConfigController.d.ts +0 -7
  14. package/dist/classes/GrpcServer.d.ts +0 -23
  15. package/dist/classes/ModuleError.d.ts +0 -5
  16. package/dist/classes/index.d.ts +0 -4
  17. package/dist/helpers/GrpcHelper.d.ts +0 -8
  18. package/dist/helpers/GrpcToParsedRouterRequest.d.ts +0 -2
  19. package/dist/helpers/TypeHelpers.d.ts +0 -70
  20. package/dist/helpers/index.d.ts +0 -4
  21. package/dist/helpers/wrapGrpcFunctions.d.ts +0 -5
  22. package/dist/interfaces/ConduitService.d.ts +0 -8
  23. package/dist/interfaces/ModuleLifecycleStage.d.ts +0 -7
  24. package/dist/interfaces/index.d.ts +0 -2
  25. package/dist/logging/index.d.ts +0 -16
  26. package/dist/metrics/MetricsServer.d.ts +0 -11
  27. package/dist/metrics/clientMiddleware.d.ts +0 -2
  28. package/dist/metrics/config/defaults.d.ts +0 -91
  29. package/dist/metrics/index.d.ts +0 -23
  30. package/dist/routing/ProxyRouteBuilder.d.ts +0 -21
  31. package/dist/routing/RouteBuilder.d.ts +0 -23
  32. package/dist/routing/RoutingController.d.ts +0 -8
  33. package/dist/routing/RoutingManager.d.ts +0 -35
  34. package/dist/routing/RoutingUtilities.d.ts +0 -7
  35. package/dist/routing/index.d.ts +0 -5
  36. package/dist/routing/wrapRouterFunctions.d.ts +0 -13
  37. package/dist/utilities/convictConfigParser.d.ts +0 -2
  38. package/dist/utilities/index.d.ts +0 -3
  39. package/dist/utilities/initializeSdk.d.ts +0 -2
  40. package/dist/utilities/linearBackoffTimeout.d.ts +0 -9
  41. package/dist/utilities/merge.d.ts +0 -1
package/dist/index.cjs CHANGED
@@ -1,1856 +1,2140 @@
1
- 'use strict';
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
2
29
 
3
- var grpcSdk = require('@conduitplatform/grpc-sdk');
4
- var grpcJs = require('@grpc/grpc-js');
5
- var protoLoader = require('@grpc/proto-loader');
6
- var fastJwt = require('fast-jwt');
7
- var lodash = require('lodash');
8
- var path = require('path');
9
- var events = require('events');
10
- var node_url = require('node:url');
11
- var convict = require('convict');
12
- var winston = require('winston');
13
- var http = require('http');
14
- var LokiTransport = require('winston-loki');
15
- var timers = require('timers');
16
- var promClient = require('prom-client');
17
- var abortControllerX = require('abort-controller-x');
18
- var niceGrpcCommon = require('nice-grpc-common');
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ ConduitActiveSchema: () => ConduitActiveSchema,
34
+ ConduitBoolean: () => ConduitBoolean,
35
+ ConduitDate: () => ConduitDate,
36
+ ConduitJson: () => ConduitJson,
37
+ ConduitNumber: () => ConduitNumber,
38
+ ConduitObjectId: () => ConduitObjectId,
39
+ ConduitRelation: () => ConduitRelation,
40
+ ConduitServiceModule: () => ConduitServiceModule,
41
+ ConduitString: () => ConduitString,
42
+ ConfigController: () => ConfigController,
43
+ DEFAULT_STRIP_FIELDS: () => DEFAULT_STRIP_FIELDS,
44
+ GrpcServer: () => GrpcServer,
45
+ ManagedModule: () => ManagedModule,
46
+ ModuleError: () => ModuleError,
47
+ ModuleLifecycleStage: () => ModuleLifecycleStage,
48
+ RouteBuilder: () => RouteBuilder,
49
+ RoutingManager: () => RoutingManager,
50
+ addServiceToServer: () => addServiceToServer,
51
+ createParsedRouterRequest: () => createParsedRouterRequest,
52
+ createServer: () => createServer,
53
+ exportModel: () => exportModel,
54
+ importModel: () => importModel,
55
+ initializeSdk: () => initializeSdk,
56
+ linearBackoffTimeout: () => linearBackoffTimeout,
57
+ linearBackoffTimeoutAsync: () => linearBackoffTimeoutAsync,
58
+ merge: () => merge,
59
+ readConduitPeersManifest: () => readConduitPeersManifest,
60
+ sanitizeDocumentsForExport: () => sanitizeDocumentsForExport,
61
+ stripInternalFields: () => stripInternalFields,
62
+ wrapFunctionsAsync: () => wrapFunctionsAsync,
63
+ wrapGrpcFunctions: () => wrapGrpcFunctions,
64
+ wrapRouterGrpcFunction: () => wrapRouterGrpcFunction
65
+ });
66
+ module.exports = __toCommonJS(index_exports);
19
67
 
20
- var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
21
- class ConduitActiveSchema extends grpcSdk.ConduitSchema {
22
- dbInstance;
23
- constructor(dbInstance, name, fields, modelOptions, collectionName) {
24
- super(name, fields ?? {}, modelOptions, collectionName);
25
- this.dbInstance = dbInstance;
26
- }
27
- findOne(query, select, populate, userId, scope) {
28
- return this.dbInstance.findOne(this.name, query, select, populate, userId, scope);
29
- }
30
- findMany(query, select, skip, limit, sort, populate, userId, scope) {
31
- return this.dbInstance.findMany(this.name, query, select, skip, limit, sort, populate, userId, scope);
32
- }
33
- create(query, userId, scope) {
34
- return this.dbInstance.create(this.name, query, userId, scope);
35
- }
36
- createMany(query, userId, scope) {
37
- return this.dbInstance.createMany(this.name, query, userId, scope);
38
- }
39
- findByIdAndUpdate(id, document, populate, userId, scope) {
40
- return this.dbInstance.findByIdAndUpdate(this.name, id, document, populate, userId, scope);
41
- }
42
- findByIdAndReplace(id, document, populate, userId, scope) {
43
- return this.dbInstance.findByIdAndReplace(this.name, id, document, populate, userId, scope);
44
- }
45
- replaceOne(filterQuery, query, populate, userId, scope) {
46
- return this.dbInstance.replaceOne(this.name, filterQuery, query, populate, userId, scope);
47
- }
48
- updateOne(filterQuery, query, populate, userId, scope) {
49
- return this.dbInstance.updateOne(this.name, filterQuery, query, populate, userId, scope);
50
- }
51
- updateMany(filterQuery, query, populate, userId, scope) {
52
- return this.dbInstance.updateMany(this.name, filterQuery, query, populate, userId, scope);
53
- }
54
- deleteOne(query, userId, scope) {
55
- return this.dbInstance.deleteOne(this.name, query, userId, scope);
56
- }
57
- deleteMany(query, userId, scope) {
58
- return this.dbInstance.deleteMany(this.name, query, userId, scope);
59
- }
60
- countDocuments(query, userId, scope) {
61
- return this.dbInstance.countDocuments(this.name, query, userId, scope);
62
- }
63
- }
68
+ // src/classes/ConduitActiveSchema.ts
69
+ var import_grpc_sdk = require("@conduitplatform/grpc-sdk");
70
+ var ConduitActiveSchema = class extends import_grpc_sdk.ConduitSchema {
71
+ dbInstance;
72
+ constructor(dbInstance, name, fields, modelOptions, collectionName) {
73
+ super(name, fields ?? {}, modelOptions, collectionName);
74
+ this.dbInstance = dbInstance;
75
+ }
76
+ findOne(query, options) {
77
+ return this.dbInstance.findOne(this.name, query, options);
78
+ }
79
+ findMany(query, options) {
80
+ return this.dbInstance.findMany(this.name, query, options);
81
+ }
82
+ create(query, options) {
83
+ return this.dbInstance.create(this.name, query, options);
84
+ }
85
+ createMany(query, options) {
86
+ return this.dbInstance.createMany(this.name, query, options);
87
+ }
88
+ findByIdAndUpdate(id, document, options) {
89
+ return this.dbInstance.findByIdAndUpdate(this.name, id, document, options);
90
+ }
91
+ findByIdAndReplace(id, document, options) {
92
+ return this.dbInstance.findByIdAndReplace(this.name, id, document, options);
93
+ }
94
+ replaceOne(filterQuery, query, options) {
95
+ return this.dbInstance.replaceOne(this.name, filterQuery, query, options);
96
+ }
97
+ updateOne(filterQuery, query, options) {
98
+ return this.dbInstance.updateOne(this.name, filterQuery, query, options);
99
+ }
100
+ updateMany(filterQuery, query, options) {
101
+ return this.dbInstance.updateMany(this.name, filterQuery, query, options);
102
+ }
103
+ deleteOne(query, options) {
104
+ return this.dbInstance.deleteOne(this.name, query, options);
105
+ }
106
+ deleteMany(query, options) {
107
+ return this.dbInstance.deleteMany(this.name, query, options);
108
+ }
109
+ countDocuments(query, options) {
110
+ return this.dbInstance.countDocuments(this.name, query, options);
111
+ }
112
+ };
64
113
 
65
- class ConduitStringConstructor {
66
- // private to disallow creating other instances of this type
67
- constructor() { }
68
- static get Optional() {
69
- return grpcSdk.TYPE.String;
70
- }
71
- static get Required() {
72
- return { type: grpcSdk.TYPE.String, required: true };
73
- }
74
- }
75
- const ConduitString = ConduitStringConstructor;
76
- class ConduitNumberConstructor {
77
- // private to disallow creating other instances of this type
78
- constructor() { }
79
- static get Optional() {
80
- return grpcSdk.TYPE.Number;
81
- }
82
- static get Required() {
83
- return { type: grpcSdk.TYPE.Number, required: true };
84
- }
85
- }
86
- const ConduitNumber = ConduitNumberConstructor;
87
- class ConduitBooleanConstructor {
88
- // private to disallow creating other instances of this type
89
- constructor() { }
90
- static get Optional() {
91
- return grpcSdk.TYPE.Boolean;
92
- }
93
- static get Required() {
94
- return { type: grpcSdk.TYPE.Boolean, required: true };
95
- }
96
- }
97
- const ConduitBoolean = ConduitBooleanConstructor;
98
- class ConduitDateConstructor {
99
- // private to disallow creating other instances of this type
100
- constructor() { }
101
- static get Optional() {
102
- return grpcSdk.TYPE.Date;
103
- }
104
- static get Required() {
105
- return { type: grpcSdk.TYPE.Date, required: true };
106
- }
107
- }
108
- const ConduitDate = ConduitDateConstructor;
109
- class ConduitObjectIdConstructor {
110
- // private to disallow creating other instances of this type
111
- constructor() { }
112
- static get Optional() {
113
- return grpcSdk.TYPE.ObjectId;
114
- }
115
- static get Required() {
116
- return { type: grpcSdk.TYPE.ObjectId, required: true };
117
- }
118
- }
119
- const ConduitObjectId = ConduitObjectIdConstructor;
120
- class ConduitJSONConstructor {
121
- // private to disallow creating other instances of this type
122
- constructor() { }
123
- static get Optional() {
124
- return grpcSdk.TYPE.JSON;
125
- }
126
- static get Required() {
127
- return { type: grpcSdk.TYPE.JSON, required: true };
128
- }
129
- }
130
- const ConduitJson = ConduitJSONConstructor;
131
- class ConduitRelationConstructor {
132
- // private to disallow creating other instances of this type
133
- constructor() { }
134
- static Optional(model) {
135
- return { type: grpcSdk.TYPE.Relation, model, required: false };
136
- }
137
- static Required(model) {
138
- return { type: grpcSdk.TYPE.Relation, model, required: true };
139
- }
140
- }
141
- const ConduitRelation = ConduitRelationConstructor;
114
+ // src/helpers/TypeHelpers.ts
115
+ var import_grpc_sdk2 = require("@conduitplatform/grpc-sdk");
116
+ var ConduitStringConstructor = class {
117
+ // private to disallow creating other instances of this type
118
+ constructor() {
119
+ }
120
+ static get Optional() {
121
+ return import_grpc_sdk2.TYPE.String;
122
+ }
123
+ static get Required() {
124
+ return { type: import_grpc_sdk2.TYPE.String, required: true };
125
+ }
126
+ static RequiredWith(rules) {
127
+ return { type: import_grpc_sdk2.TYPE.String, required: true, validate: rules };
128
+ }
129
+ static OptionalWith(rules) {
130
+ return { type: import_grpc_sdk2.TYPE.String, required: false, validate: rules };
131
+ }
132
+ };
133
+ var ConduitString = ConduitStringConstructor;
134
+ var ConduitNumberConstructor = class {
135
+ // private to disallow creating other instances of this type
136
+ constructor() {
137
+ }
138
+ static get Optional() {
139
+ return import_grpc_sdk2.TYPE.Number;
140
+ }
141
+ static get Required() {
142
+ return { type: import_grpc_sdk2.TYPE.Number, required: true };
143
+ }
144
+ static RequiredWith(rules) {
145
+ return { type: import_grpc_sdk2.TYPE.Number, required: true, validate: rules };
146
+ }
147
+ static OptionalWith(rules) {
148
+ return { type: import_grpc_sdk2.TYPE.Number, required: false, validate: rules };
149
+ }
150
+ };
151
+ var ConduitNumber = ConduitNumberConstructor;
152
+ var ConduitBooleanConstructor = class {
153
+ // private to disallow creating other instances of this type
154
+ constructor() {
155
+ }
156
+ static get Optional() {
157
+ return import_grpc_sdk2.TYPE.Boolean;
158
+ }
159
+ static get Required() {
160
+ return { type: import_grpc_sdk2.TYPE.Boolean, required: true };
161
+ }
162
+ };
163
+ var ConduitBoolean = ConduitBooleanConstructor;
164
+ var ConduitDateConstructor = class {
165
+ // private to disallow creating other instances of this type
166
+ constructor() {
167
+ }
168
+ static get Optional() {
169
+ return import_grpc_sdk2.TYPE.Date;
170
+ }
171
+ static get Required() {
172
+ return { type: import_grpc_sdk2.TYPE.Date, required: true };
173
+ }
174
+ };
175
+ var ConduitDate = ConduitDateConstructor;
176
+ var ConduitObjectIdConstructor = class {
177
+ // private to disallow creating other instances of this type
178
+ constructor() {
179
+ }
180
+ static get Optional() {
181
+ return import_grpc_sdk2.TYPE.ObjectId;
182
+ }
183
+ static get Required() {
184
+ return { type: import_grpc_sdk2.TYPE.ObjectId, required: true };
185
+ }
186
+ };
187
+ var ConduitObjectId = ConduitObjectIdConstructor;
188
+ var ConduitJSONConstructor = class {
189
+ // private to disallow creating other instances of this type
190
+ constructor() {
191
+ }
192
+ static get Optional() {
193
+ return import_grpc_sdk2.TYPE.JSON;
194
+ }
195
+ static get Required() {
196
+ return { type: import_grpc_sdk2.TYPE.JSON, required: true };
197
+ }
198
+ };
199
+ var ConduitJson = ConduitJSONConstructor;
200
+ var ConduitRelationConstructor = class {
201
+ // private to disallow creating other instances of this type
202
+ constructor() {
203
+ }
204
+ static Optional(model) {
205
+ return { type: import_grpc_sdk2.TYPE.Relation, model, required: false };
206
+ }
207
+ static Required(model) {
208
+ return { type: import_grpc_sdk2.TYPE.Relation, model, required: true };
209
+ }
210
+ };
211
+ var ConduitRelation = ConduitRelationConstructor;
142
212
 
213
+ // src/helpers/GrpcHelper.ts
214
+ var import_grpc_js = require("@grpc/grpc-js");
215
+ var import_proto_loader = __toESM(require("@grpc/proto-loader"), 1);
143
216
  function createServer(port) {
144
- const grpcServer = new grpcJs.Server({
145
- 'grpc.max_receive_message_length': 1024 * 1024 * 100,
146
- 'grpc.max_send_message_length': 1024 * 1024 * 100,
147
- });
148
- return new Promise((resolve, reject) => {
149
- grpcServer.bindAsync(port, grpcJs.ServerCredentials.createInsecure(), (err, port) => {
150
- if (err) {
151
- reject(err);
152
- }
153
- else {
154
- resolve({
155
- server: grpcServer,
156
- port,
157
- });
158
- }
217
+ const grpcServer = new import_grpc_js.Server({
218
+ "grpc.max_receive_message_length": 1024 * 1024 * 100,
219
+ "grpc.max_send_message_length": 1024 * 1024 * 100
220
+ });
221
+ return new Promise((resolve, reject) => {
222
+ grpcServer.bindAsync(port, import_grpc_js.ServerCredentials.createInsecure(), (err, port2) => {
223
+ if (err) {
224
+ reject(err);
225
+ } else {
226
+ resolve({
227
+ server: grpcServer,
228
+ port: port2
159
229
  });
230
+ }
160
231
  });
232
+ });
161
233
  }
162
234
  function addServiceToServer(server, protoPath, descriptorObject, functions) {
163
- const packageDefinition = protoLoader.loadSync(protoPath, {
164
- keepCase: true,
165
- longs: String,
166
- enums: String,
167
- defaults: true,
168
- oneofs: true,
169
- });
170
- const protoDescriptor = grpcJs.loadPackageDefinition(packageDefinition);
171
- const objs = descriptorObject.split('.');
172
- let descObj = protoDescriptor;
173
- objs.forEach((r) => {
174
- descObj = descObj[r];
175
- });
176
- server.addService(descObj.service, functions);
235
+ const packageDefinition = import_proto_loader.default.loadSync(protoPath, {
236
+ keepCase: true,
237
+ longs: String,
238
+ enums: String,
239
+ defaults: true,
240
+ oneofs: true
241
+ });
242
+ const protoDescriptor = (0, import_grpc_js.loadPackageDefinition)(packageDefinition);
243
+ const objs = descriptorObject.split(".");
244
+ let descObj = protoDescriptor;
245
+ objs.forEach((r) => {
246
+ descObj = descObj[r];
247
+ });
248
+ server.addService(descObj.service, functions);
177
249
  }
178
250
 
251
+ // src/helpers/wrapGrpcFunctions.ts
252
+ var import_fast_jwt = require("fast-jwt");
253
+ var import_grpc_js2 = require("@grpc/grpc-js");
254
+ var import_grpc_sdk3 = require("@conduitplatform/grpc-sdk");
179
255
  function wrapGrpcFunctions(functions, postponeRestart, noAuth = false) {
180
- const grpcKey = process.env.GRPC_KEY;
181
- const wrappedFunctions = {};
182
- Object.keys(functions).forEach(name => {
183
- wrappedFunctions[name] = (call, callback) => {
184
- grpcSdk.ConduitGrpcSdk.Metrics?.increment('internal_grpc_requests_total');
185
- postponeRestart();
186
- if (grpcKey && !noAuth) {
187
- const verify = fastJwt.createVerifier({ key: grpcKey });
188
- const grpcToken = call.metadata.get('grpc-token')[0];
189
- if (!grpcToken) {
190
- return throwError(callback, 'No gRPC protection token provided', grpcJs.status.PERMISSION_DENIED);
191
- }
192
- try {
193
- const jwt = verify(grpcToken);
194
- call.metadata.set('module-name', jwt.moduleName);
195
- }
196
- catch {
197
- return throwError(callback, 'Failed to verify gRPC protection token. GRPC_KEY value differs', grpcJs.status.PERMISSION_DENIED);
198
- }
199
- }
200
- let invoked;
201
- try {
202
- invoked = functions[name](call, callback);
203
- }
204
- catch (error) {
205
- return throwError(callback, error.message);
206
- }
207
- if (typeof invoked?.then === 'function') {
208
- invoked.then().catch((error) => {
209
- return throwError(callback, error.message);
210
- });
211
- }
212
- };
213
- });
214
- return wrappedFunctions;
256
+ const grpcKey = process.env.GRPC_KEY;
257
+ const wrappedFunctions = {};
258
+ Object.keys(functions).forEach((name) => {
259
+ wrappedFunctions[name] = (call, callback) => {
260
+ import_grpc_sdk3.ConduitGrpcSdk.Metrics?.increment("internal_grpc_requests_total");
261
+ postponeRestart();
262
+ if (grpcKey && !noAuth) {
263
+ const verify = (0, import_fast_jwt.createVerifier)({ key: grpcKey });
264
+ const grpcToken = call.metadata.get("grpc-token")[0];
265
+ if (!grpcToken) {
266
+ return throwError(
267
+ callback,
268
+ "No gRPC protection token provided",
269
+ import_grpc_js2.status.PERMISSION_DENIED
270
+ );
271
+ }
272
+ try {
273
+ const jwt = verify(grpcToken);
274
+ call.metadata.set("module-name", jwt.moduleName);
275
+ } catch {
276
+ return throwError(
277
+ callback,
278
+ "Failed to verify gRPC protection token. GRPC_KEY value differs",
279
+ import_grpc_js2.status.PERMISSION_DENIED
280
+ );
281
+ }
282
+ }
283
+ let invoked;
284
+ try {
285
+ invoked = functions[name](call, callback);
286
+ } catch (error) {
287
+ return throwError(callback, error.message);
288
+ }
289
+ if (typeof invoked?.then === "function") {
290
+ invoked.then().catch((error) => {
291
+ return throwError(callback, error.message);
292
+ });
293
+ }
294
+ };
295
+ });
296
+ return wrappedFunctions;
215
297
  }
216
- function throwError(callback, message, code = grpcJs.status.INTERNAL) {
217
- // Aborting server streams is not yet implemented!
218
- if (callback) {
219
- callback({ code, message });
220
- }
298
+ function throwError(callback, message, code = import_grpc_js2.status.INTERNAL) {
299
+ if (callback) {
300
+ callback({ code, message });
301
+ }
221
302
  }
222
303
 
223
- function createParsedRouterRequest(params, urlParams, queryParams, bodyParams, path, headers, context, cookies) {
224
- return {
225
- request: {
226
- params,
227
- urlParams: urlParams ?? {},
228
- queryParams: queryParams ?? {},
229
- bodyParams: bodyParams ?? {},
230
- path: path ?? '',
231
- headers: headers,
232
- context: context ?? {},
233
- cookies: cookies ?? {},
234
- },
235
- };
304
+ // src/helpers/GrpcToParsedRouterRequest.ts
305
+ function createParsedRouterRequest(params, urlParams, queryParams, bodyParams, path4, headers, context, cookies) {
306
+ return {
307
+ request: {
308
+ params,
309
+ urlParams: urlParams ?? {},
310
+ queryParams: queryParams ?? {},
311
+ bodyParams: bodyParams ?? {},
312
+ path: path4 ?? "",
313
+ headers,
314
+ context: context ?? {},
315
+ cookies: cookies ?? {}
316
+ }
317
+ };
236
318
  }
237
319
 
238
- class GrpcServer {
239
- grpcServer;
240
- started = false;
241
- startedOnce = false;
242
- _serviceNames = [];
243
- scheduledRestart;
244
- _postponeRequests = 0;
245
- _useForce;
246
- _services = [];
247
- constructor(port) {
248
- this._url = `0.0.0.0:${port ?? '5000'}`;
249
- }
250
- async waitForReady() {
251
- while (!lodash.isNil(this.scheduledRestart)) {
252
- await this.wait(200);
253
- }
254
- }
255
- _url;
256
- get url() {
257
- return this._url;
258
- }
259
- async createNewServer(useForce = false) {
260
- if (lodash.isNil(this._useForce)) {
261
- this._useForce = useForce;
262
- }
263
- const serverResult = await createServer(this._url);
264
- this.grpcServer = serverResult.server;
265
- this._url = this._url.split(':')[0] + ':' + serverResult.port.toString();
266
- return serverResult.port;
267
- }
268
- async addService(protoPath, protoDescription, functions, noAuth = false) {
269
- functions = wrapGrpcFunctions(functions, this.postponeRestart.bind(this), noAuth);
270
- if (this._serviceNames.indexOf(protoDescription) !== -1) {
271
- grpcSdk.ConduitGrpcSdk.Logger.log('Service already exists, performing replace');
272
- this._services[this._serviceNames.indexOf(protoDescription)] = {
273
- protoPath,
274
- protoDescription,
275
- functions,
276
- };
277
- this.scheduleRefresh();
278
- return this;
279
- }
280
- else {
281
- this._services.push({ protoPath, protoDescription, functions });
282
- this._serviceNames.push(protoDescription);
283
- if (this.started) {
284
- grpcSdk.ConduitGrpcSdk.Logger.log('Server already started, scheduling refresh..');
285
- this.scheduleRefresh();
286
- return this;
287
- }
288
- else {
289
- if (!this.grpcServer) {
290
- await this.wait(1000);
291
- }
292
- addServiceToServer(this.grpcServer, protoPath, protoDescription, functions);
293
- return this;
294
- }
295
- }
296
- }
297
- async wait(time) {
298
- return new Promise((resolve, reject) => {
299
- const timeout = setTimeout(() => {
300
- clearTimeout(timeout);
301
- resolve();
302
- }, time);
303
- });
304
- }
305
- async refresh() {
306
- if (this.started) {
307
- this.started = false;
308
- if (this._useForce) {
309
- this.grpcServer.forceShutdown();
310
- }
311
- else {
312
- //gracefully shutdown so that there are no service disruption
313
- await new Promise(resolve => this.grpcServer.tryShutdown(() => resolve()));
314
- }
315
- }
316
- await this.createNewServer();
317
- this._services.forEach(service => {
318
- addServiceToServer(this.grpcServer, service.protoPath, service.protoDescription, service.functions);
319
- });
320
- if (!this.started && this.startedOnce) {
321
- this.grpcServer.start();
322
- this.started = true;
323
- }
324
- }
325
- scheduleRefresh() {
326
- if (this.scheduledRestart) {
327
- clearTimeout(this.scheduledRestart);
328
- }
329
- const self = this;
330
- this.scheduledRestart = setTimeout(async () => {
331
- grpcSdk.ConduitGrpcSdk.Logger.log('Begin refresh');
332
- await self.refresh();
333
- grpcSdk.ConduitGrpcSdk.Logger.log('Refresh complete');
334
- clearTimeout(self.scheduledRestart);
335
- self.scheduledRestart = undefined;
336
- self._postponeRequests = 0;
337
- }, 2000);
338
- }
339
- start() {
340
- if (this.started) {
341
- grpcSdk.ConduitGrpcSdk.Logger.error('gRPC server is already running!');
342
- return;
343
- }
344
- else if (!this.started && this.startedOnce) {
345
- grpcSdk.ConduitGrpcSdk.Logger.error('gRPC server is down for refresh!');
346
- }
347
- this.started = true;
348
- this.startedOnce = true;
349
- this.grpcServer?.start();
350
- }
351
- postponeRestart() {
352
- if (!this.scheduledRestart || this._postponeRequests > 5)
353
- return;
354
- this._postponeRequests++;
320
+ // src/classes/GrpcServer.ts
321
+ var import_lodash_es = require("lodash-es");
322
+ var import_grpc_sdk4 = require("@conduitplatform/grpc-sdk");
323
+ var GrpcServer = class {
324
+ grpcServer;
325
+ started = false;
326
+ startedOnce = false;
327
+ _serviceNames = [];
328
+ scheduledRestart;
329
+ _postponeRequests = 0;
330
+ _useForce;
331
+ _services = [];
332
+ constructor(port) {
333
+ this._url = `0.0.0.0:${port ?? "5000"}`;
334
+ }
335
+ async waitForReady() {
336
+ while (!(0, import_lodash_es.isNil)(this.scheduledRestart)) {
337
+ await this.wait(200);
338
+ }
339
+ }
340
+ _url;
341
+ get url() {
342
+ return this._url;
343
+ }
344
+ async createNewServer(useForce = false) {
345
+ if ((0, import_lodash_es.isNil)(this._useForce)) {
346
+ this._useForce = useForce;
347
+ }
348
+ const serverResult = await createServer(this._url);
349
+ this.grpcServer = serverResult.server;
350
+ this._url = this._url.split(":")[0] + ":" + serverResult.port.toString();
351
+ return serverResult.port;
352
+ }
353
+ async addService(protoPath, protoDescription, functions, noAuth = false) {
354
+ functions = wrapGrpcFunctions(functions, this.postponeRestart.bind(this), noAuth);
355
+ if (this._serviceNames.indexOf(protoDescription) !== -1) {
356
+ import_grpc_sdk4.ConduitGrpcSdk.Logger.log("Service already exists, performing replace");
357
+ this._services[this._serviceNames.indexOf(protoDescription)] = {
358
+ protoPath,
359
+ protoDescription,
360
+ functions
361
+ };
362
+ this.scheduleRefresh();
363
+ return this;
364
+ } else {
365
+ this._services.push({ protoPath, protoDescription, functions });
366
+ this._serviceNames.push(protoDescription);
367
+ if (this.started) {
368
+ import_grpc_sdk4.ConduitGrpcSdk.Logger.log("Server already started, scheduling refresh..");
355
369
  this.scheduleRefresh();
356
- }
357
- }
358
-
359
- class RouteBuilder {
360
- manager;
361
- _options;
362
- _returns;
363
- _handler;
364
- constructor(manager) {
365
- this.manager = manager;
366
- this._options = {};
367
- }
368
- method(action) {
369
- this._options.action = action;
370
370
  return this;
371
- }
372
- name(name) {
373
- this._options.name = name;
374
- return this;
375
- }
376
- description(description) {
377
- this._options.description = description;
378
- return this;
379
- }
380
- cacheControl(cache) {
381
- this._options.cacheControl = cache;
382
- return this;
383
- }
384
- middleware(middleware, allowDuplicates = false) {
385
- if (!Array.isArray(middleware)) {
386
- middleware = [middleware];
387
- }
388
- if (this._options.middlewares !== undefined &&
389
- this._options.middlewares?.length !== 0) {
390
- if (allowDuplicates) {
391
- this._options.middlewares = this._options.middlewares?.concat(middleware);
392
- }
393
- else {
394
- // add to existing middlewares and filter out potential duplicates
395
- this._options.middlewares = this._options.middlewares?.concat(middleware.filter(mid => this._options.middlewares?.indexOf(mid) === -1));
396
- }
397
- }
398
- else {
399
- this._options.middlewares = middleware;
371
+ } else {
372
+ if (!this.grpcServer) {
373
+ await this.wait(1e3);
400
374
  }
375
+ addServiceToServer(this.grpcServer, protoPath, protoDescription, functions);
401
376
  return this;
377
+ }
402
378
  }
403
- path(path) {
404
- this._options.path = path;
405
- return this;
406
- }
407
- queryParams(params) {
408
- this._options.queryParams = params;
409
- return this;
410
- }
411
- urlParams(params) {
412
- this._options.urlParams = params;
413
- return this;
414
- }
415
- bodyParams(params) {
416
- this._options.bodyParams = params;
417
- return this;
418
- }
419
- return(name, fields) {
420
- this._returns = new grpcSdk.ConduitRouteReturnDefinition(name, fields);
421
- return this;
422
- }
423
- handler(fn) {
424
- this._handler = fn;
425
- return this;
426
- }
427
- add() {
428
- if (!this.manager)
429
- throw new Error('Builder not setup with manager');
430
- if (!this._options)
431
- throw new Error('Cannot build route without options');
432
- if (!this._options.action)
433
- throw new Error('Cannot build route without action');
434
- if (!this._options.path)
435
- throw new Error('Cannot build route without action');
436
- if (!this._returns)
437
- throw new Error('Cannot build route without return');
438
- if (!this._handler)
439
- throw new Error('Cannot build route without handler');
440
- this.manager.route(this._options, this._returns, this._handler);
441
- }
442
- build() {
443
- if (!this._options)
444
- throw new Error('Cannot build route without options');
445
- if (!this._options.action)
446
- throw new Error('Cannot build route without action');
447
- if (!this._options.path)
448
- throw new Error('Cannot build route without action');
449
- if (!this._returns)
450
- throw new Error('Cannot build route without return');
451
- if (!this._handler)
452
- throw new Error('Cannot build route without handler');
453
- return {
454
- input: this._options,
455
- returnType: this._returns,
456
- handler: this._handler,
457
- };
458
- }
459
- }
379
+ }
380
+ async wait(time) {
381
+ return new Promise((resolve, reject) => {
382
+ const timeout = setTimeout(() => {
383
+ clearTimeout(timeout);
384
+ resolve();
385
+ }, time);
386
+ });
387
+ }
388
+ async refresh() {
389
+ if (this.started) {
390
+ this.started = false;
391
+ if (this._useForce) {
392
+ this.grpcServer.forceShutdown();
393
+ } else {
394
+ await new Promise((resolve) => this.grpcServer.tryShutdown(() => resolve()));
395
+ }
396
+ }
397
+ await this.createNewServer();
398
+ this._services.forEach((service) => {
399
+ addServiceToServer(
400
+ this.grpcServer,
401
+ service.protoPath,
402
+ service.protoDescription,
403
+ service.functions
404
+ );
405
+ });
406
+ if (!this.started && this.startedOnce) {
407
+ this.grpcServer.start();
408
+ this.started = true;
409
+ }
410
+ }
411
+ scheduleRefresh() {
412
+ if (this.scheduledRestart) {
413
+ clearTimeout(this.scheduledRestart);
414
+ }
415
+ const self = this;
416
+ this.scheduledRestart = setTimeout(async () => {
417
+ import_grpc_sdk4.ConduitGrpcSdk.Logger.log("Begin refresh");
418
+ await self.refresh();
419
+ import_grpc_sdk4.ConduitGrpcSdk.Logger.log("Refresh complete");
420
+ clearTimeout(self.scheduledRestart);
421
+ self.scheduledRestart = void 0;
422
+ self._postponeRequests = 0;
423
+ }, 2e3);
424
+ }
425
+ start() {
426
+ if (this.started) {
427
+ import_grpc_sdk4.ConduitGrpcSdk.Logger.error("gRPC server is already running!");
428
+ return;
429
+ } else if (!this.started && this.startedOnce) {
430
+ import_grpc_sdk4.ConduitGrpcSdk.Logger.error("gRPC server is down for refresh!");
431
+ }
432
+ this.started = true;
433
+ this.startedOnce = true;
434
+ this.grpcServer?.start();
435
+ }
436
+ postponeRestart() {
437
+ if (!this.scheduledRestart || this._postponeRequests > 5) return;
438
+ this._postponeRequests++;
439
+ this.scheduleRefresh();
440
+ }
441
+ };
460
442
 
461
- const mapGrpcStatusToHttp = (code) => {
462
- if (!code)
463
- return '200 (OK)';
464
- switch (code) {
465
- case grpcJs.status.OK:
466
- return `200 (OK)`;
467
- case grpcJs.status.CANCELLED:
468
- return `499 (Cancelled)`;
469
- case grpcJs.status.INVALID_ARGUMENT:
470
- return `400 (Invalid Argument)`;
471
- case grpcJs.status.DEADLINE_EXCEEDED:
472
- return `504 (Deadline Exceeded)`;
473
- case grpcJs.status.NOT_FOUND:
474
- return `404 (Not Found)`;
475
- case grpcJs.status.ALREADY_EXISTS:
476
- return `409 (Already Exists)`;
477
- case grpcJs.status.PERMISSION_DENIED:
478
- return `403 (Permission Denied)`;
479
- case grpcJs.status.UNAUTHENTICATED:
480
- return `401 (Unauthenticated)`;
481
- case grpcJs.status.OUT_OF_RANGE:
482
- return `416 (Out of Range)`;
483
- case grpcJs.status.FAILED_PRECONDITION:
484
- return `412 (Failed Precondition)`;
485
- case grpcJs.status.ABORTED:
486
- return `409 (Aborted)`;
487
- case grpcJs.status.INTERNAL:
488
- return `416 (Internal)`;
489
- case grpcJs.status.UNAVAILABLE:
490
- return `503 (Service Unavailable)`;
491
- case grpcJs.status.DATA_LOSS:
492
- return `500 (Data Loss)`;
493
- default:
494
- return `500 (Unknown)`;
495
- }
443
+ // src/classes/ConduitServiceModule.ts
444
+ var import_path = __toESM(require("path"), 1);
445
+ var import_events = require("events");
446
+ var import_lodash_es2 = require("lodash-es");
447
+ var import_grpc_js4 = require("@grpc/grpc-js");
448
+ var import_grpc_sdk8 = require("@conduitplatform/grpc-sdk");
449
+
450
+ // src/routing/RouteBuilder.ts
451
+ var import_grpc_sdk5 = require("@conduitplatform/grpc-sdk");
452
+ var RouteBuilder = class {
453
+ constructor(manager) {
454
+ this.manager = manager;
455
+ this._options = {};
456
+ }
457
+ _options;
458
+ _returns;
459
+ _handler;
460
+ method(action) {
461
+ this._options.action = action;
462
+ return this;
463
+ }
464
+ name(name) {
465
+ this._options.name = name;
466
+ return this;
467
+ }
468
+ description(description) {
469
+ this._options.description = description;
470
+ return this;
471
+ }
472
+ cacheControl(cache) {
473
+ this._options.cacheControl = cache;
474
+ return this;
475
+ }
476
+ middleware(middleware, allowDuplicates = false) {
477
+ if (!Array.isArray(middleware)) {
478
+ middleware = [middleware];
479
+ }
480
+ if (this._options.middlewares !== void 0 && this._options.middlewares?.length !== 0) {
481
+ if (allowDuplicates) {
482
+ this._options.middlewares = this._options.middlewares?.concat(middleware);
483
+ } else {
484
+ this._options.middlewares = this._options.middlewares?.concat(
485
+ middleware.filter((mid) => this._options.middlewares?.indexOf(mid) === -1)
486
+ );
487
+ }
488
+ } else {
489
+ this._options.middlewares = middleware;
490
+ }
491
+ return this;
492
+ }
493
+ path(path4) {
494
+ this._options.path = path4;
495
+ return this;
496
+ }
497
+ queryParams(params) {
498
+ this._options.queryParams = params;
499
+ return this;
500
+ }
501
+ urlParams(params) {
502
+ this._options.urlParams = params;
503
+ return this;
504
+ }
505
+ bodyParams(params) {
506
+ this._options.bodyParams = params;
507
+ return this;
508
+ }
509
+ strictParams(strict) {
510
+ this._options.strictParams = strict;
511
+ return this;
512
+ }
513
+ rateLimit(maxRequests, resetInterval) {
514
+ this._options.rateLimit = { maxRequests, resetInterval };
515
+ return this;
516
+ }
517
+ return(name, fields) {
518
+ this._returns = new import_grpc_sdk5.ConduitRouteReturnDefinition(name, fields);
519
+ return this;
520
+ }
521
+ handler(fn) {
522
+ this._handler = fn;
523
+ return this;
524
+ }
525
+ add() {
526
+ if (!this.manager) throw new Error("Builder not setup with manager");
527
+ if (!this._options) throw new Error("Cannot build route without options");
528
+ if (!this._options.action) throw new Error("Cannot build route without action");
529
+ if (!this._options.path) throw new Error("Cannot build route without action");
530
+ if (!this._returns) throw new Error("Cannot build route without return");
531
+ if (!this._handler) throw new Error("Cannot build route without handler");
532
+ this.manager.route(this._options, this._returns, this._handler);
533
+ }
534
+ build() {
535
+ if (!this._options) throw new Error("Cannot build route without options");
536
+ if (!this._options.action) throw new Error("Cannot build route without action");
537
+ if (!this._options.path) throw new Error("Cannot build route without action");
538
+ if (!this._returns) throw new Error("Cannot build route without return");
539
+ if (!this._handler) throw new Error("Cannot build route without handler");
540
+ return {
541
+ input: this._options,
542
+ returnType: this._returns,
543
+ handler: this._handler
544
+ };
545
+ }
496
546
  };
497
- function generateLog(routerType, routerRequest, requestReceive, call, status) {
498
- let log = '';
499
- const latency = Date.now() - requestReceive;
500
- if (routerRequest) {
501
- log += `(${routerType}) Request: ${call.request.path}`;
502
- }
503
- else {
504
- const socketReq = call.request;
505
- const socketLabel = socketReq.socketId ?? socketReq.socket;
506
- log += `(${routerType}) Socket: ${socketReq.event} socket: ${socketLabel}`;
507
- }
508
- log += ` ${mapGrpcStatusToHttp(status) ?? '200'} ${latency}ms`;
509
- grpcSdk.ConduitGrpcSdk.Logger.log(log);
510
- grpcSdk.ConduitGrpcSdk.Metrics?.set('grpc_request_latency_seconds', latency / 1000);
511
- grpcSdk.ConduitGrpcSdk.Metrics?.set(`${routerType}_grpc_request_latency_seconds`, latency / 1000);
512
- const successStatus = !status || status.toString().startsWith('2');
513
- grpcSdk.ConduitGrpcSdk.Metrics?.increment(`${routerType}_grpc_response_statuses_total`, 1, {
514
- success: successStatus ? 'true' : 'false',
515
- });
547
+
548
+ // src/routing/wrapRouterFunctions.ts
549
+ var import_grpc_sdk6 = require("@conduitplatform/grpc-sdk");
550
+ var import_grpc_js3 = require("@grpc/grpc-js");
551
+ var mapGrpcStatusToHttp = (code) => {
552
+ if (!code) return "200 (OK)";
553
+ switch (code) {
554
+ case import_grpc_js3.status.OK:
555
+ return `200 (OK)`;
556
+ case import_grpc_js3.status.CANCELLED:
557
+ return `499 (Cancelled)`;
558
+ case import_grpc_js3.status.INVALID_ARGUMENT:
559
+ return `400 (Invalid Argument)`;
560
+ case import_grpc_js3.status.DEADLINE_EXCEEDED:
561
+ return `504 (Deadline Exceeded)`;
562
+ case import_grpc_js3.status.NOT_FOUND:
563
+ return `404 (Not Found)`;
564
+ case import_grpc_js3.status.ALREADY_EXISTS:
565
+ return `409 (Already Exists)`;
566
+ case import_grpc_js3.status.PERMISSION_DENIED:
567
+ return `403 (Permission Denied)`;
568
+ case import_grpc_js3.status.UNAUTHENTICATED:
569
+ return `401 (Unauthenticated)`;
570
+ case import_grpc_js3.status.OUT_OF_RANGE:
571
+ return `416 (Out of Range)`;
572
+ case import_grpc_js3.status.FAILED_PRECONDITION:
573
+ return `412 (Failed Precondition)`;
574
+ case import_grpc_js3.status.ABORTED:
575
+ return `409 (Aborted)`;
576
+ case import_grpc_js3.status.INTERNAL:
577
+ return `416 (Internal)`;
578
+ case import_grpc_js3.status.UNAVAILABLE:
579
+ return `503 (Service Unavailable)`;
580
+ case import_grpc_js3.status.DATA_LOSS:
581
+ return `500 (Data Loss)`;
582
+ default:
583
+ return `500 (Unknown)`;
584
+ }
585
+ };
586
+ function generateLog(routerType, routerRequest, requestReceive, call, status6) {
587
+ let log = "";
588
+ const latency = Date.now() - requestReceive;
589
+ if (routerRequest) {
590
+ log += `(${routerType}) Request: ${call.request.path}`;
591
+ } else {
592
+ const socketReq = call.request;
593
+ const socketLabel = socketReq.socketId ?? socketReq.socket;
594
+ log += `(${routerType}) Socket: ${socketReq.event} socket: ${socketLabel}`;
595
+ }
596
+ log += ` ${mapGrpcStatusToHttp(status6) ?? "200"} ${latency}ms`;
597
+ import_grpc_sdk6.ConduitGrpcSdk.Logger.log(log);
598
+ import_grpc_sdk6.ConduitGrpcSdk.Metrics?.set("grpc_request_latency_seconds", latency / 1e3);
599
+ import_grpc_sdk6.ConduitGrpcSdk.Metrics?.set(
600
+ `${routerType}_grpc_request_latency_seconds`,
601
+ latency / 1e3
602
+ );
603
+ const successStatus = !status6 || status6.toString().startsWith("2");
604
+ import_grpc_sdk6.ConduitGrpcSdk.Metrics?.increment(`${routerType}_grpc_response_statuses_total`, 1, {
605
+ success: successStatus ? "true" : "false"
606
+ });
516
607
  }
517
608
  function parseRequestData(data) {
518
- if (data && data.length !== 0) {
519
- return JSON.parse(data);
520
- }
521
- else {
522
- return {};
523
- }
609
+ if (data && data.length !== 0) {
610
+ return JSON.parse(data);
611
+ } else {
612
+ return {};
613
+ }
524
614
  }
525
615
  function parseResponseData(r, routerRequest, requestReceive, call, callback, responded, routerType) {
526
- if (responded.did)
527
- return;
528
- responded.did = true;
529
- if (!r) {
530
- callback({
531
- code: grpcJs.status.INTERNAL,
532
- message: 'Handler did not return a response',
616
+ if (responded.did) return;
617
+ responded.did = true;
618
+ if (!r) {
619
+ callback({
620
+ code: import_grpc_js3.status.INTERNAL,
621
+ message: "Handler did not return a response"
622
+ });
623
+ }
624
+ if (routerRequest) {
625
+ if (typeof r === "string") {
626
+ callback(null, { result: r });
627
+ } else {
628
+ let respObject;
629
+ if (r.removeCookies || r.setCookies) {
630
+ respObject = {
631
+ removeCookies: r.removeCookies,
632
+ setCookies: r.setCookies
633
+ };
634
+ }
635
+ if (r.result || r.redirect) {
636
+ callback(null, {
637
+ ...respObject,
638
+ redirect: r.redirect ?? void 0,
639
+ result: r.result ? typeof r.result === "string" ? r.result : JSON.stringify(r.result) : void 0
533
640
  });
534
- }
535
- if (routerRequest) {
536
- if (typeof r === 'string') {
537
- callback(null, { result: r });
538
- }
539
- else {
540
- let respObject;
541
- if (r.removeCookies || r.setCookies) {
542
- respObject = {
543
- removeCookies: r.removeCookies,
544
- setCookies: r.setCookies,
545
- };
546
- }
547
- if (r.result || r.redirect) {
548
- callback(null, {
549
- ...respObject,
550
- redirect: r.redirect ?? undefined,
551
- result: r.result
552
- ? typeof r.result === 'string'
553
- ? r.result
554
- : JSON.stringify(r.result)
555
- : undefined,
556
- });
557
- }
558
- else {
559
- callback(null, { ...respObject, result: JSON.stringify(r) });
560
- }
561
- }
562
- }
563
- else {
564
- if (r.hasOwnProperty('data'))
565
- r.data = JSON.stringify(r.data);
566
- callback(null, r);
567
- }
568
- generateLog(routerType, routerRequest, requestReceive, call, undefined);
641
+ } else {
642
+ callback(null, { ...respObject, result: JSON.stringify(r) });
643
+ }
644
+ }
645
+ } else {
646
+ if (r.hasOwnProperty("data")) r.data = JSON.stringify(r.data);
647
+ callback(null, r);
648
+ }
649
+ generateLog(routerType, routerRequest, requestReceive, call, void 0);
569
650
  }
570
651
  function wrapRouterGrpcFunction(fun, routerType) {
571
- return (call, callback) => {
572
- const requestReceive = Date.now();
573
- let routerRequest = true;
574
- grpcSdk.ConduitGrpcSdk.Metrics?.increment(`${routerType}_grpc_requests_total`);
575
- try {
576
- call.request.context = parseRequestData(call.request.context);
577
- call.request.params = parseRequestData(call.request.params);
578
- call.request.cookies = parseRequestData(call.request.cookies);
579
- call.request.urlParams = parseRequestData(call.request.urlParams);
580
- call.request.queryParams = parseRequestData(call.request.queryParams);
581
- call.request.bodyParams = parseRequestData(call.request.bodyParams);
582
- routerRequest = !call.request.hasOwnProperty('event');
583
- if (routerRequest) {
584
- call.request.headers = parseRequestData(call.request.headers);
585
- }
586
- }
587
- catch (e) {
588
- grpcSdk.ConduitGrpcSdk.Metrics?.increment(`${routerType}_grpc_errors_total`);
589
- generateLog(routerType, routerRequest, requestReceive, call, grpcJs.status.INTERNAL);
590
- grpcSdk.ConduitGrpcSdk.Logger.error(e.message ?? 'Something went wrong');
591
- return callback({
592
- code: grpcJs.status.INTERNAL,
593
- message: e.message ?? 'Something went wrong',
594
- });
595
- }
596
- const responded = { did: false };
597
- fun(call, r => {
598
- parseResponseData(r, routerRequest, requestReceive, call, callback, responded, routerType);
599
- })
600
- .then(r => parseResponseData(r, routerRequest, requestReceive, call, callback, responded, routerType))
601
- .catch(error => {
602
- grpcSdk.ConduitGrpcSdk.Metrics?.increment(`${routerType}_grpc_errors_total`);
603
- generateLog(routerType, routerRequest, requestReceive, call, error.code ?? grpcJs.status.INTERNAL);
604
- grpcSdk.ConduitGrpcSdk.Logger.error(error.debugLogInfo ?? error.message ?? 'Something went wrong');
605
- callback({
606
- code: error.code ?? grpcJs.status.INTERNAL,
607
- message: error.details ?? error.message ?? 'Something went wrong',
608
- });
609
- });
610
- };
652
+ return (call, callback) => {
653
+ const requestReceive = Date.now();
654
+ let routerRequest = true;
655
+ import_grpc_sdk6.ConduitGrpcSdk.Metrics?.increment(`${routerType}_grpc_requests_total`);
656
+ try {
657
+ call.request.context = parseRequestData(call.request.context);
658
+ call.request.params = parseRequestData(call.request.params);
659
+ call.request.cookies = parseRequestData(call.request.cookies);
660
+ call.request.urlParams = parseRequestData(call.request.urlParams);
661
+ call.request.queryParams = parseRequestData(call.request.queryParams);
662
+ call.request.bodyParams = parseRequestData(call.request.bodyParams);
663
+ routerRequest = !call.request.hasOwnProperty("event");
664
+ if (routerRequest) {
665
+ call.request.headers = parseRequestData(call.request.headers);
666
+ }
667
+ } catch (e) {
668
+ import_grpc_sdk6.ConduitGrpcSdk.Metrics?.increment(`${routerType}_grpc_errors_total`);
669
+ generateLog(routerType, routerRequest, requestReceive, call, import_grpc_js3.status.INTERNAL);
670
+ import_grpc_sdk6.ConduitGrpcSdk.Logger.error(e.message ?? "Something went wrong");
671
+ return callback({
672
+ code: import_grpc_js3.status.INTERNAL,
673
+ message: e.message ?? "Something went wrong"
674
+ });
675
+ }
676
+ const responded = { did: false };
677
+ fun(call, (r) => {
678
+ parseResponseData(
679
+ r,
680
+ routerRequest,
681
+ requestReceive,
682
+ call,
683
+ callback,
684
+ responded,
685
+ routerType
686
+ );
687
+ }).then(
688
+ (r) => parseResponseData(
689
+ r,
690
+ routerRequest,
691
+ requestReceive,
692
+ call,
693
+ callback,
694
+ responded,
695
+ routerType
696
+ )
697
+ ).catch((error) => {
698
+ import_grpc_sdk6.ConduitGrpcSdk.Metrics?.increment(`${routerType}_grpc_errors_total`);
699
+ generateLog(
700
+ routerType,
701
+ routerRequest,
702
+ requestReceive,
703
+ call,
704
+ error.code ?? import_grpc_js3.status.INTERNAL
705
+ );
706
+ import_grpc_sdk6.ConduitGrpcSdk.Logger.error(
707
+ error.debugLogInfo ?? error.message ?? "Something went wrong"
708
+ );
709
+ callback({
710
+ code: error.code ?? import_grpc_js3.status.INTERNAL,
711
+ message: error.details ?? error.message ?? "Something went wrong"
712
+ });
713
+ });
714
+ };
611
715
  }
612
716
 
717
+ // src/routing/RoutingUtilities.ts
613
718
  function wrapFunctionsAsync(functions, routerType) {
614
- const modifiedFunctions = {};
615
- Object.keys(functions).forEach(key => {
616
- modifiedFunctions[key] = wrapRouterGrpcFunction(functions[key], routerType);
617
- });
618
- return modifiedFunctions;
719
+ const modifiedFunctions = {};
720
+ Object.keys(functions).forEach((key) => {
721
+ modifiedFunctions[key] = wrapRouterGrpcFunction(functions[key], routerType);
722
+ });
723
+ return modifiedFunctions;
619
724
  }
620
725
 
621
- class ProxyRouteBuilder {
622
- manager;
623
- _options = { path: '' };
624
- _proxy = { target: '' };
625
- constructor(manager) {
626
- this.manager = manager;
627
- }
628
- method(action) {
629
- this._options.action = action;
630
- return this;
631
- }
632
- path(path) {
633
- this._options.path = path;
634
- return this;
635
- }
636
- target(target) {
637
- this._proxy.target = target;
638
- return this;
639
- }
640
- options(options) {
641
- this._options = options;
642
- return this;
643
- }
644
- proxy(proxy) {
645
- this._proxy = proxy;
646
- return this;
647
- }
648
- middleware(middleware, allowDuplicates = false) {
649
- if (!Array.isArray(middleware)) {
650
- middleware = [middleware];
651
- }
652
- if (this._options.middlewares) {
653
- if (allowDuplicates) {
654
- this._options.middlewares = this._options.middlewares.concat(middleware);
655
- }
656
- else {
657
- this._options.middlewares = this._options.middlewares.concat(middleware.filter(mid => this._options.middlewares?.indexOf(mid) === -1));
658
- }
659
- }
660
- else {
661
- this._options.middlewares = middleware;
662
- }
663
- return this;
664
- }
665
- add() {
666
- if (!this.manager)
667
- throw new Error('Builder not setup with manager');
668
- if (!this._options.path)
669
- throw new Error('Cannot build route without path');
670
- if (!this._proxy.target)
671
- throw new Error('Cannot build route without target');
672
- const input = {
673
- options: this._options,
674
- proxy: this._proxy,
675
- };
676
- this.manager.proxyRoute(input);
677
- }
678
- build() {
679
- if (!this.manager)
680
- throw new Error('Builder not setup with manager');
681
- if (!this._options.path)
682
- throw new Error('Cannot build route without path');
683
- if (!this._proxy.target)
684
- throw new Error('Cannot build route without target');
685
- if (!this._options.action)
686
- throw new Error('Cannot build route without action');
687
- return {
688
- input: {
689
- options: this._options,
690
- proxy: this._proxy,
691
- },
692
- };
693
- }
694
- }
726
+ // src/routing/RoutingManager.ts
727
+ var import_grpc_sdk7 = require("@conduitplatform/grpc-sdk");
728
+ var RoutingManager = class _RoutingManager {
729
+ constructor(_router, _server) {
730
+ this._router = _router;
731
+ this._server = _server;
732
+ if (_router instanceof import_grpc_sdk7.Admin) {
733
+ this.isAdmin = true;
734
+ }
735
+ }
736
+ _moduleRoutes = {};
737
+ static ClientController;
738
+ static AdminController;
739
+ _routeHandlers = {};
740
+ isAdmin = false;
741
+ get(path4) {
742
+ return new RouteBuilder(this).method(import_grpc_sdk7.ConduitRouteActions.GET).path(path4);
743
+ }
744
+ post(path4) {
745
+ return new RouteBuilder(this).method(import_grpc_sdk7.ConduitRouteActions.POST).path(path4);
746
+ }
747
+ delete(path4) {
748
+ return new RouteBuilder(this).method(import_grpc_sdk7.ConduitRouteActions.DELETE).path(path4);
749
+ }
750
+ update(path4) {
751
+ return new RouteBuilder(this).method(import_grpc_sdk7.ConduitRouteActions.UPDATE).path(path4);
752
+ }
753
+ patch(path4) {
754
+ return new RouteBuilder(this).method(import_grpc_sdk7.ConduitRouteActions.PATCH).path(path4);
755
+ }
756
+ clear() {
757
+ this._moduleRoutes = {};
758
+ this._routeHandlers = {};
759
+ }
760
+ middleware(input, handler) {
761
+ const routeObject = this.parseRouteObject({
762
+ options: input,
763
+ grpcFunction: input.name
764
+ });
765
+ this._moduleRoutes[routeObject.grpcFunction] = routeObject;
766
+ this._routeHandlers[routeObject.grpcFunction] = handler;
767
+ }
768
+ route(input, type, handler) {
769
+ const routeObject = this.parseRouteObject({
770
+ options: input,
771
+ returns: {
772
+ name: type.name,
773
+ fields: JSON.stringify(type.fields)
774
+ },
775
+ grpcFunction: this.generateGrpcName(input)
776
+ });
777
+ this._moduleRoutes[routeObject.grpcFunction] = routeObject;
778
+ this._routeHandlers[routeObject.grpcFunction] = handler;
779
+ }
780
+ socket(input, events) {
781
+ const eventsObj = {};
782
+ const routeObject = this.parseRouteObject({
783
+ options: input,
784
+ events: ""
785
+ });
786
+ let primary;
787
+ Object.keys(events).forEach((eventName) => {
788
+ if (!primary) primary = eventName;
789
+ const event = events[eventName];
790
+ eventsObj[eventName] = {
791
+ grpcFunction: eventName,
792
+ params: JSON.stringify(event.params),
793
+ returns: {
794
+ name: event.returnType?.name ?? "",
795
+ fields: JSON.stringify(event.returnType?.fields)
796
+ }
797
+ };
798
+ this._routeHandlers[eventName] = event.handler;
799
+ });
800
+ routeObject.events = JSON.stringify(eventsObj);
801
+ this._moduleRoutes[primary] = routeObject;
802
+ }
803
+ async registerRoutes() {
804
+ if (Object.keys(this._routeHandlers).length === 0) return;
805
+ const modifiedFunctions = wrapFunctionsAsync(this._routeHandlers, this.isAdmin ? "admin" : "client");
806
+ if (this.isAdmin) {
807
+ _RoutingManager.AdminController.setRoutes(modifiedFunctions);
808
+ } else {
809
+ _RoutingManager.ClientController.setRoutes(modifiedFunctions);
810
+ }
811
+ const paths = Object.values(this._moduleRoutes);
812
+ return this._router.register(paths);
813
+ }
814
+ generateGrpcName(options) {
815
+ if (options.name) {
816
+ return options.name.charAt(0).toUpperCase() + options.name.slice(1);
817
+ } else {
818
+ const name = options.action.charAt(0) + options.action.slice(1).toLowerCase() + this.extractNameFromPath(options.path);
819
+ return name.charAt(0).toUpperCase() + name.slice(1);
820
+ }
821
+ }
822
+ extractNameFromPath(path4) {
823
+ path4 = path4.replace(/[-:]/g, "/");
824
+ return path4.split("/").map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("");
825
+ }
826
+ parseRouteObject(routeObject) {
827
+ if (!routeObject.options.middlewares) {
828
+ routeObject.options.middlewares = [];
829
+ }
830
+ for (const option in routeObject.options) {
831
+ if (!routeObject.options.hasOwnProperty(option)) continue;
832
+ if (option === "middlewares" || option === "errors") continue;
833
+ if (typeof routeObject.options[option] === "string" || routeObject.options[option] instanceof String)
834
+ continue;
835
+ if (typeof routeObject.options[option] === "boolean" || routeObject.options[option] instanceof Boolean)
836
+ continue;
837
+ routeObject.options[option] = JSON.stringify(routeObject.options[option]);
838
+ }
839
+ return routeObject;
840
+ }
841
+ };
695
842
 
696
- class RoutingManager {
697
- _router;
698
- _server;
699
- _moduleRoutes = {};
700
- _moduleProxyRoutes = {};
701
- static ClientController;
702
- static AdminController;
703
- _routeHandlers = {};
704
- isAdmin = false;
705
- constructor(_router, _server) {
706
- this._router = _router;
707
- this._server = _server;
708
- if (_router instanceof grpcSdk.Admin) {
709
- this.isAdmin = true;
710
- }
711
- }
712
- get(path) {
713
- return new RouteBuilder(this).method(grpcSdk.ConduitRouteActions.GET).path(path);
714
- }
715
- post(path) {
716
- return new RouteBuilder(this).method(grpcSdk.ConduitRouteActions.POST).path(path);
717
- }
718
- delete(path) {
719
- return new RouteBuilder(this).method(grpcSdk.ConduitRouteActions.DELETE).path(path);
720
- }
721
- update(path) {
722
- return new RouteBuilder(this).method(grpcSdk.ConduitRouteActions.UPDATE).path(path);
723
- }
724
- patch(path) {
725
- return new RouteBuilder(this).method(grpcSdk.ConduitRouteActions.PATCH).path(path);
726
- }
727
- proxy() {
728
- return new ProxyRouteBuilder(this);
729
- }
730
- clear() {
731
- this._moduleRoutes = {};
732
- this._routeHandlers = {};
733
- this._moduleProxyRoutes = {};
734
- }
735
- middleware(input, handler) {
736
- const routeObject = this.parseRouteObject({
737
- options: input,
738
- grpcFunction: input.name,
739
- });
740
- this._moduleRoutes[routeObject.grpcFunction] = routeObject;
741
- this._routeHandlers[routeObject.grpcFunction] = handler;
742
- }
743
- route(input, type, handler) {
744
- const routeObject = this.parseRouteObject({
745
- options: input,
746
- returns: {
747
- name: type.name,
748
- fields: JSON.stringify(type.fields),
749
- },
750
- grpcFunction: this.generateGrpcName(input),
843
+ // src/classes/ConduitServiceModule.ts
844
+ var ConduitServiceModule = class {
845
+ _moduleName;
846
+ _serviceName;
847
+ _address;
848
+ // external address:port of service (LoadBalancer)
849
+ grpcServer;
850
+ events = new import_events.EventEmitter();
851
+ _serviceHealthState = import_grpc_sdk8.HealthCheckStatus.SERVING;
852
+ // default for health-agnostic modules
853
+ constructor(moduleName) {
854
+ this._moduleName = (0, import_lodash_es2.camelCase)(moduleName);
855
+ }
856
+ _registered = false;
857
+ set registered(registered) {
858
+ this._registered = registered;
859
+ }
860
+ _port;
861
+ // port to bring up gRPC service
862
+ get port() {
863
+ return this._port;
864
+ }
865
+ _grpcSdk;
866
+ get grpcSdk() {
867
+ if (!this._grpcSdk) throw new Error("grpcSdk not defined yet");
868
+ return this._grpcSdk;
869
+ }
870
+ set grpcSdk(grpcSdk) {
871
+ if (this._grpcSdk) throw new Error("grpcSdk already defined");
872
+ this._grpcSdk = grpcSdk;
873
+ }
874
+ get healthState() {
875
+ return this._serviceHealthState;
876
+ }
877
+ updateHealth(state, init = false) {
878
+ if (state === import_grpc_sdk8.HealthCheckStatus.UNKNOWN && !init || state === import_grpc_sdk8.HealthCheckStatus.SERVICE_UNKNOWN) {
879
+ throw new Error(
880
+ `Cannot explicitly set gRPC health state to ${import_grpc_sdk8.HealthCheckStatus[state]}`
881
+ );
882
+ }
883
+ if (!init) {
884
+ import_grpc_sdk8.ConduitGrpcSdk.Metrics?.set(
885
+ "module_health_state",
886
+ state === import_grpc_sdk8.HealthCheckStatus.SERVING ? 1 : 0
887
+ );
888
+ }
889
+ if (init) {
890
+ this._serviceHealthState = state;
891
+ return;
892
+ }
893
+ if (this._serviceHealthState !== state) {
894
+ this._serviceHealthState = state;
895
+ if (this._registered) {
896
+ this.events.emit(`grpc-health-change:${this._serviceName}`, state);
897
+ this._grpcSdk?.config.moduleHealthProbe(this._moduleName, this._address);
898
+ }
899
+ }
900
+ }
901
+ healthCheck(call, callback) {
902
+ const service = call.request.service.substring(call.request.service.indexOf(".") + 1);
903
+ if (service && service !== this._serviceName) {
904
+ callback(null, {
905
+ status: import_grpc_sdk8.HealthCheckStatus.SERVICE_UNKNOWN
906
+ });
907
+ } else {
908
+ if (!this._registered) {
909
+ return callback(null, {
910
+ status: import_grpc_sdk8.HealthCheckResponse_ServingStatus.UNKNOWN
751
911
  });
752
- this._moduleRoutes[routeObject.grpcFunction] = routeObject;
753
- this._routeHandlers[routeObject.grpcFunction] = handler;
912
+ }
913
+ callback(null, {
914
+ status: this._serviceHealthState
915
+ });
916
+ }
917
+ }
918
+ healthWatch(call) {
919
+ const service = call.request.service.substring(call.request.service.indexOf(".") + 1);
920
+ if (service && service !== this._serviceName) {
921
+ call.write({
922
+ status: import_grpc_sdk8.HealthCheckStatus.SERVICE_UNKNOWN
923
+ });
924
+ } else {
925
+ this.events.on(
926
+ `grpc-health-change:${this._serviceName}`,
927
+ (status6) => {
928
+ call.write({
929
+ status: status6
930
+ });
931
+ }
932
+ );
933
+ }
934
+ }
935
+ /** Override to declare which resource types this module can export/import. Default: none. */
936
+ getExportableResources() {
937
+ return [];
938
+ }
939
+ /** Override to export resources. resourceTypes empty = export all. Default: empty. */
940
+ async exportResources(_resourceTypes) {
941
+ return {};
942
+ }
943
+ /** Override to import resources. Default: no-op. */
944
+ async importResources(_resources) {
945
+ return {};
946
+ }
947
+ getExportableResourcesHandler(_call, callback) {
948
+ try {
949
+ const resources = this.getExportableResources();
950
+ callback(null, { resources: JSON.stringify(resources) });
951
+ } catch (e) {
952
+ callback({ code: import_grpc_js4.status.INTERNAL, message: e.message });
754
953
  }
755
- proxyRoute(input) {
756
- const routeObject = this.parseRouteObject({
757
- options: input,
758
- });
759
- this._moduleProxyRoutes[routeObject.options.path + routeObject.proxy.target] =
760
- routeObject;
761
- }
762
- socket(input, events) {
763
- const eventsObj = {};
764
- const routeObject = this.parseRouteObject({
765
- options: input,
766
- events: '',
767
- });
768
- let primary;
769
- Object.keys(events).forEach((eventName) => {
770
- if (!primary)
771
- primary = eventName;
772
- const event = events[eventName];
773
- eventsObj[eventName] = {
774
- grpcFunction: eventName,
775
- params: JSON.stringify(event.params),
776
- returns: {
777
- name: event.returnType?.name ?? '',
778
- fields: JSON.stringify(event.returnType?.fields),
779
- },
780
- };
781
- this._routeHandlers[eventName] = event.handler;
954
+ }
955
+ async exportResourcesHandler(call, callback) {
956
+ try {
957
+ const resourceTypes = call.request.resourceTypes ?? [];
958
+ const data = await this.exportResources(resourceTypes);
959
+ callback(null, { data: JSON.stringify(data) });
960
+ } catch (e) {
961
+ callback({ code: import_grpc_js4.status.INTERNAL, message: e.message });
962
+ }
963
+ }
964
+ async importResourcesHandler(call, callback) {
965
+ try {
966
+ const data = JSON.parse(call.request.data);
967
+ const result = await this.importResources(data);
968
+ callback(null, { result: JSON.stringify(result) });
969
+ } catch (e) {
970
+ const err = e;
971
+ if (err instanceof SyntaxError) {
972
+ callback({
973
+ code: import_grpc_js4.status.INVALID_ARGUMENT,
974
+ message: "Invalid JSON in import data"
782
975
  });
783
- routeObject.events = JSON.stringify(eventsObj);
784
- this._moduleRoutes[primary] = routeObject;
785
- }
786
- async registerRoutes() {
787
- if (Object.keys(this._routeHandlers).length === 0)
788
- return;
789
- const modifiedFunctions = wrapFunctionsAsync(this._routeHandlers, this.isAdmin ? 'admin' : 'client');
790
- if (this.isAdmin) {
791
- RoutingManager.AdminController.setRoutes(modifiedFunctions);
792
- }
793
- else {
794
- RoutingManager.ClientController.setRoutes(modifiedFunctions);
795
- }
796
- const paths = Object.values(this._moduleRoutes);
797
- return this._router.register(paths);
798
- }
799
- generateGrpcName(options) {
800
- if (options.name) {
801
- return options.name.charAt(0).toUpperCase() + options.name.slice(1);
802
- }
803
- else {
804
- const name = options.action.charAt(0) +
805
- options.action.slice(1).toLowerCase() +
806
- this.extractNameFromPath(options.path);
807
- return name.charAt(0).toUpperCase() + name.slice(1);
808
- }
809
- }
810
- extractNameFromPath(path) {
811
- path = path.replace(/[-:]/g, '/');
812
- return path
813
- .split('/')
814
- .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
815
- .join('');
816
- }
817
- parseRouteObject(routeObject) {
818
- if (!routeObject.options.middlewares) {
819
- routeObject.options.middlewares = [];
820
- }
821
- for (const option in routeObject.options) {
822
- if (!routeObject.options.hasOwnProperty(option))
823
- continue;
824
- if (option === 'middlewares' || option === 'errors')
825
- continue;
826
- if (typeof routeObject.options[option] === 'string' ||
827
- routeObject.options[option] instanceof String)
828
- continue;
829
- routeObject.options[option] = JSON.stringify(routeObject.options[option]);
830
- }
831
- return routeObject;
832
- }
833
- }
976
+ } else {
977
+ callback({ code: import_grpc_js4.status.INTERNAL, message: err.message });
978
+ }
979
+ }
980
+ }
981
+ async addHealthCheckService() {
982
+ await this.grpcServer.addService(
983
+ import_path.default.resolve(__dirname, "./grpc_health_check.proto"),
984
+ "grpc.health.v1.Health",
985
+ {
986
+ Check: this.healthCheck.bind(this),
987
+ Watch: this.healthWatch.bind(this)
988
+ },
989
+ true
990
+ );
991
+ }
992
+ async addModuleService() {
993
+ await this.grpcServer.addService(
994
+ import_path.default.resolve(__dirname, "./module.proto"),
995
+ "conduit.module.v1.ConduitModule",
996
+ {
997
+ SetConfig: this.setConfig.bind(this),
998
+ GetExportableResources: this.getExportableResourcesHandler.bind(this),
999
+ ExportResources: this.exportResourcesHandler.bind(this),
1000
+ ImportResources: this.importResourcesHandler.bind(this)
1001
+ }
1002
+ );
1003
+ await this.grpcServer.addService(
1004
+ import_path.default.resolve(__dirname, "./module.proto"),
1005
+ "conduit.module.v1.ClientRouter",
1006
+ {
1007
+ Route: RoutingManager.ClientController.handleRequest.bind(
1008
+ RoutingManager.ClientController
1009
+ ),
1010
+ SocketRoute: RoutingManager.ClientController.handleRequest.bind(
1011
+ RoutingManager.ClientController
1012
+ )
1013
+ }
1014
+ );
1015
+ await this.grpcServer.addService(
1016
+ import_path.default.resolve(__dirname, "./module.proto"),
1017
+ "conduit.module.v1.AdminRouter",
1018
+ {
1019
+ Route: RoutingManager.AdminController.handleRequest.bind(
1020
+ RoutingManager.AdminController
1021
+ ),
1022
+ SocketRoute: RoutingManager.AdminController.handleRequest.bind(
1023
+ RoutingManager.AdminController
1024
+ )
1025
+ }
1026
+ );
1027
+ }
1028
+ };
834
1029
 
835
- const __filename$2 = node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
836
- const __dirname$2 = path.dirname(__filename$2);
837
- class ConduitServiceModule {
838
- _moduleName;
839
- _serviceName;
840
- _address; // external address:port of service (LoadBalancer)
841
- grpcServer;
842
- events = new events.EventEmitter();
843
- _serviceHealthState = grpcSdk.HealthCheckStatus.SERVING; // default for health-agnostic modules
844
- constructor(moduleName) {
845
- this._moduleName = lodash.camelCase(moduleName);
846
- }
847
- _registered = false;
848
- set registered(registered) {
849
- this._registered = registered;
850
- }
851
- _port; // port to bring up gRPC service
852
- get port() {
853
- return this._port;
854
- }
855
- _grpcSdk;
856
- get grpcSdk() {
857
- if (!this._grpcSdk)
858
- throw new Error('grpcSdk not defined yet');
859
- return this._grpcSdk;
860
- }
861
- set grpcSdk(grpcSdk) {
862
- if (this._grpcSdk)
863
- throw new Error('grpcSdk already defined');
864
- this._grpcSdk = grpcSdk;
865
- }
866
- get healthState() {
867
- return this._serviceHealthState;
868
- }
869
- updateHealth(state, init = false) {
870
- if ((state === grpcSdk.HealthCheckStatus.UNKNOWN && !init) ||
871
- state === grpcSdk.HealthCheckStatus.SERVICE_UNKNOWN) {
872
- throw new Error(`Cannot explicitly set gRPC health state to ${grpcSdk.HealthCheckStatus[state]}`);
873
- }
874
- if (!init) {
875
- grpcSdk.ConduitGrpcSdk.Metrics?.set('module_health_state', state === grpcSdk.HealthCheckStatus.SERVING ? 1 : 0);
876
- }
877
- if (init) {
878
- this._serviceHealthState = state;
879
- return;
880
- }
881
- if (this._serviceHealthState !== state) {
882
- this._serviceHealthState = state;
883
- // do not emit health updates until registered
884
- if (this._registered) {
885
- this.events.emit(`grpc-health-change:${this._serviceName}`, state);
886
- this._grpcSdk?.config.moduleHealthProbe(this._moduleName, this._address);
887
- }
888
- }
889
- }
890
- healthCheck(call, callback) {
891
- const service = call.request.service.substring(call.request.service.indexOf('.') + 1);
892
- if (service && service !== this._serviceName) {
893
- callback(null, {
894
- status: grpcSdk.HealthCheckStatus.SERVICE_UNKNOWN,
895
- });
896
- }
897
- else {
898
- if (!this._registered) {
899
- return callback(null, {
900
- status: grpcSdk.HealthCheckResponse_ServingStatus.UNKNOWN,
901
- });
902
- }
903
- callback(null, {
904
- status: this._serviceHealthState,
905
- });
906
- }
907
- }
908
- healthWatch(call) {
909
- const service = call.request.service.substring(call.request.service.indexOf('.') + 1);
910
- if (service && service !== this._serviceName) {
911
- call.write({
912
- status: grpcSdk.HealthCheckStatus.SERVICE_UNKNOWN,
913
- });
914
- }
915
- else {
916
- this.events.on(`grpc-health-change:${this._serviceName}`, (status) => {
917
- call.write({
918
- status: status,
919
- });
920
- });
921
- }
922
- }
923
- async addHealthCheckService() {
924
- await this.grpcServer.addService(path.resolve(__dirname$2, './grpc_health_check.proto'), 'grpc.health.v1.Health', {
925
- Check: this.healthCheck.bind(this),
926
- Watch: this.healthWatch.bind(this),
927
- }, true);
928
- }
929
- async addModuleService() {
930
- await this.grpcServer.addService(path.resolve(__dirname$2, './module.proto'), 'conduit.module.v1.ConduitModule', {
931
- SetConfig: this.setConfig.bind(this),
932
- });
933
- await this.grpcServer.addService(path.resolve(__dirname$2, './module.proto'), 'conduit.module.v1.ClientRouter', {
934
- Route: RoutingManager.ClientController.handleRequest.bind(RoutingManager.ClientController),
935
- SocketRoute: RoutingManager.ClientController.handleRequest.bind(RoutingManager.ClientController),
936
- });
937
- await this.grpcServer.addService(path.resolve(__dirname$2, './module.proto'), 'conduit.module.v1.AdminRouter', {
938
- Route: RoutingManager.AdminController.handleRequest.bind(RoutingManager.AdminController),
939
- SocketRoute: RoutingManager.AdminController.handleRequest.bind(RoutingManager.AdminController),
940
- });
941
- }
942
- }
1030
+ // src/classes/ConfigController.ts
1031
+ var ConfigController = class _ConfigController {
1032
+ static instance;
1033
+ _config;
1034
+ get config() {
1035
+ return Object.assign({}, this._config);
1036
+ }
1037
+ set config(config) {
1038
+ this._config = config;
1039
+ }
1040
+ static getInstance() {
1041
+ if (!_ConfigController.instance) {
1042
+ _ConfigController.instance = new _ConfigController();
1043
+ }
1044
+ return _ConfigController.instance;
1045
+ }
1046
+ };
943
1047
 
944
- class ConfigController {
945
- static instance;
946
- _config;
947
- get config() {
948
- // return copy of config and not the original object;
949
- return Object.assign({}, this._config);
950
- }
951
- set config(config) {
952
- this._config = config;
953
- }
954
- static getInstance() {
955
- if (!ConfigController.instance) {
956
- ConfigController.instance = new ConfigController();
957
- }
958
- return ConfigController.instance;
959
- }
960
- }
1048
+ // src/ManagedModule.ts
1049
+ var import_lodash_es6 = require("lodash-es");
1050
+ var import_grpc_js6 = require("@grpc/grpc-js");
1051
+ var import_convict = __toESM(require("convict"), 1);
961
1052
 
962
- exports.ModuleLifecycleStage = void 0;
963
- (function (ModuleLifecycleStage) {
964
- ModuleLifecycleStage["CREATE_GRPC"] = "creat-grpc-server";
965
- ModuleLifecycleStage["PRE_SERVER_START"] = "pre-grpc-start";
966
- ModuleLifecycleStage["SERVER_STARTED"] = "grpc-server-started";
967
- ModuleLifecycleStage["PRE_REGISTER"] = "pre-register";
968
- ModuleLifecycleStage["POST_REGISTER"] = "post-register";
969
- })(exports.ModuleLifecycleStage || (exports.ModuleLifecycleStage = {}));
1053
+ // src/interfaces/ModuleLifecycleStage.ts
1054
+ var ModuleLifecycleStage = /* @__PURE__ */ ((ModuleLifecycleStage2) => {
1055
+ ModuleLifecycleStage2["CREATE_GRPC"] = "creat-grpc-server";
1056
+ ModuleLifecycleStage2["PRE_SERVER_START"] = "pre-grpc-start";
1057
+ ModuleLifecycleStage2["SERVER_STARTED"] = "grpc-server-started";
1058
+ ModuleLifecycleStage2["PRE_REGISTER"] = "pre-register";
1059
+ ModuleLifecycleStage2["POST_REGISTER"] = "post-register";
1060
+ return ModuleLifecycleStage2;
1061
+ })(ModuleLifecycleStage || {});
970
1062
 
971
- const processMeta = (meta) => {
972
- if (Array.isArray(meta)) {
973
- return meta.reduce((message, meta) => `${message} \n ${JSON.stringify(meta, null, 2)}`, '');
974
- }
975
- else {
976
- return JSON.stringify(meta, null, 2);
977
- }
1063
+ // src/ManagedModule.ts
1064
+ var import_grpc_sdk14 = require("@conduitplatform/grpc-sdk");
1065
+
1066
+ // src/utilities/initializeSdk.ts
1067
+ var import_grpc_sdk13 = require("@conduitplatform/grpc-sdk");
1068
+
1069
+ // src/logging/index.ts
1070
+ var import_grpc_sdk9 = require("@conduitplatform/grpc-sdk");
1071
+ var import_winston = __toESM(require("winston"), 1);
1072
+ var import_lodash_es3 = require("lodash-es");
1073
+ var import_http = require("http");
1074
+ var import_winston_loki = __toESM(require("winston-loki"), 1);
1075
+
1076
+ // src/utilities/linearBackoffTimeout.ts
1077
+ var import_timers = require("timers");
1078
+ function linearBackoffTimeout(onTry, delay, reps, onFailure) {
1079
+ const nextRep = () => reps === void 0 || --reps > 0;
1080
+ let stopSignal = false;
1081
+ const stop = () => {
1082
+ stopSignal = true;
1083
+ };
1084
+ let timeout;
1085
+ const invoker = async () => {
1086
+ if (stopSignal) {
1087
+ return timeout ? (0, import_timers.clearTimeout)(timeout) : null;
1088
+ }
1089
+ delay = Math.floor(delay * 2);
1090
+ if (delay > 0 && nextRep()) {
1091
+ timeout = setTimeout(invoker, delay);
1092
+ onTry(stop);
1093
+ } else {
1094
+ timeout = null;
1095
+ onFailure && onFailure();
1096
+ }
1097
+ };
1098
+ timeout = setTimeout(invoker, delay);
1099
+ return {
1100
+ clear: () => {
1101
+ if (timeout) {
1102
+ (0, import_timers.clearTimeout)(timeout);
1103
+ timeout = null;
1104
+ }
1105
+ }
1106
+ };
1107
+ }
1108
+ async function linearBackoffTimeoutAsync(onTry, delay, reps, onFailure, startNow = false) {
1109
+ const nextRep = () => reps === void 0 || --reps > 0;
1110
+ const invoker = async () => {
1111
+ delay = Math.floor(delay * 2);
1112
+ if (delay > 0 && nextRep()) {
1113
+ if (await onTry()) setTimeout(invoker, delay);
1114
+ } else {
1115
+ onFailure && await onFailure();
1116
+ }
1117
+ };
1118
+ setTimeout(invoker, startNow ? 0 : delay);
1119
+ }
1120
+
1121
+ // src/logging/index.ts
1122
+ var processMeta = (meta) => {
1123
+ if (Array.isArray(meta)) {
1124
+ return meta.reduce(
1125
+ (message, meta2) => `${message}
1126
+ ${JSON.stringify(meta2, null, 2)}`,
1127
+ ""
1128
+ );
1129
+ } else {
1130
+ return JSON.stringify(meta, null, 2);
1131
+ }
978
1132
  };
979
- const createFormat = (logMeta = true) => {
980
- return winston.format.combine(winston.format.printf(info => {
981
- // This will customize the Error Message
982
- if (info.stack) {
983
- return `[${info.timestamp}] [${info.level.toUpperCase()}]: ${info.message} ${info.stack}`;
984
- }
985
- if (logMeta) {
986
- return `[${info.timestamp}] [${info.level.toUpperCase()}]: ${info.message} ${!lodash.isEmpty(info.meta) ? processMeta(info.meta) : ''}`;
987
- }
988
- else {
989
- return `[${info.timestamp}] [${info.level.toUpperCase()}]: ${info.message}`;
990
- }
991
- }));
1133
+ var createFormat = (logMeta = true) => {
1134
+ return import_winston.format.combine(
1135
+ import_winston.format.printf((info) => {
1136
+ if (info.stack) {
1137
+ return `[${info.timestamp}] [${info.level.toUpperCase()}]: ${info.message} ${info.stack}`;
1138
+ }
1139
+ if (logMeta) {
1140
+ return `[${info.timestamp}] [${info.level.toUpperCase()}]: ${info.message} ${!(0, import_lodash_es3.isEmpty)(info.meta) ? processMeta(info.meta) : ""}`;
1141
+ } else {
1142
+ return `[${info.timestamp}] [${info.level.toUpperCase()}]: ${info.message}`;
1143
+ }
1144
+ })
1145
+ );
992
1146
  };
993
- const defaultTransport = new winston.transports.Console({
994
- level: 'debug',
995
- format: winston.format.combine(winston.format.prettyPrint(), winston.format.errors({ stack: true }), winston.format.timestamp({
996
- format: 'YYYY-MM-DD HH:mm:ss',
997
- }), createFormat(false), winston.format.colorize({
998
- all: true,
999
- })),
1147
+ var defaultTransport = new import_winston.default.transports.Console({
1148
+ level: "debug",
1149
+ format: import_winston.format.combine(
1150
+ import_winston.format.prettyPrint(),
1151
+ import_winston.format.errors({ stack: true }),
1152
+ import_winston.format.timestamp({
1153
+ format: "YYYY-MM-DD HH:mm:ss"
1154
+ }),
1155
+ createFormat(false),
1156
+ import_winston.format.colorize({
1157
+ all: true
1158
+ })
1159
+ )
1000
1160
  });
1001
- class ConduitLogger {
1002
- _winston;
1003
- constructor(transports) {
1004
- this._winston = winston.createLogger({
1005
- format: winston.format.combine(winston.format.prettyPrint(), winston.format.errors({ stack: true }), winston.format.timestamp({
1006
- format: 'YYYY-MM-DD HH:mm:ss',
1007
- }), createFormat()),
1008
- transports: transports ? [...transports, defaultTransport] : [defaultTransport],
1009
- });
1010
- }
1011
- get winston() {
1012
- return this._winston;
1013
- }
1014
- addTransport(transport) {
1015
- this._winston.add(transport);
1016
- }
1017
- log(message, level = 'info', cb) {
1018
- return this._winston.log(level, message, cb);
1019
- }
1020
- logObject(object, message = '', level = 'info', cb) {
1021
- return this._winston.log(level, message, object, cb);
1022
- }
1023
- info(message, cb) {
1024
- return this._winston.info(message, cb);
1025
- }
1026
- warn(message, cb) {
1027
- return this._winston.warn(message, cb);
1028
- }
1029
- error(message, cb) {
1030
- if (typeof message === 'object') {
1031
- return this._winston.error(message);
1032
- }
1033
- return this._winston.error(message, cb);
1034
- }
1035
- http(message, cb) {
1036
- return this._winston.http(message, cb);
1037
- }
1038
- verbose(message, cb) {
1039
- return this._winston.verbose(message, cb);
1040
- }
1041
- }
1161
+ var ConduitLogger = class {
1162
+ _winston;
1163
+ constructor(transports) {
1164
+ this._winston = import_winston.default.createLogger({
1165
+ format: import_winston.format.combine(
1166
+ import_winston.format.prettyPrint(),
1167
+ import_winston.format.errors({ stack: true }),
1168
+ import_winston.format.timestamp({
1169
+ format: "YYYY-MM-DD HH:mm:ss"
1170
+ }),
1171
+ createFormat()
1172
+ ),
1173
+ transports: transports ? [...transports, defaultTransport] : [defaultTransport]
1174
+ });
1175
+ }
1176
+ get winston() {
1177
+ return this._winston;
1178
+ }
1179
+ addTransport(transport) {
1180
+ this._winston.add(transport);
1181
+ }
1182
+ log(message, level = "info") {
1183
+ return this._winston.log(level, message);
1184
+ }
1185
+ logObject(object, message = "", level = "info") {
1186
+ return this._winston.log(level, message, object);
1187
+ }
1188
+ info(message) {
1189
+ return this._winston.info(message);
1190
+ }
1191
+ warn(message) {
1192
+ return this._winston.warn(message);
1193
+ }
1194
+ error(message, error) {
1195
+ if (typeof message === "object") {
1196
+ return this._winston.error(message);
1197
+ }
1198
+ return this._winston.error(message, error);
1199
+ }
1200
+ http(message) {
1201
+ return this._winston.http(message);
1202
+ }
1203
+ verbose(message) {
1204
+ return this._winston.verbose(message);
1205
+ }
1206
+ };
1042
1207
  async function lokiReadyCheck(lokiUrl) {
1043
- return new Promise((resolve, reject) => {
1044
- const data = [];
1045
- http.get(`${lokiUrl}/ready`, r => {
1046
- r.on('data', chunk => data.push(chunk));
1047
- r.on('end', () => {
1048
- if (Buffer.concat(data).toString() === 'ready\n')
1049
- resolve();
1050
- else
1051
- reject(false);
1052
- });
1053
- }).on('error', err => {
1054
- reject(err.message);
1055
- });
1208
+ return new Promise((resolve, reject) => {
1209
+ const data = [];
1210
+ (0, import_http.get)(`${lokiUrl}/ready`, (r) => {
1211
+ r.on("data", (chunk) => data.push(chunk));
1212
+ r.on("end", () => {
1213
+ if (Buffer.concat(data).toString() === "ready\n") resolve();
1214
+ else reject(false);
1215
+ });
1216
+ }).on("error", (err) => {
1217
+ reject(err.message);
1056
1218
  });
1219
+ });
1057
1220
  }
1058
- async function setupLoki(module, instance) {
1059
- let lokiUrl = process.env.LOKI_URL;
1060
- if (lokiUrl && lokiUrl !== '') {
1061
- if (lokiUrl.endsWith('/'))
1062
- lokiUrl = lokiUrl.slice(0, -1);
1063
- const onTry = async () => {
1064
- return await lokiReadyCheck(lokiUrl)
1065
- .then(() => {
1066
- grpcSdk.ConduitGrpcSdk.Logger.addTransport(new LokiTransport({
1067
- level: 'debug',
1068
- host: lokiUrl,
1069
- batching: false,
1070
- replaceTimestamp: true,
1071
- labels: {
1072
- module,
1073
- instance,
1074
- ...(process.env.CONDUIT_NAMESPACE && {
1075
- namespace: process.env.CONDUIT_NAMESPACE,
1076
- }),
1077
- },
1078
- }));
1079
- return false;
1080
- })
1081
- .catch(() => true); // retry
1082
- };
1083
- const onFailure = () => {
1084
- grpcSdk.ConduitGrpcSdk.Logger.error(`Failed to connect to Loki on '${lokiUrl}'`);
1085
- };
1086
- await linearBackoffTimeoutAsync(onTry, 250, 15, onFailure, true);
1087
- }
1221
+ async function setupLoki(module2, instance) {
1222
+ let lokiUrl = process.env.LOKI_URL;
1223
+ if (lokiUrl && lokiUrl !== "") {
1224
+ if (lokiUrl.endsWith("/")) lokiUrl = lokiUrl.slice(0, -1);
1225
+ const onTry = async () => {
1226
+ return await lokiReadyCheck(lokiUrl).then(() => {
1227
+ import_grpc_sdk9.ConduitGrpcSdk.Logger.addTransport(
1228
+ new import_winston_loki.default({
1229
+ level: "debug",
1230
+ host: lokiUrl,
1231
+ batching: false,
1232
+ replaceTimestamp: true,
1233
+ labels: {
1234
+ module: module2,
1235
+ instance,
1236
+ ...process.env.CONDUIT_NAMESPACE && {
1237
+ namespace: process.env.CONDUIT_NAMESPACE
1238
+ }
1239
+ }
1240
+ })
1241
+ );
1242
+ return false;
1243
+ }).catch(() => true);
1244
+ };
1245
+ const onFailure = () => {
1246
+ import_grpc_sdk9.ConduitGrpcSdk.Logger.error(`Failed to connect to Loki on '${lokiUrl}'`);
1247
+ };
1248
+ await linearBackoffTimeoutAsync(onTry, 250, 15, onFailure, true);
1249
+ }
1088
1250
  }
1089
1251
 
1090
- class MetricsServer {
1091
- moduleName;
1092
- instance;
1093
- Registry;
1094
- constructor(moduleName, instance, registry) {
1095
- this.moduleName = moduleName;
1096
- this.instance = instance;
1097
- this.Registry = registry;
1098
- }
1099
- initialize() {
1100
- const port = this.getHttpPort();
1101
- const url = '0.0.0.0:' + port.toString();
1102
- const server = http.createServer(this.requestHandler.bind(this));
1103
- server.listen(port, () => {
1104
- grpcSdk.ConduitGrpcSdk.Logger.info(`Metrics server listening on: ${url}`);
1105
- });
1106
- return server;
1107
- }
1108
- async requestHandler(req, res) {
1109
- if (req.url === '/' && req.method === 'GET') {
1110
- res.writeHead(200, { 'Content-Type': 'text/plain' });
1111
- res.end(`Conduit Metrics for module: ${this.moduleName}, instance: ${this.instance}`);
1112
- }
1113
- else if (req.url === '/metrics' && req.method === 'GET') {
1114
- const metrics = await this.Registry.metrics();
1115
- res.writeHead(200, { 'Content-Type': 'text/plain' });
1116
- res.end(metrics);
1117
- }
1118
- else {
1119
- res.writeHead(404, { 'Content-Type': 'text/plain' });
1120
- res.end('Not Found');
1121
- }
1122
- }
1123
- getHttpPort() {
1124
- const value = process.env['METRICS_PORT'];
1125
- if (!value) {
1126
- grpcSdk.ConduitGrpcSdk.Logger.error(`Metrics HTTP port not set`);
1127
- process.exit(-1);
1128
- }
1129
- const port = parseInt(value, 10);
1130
- if (lodash.isNaN(port) || port < 1 || port > 65535) {
1131
- grpcSdk.ConduitGrpcSdk.Logger.error(`Invalid HTTP port value: ${port}`);
1132
- process.exit(-1);
1133
- }
1134
- return port;
1135
- }
1136
- }
1252
+ // src/utilities/initializeSdk.ts
1253
+ var import_winston2 = __toESM(require("winston"), 1);
1254
+ var import_path2 = __toESM(require("path"), 1);
1137
1255
 
1138
- var defaultMetrics = {
1139
- internalGrpcRequests: {
1140
- type: grpcSdk.MetricType.Counter,
1141
- config: {
1142
- name: 'internal_grpc_requests_total',
1143
- help: "Tracks the total number of Conduit's internal gRPC requests",
1144
- },
1145
- },
1146
- clientGrpcRequests: {
1147
- type: grpcSdk.MetricType.Counter,
1148
- config: {
1149
- name: 'client_grpc_requests_total',
1150
- help: 'Tracks the total number of the client gRPC requests',
1151
- },
1152
- },
1153
- adminGrpcRequests: {
1154
- type: grpcSdk.MetricType.Counter,
1155
- config: {
1156
- name: 'admin_grpc_requests_total',
1157
- help: 'Tracks the total number of the admin gRPC requests',
1158
- },
1159
- },
1160
- clientGrpcErrors: {
1161
- type: grpcSdk.MetricType.Counter,
1162
- config: {
1163
- name: 'client_grpc_errors_total',
1164
- help: 'Tracks the total number of client grpc errors',
1165
- },
1166
- },
1167
- adminGrpcErrors: {
1168
- type: grpcSdk.MetricType.Counter,
1169
- config: {
1170
- name: 'admin_grpc_errors_total',
1171
- help: 'Tracks the total number of admin grpc errors',
1172
- },
1173
- },
1174
- moduleHealthState: {
1175
- type: grpcSdk.MetricType.Gauge,
1176
- config: {
1177
- name: 'module_health_state',
1178
- help: 'Tracks the health state of the module',
1179
- },
1180
- },
1181
- grpcRequestLatency: {
1182
- type: grpcSdk.MetricType.Gauge,
1183
- config: {
1184
- name: 'grpc_request_latency_seconds',
1185
- help: 'Tracks the latency of grpc requests in seconds',
1186
- },
1187
- },
1188
- adminGrpcRequestLatency: {
1189
- type: grpcSdk.MetricType.Gauge,
1190
- config: {
1191
- name: 'admin_grpc_request_latency_seconds',
1192
- help: 'Tracks the latency of admin grpc requests in seconds',
1193
- },
1194
- },
1195
- clientGrpcRequestLatency: {
1196
- type: grpcSdk.MetricType.Gauge,
1197
- config: {
1198
- name: 'client_grpc_request_latency_seconds',
1199
- help: 'Tracks the latency of client grpc requests in seconds',
1200
- },
1201
- },
1202
- grpcResponseStatuses: {
1203
- type: grpcSdk.MetricType.Counter,
1204
- config: {
1205
- name: 'grpc_response_statuses_total',
1206
- help: 'Tracks the total number of grpc response codes',
1207
- labelNames: ['success'],
1208
- },
1209
- },
1210
- adminGrpcResponseStatuses: {
1211
- type: grpcSdk.MetricType.Counter,
1212
- config: {
1213
- name: 'admin_grpc_response_statuses_total',
1214
- help: 'Tracks the total number of admin grpc response codes',
1215
- labelNames: ['success'],
1216
- },
1217
- },
1218
- clientGrpcResponseStatuses: {
1219
- type: grpcSdk.MetricType.Counter,
1220
- config: {
1221
- name: 'client_grpc_response_statuses_total',
1222
- help: 'Tracks the total number of client grpc response codes',
1223
- labelNames: ['success'],
1224
- },
1225
- },
1256
+ // src/metrics/index.ts
1257
+ var import_prom_client = require("prom-client");
1258
+
1259
+ // src/metrics/MetricsServer.ts
1260
+ var import_http2 = require("http");
1261
+ var import_lodash_es4 = require("lodash-es");
1262
+ var import_grpc_sdk10 = require("@conduitplatform/grpc-sdk");
1263
+ var MetricsServer = class {
1264
+ moduleName;
1265
+ instance;
1266
+ Registry;
1267
+ constructor(moduleName, instance, registry2) {
1268
+ this.moduleName = moduleName;
1269
+ this.instance = instance;
1270
+ this.Registry = registry2;
1271
+ }
1272
+ initialize() {
1273
+ const port = this.getHttpPort();
1274
+ const url = "0.0.0.0:" + port.toString();
1275
+ const server = (0, import_http2.createServer)(this.requestHandler.bind(this));
1276
+ server.listen(port, () => {
1277
+ import_grpc_sdk10.ConduitGrpcSdk.Logger.info(`Metrics server listening on: ${url}`);
1278
+ });
1279
+ return server;
1280
+ }
1281
+ async requestHandler(req, res) {
1282
+ if (req.url === "/" && req.method === "GET") {
1283
+ res.writeHead(200, { "Content-Type": "text/plain" });
1284
+ res.end(
1285
+ `Conduit Metrics for module: ${this.moduleName}, instance: ${this.instance}`
1286
+ );
1287
+ } else if (req.url === "/metrics" && req.method === "GET") {
1288
+ const metrics = await this.Registry.metrics();
1289
+ res.writeHead(200, { "Content-Type": "text/plain" });
1290
+ res.end(metrics);
1291
+ } else {
1292
+ res.writeHead(404, { "Content-Type": "text/plain" });
1293
+ res.end("Not Found");
1294
+ }
1295
+ }
1296
+ getHttpPort() {
1297
+ const value = process.env["METRICS_PORT"];
1298
+ if (!value) {
1299
+ import_grpc_sdk10.ConduitGrpcSdk.Logger.error(`Metrics HTTP port not set`);
1300
+ process.exit(-1);
1301
+ }
1302
+ const port = parseInt(value, 10);
1303
+ if ((0, import_lodash_es4.isNaN)(port) || port < 1 || port > 65535) {
1304
+ import_grpc_sdk10.ConduitGrpcSdk.Logger.error(`Invalid HTTP port value: ${port}`);
1305
+ process.exit(-1);
1306
+ }
1307
+ return port;
1308
+ }
1226
1309
  };
1227
1310
 
1228
- const METRICS_PREFIX = 'cnd_';
1229
- class ConduitMetrics {
1230
- moduleName;
1231
- instance;
1232
- _registry;
1233
- _httpServer;
1234
- constructor(moduleName, instance) {
1235
- this.moduleName = moduleName;
1236
- this.instance = instance;
1237
- this._registry = new promClient.Registry();
1238
- this._registry.setDefaultLabels({
1239
- module_name: this.moduleName,
1240
- module_instance: this.instance,
1241
- });
1242
- this.collectDefaultMetrics();
1243
- this._httpServer = new MetricsServer(moduleName, instance, this._registry);
1244
- this._httpServer.initialize();
1245
- }
1246
- get registry() {
1247
- return this._registry;
1248
- }
1249
- initializeDefaultMetrics() {
1250
- for (const metric of Object.values(defaultMetrics)) {
1251
- this.registerMetric(metric.type, metric.config);
1252
- }
1253
- }
1254
- registerMetric(type, config) {
1255
- if (this.getMetric(config.name))
1256
- return;
1257
- const metricConfig = JSON.parse(JSON.stringify(config));
1258
- metricConfig.name = this.addPrefix(config.name);
1259
- switch (type) {
1260
- case grpcSdk.MetricType.Counter:
1261
- this.createCounter(metricConfig);
1262
- break;
1263
- case grpcSdk.MetricType.Gauge:
1264
- this.createGauge(metricConfig);
1265
- break;
1266
- case grpcSdk.MetricType.Histogram:
1267
- this.createHistogram(metricConfig);
1268
- break;
1269
- case grpcSdk.MetricType.Summary:
1270
- this.createSummary(metricConfig);
1271
- break;
1272
- }
1273
- }
1274
- collectDefaultMetrics() {
1275
- promClient.collectDefaultMetrics({
1276
- register: this._registry,
1277
- prefix: METRICS_PREFIX,
1278
- // probably not needed
1279
- labels: {
1280
- module_name: this.moduleName,
1281
- instance_instance: this.instance,
1282
- },
1283
- });
1284
- }
1285
- createCounter(config) {
1286
- return new promClient.Counter({
1287
- ...config,
1288
- name: this.addPrefix(config.name),
1289
- registers: [this._registry],
1290
- });
1291
- }
1292
- createSummary(config) {
1293
- return new promClient.Summary({
1294
- ...config,
1295
- name: this.addPrefix(config.name),
1296
- registers: [this._registry],
1297
- });
1298
- }
1299
- createHistogram(config) {
1300
- return new promClient.Histogram({
1301
- ...config,
1302
- name: this.addPrefix(config.name),
1303
- registers: [this._registry],
1304
- });
1305
- }
1306
- createGauge(config) {
1307
- return new promClient.Gauge({
1308
- ...config,
1309
- name: this.addPrefix(config.name),
1310
- registers: [this._registry],
1311
- });
1312
- }
1313
- getMetric(name) {
1314
- return this._registry.getSingleMetric(this.addPrefix(name));
1315
- }
1316
- increment(metric, increment = 1, labels) {
1317
- const metricInstance = this.getMetric(metric);
1318
- if (!(metricInstance instanceof promClient.Counter) && !(metricInstance instanceof promClient.Gauge)) {
1319
- throw new Error(`Metric ${metric} is not an incrementable metric`);
1320
- }
1321
- return labels
1322
- ? metricInstance.labels({ ...labels }).inc(increment)
1323
- : metricInstance.inc(increment);
1324
- }
1325
- decrement(metric, decrement = 1, labels) {
1326
- const metricInstance = this.getMetric(metric);
1327
- if (!(metricInstance instanceof promClient.Gauge)) {
1328
- throw new Error(`Metric ${metric} is not a decrementable metric`);
1329
- }
1330
- return labels
1331
- ? metricInstance.labels({ ...labels }).dec(decrement)
1332
- : metricInstance.dec(decrement);
1333
- }
1334
- set(metric, value, labels) {
1335
- const metricInstance = this.getMetric(metric);
1336
- if (!(metricInstance instanceof promClient.Gauge)) {
1337
- throw new Error(`Metric ${metric} is not a Gauge`);
1338
- }
1339
- return labels
1340
- ? metricInstance.labels({ ...labels }).set(value)
1341
- : metricInstance.set(value);
1342
- }
1343
- observe(metric, value, labels) {
1344
- const metricInstance = this.getMetric(metric);
1345
- if (!(metricInstance instanceof promClient.Histogram) && !(metricInstance instanceof promClient.Summary)) {
1346
- throw new Error(`Metric ${metric} is not a Histogram`);
1347
- }
1348
- return labels
1349
- ? metricInstance.labels({ ...labels }).observe(value)
1350
- : metricInstance.observe(value);
1351
- }
1352
- addPrefix(metric) {
1353
- if (!metric.includes(METRICS_PREFIX)) {
1354
- return `${METRICS_PREFIX}${metric}`;
1355
- }
1356
- return metric;
1357
- }
1358
- }
1311
+ // src/metrics/config/defaults.ts
1312
+ var import_grpc_sdk11 = require("@conduitplatform/grpc-sdk");
1313
+ var defaults_default = {
1314
+ internalGrpcRequests: {
1315
+ type: import_grpc_sdk11.MetricType.Counter,
1316
+ config: {
1317
+ name: "internal_grpc_requests_total",
1318
+ help: "Tracks the total number of Conduit's internal gRPC requests"
1319
+ }
1320
+ },
1321
+ clientGrpcRequests: {
1322
+ type: import_grpc_sdk11.MetricType.Counter,
1323
+ config: {
1324
+ name: "client_grpc_requests_total",
1325
+ help: "Tracks the total number of the client gRPC requests"
1326
+ }
1327
+ },
1328
+ adminGrpcRequests: {
1329
+ type: import_grpc_sdk11.MetricType.Counter,
1330
+ config: {
1331
+ name: "admin_grpc_requests_total",
1332
+ help: "Tracks the total number of the admin gRPC requests"
1333
+ }
1334
+ },
1335
+ clientGrpcErrors: {
1336
+ type: import_grpc_sdk11.MetricType.Counter,
1337
+ config: {
1338
+ name: "client_grpc_errors_total",
1339
+ help: "Tracks the total number of client grpc errors"
1340
+ }
1341
+ },
1342
+ adminGrpcErrors: {
1343
+ type: import_grpc_sdk11.MetricType.Counter,
1344
+ config: {
1345
+ name: "admin_grpc_errors_total",
1346
+ help: "Tracks the total number of admin grpc errors"
1347
+ }
1348
+ },
1349
+ moduleHealthState: {
1350
+ type: import_grpc_sdk11.MetricType.Gauge,
1351
+ config: {
1352
+ name: "module_health_state",
1353
+ help: "Tracks the health state of the module"
1354
+ }
1355
+ },
1356
+ grpcRequestLatency: {
1357
+ type: import_grpc_sdk11.MetricType.Gauge,
1358
+ config: {
1359
+ name: "grpc_request_latency_seconds",
1360
+ help: "Tracks the latency of grpc requests in seconds"
1361
+ }
1362
+ },
1363
+ adminGrpcRequestLatency: {
1364
+ type: import_grpc_sdk11.MetricType.Gauge,
1365
+ config: {
1366
+ name: "admin_grpc_request_latency_seconds",
1367
+ help: "Tracks the latency of admin grpc requests in seconds"
1368
+ }
1369
+ },
1370
+ clientGrpcRequestLatency: {
1371
+ type: import_grpc_sdk11.MetricType.Gauge,
1372
+ config: {
1373
+ name: "client_grpc_request_latency_seconds",
1374
+ help: "Tracks the latency of client grpc requests in seconds"
1375
+ }
1376
+ },
1377
+ grpcResponseStatuses: {
1378
+ type: import_grpc_sdk11.MetricType.Counter,
1379
+ config: {
1380
+ name: "grpc_response_statuses_total",
1381
+ help: "Tracks the total number of grpc response codes",
1382
+ labelNames: ["success"]
1383
+ }
1384
+ },
1385
+ adminGrpcResponseStatuses: {
1386
+ type: import_grpc_sdk11.MetricType.Counter,
1387
+ config: {
1388
+ name: "admin_grpc_response_statuses_total",
1389
+ help: "Tracks the total number of admin grpc response codes",
1390
+ labelNames: ["success"]
1391
+ }
1392
+ },
1393
+ clientGrpcResponseStatuses: {
1394
+ type: import_grpc_sdk11.MetricType.Counter,
1395
+ config: {
1396
+ name: "client_grpc_response_statuses_total",
1397
+ help: "Tracks the total number of client grpc response codes",
1398
+ labelNames: ["success"]
1399
+ }
1400
+ }
1401
+ };
1402
+
1403
+ // src/metrics/index.ts
1404
+ var import_grpc_sdk12 = require("@conduitplatform/grpc-sdk");
1405
+ var METRICS_PREFIX = "cnd_";
1406
+ var ConduitMetrics = class {
1407
+ moduleName;
1408
+ instance;
1409
+ _registry;
1410
+ _httpServer;
1411
+ constructor(moduleName, instance) {
1412
+ this.moduleName = moduleName;
1413
+ this.instance = instance;
1414
+ this._registry = new import_prom_client.Registry();
1415
+ this._registry.setDefaultLabels({
1416
+ module_name: this.moduleName,
1417
+ module_instance: this.instance
1418
+ });
1419
+ this.collectDefaultMetrics();
1420
+ this._httpServer = new MetricsServer(moduleName, instance, this._registry);
1421
+ this._httpServer.initialize();
1422
+ }
1423
+ get registry() {
1424
+ return this._registry;
1425
+ }
1426
+ initializeDefaultMetrics() {
1427
+ for (const metric of Object.values(defaults_default)) {
1428
+ this.registerMetric(metric.type, metric.config);
1429
+ }
1430
+ }
1431
+ registerMetric(type, config) {
1432
+ if (this.getMetric(config.name)) return;
1433
+ const metricConfig = JSON.parse(JSON.stringify(config));
1434
+ metricConfig.name = this.addPrefix(config.name);
1435
+ switch (type) {
1436
+ case import_grpc_sdk12.MetricType.Counter:
1437
+ this.createCounter(metricConfig);
1438
+ break;
1439
+ case import_grpc_sdk12.MetricType.Gauge:
1440
+ this.createGauge(metricConfig);
1441
+ break;
1442
+ case import_grpc_sdk12.MetricType.Histogram:
1443
+ this.createHistogram(metricConfig);
1444
+ break;
1445
+ case import_grpc_sdk12.MetricType.Summary:
1446
+ this.createSummary(metricConfig);
1447
+ break;
1448
+ }
1449
+ }
1450
+ collectDefaultMetrics() {
1451
+ (0, import_prom_client.collectDefaultMetrics)({
1452
+ register: this._registry,
1453
+ prefix: METRICS_PREFIX,
1454
+ // probably not needed
1455
+ labels: {
1456
+ module_name: this.moduleName,
1457
+ instance_instance: this.instance
1458
+ }
1459
+ });
1460
+ }
1461
+ createCounter(config) {
1462
+ return new import_prom_client.Counter({
1463
+ ...config,
1464
+ name: this.addPrefix(config.name),
1465
+ registers: [this._registry]
1466
+ });
1467
+ }
1468
+ createSummary(config) {
1469
+ return new import_prom_client.Summary({
1470
+ ...config,
1471
+ name: this.addPrefix(config.name),
1472
+ registers: [this._registry]
1473
+ });
1474
+ }
1475
+ createHistogram(config) {
1476
+ return new import_prom_client.Histogram({
1477
+ ...config,
1478
+ name: this.addPrefix(config.name),
1479
+ registers: [this._registry]
1480
+ });
1481
+ }
1482
+ createGauge(config) {
1483
+ return new import_prom_client.Gauge({
1484
+ ...config,
1485
+ name: this.addPrefix(config.name),
1486
+ registers: [this._registry]
1487
+ });
1488
+ }
1489
+ getMetric(name) {
1490
+ return this._registry.getSingleMetric(this.addPrefix(name));
1491
+ }
1492
+ increment(metric, increment = 1, labels) {
1493
+ const metricInstance = this.getMetric(metric);
1494
+ if (!(metricInstance instanceof import_prom_client.Counter) && !(metricInstance instanceof import_prom_client.Gauge)) {
1495
+ throw new Error(`Metric ${metric} is not an incrementable metric`);
1496
+ }
1497
+ return labels ? metricInstance.labels({ ...labels }).inc(increment) : metricInstance.inc(increment);
1498
+ }
1499
+ decrement(metric, decrement = 1, labels) {
1500
+ const metricInstance = this.getMetric(metric);
1501
+ if (!(metricInstance instanceof import_prom_client.Gauge)) {
1502
+ throw new Error(`Metric ${metric} is not a decrementable metric`);
1503
+ }
1504
+ return labels ? metricInstance.labels({ ...labels }).dec(decrement) : metricInstance.dec(decrement);
1505
+ }
1506
+ set(metric, value, labels) {
1507
+ const metricInstance = this.getMetric(metric);
1508
+ if (!(metricInstance instanceof import_prom_client.Gauge)) {
1509
+ throw new Error(`Metric ${metric} is not a Gauge`);
1510
+ }
1511
+ return labels ? metricInstance.labels({ ...labels }).set(value) : metricInstance.set(value);
1512
+ }
1513
+ observe(metric, value, labels) {
1514
+ const metricInstance = this.getMetric(metric);
1515
+ if (!(metricInstance instanceof import_prom_client.Histogram) && !(metricInstance instanceof import_prom_client.Summary)) {
1516
+ throw new Error(`Metric ${metric} is not a Histogram`);
1517
+ }
1518
+ return labels ? metricInstance.labels({ ...labels }).observe(value) : metricInstance.observe(value);
1519
+ }
1520
+ addPrefix(metric) {
1521
+ if (!metric.includes(METRICS_PREFIX)) {
1522
+ return `${METRICS_PREFIX}${metric}`;
1523
+ }
1524
+ return metric;
1525
+ }
1526
+ };
1359
1527
 
1360
- // Forked from nice-grpc-prometheus
1361
- const registry = new promClient.Registry();
1362
- const typeLabel = 'grpc_type';
1363
- const serviceLabel = 'grpc_service';
1364
- const methodLabel = 'grpc_method';
1365
- const pathLabel = 'grpc_path';
1366
- const codeLabel = 'grpc_code';
1367
- /**
1368
- * 1ms, 4ms, 16ms, ..., ~1 hour in seconds
1369
- */
1370
- const latencySecondsBuckets = promClient.exponentialBuckets(0.001, 4, 12);
1528
+ // src/metrics/clientMiddleware.ts
1529
+ var import_abort_controller_x = require("abort-controller-x");
1530
+ var import_nice_grpc_common = require("nice-grpc-common");
1531
+ var import_prom_client2 = require("prom-client");
1532
+ var registry = new import_prom_client2.Registry();
1533
+ var typeLabel = "grpc_type";
1534
+ var serviceLabel = "grpc_service";
1535
+ var methodLabel = "grpc_method";
1536
+ var pathLabel = "grpc_path";
1537
+ var codeLabel = "grpc_code";
1538
+ var latencySecondsBuckets = (0, import_prom_client2.exponentialBuckets)(1e-3, 4, 12);
1371
1539
  function getLabels(method) {
1372
- const callType = method.requestStream
1373
- ? method.responseStream
1374
- ? 'bidi_stream'
1375
- : 'client_stream'
1376
- : method.responseStream
1377
- ? 'server_stream'
1378
- : 'unary';
1379
- const { path } = method;
1380
- const [serviceName, methodName] = path.split('/').slice(1);
1381
- return {
1382
- [typeLabel]: callType,
1383
- [serviceLabel]: serviceName,
1384
- [methodLabel]: methodName,
1385
- [pathLabel]: path,
1386
- };
1540
+ const callType = method.requestStream ? method.responseStream ? "bidi_stream" : "client_stream" : method.responseStream ? "server_stream" : "unary";
1541
+ const { path: path4 } = method;
1542
+ const [serviceName, methodName] = path4.split("/").slice(1);
1543
+ return {
1544
+ [typeLabel]: callType,
1545
+ [serviceLabel]: serviceName,
1546
+ [methodLabel]: methodName,
1547
+ [pathLabel]: path4
1548
+ };
1387
1549
  }
1388
1550
  async function* incrementStreamMessagesCounter(iterable, counter) {
1389
- for await (const item of iterable) {
1390
- counter.inc();
1391
- yield item;
1392
- }
1551
+ for await (const item of iterable) {
1552
+ counter.inc();
1553
+ yield item;
1554
+ }
1393
1555
  }
1394
- const clientStartedMetric = new promClient.Counter({
1395
- registers: [registry],
1396
- name: 'cnd_grpc_client_started_total',
1397
- help: 'Total number of RPCs started on the client.',
1398
- labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel],
1556
+ var clientStartedMetric = new import_prom_client2.Counter({
1557
+ registers: [registry],
1558
+ name: "cnd_grpc_client_started_total",
1559
+ help: "Total number of RPCs started on the client.",
1560
+ labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel]
1399
1561
  });
1400
- const clientHandledMetric = new promClient.Counter({
1401
- registers: [registry],
1402
- name: 'cnd_grpc_client_handled_total',
1403
- help: 'Total number of RPCs completed on the client, regardless of success or failure.',
1404
- labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel, codeLabel],
1562
+ var clientHandledMetric = new import_prom_client2.Counter({
1563
+ registers: [registry],
1564
+ name: "cnd_grpc_client_handled_total",
1565
+ help: "Total number of RPCs completed on the client, regardless of success or failure.",
1566
+ labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel, codeLabel]
1405
1567
  });
1406
- const clientStreamMsgReceivedMetric = new promClient.Counter({
1407
- registers: [registry],
1408
- name: 'cnd_grpc_client_msg_received_total',
1409
- help: 'Total number of RPC stream messages received by the client.',
1410
- labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel],
1568
+ var clientStreamMsgReceivedMetric = new import_prom_client2.Counter({
1569
+ registers: [registry],
1570
+ name: "cnd_grpc_client_msg_received_total",
1571
+ help: "Total number of RPC stream messages received by the client.",
1572
+ labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel]
1411
1573
  });
1412
- const clientStreamMsgSentMetric = new promClient.Counter({
1413
- registers: [registry],
1414
- name: 'cnd_grpc_client_msg_sent_total',
1415
- help: 'Total number of gRPC stream messages sent by the client.',
1416
- labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel],
1574
+ var clientStreamMsgSentMetric = new import_prom_client2.Counter({
1575
+ registers: [registry],
1576
+ name: "cnd_grpc_client_msg_sent_total",
1577
+ help: "Total number of gRPC stream messages sent by the client.",
1578
+ labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel]
1417
1579
  });
1418
- const clientHandlingSecondsMetric = new promClient.Histogram({
1419
- registers: [registry],
1420
- name: 'grpc_client_handling_seconds',
1421
- help: 'Histogram of response latency (seconds) of the gRPC until it is finished by the application.',
1422
- labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel, codeLabel],
1423
- buckets: latencySecondsBuckets,
1580
+ var clientHandlingSecondsMetric = new import_prom_client2.Histogram({
1581
+ registers: [registry],
1582
+ name: "grpc_client_handling_seconds",
1583
+ help: "Histogram of response latency (seconds) of the gRPC until it is finished by the application.",
1584
+ labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel, codeLabel],
1585
+ buckets: latencySecondsBuckets
1424
1586
  });
1425
1587
  function clientMiddleware() {
1426
- return async function* prometheusClientMiddlewareGenerator(call, options) {
1427
- const labels = getLabels(call.method);
1428
- clientStartedMetric.inc(labels);
1429
- const stopTimer = clientHandlingSecondsMetric.startTimer(labels);
1430
- let settled = false;
1431
- let status = niceGrpcCommon.Status.OK;
1432
- try {
1433
- let request;
1434
- if (!call.requestStream) {
1435
- request = call.request;
1436
- }
1437
- else {
1438
- request = incrementStreamMessagesCounter(call.request, clientStreamMsgSentMetric.labels(labels));
1439
- }
1440
- if (!call.responseStream) {
1441
- const response = yield* call.next(request, options);
1442
- settled = true;
1443
- return response;
1444
- }
1445
- else {
1446
- yield* incrementStreamMessagesCounter(call.next(request, options), clientStreamMsgReceivedMetric.labels(labels));
1447
- settled = true;
1448
- return;
1449
- }
1450
- }
1451
- catch (err) {
1452
- settled = true;
1453
- if (err instanceof niceGrpcCommon.ClientError) {
1454
- status = err.code;
1455
- }
1456
- else if (abortControllerX.isAbortError(err)) {
1457
- status = niceGrpcCommon.Status.ABORTED;
1458
- }
1459
- else {
1460
- status = niceGrpcCommon.Status.UNKNOWN;
1461
- }
1462
- throw err;
1463
- }
1464
- finally {
1465
- if (!settled) {
1466
- status = niceGrpcCommon.Status.CANCELLED;
1467
- }
1468
- stopTimer({ [codeLabel]: niceGrpcCommon.Status[status] });
1469
- clientHandledMetric.inc({
1470
- ...labels,
1471
- [codeLabel]: niceGrpcCommon.Status[status],
1472
- });
1473
- }
1474
- };
1588
+ return async function* prometheusClientMiddlewareGenerator(call, options) {
1589
+ const labels = getLabels(call.method);
1590
+ clientStartedMetric.inc(labels);
1591
+ const stopTimer = clientHandlingSecondsMetric.startTimer(labels);
1592
+ let settled = false;
1593
+ let status6 = import_nice_grpc_common.Status.OK;
1594
+ try {
1595
+ let request;
1596
+ if (!call.requestStream) {
1597
+ request = call.request;
1598
+ } else {
1599
+ request = incrementStreamMessagesCounter(
1600
+ call.request,
1601
+ clientStreamMsgSentMetric.labels(labels)
1602
+ );
1603
+ }
1604
+ if (!call.responseStream) {
1605
+ const response = yield* call.next(request, options);
1606
+ settled = true;
1607
+ return response;
1608
+ } else {
1609
+ yield* incrementStreamMessagesCounter(
1610
+ call.next(request, options),
1611
+ clientStreamMsgReceivedMetric.labels(labels)
1612
+ );
1613
+ settled = true;
1614
+ return;
1615
+ }
1616
+ } catch (err) {
1617
+ settled = true;
1618
+ if (err instanceof import_nice_grpc_common.ClientError) {
1619
+ status6 = err.code;
1620
+ } else if ((0, import_abort_controller_x.isAbortError)(err)) {
1621
+ status6 = import_nice_grpc_common.Status.ABORTED;
1622
+ } else {
1623
+ status6 = import_nice_grpc_common.Status.UNKNOWN;
1624
+ }
1625
+ throw err;
1626
+ } finally {
1627
+ if (!settled) {
1628
+ status6 = import_nice_grpc_common.Status.CANCELLED;
1629
+ }
1630
+ stopTimer({ [codeLabel]: import_nice_grpc_common.Status[status6] });
1631
+ clientHandledMetric.inc({
1632
+ ...labels,
1633
+ [codeLabel]: import_nice_grpc_common.Status[status6]
1634
+ });
1635
+ }
1636
+ };
1475
1637
  }
1476
1638
 
1477
- const __filename$1 = node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
1478
- const __dirname$1 = path.dirname(__filename$1);
1479
- const initializeSdk = (coreUrl, moduleName, watchModules = true, serviceHealthStatusGetter = () => grpcSdk.HealthCheckStatus.SERVING) => {
1480
- let logger;
1481
- try {
1482
- logger = new ConduitLogger([
1483
- new winston.transports.File({
1484
- filename: path.join(__dirname$1, '.logs/combined.log'),
1485
- level: 'info',
1486
- }),
1487
- new winston.transports.File({
1488
- filename: path.join(__dirname$1, '.logs/errors.log'),
1489
- level: 'error',
1490
- }),
1491
- ]);
1492
- }
1493
- catch (e) {
1494
- logger = new ConduitLogger();
1495
- }
1496
- const grpcSdk$1 = new grpcSdk.ConduitGrpcSdk(coreUrl, moduleName, watchModules, logger, serviceHealthStatusGetter);
1497
- setupLoki(grpcSdk$1.name, grpcSdk$1.instance).then();
1498
- if (process.env.METRICS_PORT) {
1499
- grpcSdk.ConduitGrpcSdk.Metrics = new ConduitMetrics(grpcSdk$1.name, grpcSdk$1.instance);
1500
- grpcSdk$1.addMiddleware(clientMiddleware());
1501
- }
1502
- return grpcSdk$1;
1639
+ // src/utilities/initializeSdk.ts
1640
+ var initializeSdk = (coreUrl, moduleName, watchModules = true, serviceHealthStatusGetter = () => import_grpc_sdk13.HealthCheckStatus.SERVING) => {
1641
+ let logger;
1642
+ try {
1643
+ logger = new ConduitLogger([
1644
+ new import_winston2.default.transports.File({
1645
+ filename: import_path2.default.join(__dirname, ".logs/combined.log"),
1646
+ level: "info"
1647
+ }),
1648
+ new import_winston2.default.transports.File({
1649
+ filename: import_path2.default.join(__dirname, ".logs/errors.log"),
1650
+ level: "error"
1651
+ })
1652
+ ]);
1653
+ } catch (e) {
1654
+ logger = new ConduitLogger();
1655
+ }
1656
+ const grpcSdk = new import_grpc_sdk13.ConduitGrpcSdk(
1657
+ coreUrl,
1658
+ moduleName,
1659
+ watchModules,
1660
+ logger,
1661
+ serviceHealthStatusGetter
1662
+ );
1663
+ setupLoki(grpcSdk.name, grpcSdk.instance).then();
1664
+ if (process.env.METRICS_PORT) {
1665
+ import_grpc_sdk13.ConduitGrpcSdk.Metrics = new ConduitMetrics(grpcSdk.name, grpcSdk.instance);
1666
+ grpcSdk.addMiddleware(clientMiddleware());
1667
+ }
1668
+ return grpcSdk;
1503
1669
  };
1504
1670
 
1505
- /**
1506
- * Registers a timeout with linear backoff.
1507
- * onFailure() only runs on rep exhaustion.
1508
- * Timeout can be cleared through returned clear() or inside onTry().
1509
- */
1510
- function linearBackoffTimeout(onTry, delay, reps, onFailure) {
1511
- const nextRep = () => reps === undefined || --reps > 0;
1512
- let stopSignal = false;
1513
- const stop = () => {
1514
- stopSignal = true;
1515
- };
1516
- let timeout;
1517
- const invoker = async () => {
1518
- if (stopSignal) {
1519
- return timeout ? timers.clearTimeout(timeout) : null;
1520
- }
1521
- delay = Math.floor(delay * 2);
1522
- if (delay > 0 && nextRep()) {
1523
- timeout = setTimeout(invoker, delay);
1524
- onTry(stop);
1525
- }
1526
- else {
1527
- timeout = null;
1528
- onFailure && onFailure();
1529
- }
1530
- };
1531
- timeout = setTimeout(invoker, delay);
1532
- return {
1533
- clear: () => {
1534
- if (timeout) {
1535
- timers.clearTimeout(timeout);
1536
- timeout = null;
1537
- }
1538
- },
1539
- };
1671
+ // src/utilities/merge.ts
1672
+ var import_lodash_es5 = require("lodash-es");
1673
+ function merge(objA, objB) {
1674
+ const customizer = (objValue, srcValue) => {
1675
+ if (Array.isArray(objValue)) {
1676
+ return srcValue;
1677
+ }
1678
+ };
1679
+ return (0, import_lodash_es5.mergeWith)({}, objA, objB, customizer);
1540
1680
  }
1541
- /**
1542
- * Registers a timeout with linear backoff.
1543
- * onFailure() only runs on rep exhaustion.
1544
- * @param {() => Promise<boolean>} onTry - Async handler, returns 'continue' boolean flag
1545
- */
1546
- async function linearBackoffTimeoutAsync(onTry, delay, reps, onFailure, startNow = false) {
1547
- const nextRep = () => reps === undefined || --reps > 0;
1548
- const invoker = async () => {
1549
- delay = Math.floor(delay * 2);
1550
- if (delay > 0 && nextRep()) {
1551
- if (await onTry())
1552
- setTimeout(invoker, delay);
1553
- }
1554
- else {
1555
- onFailure && (await onFailure());
1556
- }
1557
- };
1558
- setTimeout(invoker, startNow ? 0 : delay);
1681
+
1682
+ // src/utilities/exportHelpers.ts
1683
+ var DEFAULT_STRIP_FIELDS = ["_id", "createdAt", "updatedAt", "__v"];
1684
+ function stripInternalFields(obj, additionalFields = []) {
1685
+ const strip = /* @__PURE__ */ new Set([...DEFAULT_STRIP_FIELDS, ...additionalFields]);
1686
+ const out = {};
1687
+ for (const [k, v] of Object.entries(obj)) {
1688
+ if (!strip.has(k)) out[k] = v;
1689
+ }
1690
+ return out;
1691
+ }
1692
+ function sanitizeDocumentsForExport(documents, options = {}) {
1693
+ const exclude = options.excludeFields ?? [];
1694
+ return documents.map((doc) => stripInternalFields(doc, exclude));
1695
+ }
1696
+ async function exportModel(adapter, options = {}) {
1697
+ const { query, select, excludeFields, ...rest } = options;
1698
+ const docs = await adapter.findMany(query ?? {}, select ? { select } : {});
1699
+ return sanitizeDocumentsForExport(docs, { excludeFields, ...rest });
1700
+ }
1701
+ async function importModel(adapter, records, options = {}) {
1702
+ const entry = { created: 0, updated: 0, failed: 0, errors: [] };
1703
+ const keyField = adapter.keyField;
1704
+ for (const record of records) {
1705
+ const key = record[keyField];
1706
+ if (key == null || key === "") {
1707
+ entry.failed += 1;
1708
+ entry.errors.push(`Missing or empty key "${keyField}" in record`);
1709
+ continue;
1710
+ }
1711
+ try {
1712
+ const existing = await adapter.findOne({ [keyField]: key });
1713
+ const sanitized = stripInternalFields(record, options.excludeFields ?? []);
1714
+ if (existing) {
1715
+ await adapter.updateOne({ [keyField]: key }, sanitized);
1716
+ entry.updated += 1;
1717
+ } else {
1718
+ await adapter.create(sanitized);
1719
+ entry.created += 1;
1720
+ }
1721
+ } catch (err) {
1722
+ entry.failed += 1;
1723
+ entry.errors.push(`${String(key)}: ${err.message}`);
1724
+ }
1725
+ }
1726
+ return entry;
1559
1727
  }
1560
1728
 
1561
- function merge(objA, objB) {
1562
- const customizer = (objValue, srcValue) => {
1563
- if (Array.isArray(objValue)) {
1564
- // don't merge arrays
1565
- return srcValue;
1566
- }
1567
- };
1568
- return lodash.mergeWith({}, objA, objB, customizer);
1729
+ // src/utilities/conduitPeers.ts
1730
+ var import_node_fs = require("fs");
1731
+ var import_node_path = __toESM(require("path"), 1);
1732
+ function readConduitPeersManifest(moduleRootDir = process.cwd()) {
1733
+ const pkgPath = import_node_path.default.join(moduleRootDir, "package.json");
1734
+ if (!(0, import_node_fs.existsSync)(pkgPath)) return null;
1735
+ try {
1736
+ const pkg = JSON.parse((0, import_node_fs.readFileSync)(pkgPath, "utf8"));
1737
+ if (!pkg.conduit?.peers) return null;
1738
+ return pkg.conduit;
1739
+ } catch (e) {
1740
+ console.warn(`[conduitPeers] Failed to parse ${pkgPath}: ${e.message}`);
1741
+ return null;
1742
+ }
1569
1743
  }
1570
1744
 
1571
- const convictConfigParser = (config) => {
1572
- if (typeof config === 'object') {
1573
- Object.keys(config).forEach(key => {
1574
- if (key === '_cvtProperties') {
1575
- config = convictConfigParser(config._cvtProperties);
1576
- }
1577
- else {
1578
- config[key] = convictConfigParser(config[key]);
1579
- }
1580
- });
1581
- }
1582
- return config;
1745
+ // src/utilities/convictConfigParser.ts
1746
+ var convictConfigParser = (config) => {
1747
+ if (typeof config === "object") {
1748
+ Object.keys(config).forEach((key) => {
1749
+ if (key === "_cvtProperties") {
1750
+ config = convictConfigParser(config._cvtProperties);
1751
+ } else {
1752
+ config[key] = convictConfigParser(config[key]);
1753
+ }
1754
+ });
1755
+ }
1756
+ return config;
1583
1757
  };
1584
1758
 
1585
- class RoutingController {
1586
- _routeHandlers = {};
1587
- handleRequest(call, callback) {
1588
- if (this._routeHandlers[call.request.functionName]) {
1589
- this._routeHandlers[call.request.functionName](call, callback);
1590
- }
1591
- else {
1592
- callback({
1593
- code: grpcJs.status.NOT_FOUND,
1594
- message: 'Not found',
1595
- });
1596
- }
1597
- }
1598
- setRoutes(routes) {
1599
- this._routeHandlers = routes;
1600
- }
1601
- }
1759
+ // src/routing/RoutingController.ts
1760
+ var import_grpc_js5 = require("@grpc/grpc-js");
1761
+ var RoutingController = class {
1762
+ _routeHandlers = {};
1763
+ handleRequest(call, callback) {
1764
+ if (this._routeHandlers[call.request.functionName]) {
1765
+ this._routeHandlers[call.request.functionName](call, callback);
1766
+ } else {
1767
+ callback({
1768
+ code: import_grpc_js5.status.NOT_FOUND,
1769
+ message: "Not found"
1770
+ });
1771
+ }
1772
+ }
1773
+ setRoutes(routes) {
1774
+ this._routeHandlers = routes;
1775
+ }
1776
+ };
1602
1777
 
1603
- class ManagedModule extends ConduitServiceModule {
1604
- config;
1605
- configOverride = false;
1606
- service;
1607
- serviceAddress;
1608
- servicePort;
1609
- _moduleState;
1610
- constructor(moduleName) {
1611
- super(moduleName);
1612
- this._moduleState = exports.ModuleLifecycleStage.CREATE_GRPC;
1613
- if (!process.env.CONDUIT_SERVER) {
1614
- throw new Error('CONDUIT_SERVER is undefined, specify Conduit server URL');
1615
- }
1616
- this.servicePort = process.env.GRPC_PORT ?? '5000';
1617
- this.serviceAddress =
1618
- // @compat (v0.15): SERVICE_IP -> SERVICE_URL
1619
- process.env.SERVICE_URL || process.env.SERVICE_IP || '0.0.0.0:' + this.servicePort;
1620
- try {
1621
- this.grpcSdk = initializeSdk(process.env.CONDUIT_SERVER, this.name, true, () => {
1622
- return this.healthState;
1623
- });
1624
- }
1625
- catch (e) {
1626
- throw new Error('Failed to initialize grpcSdk');
1627
- }
1628
- }
1629
- get name() {
1630
- return this._moduleName;
1631
- }
1632
- get address() {
1633
- return this._address;
1634
- }
1635
- async start() {
1636
- await this.grpcSdk.initialize();
1637
- this.initialize(this.serviceAddress, this.servicePort);
1638
- try {
1639
- await this.preRegisterLifecycle();
1640
- await this.grpcSdk.config.registerModule(this.name, this.address, this.healthState);
1641
- }
1642
- catch (err) {
1643
- grpcSdk.ConduitGrpcSdk.Logger.error('Failed to initialize server');
1644
- grpcSdk.ConduitGrpcSdk.Logger.error(err);
1645
- process.exit(-1);
1646
- }
1647
- await this.postRegisterLifecycle().catch((err) => {
1648
- grpcSdk.ConduitGrpcSdk.Logger.error('Failed to activate module');
1649
- grpcSdk.ConduitGrpcSdk.Logger.error(err);
1650
- process.exit(-1);
1778
+ // src/ManagedModule.ts
1779
+ var ManagedModule = class extends ConduitServiceModule {
1780
+ config;
1781
+ configOverride = false;
1782
+ service;
1783
+ serviceAddress;
1784
+ servicePort;
1785
+ _moduleState;
1786
+ _declaredPeerWatchDisposers = [];
1787
+ _cachedPeersManifest;
1788
+ _lastPeerBatchStates = {};
1789
+ _peerManifestRoot;
1790
+ _configInitialized = false;
1791
+ _pendingConfigUpdates = [];
1792
+ _processingConfig = false;
1793
+ constructor(moduleName, peerManifestRoot) {
1794
+ super(moduleName);
1795
+ this._peerManifestRoot = peerManifestRoot;
1796
+ this._moduleState = "creat-grpc-server" /* CREATE_GRPC */;
1797
+ if (!process.env.CONDUIT_SERVER) {
1798
+ throw new Error("CONDUIT_SERVER is undefined, specify Conduit server URL");
1799
+ }
1800
+ this.servicePort = process.env.GRPC_PORT ?? "5000";
1801
+ this.serviceAddress = // @compat (v0.15): SERVICE_IP -> SERVICE_URL
1802
+ process.env.SERVICE_URL || process.env.SERVICE_IP || "0.0.0.0:" + this.servicePort;
1803
+ try {
1804
+ this.grpcSdk = initializeSdk(process.env.CONDUIT_SERVER, this.name, true, () => {
1805
+ return this.healthState;
1806
+ });
1807
+ } catch (e) {
1808
+ throw new Error("Failed to initialize grpcSdk");
1809
+ }
1810
+ }
1811
+ get name() {
1812
+ return this._moduleName;
1813
+ }
1814
+ get address() {
1815
+ return this._address;
1816
+ }
1817
+ async start() {
1818
+ await this.grpcSdk.initialize();
1819
+ this.initialize(this.serviceAddress, this.servicePort);
1820
+ try {
1821
+ await this.preRegisterLifecycle();
1822
+ await this.grpcSdk.config.registerModule(this.name, this.address, this.healthState);
1823
+ } catch (err) {
1824
+ import_grpc_sdk14.ConduitGrpcSdk.Logger.error("Failed to initialize server");
1825
+ import_grpc_sdk14.ConduitGrpcSdk.Logger.error(err);
1826
+ process.exit(-1);
1827
+ }
1828
+ await this.postRegisterLifecycle().catch((err) => {
1829
+ import_grpc_sdk14.ConduitGrpcSdk.Logger.error("Failed to activate module");
1830
+ import_grpc_sdk14.ConduitGrpcSdk.Logger.error(err);
1831
+ process.exit(-1);
1832
+ });
1833
+ }
1834
+ /**
1835
+ * This is the first function triggered on module spin up.
1836
+ * @param address {string} external address:port of service (LoadBalancer)
1837
+ * @param port {string} port to bring up gRPC service
1838
+ */
1839
+ initialize(address, port) {
1840
+ this._address = address;
1841
+ this._port = port;
1842
+ if (this.configSchema) {
1843
+ this.config = (0, import_convict.default)(this.configSchema);
1844
+ }
1845
+ }
1846
+ /**
1847
+ * This is triggered before spinning up the Grpc Server
1848
+ * of the module. On this stage, the redis bus and state have
1849
+ * not yet been initialized, and only communications with Conduit core
1850
+ * have been established.
1851
+ */
1852
+ async preServerStart() {
1853
+ }
1854
+ /**
1855
+ * This is triggered right after Grpc Server start.
1856
+ * At this stage the core module will communicate with your service
1857
+ * for health checks, and the Bus and State management mechanics
1858
+ * are online and available.
1859
+ */
1860
+ async onServerStart() {
1861
+ }
1862
+ /**
1863
+ * Triggers right before registering the module with Conduit Core.
1864
+ */
1865
+ async preRegister() {
1866
+ }
1867
+ /**
1868
+ * Triggers when module has been registered with Conduit Core
1869
+ * This is the step where you should initialize your
1870
+ * module's general routing (admin or client) and any other operation,
1871
+ * that would require other modules being able to reach you
1872
+ */
1873
+ async onRegister() {
1874
+ }
1875
+ /**
1876
+ * This is triggered when the module receives new configuration.
1877
+ * At this step the configuration has not been checked yet,
1878
+ * and has only been parsed.
1879
+ * @param config
1880
+ */
1881
+ async preConfig(config) {
1882
+ return config;
1883
+ }
1884
+ /**
1885
+ * This is triggered when new configuration has been applied.
1886
+ * This happens either when changes are being made on the configuration,
1887
+ * or on module start up, where the config is recovered from the database.
1888
+ * In either case the configuration is now available through the
1889
+ * Config Controller of the sdk.
1890
+ */
1891
+ async onConfig() {
1892
+ }
1893
+ /**
1894
+ * Registers common and module-specific metric types.
1895
+ */
1896
+ async registerMetrics() {
1897
+ if (import_grpc_sdk14.ConduitGrpcSdk.Metrics) {
1898
+ import_grpc_sdk14.ConduitGrpcSdk.Metrics.initializeDefaultMetrics();
1899
+ if (this.metricsSchema) {
1900
+ Object.values(this.metricsSchema).forEach((metric) => {
1901
+ import_grpc_sdk14.ConduitGrpcSdk.Metrics.registerMetric(metric.type, metric.config);
1651
1902
  });
1652
- }
1653
- /**
1654
- * This is the first function triggered on module spin up.
1655
- * @param address {string} external address:port of service (LoadBalancer)
1656
- * @param port {string} port to bring up gRPC service
1657
- */
1658
- initialize(address, port) {
1659
- this._address = address;
1660
- this._port = port;
1661
- if (this.configSchema) {
1662
- this.config = convict(this.configSchema);
1663
- }
1664
- }
1665
- /**
1666
- * This is triggered before spinning up the Grpc Server
1667
- * of the module. On this stage, the redis bus and state have
1668
- * not yet been initialized, and only communications with Conduit core
1669
- * have been established.
1670
- */
1671
- async preServerStart() { }
1672
- /**
1673
- * This is triggered right after Grpc Server start.
1674
- * At this stage the core module will communicate with your service
1675
- * for health checks, and the Bus and State management mechanics
1676
- * are online and available.
1677
- */
1678
- async onServerStart() { }
1679
- /**
1680
- * Triggers right before registering the module with Conduit Core.
1681
- */
1682
- async preRegister() { }
1683
- /**
1684
- * Triggers when module has been registered with Conduit Core
1685
- * This is the step where you should initialize your
1686
- * module's general routing (admin or client) and any other operation,
1687
- * that would require other modules being able to reach you
1688
- */
1689
- async onRegister() { }
1690
- /**
1691
- * This is triggered when the module receives new configuration.
1692
- * At this step the configuration has not been checked yet,
1693
- * and has only been parsed.
1694
- * @param config
1695
- */
1696
- async preConfig(config) {
1697
- return config;
1698
- }
1699
- /**
1700
- * This is triggered when new configuration has been applied.
1701
- * This happens either when changes are being made on the configuration,
1702
- * or on module start up, where the config is recovered from the database.
1703
- * In either case the configuration is now available through the
1704
- * Config Controller of the sdk.
1705
- */
1706
- async onConfig() { }
1707
- /**
1708
- * Registers common and module-specific metric types.
1709
- */
1710
- async registerMetrics() {
1711
- if (grpcSdk.ConduitGrpcSdk.Metrics) {
1712
- grpcSdk.ConduitGrpcSdk.Metrics.initializeDefaultMetrics();
1713
- if (this.metricsSchema) {
1714
- Object.values(this.metricsSchema).forEach(metric => {
1715
- grpcSdk.ConduitGrpcSdk.Metrics.registerMetric(metric.type, metric.config);
1716
- });
1717
- }
1718
- }
1719
- }
1720
- /**
1721
- * Initializes metric startup values.
1722
- * Implemented by individual modules.
1723
- */
1724
- async initializeMetrics() { }
1725
- async createGrpcServer() {
1726
- this.grpcServer = new GrpcServer(this._port);
1727
- this._port = (await this.grpcServer.createNewServer()).toString();
1728
- }
1729
- async startGrpcServer() {
1730
- if (this.service) {
1731
- this._serviceName = this.service.protoDescription.substring(this.service.protoDescription.indexOf('.') + 1);
1732
- await this.grpcServer.addService(this.service.protoPath, this.service.protoDescription, this.service.functions);
1733
- }
1734
- RoutingManager.ClientController = new RoutingController();
1735
- RoutingManager.AdminController = new RoutingController();
1736
- await this.addHealthCheckService();
1737
- await this.addModuleService();
1738
- await this.grpcServer.start();
1739
- grpcSdk.ConduitGrpcSdk.Logger.log('gRPC server listening on ' + this._port);
1740
- }
1741
- async setConfig(call, callback) {
1742
- try {
1743
- if (!this.config) {
1744
- return callback({
1745
- code: grpcJs.status.INVALID_ARGUMENT,
1746
- message: 'Module is not configurable',
1747
- });
1748
- }
1749
- let config = JSON.parse(call.request.newConfig);
1750
- config = merge(this.config.getProperties(), config);
1751
- config = await this.preConfig(config);
1752
- const previousConfig = this.config.getProperties();
1753
- try {
1754
- this.config.load(config).validate({
1755
- allowed: 'warn',
1756
- });
1757
- config = this.config.getProperties();
1758
- callback(null, { updatedConfig: JSON.stringify(config) });
1759
- }
1760
- catch (e) {
1761
- this.config = convict(this.configSchema);
1762
- this.config.load(previousConfig);
1763
- return callback({
1764
- code: grpcJs.status.INVALID_ARGUMENT,
1765
- message: 'Invalid configuration values: ' + e.message,
1766
- });
1767
- }
1768
- ConfigController.getInstance().config = config;
1769
- await this.onConfig();
1770
- this.grpcSdk.bus?.publish(lodash.kebabCase(this.name) + ':config:update', JSON.stringify(config));
1771
- }
1772
- catch (e) {
1773
- return callback({ code: grpcJs.status.INTERNAL, message: e.message });
1774
- }
1775
- }
1776
- /** Used to update the module's configuration on initial Redis/DB reconciliation. */
1777
- async handleConfigSyncUpdate() {
1778
- if (!this.config)
1779
- return;
1780
- this.grpcSdk.bus.subscribe(`${lodash.kebabCase(this.name)}:config:update`, async (message) => {
1781
- ConfigController.getInstance().config = await this.preConfig(JSON.parse(message));
1782
- await this.onConfig();
1903
+ }
1904
+ }
1905
+ }
1906
+ /**
1907
+ * Initializes metric startup values.
1908
+ * Implemented by individual modules.
1909
+ */
1910
+ async initializeMetrics() {
1911
+ }
1912
+ getPeerManifestRoot() {
1913
+ return this._peerManifestRoot ?? process.cwd();
1914
+ }
1915
+ loadConduitPeersManifest() {
1916
+ if (this._cachedPeersManifest === void 0) {
1917
+ this._cachedPeersManifest = readConduitPeersManifest(this.getPeerManifestRoot());
1918
+ }
1919
+ return this._cachedPeersManifest;
1920
+ }
1921
+ validateDeclaredPeerNames(names) {
1922
+ const known = new Set(this.grpcSdk.getKnownModuleNames());
1923
+ for (const n of names) {
1924
+ if (!known.has(n)) {
1925
+ import_grpc_sdk14.ConduitGrpcSdk.Logger.warn(
1926
+ `Declared Conduit peer "${n}" is not a known built-in module (see ConduitGrpcSdk.getKnownModuleNames).`
1927
+ );
1928
+ }
1929
+ }
1930
+ }
1931
+ async awaitPeersFromManifest() {
1932
+ const manifest = this.loadConduitPeersManifest();
1933
+ const peers = manifest?.peers?.await;
1934
+ if (!peers?.length) return;
1935
+ this.validateDeclaredPeerNames(peers);
1936
+ await Promise.all(peers.map((m) => this.grpcSdk.awaitPeer(m)));
1937
+ }
1938
+ registerDeclaredPeerWatches() {
1939
+ this.disposeDeclaredPeerWatches();
1940
+ const manifest = this.loadConduitPeersManifest();
1941
+ const watch = manifest?.peers?.watch;
1942
+ if (!watch?.length) return;
1943
+ this.validateDeclaredPeerNames(watch.map((w) => w.module));
1944
+ const specs = watch.map((entry) => ({
1945
+ module: entry.module,
1946
+ edge: entry.edge ?? "rising"
1947
+ }));
1948
+ const dispose = this.grpcSdk.watchPeers(
1949
+ specs,
1950
+ (statuses) => {
1951
+ for (const [mod, s] of Object.entries(statuses)) {
1952
+ if (this._lastPeerBatchStates[mod] !== s) {
1953
+ this.onDeclaredPeerConnectionUpdate(mod, s);
1954
+ this._lastPeerBatchStates[mod] = s;
1955
+ }
1956
+ }
1957
+ },
1958
+ { debounceMs: 100, syncInitialState: true }
1959
+ );
1960
+ this._declaredPeerWatchDisposers.push(dispose);
1961
+ }
1962
+ disposeDeclaredPeerWatches() {
1963
+ for (const d of this._declaredPeerWatchDisposers) {
1964
+ d();
1965
+ }
1966
+ this._declaredPeerWatchDisposers = [];
1967
+ this._lastPeerBatchStates = {};
1968
+ }
1969
+ onDeclaredPeerConnectionUpdate(_moduleName, _serving) {
1970
+ }
1971
+ async createGrpcServer() {
1972
+ this.grpcServer = new GrpcServer(this._port);
1973
+ this._port = (await this.grpcServer.createNewServer()).toString();
1974
+ }
1975
+ async startGrpcServer() {
1976
+ if (this.service) {
1977
+ this._serviceName = this.service.protoDescription.substring(
1978
+ this.service.protoDescription.indexOf(".") + 1
1979
+ );
1980
+ await this.grpcServer.addService(
1981
+ this.service.protoPath,
1982
+ this.service.protoDescription,
1983
+ this.service.functions
1984
+ );
1985
+ }
1986
+ RoutingManager.ClientController = new RoutingController();
1987
+ RoutingManager.AdminController = new RoutingController();
1988
+ await this.addHealthCheckService();
1989
+ await this.addModuleService();
1990
+ await this.grpcServer.start();
1991
+ import_grpc_sdk14.ConduitGrpcSdk.Logger.log("gRPC server listening on " + this._port);
1992
+ }
1993
+ async setConfig(call, callback) {
1994
+ try {
1995
+ if (!this.config) {
1996
+ return callback({
1997
+ code: import_grpc_js6.status.INVALID_ARGUMENT,
1998
+ message: "Module is not configurable"
1783
1999
  });
1784
- }
1785
- async preRegisterLifecycle() {
1786
- await this.createGrpcServer();
1787
- this._moduleState = exports.ModuleLifecycleStage.PRE_SERVER_START;
1788
- await this.preServerStart();
1789
- await this.grpcSdk.initializeEventBus();
1790
- await this.handleConfigSyncUpdate();
1791
- await this.registerMetrics();
1792
- await this.startGrpcServer();
1793
- this._moduleState = exports.ModuleLifecycleStage.SERVER_STARTED;
1794
- await this.onServerStart();
1795
- await this.initializeMetrics();
1796
- this._moduleState = exports.ModuleLifecycleStage.PRE_REGISTER;
1797
- await this.preRegister();
1798
- }
1799
- async postRegisterLifecycle() {
1800
- this._moduleState = exports.ModuleLifecycleStage.POST_REGISTER;
1801
- this.registered = true;
1802
- await this.onRegister();
1803
- if (this.config) {
1804
- const configSchema = this.config.getSchema();
1805
- let config = this.config.getProperties();
1806
- config = await this.preConfig(config);
1807
- config = await this.grpcSdk.config.configure(config, convictConfigParser(configSchema), this.configOverride);
1808
- ConfigController.getInstance();
1809
- if (config)
1810
- ConfigController.getInstance().config = config;
1811
- if (!config || config.active || !config.hasOwnProperty('active'))
1812
- await this.onConfig();
1813
- }
1814
- }
1815
- }
1816
-
1817
- class ModuleError extends grpcSdk.GrpcError {
1818
- debugLogInfo;
1819
- constructor(errorDefinition, debugLogInfo) {
1820
- const { grpcCode, conduitCode, message } = errorDefinition;
1821
- super(grpcCode, JSON.stringify({
1822
- message,
1823
- conduitCode: conduitCode,
1824
- }));
1825
- if (debugLogInfo)
1826
- this.debugLogInfo = debugLogInfo;
1827
- }
1828
- }
2000
+ }
2001
+ let config = JSON.parse(call.request.newConfig);
2002
+ config = merge(this.config.getProperties(), config);
2003
+ config = await this.preConfig(config);
2004
+ const previousConfig = this.config.getProperties();
2005
+ try {
2006
+ this.config.load(config).validate({
2007
+ allowed: "warn"
2008
+ });
2009
+ config = this.config.getProperties();
2010
+ callback(null, { updatedConfig: JSON.stringify(config) });
2011
+ } catch (e) {
2012
+ this.config = (0, import_convict.default)(this.configSchema);
2013
+ this.config.load(previousConfig);
2014
+ return callback({
2015
+ code: import_grpc_js6.status.INVALID_ARGUMENT,
2016
+ message: "Invalid configuration values: " + e.message
2017
+ });
2018
+ }
2019
+ const configJson = JSON.stringify(config);
2020
+ this._pendingConfigUpdates.push(configJson);
2021
+ await this._drainConfigQueue();
2022
+ this.grpcSdk.bus?.publish((0, import_lodash_es6.kebabCase)(this.name) + ":config:update", configJson);
2023
+ } catch (e) {
2024
+ return callback({ code: import_grpc_js6.status.INTERNAL, message: e.message });
2025
+ }
2026
+ }
2027
+ /** Used to update the module's configuration on initial Redis/DB reconciliation. */
2028
+ async handleConfigSyncUpdate() {
2029
+ if (!this.config) return;
2030
+ this.grpcSdk.bus.subscribe(
2031
+ `${(0, import_lodash_es6.kebabCase)(this.name)}:config:update`,
2032
+ async (message) => {
2033
+ this._pendingConfigUpdates.push(message);
2034
+ await this._drainConfigQueue();
2035
+ }
2036
+ );
2037
+ }
2038
+ /** Serializes config updates; waits for initial lifecycle before draining. */
2039
+ async _drainConfigQueue() {
2040
+ if (!this._configInitialized || this._processingConfig) return;
2041
+ this._processingConfig = true;
2042
+ try {
2043
+ while (this._pendingConfigUpdates.length > 0) {
2044
+ const message = this._pendingConfigUpdates.shift();
2045
+ ConfigController.getInstance().config = await this.preConfig(JSON.parse(message));
2046
+ await this.onConfig();
2047
+ }
2048
+ } finally {
2049
+ this._processingConfig = false;
2050
+ }
2051
+ }
2052
+ async preRegisterLifecycle() {
2053
+ await this.createGrpcServer();
2054
+ this._moduleState = "pre-grpc-start" /* PRE_SERVER_START */;
2055
+ await this.preServerStart();
2056
+ await this.grpcSdk.initializeEventBus();
2057
+ await this.handleConfigSyncUpdate();
2058
+ await this.registerMetrics();
2059
+ await this.startGrpcServer();
2060
+ this._moduleState = "grpc-server-started" /* SERVER_STARTED */;
2061
+ await this.onServerStart();
2062
+ await this.initializeMetrics();
2063
+ this._moduleState = "pre-register" /* PRE_REGISTER */;
2064
+ await this.preRegister();
2065
+ }
2066
+ async postRegisterLifecycle() {
2067
+ this._moduleState = "post-register" /* POST_REGISTER */;
2068
+ this.registered = true;
2069
+ await this.onRegister();
2070
+ if (this.config) {
2071
+ const configSchema = this.config.getSchema();
2072
+ let config = this.config.getProperties();
2073
+ config = await this.preConfig(config);
2074
+ config = await this.grpcSdk.config.configure(
2075
+ config,
2076
+ convictConfigParser(configSchema),
2077
+ this.configOverride
2078
+ );
2079
+ ConfigController.getInstance();
2080
+ if (config) ConfigController.getInstance().config = config;
2081
+ if (!config || config.active || !config.hasOwnProperty("active"))
2082
+ await this.onConfig();
2083
+ }
2084
+ this._configInitialized = true;
2085
+ await this._drainConfigQueue();
2086
+ }
2087
+ };
1829
2088
 
1830
- exports.ConduitActiveSchema = ConduitActiveSchema;
1831
- exports.ConduitBoolean = ConduitBoolean;
1832
- exports.ConduitDate = ConduitDate;
1833
- exports.ConduitJson = ConduitJson;
1834
- exports.ConduitNumber = ConduitNumber;
1835
- exports.ConduitObjectId = ConduitObjectId;
1836
- exports.ConduitRelation = ConduitRelation;
1837
- exports.ConduitServiceModule = ConduitServiceModule;
1838
- exports.ConduitString = ConduitString;
1839
- exports.ConfigController = ConfigController;
1840
- exports.GrpcServer = GrpcServer;
1841
- exports.ManagedModule = ManagedModule;
1842
- exports.ModuleError = ModuleError;
1843
- exports.ProxyRouteBuilder = ProxyRouteBuilder;
1844
- exports.RouteBuilder = RouteBuilder;
1845
- exports.RoutingManager = RoutingManager;
1846
- exports.addServiceToServer = addServiceToServer;
1847
- exports.createParsedRouterRequest = createParsedRouterRequest;
1848
- exports.createServer = createServer;
1849
- exports.initializeSdk = initializeSdk;
1850
- exports.linearBackoffTimeout = linearBackoffTimeout;
1851
- exports.linearBackoffTimeoutAsync = linearBackoffTimeoutAsync;
1852
- exports.merge = merge;
1853
- exports.wrapFunctionsAsync = wrapFunctionsAsync;
1854
- exports.wrapGrpcFunctions = wrapGrpcFunctions;
1855
- exports.wrapRouterGrpcFunction = wrapRouterGrpcFunction;
1856
- //# sourceMappingURL=index.cjs.map
2089
+ // src/classes/ModuleError.ts
2090
+ var import_grpc_sdk15 = require("@conduitplatform/grpc-sdk");
2091
+ var ModuleError = class extends import_grpc_sdk15.GrpcError {
2092
+ debugLogInfo;
2093
+ constructor(errorDefinition, debugLogInfo) {
2094
+ const { grpcCode, conduitCode, message } = errorDefinition;
2095
+ super(
2096
+ grpcCode,
2097
+ JSON.stringify({
2098
+ message,
2099
+ conduitCode
2100
+ })
2101
+ );
2102
+ if (debugLogInfo) this.debugLogInfo = debugLogInfo;
2103
+ }
2104
+ };
2105
+ // Annotate the CommonJS export names for ESM import in node:
2106
+ 0 && (module.exports = {
2107
+ ConduitActiveSchema,
2108
+ ConduitBoolean,
2109
+ ConduitDate,
2110
+ ConduitJson,
2111
+ ConduitNumber,
2112
+ ConduitObjectId,
2113
+ ConduitRelation,
2114
+ ConduitServiceModule,
2115
+ ConduitString,
2116
+ ConfigController,
2117
+ DEFAULT_STRIP_FIELDS,
2118
+ GrpcServer,
2119
+ ManagedModule,
2120
+ ModuleError,
2121
+ ModuleLifecycleStage,
2122
+ RouteBuilder,
2123
+ RoutingManager,
2124
+ addServiceToServer,
2125
+ createParsedRouterRequest,
2126
+ createServer,
2127
+ exportModel,
2128
+ importModel,
2129
+ initializeSdk,
2130
+ linearBackoffTimeout,
2131
+ linearBackoffTimeoutAsync,
2132
+ merge,
2133
+ readConduitPeersManifest,
2134
+ sanitizeDocumentsForExport,
2135
+ stripInternalFields,
2136
+ wrapFunctionsAsync,
2137
+ wrapGrpcFunctions,
2138
+ wrapRouterGrpcFunction
2139
+ });
2140
+ //# sourceMappingURL=index.cjs.map