@conduitplatform/module-tools 0.16.4 → 0.16.6

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 (70) hide show
  1. package/dist/classes/ConduitServiceModule.d.ts +1 -1
  2. package/dist/index.cjs +1776 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.esm.js +1749 -0
  5. package/dist/index.esm.js.map +1 -0
  6. package/package.json +24 -5
  7. package/dist/ManagedModule.js +0 -221
  8. package/dist/ManagedModule.js.map +0 -1
  9. package/dist/classes/ConduitActiveSchema.js +0 -44
  10. package/dist/classes/ConduitActiveSchema.js.map +0 -1
  11. package/dist/classes/ConduitServiceModule.js +0 -110
  12. package/dist/classes/ConduitServiceModule.js.map +0 -1
  13. package/dist/classes/ConfigController.js +0 -16
  14. package/dist/classes/ConfigController.js.map +0 -1
  15. package/dist/classes/GrpcServer.js +0 -121
  16. package/dist/classes/GrpcServer.js.map +0 -1
  17. package/dist/classes/index.js +0 -5
  18. package/dist/classes/index.js.map +0 -1
  19. package/dist/helpers/GrpcHelper.js +0 -38
  20. package/dist/helpers/GrpcHelper.js.map +0 -1
  21. package/dist/helpers/GrpcToParsedRouterRequest.js +0 -15
  22. package/dist/helpers/GrpcToParsedRouterRequest.js.map +0 -1
  23. package/dist/helpers/TypeHelpers.js +0 -79
  24. package/dist/helpers/TypeHelpers.js.map +0 -1
  25. package/dist/helpers/index.js +0 -5
  26. package/dist/helpers/index.js.map +0 -1
  27. package/dist/helpers/wrapGrpcFunctions.js +0 -48
  28. package/dist/helpers/wrapGrpcFunctions.js.map +0 -1
  29. package/dist/index.js +0 -7
  30. package/dist/index.js.map +0 -1
  31. package/dist/interfaces/ConduitService.js +0 -2
  32. package/dist/interfaces/ConduitService.js.map +0 -1
  33. package/dist/interfaces/ModuleLifecycleStage.js +0 -9
  34. package/dist/interfaces/ModuleLifecycleStage.js.map +0 -1
  35. package/dist/interfaces/index.js +0 -3
  36. package/dist/interfaces/index.js.map +0 -1
  37. package/dist/logging/index.js +0 -124
  38. package/dist/logging/index.js.map +0 -1
  39. package/dist/metrics/MetricsServer.js +0 -42
  40. package/dist/metrics/MetricsServer.js.map +0 -1
  41. package/dist/metrics/clientMiddleware.js +0 -120
  42. package/dist/metrics/clientMiddleware.js.map +0 -1
  43. package/dist/metrics/config/defaults.js +0 -91
  44. package/dist/metrics/config/defaults.js.map +0 -1
  45. package/dist/metrics/index.js +0 -109
  46. package/dist/metrics/index.js.map +0 -1
  47. package/dist/routing/ProxyRouteBuilder.js +0 -74
  48. package/dist/routing/ProxyRouteBuilder.js.map +0 -1
  49. package/dist/routing/RouteBuilder.js +0 -100
  50. package/dist/routing/RouteBuilder.js.map +0 -1
  51. package/dist/routing/RoutingController.js +0 -21
  52. package/dist/routing/RoutingController.js.map +0 -1
  53. package/dist/routing/RoutingManager.js +0 -140
  54. package/dist/routing/RoutingManager.js.map +0 -1
  55. package/dist/routing/RoutingUtilities.js +0 -9
  56. package/dist/routing/RoutingUtilities.js.map +0 -1
  57. package/dist/routing/index.js +0 -6
  58. package/dist/routing/index.js.map +0 -1
  59. package/dist/routing/wrapRouterFunctions.js +0 -120
  60. package/dist/routing/wrapRouterFunctions.js.map +0 -1
  61. package/dist/utilities/convictConfigParser.js +0 -14
  62. package/dist/utilities/convictConfigParser.js.map +0 -1
  63. package/dist/utilities/index.js +0 -4
  64. package/dist/utilities/index.js.map +0 -1
  65. package/dist/utilities/initializeSdk.js +0 -35
  66. package/dist/utilities/initializeSdk.js.map +0 -1
  67. package/dist/utilities/linearBackoffTimeout.js +0 -57
  68. package/dist/utilities/linearBackoffTimeout.js.map +0 -1
  69. package/dist/utilities/merge.js +0 -11
  70. package/dist/utilities/merge.js.map +0 -1
@@ -0,0 +1,1749 @@
1
+ import ConduitGrpcSdk, { ConduitSchema, TYPE, ConduitRouteReturnDefinition, Admin, ConduitRouteActions, HealthCheckStatus, HealthCheckResponse_ServingStatus, MetricType } from '@conduitplatform/grpc-sdk';
2
+ import { Server, ServerCredentials, loadPackageDefinition, status } from '@grpc/grpc-js';
3
+ import protoLoader from '@grpc/proto-loader';
4
+ import { createVerifier } from 'fast-jwt';
5
+ import { isNil, camelCase, isEmpty, isNaN, mergeWith, kebabCase } from 'lodash-es';
6
+ import path from 'path';
7
+ import { EventEmitter } from 'events';
8
+ import { fileURLToPath } from 'node:url';
9
+ import convict from 'convict';
10
+ import winston, { format } from 'winston';
11
+ import { get } from 'http';
12
+ import LokiTransport from 'winston-loki';
13
+ import { clearTimeout as clearTimeout$1 } from 'timers';
14
+ import { Registry, collectDefaultMetrics, Counter, Summary, Histogram, Gauge, exponentialBuckets } from 'prom-client';
15
+ import express from 'express';
16
+ import { isAbortError } from 'abort-controller-x';
17
+ import { Status, ClientError } from 'nice-grpc-common';
18
+
19
+ class ConduitActiveSchema extends ConduitSchema {
20
+ dbInstance;
21
+ constructor(dbInstance, name, fields, modelOptions, collectionName) {
22
+ super(name, fields ?? {}, modelOptions, collectionName);
23
+ this.dbInstance = dbInstance;
24
+ }
25
+ findOne(query, select, populate, userId, scope) {
26
+ return this.dbInstance.findOne(this.name, query, select, populate, userId, scope);
27
+ }
28
+ findMany(query, select, skip, limit, sort, populate, userId, scope) {
29
+ return this.dbInstance.findMany(this.name, query, select, skip, limit, sort, populate, userId, scope);
30
+ }
31
+ create(query, userId, scope) {
32
+ return this.dbInstance.create(this.name, query, userId, scope);
33
+ }
34
+ createMany(query, userId, scope) {
35
+ return this.dbInstance.createMany(this.name, query, userId, scope);
36
+ }
37
+ findByIdAndUpdate(id, document, populate, userId, scope) {
38
+ return this.dbInstance.findByIdAndUpdate(this.name, id, document, populate, userId, scope);
39
+ }
40
+ findByIdAndReplace(id, document, populate, userId, scope) {
41
+ return this.dbInstance.findByIdAndReplace(this.name, id, document, populate, userId, scope);
42
+ }
43
+ replaceOne(filterQuery, query, populate, userId, scope) {
44
+ return this.dbInstance.replaceOne(this.name, filterQuery, query, populate, userId, scope);
45
+ }
46
+ updateOne(filterQuery, query, populate, userId, scope) {
47
+ return this.dbInstance.updateOne(this.name, filterQuery, query, populate, userId, scope);
48
+ }
49
+ updateMany(filterQuery, query, populate, userId, scope) {
50
+ return this.dbInstance.updateMany(this.name, filterQuery, query, populate, userId, scope);
51
+ }
52
+ deleteOne(query, userId, scope) {
53
+ return this.dbInstance.deleteOne(this.name, query, userId, scope);
54
+ }
55
+ deleteMany(query, userId, scope) {
56
+ return this.dbInstance.deleteMany(this.name, query, userId, scope);
57
+ }
58
+ countDocuments(query, userId, scope) {
59
+ return this.dbInstance.countDocuments(this.name, query, userId, scope);
60
+ }
61
+ }
62
+
63
+ class ConduitStringConstructor {
64
+ // private to disallow creating other instances of this type
65
+ constructor() { }
66
+ static get Optional() {
67
+ return TYPE.String;
68
+ }
69
+ static get Required() {
70
+ return { type: TYPE.String, required: true };
71
+ }
72
+ }
73
+ const ConduitString = ConduitStringConstructor;
74
+ class ConduitNumberConstructor {
75
+ // private to disallow creating other instances of this type
76
+ constructor() { }
77
+ static get Optional() {
78
+ return TYPE.Number;
79
+ }
80
+ static get Required() {
81
+ return { type: TYPE.Number, required: true };
82
+ }
83
+ }
84
+ const ConduitNumber = ConduitNumberConstructor;
85
+ class ConduitBooleanConstructor {
86
+ // private to disallow creating other instances of this type
87
+ constructor() { }
88
+ static get Optional() {
89
+ return TYPE.Boolean;
90
+ }
91
+ static get Required() {
92
+ return { type: TYPE.Boolean, required: true };
93
+ }
94
+ }
95
+ const ConduitBoolean = ConduitBooleanConstructor;
96
+ class ConduitDateConstructor {
97
+ // private to disallow creating other instances of this type
98
+ constructor() { }
99
+ static get Optional() {
100
+ return TYPE.Date;
101
+ }
102
+ static get Required() {
103
+ return { type: TYPE.Date, required: true };
104
+ }
105
+ }
106
+ const ConduitDate = ConduitDateConstructor;
107
+ class ConduitObjectIdConstructor {
108
+ // private to disallow creating other instances of this type
109
+ constructor() { }
110
+ static get Optional() {
111
+ return TYPE.ObjectId;
112
+ }
113
+ static get Required() {
114
+ return { type: TYPE.ObjectId, required: true };
115
+ }
116
+ }
117
+ const ConduitObjectId = ConduitObjectIdConstructor;
118
+ class ConduitJSONConstructor {
119
+ // private to disallow creating other instances of this type
120
+ constructor() { }
121
+ static get Optional() {
122
+ return TYPE.JSON;
123
+ }
124
+ static get Required() {
125
+ return { type: TYPE.JSON, required: true };
126
+ }
127
+ }
128
+ const ConduitJson = ConduitJSONConstructor;
129
+ class ConduitRelationConstructor {
130
+ // private to disallow creating other instances of this type
131
+ constructor() { }
132
+ static Optional(model) {
133
+ return { type: TYPE.Relation, model, required: false };
134
+ }
135
+ static Required(model) {
136
+ return { type: TYPE.Relation, model, required: true };
137
+ }
138
+ }
139
+ const ConduitRelation = ConduitRelationConstructor;
140
+
141
+ function createServer(port) {
142
+ const grpcServer = new Server({
143
+ 'grpc.max_receive_message_length': 1024 * 1024 * 100,
144
+ 'grpc.max_send_message_length': 1024 * 1024 * 100,
145
+ });
146
+ return new Promise((resolve, reject) => {
147
+ grpcServer.bindAsync(port, ServerCredentials.createInsecure(), (err, port) => {
148
+ if (err) {
149
+ reject(err);
150
+ }
151
+ else {
152
+ resolve({
153
+ server: grpcServer,
154
+ port,
155
+ });
156
+ }
157
+ });
158
+ });
159
+ }
160
+ function addServiceToServer(server, protoPath, descriptorObject, functions) {
161
+ const packageDefinition = protoLoader.loadSync(protoPath, {
162
+ keepCase: true,
163
+ longs: String,
164
+ enums: String,
165
+ defaults: true,
166
+ oneofs: true,
167
+ });
168
+ const protoDescriptor = loadPackageDefinition(packageDefinition);
169
+ const objs = descriptorObject.split('.');
170
+ let descObj = protoDescriptor;
171
+ objs.forEach((r) => {
172
+ descObj = descObj[r];
173
+ });
174
+ server.addService(descObj.service, functions);
175
+ }
176
+
177
+ function wrapGrpcFunctions(functions, postponeRestart) {
178
+ const grpcKey = process.env.GRPC_KEY;
179
+ const wrappedFunctions = {};
180
+ Object.keys(functions).forEach(name => {
181
+ wrappedFunctions[name] = (call, callback) => {
182
+ ConduitGrpcSdk.Metrics?.increment('internal_grpc_requests_total');
183
+ postponeRestart();
184
+ if (grpcKey) {
185
+ const verify = createVerifier({ key: grpcKey });
186
+ const grpcToken = call.metadata.get('grpc-token')[0];
187
+ if (!grpcToken) {
188
+ return throwError(callback, 'No gRPC protection token provided', status.PERMISSION_DENIED);
189
+ }
190
+ try {
191
+ const jwt = verify(grpcToken);
192
+ call.metadata.set('module-name', jwt.moduleName);
193
+ }
194
+ catch {
195
+ return throwError(callback, 'Failed to verify gRPC protection token. GRPC_KEY value differs', status.PERMISSION_DENIED);
196
+ }
197
+ }
198
+ let invoked;
199
+ try {
200
+ invoked = functions[name](call, callback);
201
+ }
202
+ catch (error) {
203
+ return throwError(callback, error.message);
204
+ }
205
+ if (typeof invoked?.then === 'function') {
206
+ invoked.then().catch((error) => {
207
+ return throwError(callback, error.message);
208
+ });
209
+ }
210
+ };
211
+ });
212
+ return wrappedFunctions;
213
+ }
214
+ function throwError(callback, message, code = status.INTERNAL) {
215
+ // Aborting server streams is not yet implemented!
216
+ if (callback) {
217
+ callback({ code, message });
218
+ }
219
+ }
220
+
221
+ function createParsedRouterRequest(params, urlParams, queryParams, bodyParams, path, headers, context, cookies) {
222
+ return {
223
+ request: {
224
+ params,
225
+ urlParams: urlParams ?? {},
226
+ queryParams: queryParams ?? {},
227
+ bodyParams: bodyParams ?? {},
228
+ path: path ?? '',
229
+ headers: headers,
230
+ context: context ?? {},
231
+ cookies: cookies ?? {},
232
+ },
233
+ };
234
+ }
235
+
236
+ class GrpcServer {
237
+ grpcServer;
238
+ started = false;
239
+ startedOnce = false;
240
+ _serviceNames = [];
241
+ scheduledRestart;
242
+ _postponeRequests = 0;
243
+ _useForce;
244
+ _services = [];
245
+ constructor(port) {
246
+ this._url = `0.0.0.0:${port ?? '5000'}`;
247
+ }
248
+ async waitForReady() {
249
+ while (!isNil(this.scheduledRestart)) {
250
+ await this.wait(200);
251
+ }
252
+ }
253
+ _url;
254
+ get url() {
255
+ return this._url;
256
+ }
257
+ async createNewServer(useForce = false) {
258
+ if (isNil(this._useForce)) {
259
+ this._useForce = useForce;
260
+ }
261
+ const serverResult = await createServer(this._url);
262
+ this.grpcServer = serverResult.server;
263
+ this._url = this._url.split(':')[0] + ':' + serverResult.port.toString();
264
+ return serverResult.port;
265
+ }
266
+ async addService(protoPath, protoDescription, functions) {
267
+ functions = wrapGrpcFunctions(functions, this.postponeRestart.bind(this));
268
+ if (this._serviceNames.indexOf(protoDescription) !== -1) {
269
+ ConduitGrpcSdk.Logger.log('Service already exists, performing replace');
270
+ this._services[this._serviceNames.indexOf(protoDescription)] = {
271
+ protoPath,
272
+ protoDescription,
273
+ functions,
274
+ };
275
+ this.scheduleRefresh();
276
+ return this;
277
+ }
278
+ else {
279
+ this._services.push({ protoPath, protoDescription, functions });
280
+ this._serviceNames.push(protoDescription);
281
+ if (this.started) {
282
+ ConduitGrpcSdk.Logger.log('Server already started, scheduling refresh..');
283
+ this.scheduleRefresh();
284
+ return this;
285
+ }
286
+ else {
287
+ if (!this.grpcServer) {
288
+ await this.wait(1000);
289
+ }
290
+ addServiceToServer(this.grpcServer, protoPath, protoDescription, functions);
291
+ return this;
292
+ }
293
+ }
294
+ }
295
+ async wait(time) {
296
+ return new Promise((resolve, reject) => {
297
+ const timeout = setTimeout(() => {
298
+ clearTimeout(timeout);
299
+ resolve();
300
+ }, time);
301
+ });
302
+ }
303
+ async refresh() {
304
+ if (this.started) {
305
+ this.started = false;
306
+ if (this._useForce) {
307
+ this.grpcServer.forceShutdown();
308
+ }
309
+ else {
310
+ //gracefully shutdown so that there are no service disruption
311
+ await new Promise(resolve => this.grpcServer.tryShutdown(() => resolve()));
312
+ }
313
+ }
314
+ await this.createNewServer();
315
+ this._services.forEach(service => {
316
+ addServiceToServer(this.grpcServer, service.protoPath, service.protoDescription, service.functions);
317
+ });
318
+ if (!this.started && this.startedOnce) {
319
+ this.grpcServer.start();
320
+ this.started = true;
321
+ }
322
+ }
323
+ scheduleRefresh() {
324
+ if (this.scheduledRestart) {
325
+ clearTimeout(this.scheduledRestart);
326
+ }
327
+ const self = this;
328
+ this.scheduledRestart = setTimeout(async () => {
329
+ ConduitGrpcSdk.Logger.log('Begin refresh');
330
+ await self.refresh();
331
+ ConduitGrpcSdk.Logger.log('Refresh complete');
332
+ clearTimeout(self.scheduledRestart);
333
+ self.scheduledRestart = undefined;
334
+ self._postponeRequests = 0;
335
+ }, 2000);
336
+ }
337
+ start() {
338
+ if (this.started) {
339
+ ConduitGrpcSdk.Logger.error('gRPC server is already running!');
340
+ return;
341
+ }
342
+ else if (!this.started && this.startedOnce) {
343
+ ConduitGrpcSdk.Logger.error('gRPC server is down for refresh!');
344
+ }
345
+ this.started = true;
346
+ this.startedOnce = true;
347
+ this.grpcServer?.start();
348
+ }
349
+ postponeRestart() {
350
+ if (!this.scheduledRestart || this._postponeRequests > 5)
351
+ return;
352
+ this._postponeRequests++;
353
+ this.scheduleRefresh();
354
+ }
355
+ }
356
+
357
+ class RouteBuilder {
358
+ manager;
359
+ _options;
360
+ _returns;
361
+ _handler;
362
+ constructor(manager) {
363
+ this.manager = manager;
364
+ this._options = {};
365
+ }
366
+ method(action) {
367
+ this._options.action = action;
368
+ return this;
369
+ }
370
+ name(name) {
371
+ this._options.name = name;
372
+ return this;
373
+ }
374
+ description(description) {
375
+ this._options.description = description;
376
+ return this;
377
+ }
378
+ cacheControl(cache) {
379
+ this._options.cacheControl = cache;
380
+ return this;
381
+ }
382
+ middleware(middleware, allowDuplicates = false) {
383
+ if (!Array.isArray(middleware)) {
384
+ middleware = [middleware];
385
+ }
386
+ if (this._options.middlewares !== undefined &&
387
+ this._options.middlewares?.length !== 0) {
388
+ if (allowDuplicates) {
389
+ this._options.middlewares = this._options.middlewares?.concat(middleware);
390
+ }
391
+ else {
392
+ // add to existing middlewares and filter out potential duplicates
393
+ this._options.middlewares = this._options.middlewares?.concat(middleware.filter(mid => this._options.middlewares?.indexOf(mid) === -1));
394
+ }
395
+ }
396
+ else {
397
+ this._options.middlewares = middleware;
398
+ }
399
+ return this;
400
+ }
401
+ path(path) {
402
+ this._options.path = path;
403
+ return this;
404
+ }
405
+ queryParams(params) {
406
+ this._options.queryParams = params;
407
+ return this;
408
+ }
409
+ urlParams(params) {
410
+ this._options.urlParams = params;
411
+ return this;
412
+ }
413
+ bodyParams(params) {
414
+ this._options.bodyParams = params;
415
+ return this;
416
+ }
417
+ return(name, fields) {
418
+ this._returns = new ConduitRouteReturnDefinition(name, fields);
419
+ return this;
420
+ }
421
+ handler(fn) {
422
+ this._handler = fn;
423
+ return this;
424
+ }
425
+ add() {
426
+ if (!this.manager)
427
+ throw new Error('Builder not setup with manager');
428
+ if (!this._options)
429
+ throw new Error('Cannot build route without options');
430
+ if (!this._options.action)
431
+ throw new Error('Cannot build route without action');
432
+ if (!this._options.path)
433
+ throw new Error('Cannot build route without action');
434
+ if (!this._returns)
435
+ throw new Error('Cannot build route without return');
436
+ if (!this._handler)
437
+ throw new Error('Cannot build route without handler');
438
+ this.manager.route(this._options, this._returns, this._handler);
439
+ }
440
+ build() {
441
+ if (!this._options)
442
+ throw new Error('Cannot build route without options');
443
+ if (!this._options.action)
444
+ throw new Error('Cannot build route without action');
445
+ if (!this._options.path)
446
+ throw new Error('Cannot build route without action');
447
+ if (!this._returns)
448
+ throw new Error('Cannot build route without return');
449
+ if (!this._handler)
450
+ throw new Error('Cannot build route without handler');
451
+ return {
452
+ input: this._options,
453
+ returnType: this._returns,
454
+ handler: this._handler,
455
+ };
456
+ }
457
+ }
458
+
459
+ function generateLog(routerType, routerRequest, requestReceive, call, status) {
460
+ let log = '';
461
+ const latency = Date.now() - requestReceive;
462
+ if (routerRequest) {
463
+ log += `(${routerType}) Request: ${call.request.path}`;
464
+ }
465
+ else {
466
+ log += `(${routerType}) Socket: ${call.request.event} socket: ${call.request.socket}`;
467
+ }
468
+ log += ` ${status ?? '200'} ${latency}`;
469
+ ConduitGrpcSdk.Logger.log(log);
470
+ ConduitGrpcSdk.Metrics?.set('grpc_request_latency_seconds', latency / 1000);
471
+ ConduitGrpcSdk.Metrics?.set(`${routerType}_grpc_request_latency_seconds`, latency / 1000);
472
+ const successStatus = !status || status.toString().charAt(0) === '2';
473
+ ConduitGrpcSdk.Metrics?.increment(`${routerType}_grpc_response_statuses_total`, 1, {
474
+ success: successStatus ? 'true' : 'false',
475
+ });
476
+ }
477
+ function parseRequestData(data) {
478
+ if (data && data.length !== 0) {
479
+ return JSON.parse(data);
480
+ }
481
+ else {
482
+ return {};
483
+ }
484
+ }
485
+ function parseResponseData(r, routerRequest, requestReceive, call, callback, responded, routerType) {
486
+ if (responded.did)
487
+ return;
488
+ responded.did = true;
489
+ if (!r) {
490
+ callback({
491
+ code: status.INTERNAL,
492
+ message: 'Handler did not return a response',
493
+ });
494
+ }
495
+ if (routerRequest) {
496
+ if (typeof r === 'string') {
497
+ callback(null, { result: r });
498
+ }
499
+ else {
500
+ let respObject;
501
+ if (r.removeCookies || r.setCookies) {
502
+ respObject = {
503
+ removeCookies: r.removeCookies,
504
+ setCookies: r.setCookies,
505
+ };
506
+ }
507
+ if (r.result || r.redirect) {
508
+ callback(null, {
509
+ ...respObject,
510
+ redirect: r.redirect ?? undefined,
511
+ result: r.result
512
+ ? typeof r.result === 'string'
513
+ ? r.result
514
+ : JSON.stringify(r.result)
515
+ : undefined,
516
+ });
517
+ }
518
+ else {
519
+ callback(null, { ...respObject, result: JSON.stringify(r) });
520
+ }
521
+ }
522
+ }
523
+ else {
524
+ if (r.hasOwnProperty('data'))
525
+ r.data = JSON.stringify(r.data);
526
+ callback(null, r);
527
+ }
528
+ generateLog(routerType, routerRequest, requestReceive, call, undefined);
529
+ }
530
+ function wrapRouterGrpcFunction(fun, routerType) {
531
+ return (call, callback) => {
532
+ const requestReceive = Date.now();
533
+ let routerRequest = true;
534
+ ConduitGrpcSdk.Metrics?.increment(`${routerType}_grpc_requests_total`);
535
+ try {
536
+ call.request.context = parseRequestData(call.request.context);
537
+ call.request.params = parseRequestData(call.request.params);
538
+ call.request.cookies = parseRequestData(call.request.cookies);
539
+ call.request.urlParams = parseRequestData(call.request.urlParams);
540
+ call.request.queryParams = parseRequestData(call.request.queryParams);
541
+ call.request.bodyParams = parseRequestData(call.request.bodyParams);
542
+ routerRequest = !call.request.hasOwnProperty('event');
543
+ if (routerRequest) {
544
+ call.request.headers = parseRequestData(call.request.headers);
545
+ }
546
+ }
547
+ catch (e) {
548
+ ConduitGrpcSdk.Metrics?.increment(`${routerType}_grpc_errors_total`);
549
+ generateLog(routerType, routerRequest, requestReceive, call, status.INTERNAL);
550
+ ConduitGrpcSdk.Logger.error(e.message ?? 'Something went wrong');
551
+ return callback({
552
+ code: status.INTERNAL,
553
+ message: e.message ?? 'Something went wrong',
554
+ });
555
+ }
556
+ const responded = { did: false };
557
+ fun(call, r => {
558
+ parseResponseData(r, routerRequest, requestReceive, call, callback, responded, routerType);
559
+ })
560
+ .then(r => parseResponseData(r, routerRequest, requestReceive, call, callback, responded, routerType))
561
+ .catch(error => {
562
+ ConduitGrpcSdk.Metrics?.increment(`${routerType}_grpc_errors_total`);
563
+ generateLog(routerType, routerRequest, requestReceive, call, error.code ?? status.INTERNAL);
564
+ ConduitGrpcSdk.Logger.error(error.message ?? 'Something went wrong');
565
+ callback({
566
+ code: error.code ?? status.INTERNAL,
567
+ message: error.details ?? error.message ?? 'Something went wrong',
568
+ });
569
+ });
570
+ };
571
+ }
572
+
573
+ function wrapFunctionsAsync(functions, routerType) {
574
+ const modifiedFunctions = {};
575
+ Object.keys(functions).forEach(key => {
576
+ modifiedFunctions[key] = wrapRouterGrpcFunction(functions[key], routerType);
577
+ });
578
+ return modifiedFunctions;
579
+ }
580
+
581
+ class ProxyRouteBuilder {
582
+ manager;
583
+ _options = { path: '' };
584
+ _proxy = { target: '' };
585
+ constructor(manager) {
586
+ this.manager = manager;
587
+ }
588
+ method(action) {
589
+ this._options.action = action;
590
+ return this;
591
+ }
592
+ path(path) {
593
+ this._options.path = path;
594
+ return this;
595
+ }
596
+ target(target) {
597
+ this._proxy.target = target;
598
+ return this;
599
+ }
600
+ options(options) {
601
+ this._options = options;
602
+ return this;
603
+ }
604
+ proxy(proxy) {
605
+ this._proxy = proxy;
606
+ return this;
607
+ }
608
+ middleware(middleware, allowDuplicates = false) {
609
+ if (!Array.isArray(middleware)) {
610
+ middleware = [middleware];
611
+ }
612
+ if (this._options.middlewares) {
613
+ if (allowDuplicates) {
614
+ this._options.middlewares = this._options.middlewares.concat(middleware);
615
+ }
616
+ else {
617
+ this._options.middlewares = this._options.middlewares.concat(middleware.filter(mid => this._options.middlewares?.indexOf(mid) === -1));
618
+ }
619
+ }
620
+ else {
621
+ this._options.middlewares = middleware;
622
+ }
623
+ return this;
624
+ }
625
+ add() {
626
+ if (!this.manager)
627
+ throw new Error('Builder not setup with manager');
628
+ if (!this._options.path)
629
+ throw new Error('Cannot build route without path');
630
+ if (!this._proxy.target)
631
+ throw new Error('Cannot build route without target');
632
+ const input = {
633
+ options: this._options,
634
+ proxy: this._proxy,
635
+ };
636
+ this.manager.proxyRoute(input);
637
+ }
638
+ build() {
639
+ if (!this.manager)
640
+ throw new Error('Builder not setup with manager');
641
+ if (!this._options.path)
642
+ throw new Error('Cannot build route without path');
643
+ if (!this._proxy.target)
644
+ throw new Error('Cannot build route without target');
645
+ if (!this._options.action)
646
+ throw new Error('Cannot build route without action');
647
+ return {
648
+ input: {
649
+ options: this._options,
650
+ proxy: this._proxy,
651
+ },
652
+ };
653
+ }
654
+ }
655
+
656
+ class RoutingManager {
657
+ _router;
658
+ _server;
659
+ _moduleRoutes = {};
660
+ _moduleProxyRoutes = {};
661
+ static ClientController;
662
+ static AdminController;
663
+ _routeHandlers = {};
664
+ isAdmin = false;
665
+ constructor(_router, _server) {
666
+ this._router = _router;
667
+ this._server = _server;
668
+ if (_router instanceof Admin) {
669
+ this.isAdmin = true;
670
+ }
671
+ }
672
+ get(path) {
673
+ return new RouteBuilder(this).method(ConduitRouteActions.GET).path(path);
674
+ }
675
+ post(path) {
676
+ return new RouteBuilder(this).method(ConduitRouteActions.POST).path(path);
677
+ }
678
+ delete(path) {
679
+ return new RouteBuilder(this).method(ConduitRouteActions.DELETE).path(path);
680
+ }
681
+ update(path) {
682
+ return new RouteBuilder(this).method(ConduitRouteActions.UPDATE).path(path);
683
+ }
684
+ patch(path) {
685
+ return new RouteBuilder(this).method(ConduitRouteActions.PATCH).path(path);
686
+ }
687
+ proxy() {
688
+ return new ProxyRouteBuilder(this);
689
+ }
690
+ clear() {
691
+ this._moduleRoutes = {};
692
+ this._routeHandlers = {};
693
+ this._moduleProxyRoutes = {};
694
+ }
695
+ middleware(input, handler) {
696
+ const routeObject = this.parseRouteObject({
697
+ options: input,
698
+ grpcFunction: input.name,
699
+ });
700
+ this._moduleRoutes[routeObject.grpcFunction] = routeObject;
701
+ this._routeHandlers[routeObject.grpcFunction] = handler;
702
+ }
703
+ route(input, type, handler) {
704
+ const routeObject = this.parseRouteObject({
705
+ options: input,
706
+ returns: {
707
+ name: type.name,
708
+ fields: JSON.stringify(type.fields),
709
+ },
710
+ grpcFunction: this.generateGrpcName(input),
711
+ });
712
+ this._moduleRoutes[routeObject.grpcFunction] = routeObject;
713
+ this._routeHandlers[routeObject.grpcFunction] = handler;
714
+ }
715
+ proxyRoute(input) {
716
+ const routeObject = this.parseRouteObject({
717
+ options: input,
718
+ });
719
+ this._moduleProxyRoutes[routeObject.options.path + routeObject.proxy.target] =
720
+ routeObject;
721
+ }
722
+ socket(input, events) {
723
+ const eventsObj = {};
724
+ const routeObject = this.parseRouteObject({
725
+ options: input,
726
+ events: '',
727
+ });
728
+ let primary;
729
+ Object.keys(events).forEach((eventName) => {
730
+ if (!primary)
731
+ primary = eventName;
732
+ const event = events[eventName];
733
+ eventsObj[eventName] = {
734
+ grpcFunction: eventName,
735
+ params: JSON.stringify(event.params),
736
+ returns: {
737
+ name: event.returnType?.name ?? '',
738
+ fields: JSON.stringify(event.returnType?.fields),
739
+ },
740
+ };
741
+ this._routeHandlers[eventName] = event.handler;
742
+ });
743
+ routeObject.events = JSON.stringify(eventsObj);
744
+ this._moduleRoutes[primary] = routeObject;
745
+ }
746
+ async registerRoutes() {
747
+ if (Object.keys(this._routeHandlers).length === 0)
748
+ return;
749
+ const modifiedFunctions = wrapFunctionsAsync(this._routeHandlers, this.isAdmin ? 'admin' : 'client');
750
+ if (this.isAdmin) {
751
+ RoutingManager.AdminController.setRoutes(modifiedFunctions);
752
+ }
753
+ else {
754
+ RoutingManager.ClientController.setRoutes(modifiedFunctions);
755
+ }
756
+ const paths = Object.values(this._moduleRoutes);
757
+ return this._router.register(paths);
758
+ }
759
+ generateGrpcName(options) {
760
+ if (options.name) {
761
+ return options.name.charAt(0).toUpperCase() + options.name.slice(1);
762
+ }
763
+ else {
764
+ const name = options.action.charAt(0) +
765
+ options.action.slice(1).toLowerCase() +
766
+ this.extractNameFromPath(options.path);
767
+ return name.charAt(0).toUpperCase() + name.slice(1);
768
+ }
769
+ }
770
+ extractNameFromPath(path) {
771
+ path = path.replace(/[-:]/g, '/');
772
+ return path
773
+ .split('/')
774
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
775
+ .join('');
776
+ }
777
+ parseRouteObject(routeObject) {
778
+ if (!routeObject.options.middlewares) {
779
+ routeObject.options.middlewares = [];
780
+ }
781
+ for (const option in routeObject.options) {
782
+ if (!routeObject.options.hasOwnProperty(option))
783
+ continue;
784
+ if (option === 'middlewares')
785
+ continue;
786
+ if (typeof routeObject.options[option] === 'string' ||
787
+ routeObject.options[option] instanceof String)
788
+ continue;
789
+ routeObject.options[option] = JSON.stringify(routeObject.options[option]);
790
+ }
791
+ return routeObject;
792
+ }
793
+ }
794
+
795
+ const __filename$1 = fileURLToPath(import.meta.url);
796
+ const __dirname$1 = path.dirname(__filename$1);
797
+ class ConduitServiceModule {
798
+ _moduleName;
799
+ _serviceName;
800
+ _address; // external address:port of service (LoadBalancer)
801
+ grpcServer;
802
+ events = new EventEmitter();
803
+ _serviceHealthState = HealthCheckStatus.SERVING; // default for health-agnostic modules
804
+ constructor(moduleName) {
805
+ this._moduleName = camelCase(moduleName);
806
+ }
807
+ _registered = false;
808
+ set registered(registered) {
809
+ this._registered = registered;
810
+ }
811
+ _port; // port to bring up gRPC service
812
+ get port() {
813
+ return this._port;
814
+ }
815
+ _grpcSdk;
816
+ get grpcSdk() {
817
+ if (!this._grpcSdk)
818
+ throw new Error('grpcSdk not defined yet');
819
+ return this._grpcSdk;
820
+ }
821
+ set grpcSdk(grpcSdk) {
822
+ if (this._grpcSdk)
823
+ throw new Error('grpcSdk already defined');
824
+ this._grpcSdk = grpcSdk;
825
+ }
826
+ get healthState() {
827
+ return this._serviceHealthState;
828
+ }
829
+ updateHealth(state, init = false) {
830
+ if ((state === HealthCheckStatus.UNKNOWN && !init) ||
831
+ state === HealthCheckStatus.SERVICE_UNKNOWN) {
832
+ throw new Error(`Cannot explicitly set gRPC health state to ${HealthCheckStatus[state]}`);
833
+ }
834
+ if (!init) {
835
+ ConduitGrpcSdk.Metrics?.set('module_health_state', state === HealthCheckStatus.SERVING ? 1 : 0);
836
+ }
837
+ if (init) {
838
+ this._serviceHealthState = state;
839
+ return;
840
+ }
841
+ if (this._serviceHealthState !== state) {
842
+ this._serviceHealthState = state;
843
+ // do not emit health updates until registered
844
+ if (this._registered) {
845
+ this.events.emit(`grpc-health-change:${this._serviceName}`, state);
846
+ this._grpcSdk?.config.moduleHealthProbe(this._moduleName, this._address);
847
+ }
848
+ }
849
+ }
850
+ healthCheck(call, callback) {
851
+ const service = call.request.service.substring(call.request.service.indexOf('.') + 1);
852
+ if (service && service !== this._serviceName) {
853
+ callback(null, {
854
+ status: HealthCheckStatus.SERVICE_UNKNOWN,
855
+ });
856
+ }
857
+ else {
858
+ if (!this._registered) {
859
+ return callback(null, {
860
+ status: HealthCheckResponse_ServingStatus.UNKNOWN,
861
+ });
862
+ }
863
+ callback(null, {
864
+ status: this._serviceHealthState,
865
+ });
866
+ }
867
+ }
868
+ healthWatch(call) {
869
+ const service = call.request.service.substring(call.request.service.indexOf('.') + 1);
870
+ if (service && service !== this._serviceName) {
871
+ call.write({
872
+ status: HealthCheckStatus.SERVICE_UNKNOWN,
873
+ });
874
+ }
875
+ else {
876
+ this.events.on(`grpc-health-change:${this._serviceName}`, (status) => {
877
+ call.write({
878
+ status: status,
879
+ });
880
+ });
881
+ }
882
+ }
883
+ async addHealthCheckService() {
884
+ await this.grpcServer.addService(path.resolve(__dirname$1, './grpc_health_check.proto'), 'grpc.health.v1.Health', {
885
+ Check: this.healthCheck.bind(this),
886
+ Watch: this.healthWatch.bind(this),
887
+ });
888
+ }
889
+ async addModuleService() {
890
+ await this.grpcServer.addService(path.resolve(__dirname$1, './module.proto'), 'conduit.module.v1.ConduitModule', {
891
+ SetConfig: this.setConfig.bind(this),
892
+ });
893
+ await this.grpcServer.addService(path.resolve(__dirname$1, './module.proto'), 'conduit.module.v1.ClientRouter', {
894
+ Route: RoutingManager.ClientController.handleRequest.bind(RoutingManager.ClientController),
895
+ SocketRoute: RoutingManager.ClientController.handleRequest.bind(RoutingManager.ClientController),
896
+ });
897
+ await this.grpcServer.addService(path.resolve(__dirname$1, './module.proto'), 'conduit.module.v1.AdminRouter', {
898
+ Route: RoutingManager.AdminController.handleRequest.bind(RoutingManager.AdminController),
899
+ SocketRoute: RoutingManager.AdminController.handleRequest.bind(RoutingManager.AdminController),
900
+ });
901
+ }
902
+ }
903
+
904
+ class ConfigController {
905
+ static instance;
906
+ _config;
907
+ get config() {
908
+ // return copy of config and not the original object;
909
+ return Object.assign({}, this._config);
910
+ }
911
+ set config(config) {
912
+ this._config = config;
913
+ }
914
+ static getInstance() {
915
+ if (!ConfigController.instance) {
916
+ ConfigController.instance = new ConfigController();
917
+ }
918
+ return ConfigController.instance;
919
+ }
920
+ }
921
+
922
+ var ModuleLifecycleStage;
923
+ (function (ModuleLifecycleStage) {
924
+ ModuleLifecycleStage["CREATE_GRPC"] = "creat-grpc-server";
925
+ ModuleLifecycleStage["PRE_SERVER_START"] = "pre-grpc-start";
926
+ ModuleLifecycleStage["SERVER_STARTED"] = "grpc-server-started";
927
+ ModuleLifecycleStage["PRE_REGISTER"] = "pre-register";
928
+ ModuleLifecycleStage["POST_REGISTER"] = "post-register";
929
+ })(ModuleLifecycleStage || (ModuleLifecycleStage = {}));
930
+
931
+ const processMeta = (meta) => {
932
+ if (Array.isArray(meta)) {
933
+ return meta.reduce((message, meta) => `${message} \n ${JSON.stringify(meta, null, 2)}`, '');
934
+ }
935
+ else {
936
+ return JSON.stringify(meta, null, 2);
937
+ }
938
+ };
939
+ const createFormat = (logMeta = true) => {
940
+ return format.combine(format.printf(info => {
941
+ // This will customize the Error Message
942
+ if (info.stack) {
943
+ return `[${info.timestamp}] [${info.level.toUpperCase()}]: ${info.message} ${info.stack}`;
944
+ }
945
+ if (logMeta) {
946
+ return `[${info.timestamp}] [${info.level.toUpperCase()}]: ${info.message} ${!isEmpty(info.meta) ? processMeta(info.meta) : ''}`;
947
+ }
948
+ else {
949
+ return `[${info.timestamp}] [${info.level.toUpperCase()}]: ${info.message}`;
950
+ }
951
+ }));
952
+ };
953
+ const defaultTransport = new winston.transports.Console({
954
+ level: 'debug',
955
+ format: format.combine(format.prettyPrint(), format.errors({ stack: true }), format.timestamp({
956
+ format: 'YYYY-MM-DD HH:mm:ss',
957
+ }), createFormat(false), format.colorize({
958
+ all: true,
959
+ })),
960
+ });
961
+ class ConduitLogger {
962
+ _winston;
963
+ constructor(transports) {
964
+ this._winston = winston.createLogger({
965
+ format: format.combine(format.prettyPrint(), format.errors({ stack: true }), format.timestamp({
966
+ format: 'YYYY-MM-DD HH:mm:ss',
967
+ }), createFormat()),
968
+ transports: transports ? [...transports, defaultTransport] : [defaultTransport],
969
+ });
970
+ }
971
+ get winston() {
972
+ return this._winston;
973
+ }
974
+ addTransport(transport) {
975
+ this._winston.add(transport);
976
+ }
977
+ log(message, level = 'info', cb) {
978
+ return this._winston.log(level, message, cb);
979
+ }
980
+ logObject(object, message = '', level = 'info', cb) {
981
+ return this._winston.log(level, message, object, cb);
982
+ }
983
+ info(message, cb) {
984
+ return this._winston.info(message, cb);
985
+ }
986
+ warn(message, cb) {
987
+ return this._winston.warn(message, cb);
988
+ }
989
+ error(message, cb) {
990
+ if (typeof message === 'object') {
991
+ return this._winston.error(message);
992
+ }
993
+ return this._winston.error(message, cb);
994
+ }
995
+ http(message, cb) {
996
+ return this._winston.http(message, cb);
997
+ }
998
+ verbose(message, cb) {
999
+ return this._winston.verbose(message, cb);
1000
+ }
1001
+ }
1002
+ async function lokiReadyCheck(lokiUrl) {
1003
+ return new Promise((resolve, reject) => {
1004
+ const data = [];
1005
+ get(`${lokiUrl}/ready`, r => {
1006
+ r.on('data', chunk => data.push(chunk));
1007
+ r.on('end', () => {
1008
+ if (Buffer.concat(data).toString() === 'ready\n')
1009
+ resolve();
1010
+ else
1011
+ reject(false);
1012
+ });
1013
+ }).on('error', err => {
1014
+ reject(err.message);
1015
+ });
1016
+ });
1017
+ }
1018
+ async function setupLoki(module, instance) {
1019
+ let lokiUrl = process.env.LOKI_URL;
1020
+ if (lokiUrl && lokiUrl !== '') {
1021
+ if (lokiUrl.endsWith('/'))
1022
+ lokiUrl = lokiUrl.slice(0, -1);
1023
+ const onTry = async () => {
1024
+ return await lokiReadyCheck(lokiUrl)
1025
+ .then(() => {
1026
+ ConduitGrpcSdk.Logger.addTransport(new LokiTransport({
1027
+ level: 'debug',
1028
+ host: lokiUrl,
1029
+ batching: false,
1030
+ replaceTimestamp: true,
1031
+ labels: {
1032
+ module,
1033
+ instance,
1034
+ ...(process.env.CONDUIT_NAMESPACE && {
1035
+ namespace: process.env.CONDUIT_NAMESPACE,
1036
+ }),
1037
+ },
1038
+ }));
1039
+ return false;
1040
+ })
1041
+ .catch(() => true); // retry
1042
+ };
1043
+ const onFailure = () => {
1044
+ ConduitGrpcSdk.Logger.error(`Failed to connect to Loki on '${lokiUrl}'`);
1045
+ };
1046
+ await linearBackoffTimeoutAsync(onTry, 250, 15, onFailure, true);
1047
+ }
1048
+ }
1049
+
1050
+ class MetricsServer {
1051
+ moduleName;
1052
+ instance;
1053
+ Registry;
1054
+ constructor(moduleName, instance, registry) {
1055
+ this.moduleName = moduleName;
1056
+ this.instance = instance;
1057
+ this.Registry = registry;
1058
+ }
1059
+ initialize() {
1060
+ const server = express();
1061
+ const port = this.getHttpPort();
1062
+ const url = '0.0.0.0:' + port.toString();
1063
+ server.listen(port, () => {
1064
+ ConduitGrpcSdk.Logger.info(`Metrics server listening on: ${url}`);
1065
+ });
1066
+ server.get('/', (req, res) => {
1067
+ return res
1068
+ .status(200)
1069
+ .send(`Conduit Metrics for module: ${this.moduleName}, instance: ${this.instance}`);
1070
+ });
1071
+ server.get('/metrics', async (req, res) => {
1072
+ const metrics = await this.Registry.metrics();
1073
+ return res.status(200).send(metrics);
1074
+ });
1075
+ return server;
1076
+ }
1077
+ getHttpPort() {
1078
+ const value = process.env['METRICS_PORT'];
1079
+ if (!value) {
1080
+ ConduitGrpcSdk.Logger.error(`Metrics HTTP port not set`);
1081
+ process.exit(-1);
1082
+ }
1083
+ const port = parseInt(value, 10);
1084
+ if (isNaN(port) || port < 1 || port > 65535) {
1085
+ ConduitGrpcSdk.Logger.error(`Invalid HTTP port value: ${port}`);
1086
+ process.exit(-1);
1087
+ }
1088
+ return port;
1089
+ }
1090
+ }
1091
+
1092
+ var defaultMetrics = {
1093
+ internalGrpcRequests: {
1094
+ type: MetricType.Counter,
1095
+ config: {
1096
+ name: 'internal_grpc_requests_total',
1097
+ help: "Tracks the total number of Conduit's internal gRPC requests",
1098
+ },
1099
+ },
1100
+ clientGrpcRequests: {
1101
+ type: MetricType.Counter,
1102
+ config: {
1103
+ name: 'client_grpc_requests_total',
1104
+ help: 'Tracks the total number of the client gRPC requests',
1105
+ },
1106
+ },
1107
+ adminGrpcRequests: {
1108
+ type: MetricType.Counter,
1109
+ config: {
1110
+ name: 'admin_grpc_requests_total',
1111
+ help: 'Tracks the total number of the admin gRPC requests',
1112
+ },
1113
+ },
1114
+ clientGrpcErrors: {
1115
+ type: MetricType.Counter,
1116
+ config: {
1117
+ name: 'client_grpc_errors_total',
1118
+ help: 'Tracks the total number of client grpc errors',
1119
+ },
1120
+ },
1121
+ adminGrpcErrors: {
1122
+ type: MetricType.Counter,
1123
+ config: {
1124
+ name: 'admin_grpc_errors_total',
1125
+ help: 'Tracks the total number of admin grpc errors',
1126
+ },
1127
+ },
1128
+ moduleHealthState: {
1129
+ type: MetricType.Gauge,
1130
+ config: {
1131
+ name: 'module_health_state',
1132
+ help: 'Tracks the health state of the module',
1133
+ },
1134
+ },
1135
+ grpcRequestLatency: {
1136
+ type: MetricType.Gauge,
1137
+ config: {
1138
+ name: 'grpc_request_latency_seconds',
1139
+ help: 'Tracks the latency of grpc requests in seconds',
1140
+ },
1141
+ },
1142
+ adminGrpcRequestLatency: {
1143
+ type: MetricType.Gauge,
1144
+ config: {
1145
+ name: 'admin_grpc_request_latency_seconds',
1146
+ help: 'Tracks the latency of admin grpc requests in seconds',
1147
+ },
1148
+ },
1149
+ clientGrpcRequestLatency: {
1150
+ type: MetricType.Gauge,
1151
+ config: {
1152
+ name: 'client_grpc_request_latency_seconds',
1153
+ help: 'Tracks the latency of client grpc requests in seconds',
1154
+ },
1155
+ },
1156
+ grpcResponseStatuses: {
1157
+ type: MetricType.Counter,
1158
+ config: {
1159
+ name: 'grpc_response_statuses_total',
1160
+ help: 'Tracks the total number of grpc response codes',
1161
+ labelNames: ['success'],
1162
+ },
1163
+ },
1164
+ adminGrpcResponseStatuses: {
1165
+ type: MetricType.Counter,
1166
+ config: {
1167
+ name: 'admin_grpc_response_statuses_total',
1168
+ help: 'Tracks the total number of admin grpc response codes',
1169
+ labelNames: ['success'],
1170
+ },
1171
+ },
1172
+ clientGrpcResponseStatuses: {
1173
+ type: MetricType.Counter,
1174
+ config: {
1175
+ name: 'client_grpc_response_statuses_total',
1176
+ help: 'Tracks the total number of client grpc response codes',
1177
+ labelNames: ['success'],
1178
+ },
1179
+ },
1180
+ };
1181
+
1182
+ class ConduitMetrics {
1183
+ moduleName;
1184
+ instance;
1185
+ _registry;
1186
+ _httpServer;
1187
+ constructor(moduleName, instance) {
1188
+ this.moduleName = moduleName;
1189
+ this.instance = instance;
1190
+ this._registry = new Registry();
1191
+ this._registry.setDefaultLabels({
1192
+ module_name: this.moduleName,
1193
+ module_instance: this.instance,
1194
+ });
1195
+ this.collectDefaultMetrics();
1196
+ this._httpServer = new MetricsServer(moduleName, instance, this._registry);
1197
+ this._httpServer.initialize();
1198
+ }
1199
+ initializeDefaultMetrics() {
1200
+ for (const metric of Object.values(defaultMetrics)) {
1201
+ this.registerMetric(metric.type, metric.config);
1202
+ }
1203
+ }
1204
+ registerMetric(type, config) {
1205
+ if (this.getMetric(config.name))
1206
+ return;
1207
+ const metricConfig = JSON.parse(JSON.stringify(config));
1208
+ metricConfig.name = this.addPrefix(config.name);
1209
+ switch (type) {
1210
+ case MetricType.Counter:
1211
+ this.createCounter(metricConfig);
1212
+ break;
1213
+ case MetricType.Gauge:
1214
+ this.createGauge(metricConfig);
1215
+ break;
1216
+ case MetricType.Histogram:
1217
+ this.createHistogram(metricConfig);
1218
+ break;
1219
+ case MetricType.Summary:
1220
+ this.createSummary(metricConfig);
1221
+ break;
1222
+ }
1223
+ }
1224
+ collectDefaultMetrics() {
1225
+ collectDefaultMetrics({
1226
+ register: this._registry,
1227
+ prefix: 'conduit_',
1228
+ // probably not needed
1229
+ labels: {
1230
+ module_name: this.moduleName,
1231
+ instance_instance: this.instance,
1232
+ },
1233
+ });
1234
+ }
1235
+ createCounter(config) {
1236
+ return new Counter({ ...config, registers: [this._registry] });
1237
+ }
1238
+ createSummary(config) {
1239
+ return new Summary({ ...config, registers: [this._registry] });
1240
+ }
1241
+ createHistogram(config) {
1242
+ return new Histogram({ ...config, registers: [this._registry] });
1243
+ }
1244
+ createGauge(config) {
1245
+ return new Gauge({ ...config, registers: [this._registry] });
1246
+ }
1247
+ getMetric(name) {
1248
+ return this._registry.getSingleMetric(this.addPrefix(name));
1249
+ }
1250
+ increment(metric, increment = 1, labels) {
1251
+ const metricInstance = this._registry.getSingleMetric(this.addPrefix(metric));
1252
+ if (!(metricInstance instanceof Counter) && !(metricInstance instanceof Gauge)) {
1253
+ throw new Error(`Metric ${metric} is not an incrementable metric`);
1254
+ }
1255
+ return labels
1256
+ ? metricInstance.labels({ ...labels }).inc(increment)
1257
+ : metricInstance.inc(increment);
1258
+ }
1259
+ decrement(metric, decrement = 1, labels) {
1260
+ const metricInstance = this._registry.getSingleMetric(this.addPrefix(metric));
1261
+ if (!(metricInstance instanceof Gauge)) {
1262
+ throw new Error(`Metric ${metric} is not a decrementable metric`);
1263
+ }
1264
+ return labels
1265
+ ? metricInstance.labels({ ...labels }).dec(decrement)
1266
+ : metricInstance.dec(decrement);
1267
+ }
1268
+ set(metric, value, labels) {
1269
+ const metricInstance = this._registry.getSingleMetric(this.addPrefix(metric));
1270
+ if (!(metricInstance instanceof Gauge)) {
1271
+ throw new Error(`Metric ${metric} is not a Gauge`);
1272
+ }
1273
+ return labels
1274
+ ? metricInstance.labels({ ...labels }).set(value)
1275
+ : metricInstance.set(value);
1276
+ }
1277
+ observe(metric, value, labels) {
1278
+ const metricInstance = this._registry.getSingleMetric(this.addPrefix(metric));
1279
+ if (!(metricInstance instanceof Histogram) && !(metricInstance instanceof Summary)) {
1280
+ throw new Error(`Metric ${metric} is not a Histogram`);
1281
+ }
1282
+ return labels
1283
+ ? metricInstance.labels({ ...labels }).observe(value)
1284
+ : metricInstance.observe(value);
1285
+ }
1286
+ addPrefix(metric) {
1287
+ return `conduit_${metric}`;
1288
+ }
1289
+ }
1290
+
1291
+ // Forked from nice-grpc-prometheus
1292
+ const registry = new Registry();
1293
+ const typeLabel = 'grpc_type';
1294
+ const serviceLabel = 'grpc_service';
1295
+ const methodLabel = 'grpc_method';
1296
+ const pathLabel = 'grpc_path';
1297
+ const codeLabel = 'grpc_code';
1298
+ /**
1299
+ * 1ms, 4ms, 16ms, ..., ~1 hour in seconds
1300
+ */
1301
+ const latencySecondsBuckets = exponentialBuckets(0.001, 4, 12);
1302
+ function getLabels(method) {
1303
+ const callType = method.requestStream
1304
+ ? method.responseStream
1305
+ ? 'bidi_stream'
1306
+ : 'client_stream'
1307
+ : method.responseStream
1308
+ ? 'server_stream'
1309
+ : 'unary';
1310
+ const { path } = method;
1311
+ const [serviceName, methodName] = path.split('/').slice(1);
1312
+ return {
1313
+ [typeLabel]: callType,
1314
+ [serviceLabel]: serviceName,
1315
+ [methodLabel]: methodName,
1316
+ [pathLabel]: path,
1317
+ };
1318
+ }
1319
+ async function* incrementStreamMessagesCounter(iterable, counter) {
1320
+ for await (const item of iterable) {
1321
+ counter.inc();
1322
+ yield item;
1323
+ }
1324
+ }
1325
+ const clientStartedMetric = new Counter({
1326
+ registers: [registry],
1327
+ name: 'conduit_grpc_client_started_total',
1328
+ help: 'Total number of RPCs started on the client.',
1329
+ labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel],
1330
+ });
1331
+ const clientHandledMetric = new Counter({
1332
+ registers: [registry],
1333
+ name: 'conduit_grpc_client_handled_total',
1334
+ help: 'Total number of RPCs completed on the client, regardless of success or failure.',
1335
+ labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel, codeLabel],
1336
+ });
1337
+ const clientStreamMsgReceivedMetric = new Counter({
1338
+ registers: [registry],
1339
+ name: 'conduit_grpc_client_msg_received_total',
1340
+ help: 'Total number of RPC stream messages received by the client.',
1341
+ labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel],
1342
+ });
1343
+ const clientStreamMsgSentMetric = new Counter({
1344
+ registers: [registry],
1345
+ name: 'conduit_grpc_client_msg_sent_total',
1346
+ help: 'Total number of gRPC stream messages sent by the client.',
1347
+ labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel],
1348
+ });
1349
+ const clientHandlingSecondsMetric = new Histogram({
1350
+ registers: [registry],
1351
+ name: 'grpc_client_handling_seconds',
1352
+ help: 'Histogram of response latency (seconds) of the gRPC until it is finished by the application.',
1353
+ labelNames: [typeLabel, serviceLabel, methodLabel, pathLabel, codeLabel],
1354
+ buckets: latencySecondsBuckets,
1355
+ });
1356
+ function clientMiddleware() {
1357
+ return async function* prometheusClientMiddlewareGenerator(call, options) {
1358
+ const labels = getLabels(call.method);
1359
+ clientStartedMetric.inc(labels);
1360
+ const stopTimer = clientHandlingSecondsMetric.startTimer(labels);
1361
+ let settled = false;
1362
+ let status = Status.OK;
1363
+ try {
1364
+ let request;
1365
+ if (!call.requestStream) {
1366
+ request = call.request;
1367
+ }
1368
+ else {
1369
+ request = incrementStreamMessagesCounter(call.request, clientStreamMsgSentMetric.labels(labels));
1370
+ }
1371
+ if (!call.responseStream) {
1372
+ const response = yield* call.next(request, options);
1373
+ settled = true;
1374
+ return response;
1375
+ }
1376
+ else {
1377
+ yield* incrementStreamMessagesCounter(call.next(request, options), clientStreamMsgReceivedMetric.labels(labels));
1378
+ settled = true;
1379
+ return;
1380
+ }
1381
+ }
1382
+ catch (err) {
1383
+ settled = true;
1384
+ if (err instanceof ClientError) {
1385
+ status = err.code;
1386
+ }
1387
+ else if (isAbortError(err)) {
1388
+ status = Status.ABORTED;
1389
+ }
1390
+ else {
1391
+ status = Status.UNKNOWN;
1392
+ }
1393
+ throw err;
1394
+ }
1395
+ finally {
1396
+ if (!settled) {
1397
+ status = Status.CANCELLED;
1398
+ }
1399
+ stopTimer({ [codeLabel]: Status[status] });
1400
+ clientHandledMetric.inc({
1401
+ ...labels,
1402
+ [codeLabel]: Status[status],
1403
+ });
1404
+ }
1405
+ };
1406
+ }
1407
+
1408
+ const __filename = fileURLToPath(import.meta.url);
1409
+ const __dirname = path.dirname(__filename);
1410
+ const initializeSdk = (coreUrl, moduleName, watchModules = true, serviceHealthStatusGetter = () => HealthCheckStatus.SERVING) => {
1411
+ let logger;
1412
+ try {
1413
+ logger = new ConduitLogger([
1414
+ new winston.transports.File({
1415
+ filename: path.join(__dirname, '.logs/combined.log'),
1416
+ level: 'info',
1417
+ }),
1418
+ new winston.transports.File({
1419
+ filename: path.join(__dirname, '.logs/errors.log'),
1420
+ level: 'error',
1421
+ }),
1422
+ ]);
1423
+ }
1424
+ catch (e) {
1425
+ logger = new ConduitLogger();
1426
+ }
1427
+ const grpcSdk = new ConduitGrpcSdk(coreUrl, moduleName, watchModules, logger, serviceHealthStatusGetter);
1428
+ setupLoki(grpcSdk.name, grpcSdk.instance).then();
1429
+ if (process.env.METRICS_PORT) {
1430
+ ConduitGrpcSdk.Metrics = new ConduitMetrics(grpcSdk.name, grpcSdk.instance);
1431
+ grpcSdk.addMiddleware(clientMiddleware());
1432
+ }
1433
+ return grpcSdk;
1434
+ };
1435
+
1436
+ /**
1437
+ * Registers a timeout with linear backoff.
1438
+ * onFailure() only runs on rep exhaustion.
1439
+ * Timeout can be cleared through returned clear() or inside onTry().
1440
+ */
1441
+ function linearBackoffTimeout(onTry, delay, reps, onFailure) {
1442
+ const nextRep = () => reps === undefined || --reps > 0;
1443
+ let stopSignal = false;
1444
+ const stop = () => {
1445
+ stopSignal = true;
1446
+ };
1447
+ let timeout;
1448
+ const invoker = async () => {
1449
+ if (stopSignal) {
1450
+ return timeout ? clearTimeout$1(timeout) : null;
1451
+ }
1452
+ delay = Math.floor(delay * 2);
1453
+ if (delay > 0 && nextRep()) {
1454
+ timeout = setTimeout(invoker, delay);
1455
+ onTry(stop);
1456
+ }
1457
+ else {
1458
+ timeout = null;
1459
+ onFailure && onFailure();
1460
+ }
1461
+ };
1462
+ timeout = setTimeout(invoker, delay);
1463
+ return {
1464
+ clear: () => {
1465
+ if (timeout) {
1466
+ clearTimeout$1(timeout);
1467
+ timeout = null;
1468
+ }
1469
+ },
1470
+ };
1471
+ }
1472
+ /**
1473
+ * Registers a timeout with linear backoff.
1474
+ * onFailure() only runs on rep exhaustion.
1475
+ * @param {() => Promise<boolean>} onTry - Async handler, returns 'continue' boolean flag
1476
+ */
1477
+ async function linearBackoffTimeoutAsync(onTry, delay, reps, onFailure, startNow = false) {
1478
+ const nextRep = () => reps === undefined || --reps > 0;
1479
+ const invoker = async () => {
1480
+ delay = Math.floor(delay * 2);
1481
+ if (delay > 0 && nextRep()) {
1482
+ if (await onTry())
1483
+ setTimeout(invoker, delay);
1484
+ }
1485
+ else {
1486
+ onFailure && (await onFailure());
1487
+ }
1488
+ };
1489
+ setTimeout(invoker, startNow ? 0 : delay);
1490
+ }
1491
+
1492
+ function merge(objA, objB) {
1493
+ const customizer = (objValue, srcValue) => {
1494
+ if (Array.isArray(objValue)) {
1495
+ // don't merge arrays
1496
+ return srcValue;
1497
+ }
1498
+ };
1499
+ return mergeWith({}, objA, objB, customizer);
1500
+ }
1501
+
1502
+ const convictConfigParser = (config) => {
1503
+ if (typeof config === 'object') {
1504
+ Object.keys(config).forEach(key => {
1505
+ if (key === '_cvtProperties') {
1506
+ config = convictConfigParser(config._cvtProperties);
1507
+ }
1508
+ else {
1509
+ config[key] = convictConfigParser(config[key]);
1510
+ }
1511
+ });
1512
+ }
1513
+ return config;
1514
+ };
1515
+
1516
+ class RoutingController {
1517
+ _routeHandlers = {};
1518
+ handleRequest(call, callback) {
1519
+ if (this._routeHandlers[call.request.functionName]) {
1520
+ this._routeHandlers[call.request.functionName](call, callback);
1521
+ }
1522
+ else {
1523
+ callback({
1524
+ code: status.NOT_FOUND,
1525
+ message: 'Not found',
1526
+ });
1527
+ }
1528
+ }
1529
+ setRoutes(routes) {
1530
+ this._routeHandlers = routes;
1531
+ }
1532
+ }
1533
+
1534
+ class ManagedModule extends ConduitServiceModule {
1535
+ config;
1536
+ configOverride = false;
1537
+ service;
1538
+ serviceAddress;
1539
+ servicePort;
1540
+ _moduleState;
1541
+ constructor(moduleName) {
1542
+ super(moduleName);
1543
+ this._moduleState = ModuleLifecycleStage.CREATE_GRPC;
1544
+ if (!process.env.CONDUIT_SERVER) {
1545
+ throw new Error('CONDUIT_SERVER is undefined, specify Conduit server URL');
1546
+ }
1547
+ this.servicePort = process.env.GRPC_PORT ?? '5000';
1548
+ this.serviceAddress =
1549
+ // @compat (v0.15): SERVICE_IP -> SERVICE_URL
1550
+ process.env.SERVICE_URL || process.env.SERVICE_IP || '0.0.0.0:' + this.servicePort;
1551
+ try {
1552
+ this.grpcSdk = initializeSdk(process.env.CONDUIT_SERVER, this.name, true, () => {
1553
+ return this.healthState;
1554
+ });
1555
+ }
1556
+ catch (e) {
1557
+ throw new Error('Failed to initialize grpcSdk');
1558
+ }
1559
+ }
1560
+ get name() {
1561
+ return this._moduleName;
1562
+ }
1563
+ get address() {
1564
+ return this._address;
1565
+ }
1566
+ async start() {
1567
+ await this.grpcSdk.initialize();
1568
+ this.initialize(this.serviceAddress, this.servicePort);
1569
+ try {
1570
+ await this.preRegisterLifecycle();
1571
+ await this.grpcSdk.config.registerModule(this.name, this.address, this.healthState);
1572
+ }
1573
+ catch (err) {
1574
+ ConduitGrpcSdk.Logger.error('Failed to initialize server');
1575
+ ConduitGrpcSdk.Logger.error(err);
1576
+ process.exit(-1);
1577
+ }
1578
+ await this.postRegisterLifecycle().catch((err) => {
1579
+ ConduitGrpcSdk.Logger.error('Failed to activate module');
1580
+ ConduitGrpcSdk.Logger.error(err);
1581
+ process.exit(-1);
1582
+ });
1583
+ }
1584
+ /**
1585
+ * This is the first function triggered on module spin up.
1586
+ * @param address {string} external address:port of service (LoadBalancer)
1587
+ * @param port {string} port to bring up gRPC service
1588
+ */
1589
+ initialize(address, port) {
1590
+ this._address = address;
1591
+ this._port = port;
1592
+ if (this.configSchema) {
1593
+ this.config = convict(this.configSchema);
1594
+ }
1595
+ }
1596
+ /**
1597
+ * This is triggered before spinning up the Grpc Server
1598
+ * of the module. On this stage, the redis bus and state have
1599
+ * not yet been initialized, and only communications with Conduit core
1600
+ * have been established.
1601
+ */
1602
+ async preServerStart() { }
1603
+ /**
1604
+ * This is triggered right after Grpc Server start.
1605
+ * At this stage the core module will communicate with your service
1606
+ * for health checks, and the Bus and State management mechanics
1607
+ * are online and available.
1608
+ */
1609
+ async onServerStart() { }
1610
+ /**
1611
+ * Triggers right before registering the module with Conduit Core.
1612
+ */
1613
+ async preRegister() { }
1614
+ /**
1615
+ * Triggers when module has been registered with Conduit Core
1616
+ * This is the step where you should initialize your
1617
+ * module's general routing (admin or client) and any other operation,
1618
+ * that would require other modules being able to reach you
1619
+ */
1620
+ async onRegister() { }
1621
+ /**
1622
+ * This is triggered when the module receives new configuration.
1623
+ * At this step the configuration has not been checked yet,
1624
+ * and has only been parsed.
1625
+ * @param config
1626
+ */
1627
+ async preConfig(config) {
1628
+ return config;
1629
+ }
1630
+ /**
1631
+ * This is triggered when new configuration has been applied.
1632
+ * This happens either when changes are being made on the configuration,
1633
+ * or on module start up, where the config is recovered from the database.
1634
+ * In either case the configuration is now available through the
1635
+ * Config Controller of the sdk.
1636
+ */
1637
+ async onConfig() { }
1638
+ /**
1639
+ * Registers common and module-specific metric types.
1640
+ */
1641
+ async registerMetrics() {
1642
+ if (ConduitGrpcSdk.Metrics) {
1643
+ ConduitGrpcSdk.Metrics.initializeDefaultMetrics();
1644
+ if (this.metricsSchema) {
1645
+ Object.values(this.metricsSchema).forEach(metric => {
1646
+ ConduitGrpcSdk.Metrics.registerMetric(metric.type, metric.config);
1647
+ });
1648
+ }
1649
+ }
1650
+ }
1651
+ /**
1652
+ * Initializes metric startup values.
1653
+ * Implemented by individual modules.
1654
+ */
1655
+ async initializeMetrics() { }
1656
+ async createGrpcServer() {
1657
+ this.grpcServer = new GrpcServer(this._port);
1658
+ this._port = (await this.grpcServer.createNewServer()).toString();
1659
+ }
1660
+ async startGrpcServer() {
1661
+ if (this.service) {
1662
+ this._serviceName = this.service.protoDescription.substring(this.service.protoDescription.indexOf('.') + 1);
1663
+ await this.grpcServer.addService(this.service.protoPath, this.service.protoDescription, this.service.functions);
1664
+ }
1665
+ RoutingManager.ClientController = new RoutingController();
1666
+ RoutingManager.AdminController = new RoutingController();
1667
+ await this.addHealthCheckService();
1668
+ await this.addModuleService();
1669
+ await this.grpcServer.start();
1670
+ ConduitGrpcSdk.Logger.log('gRPC server listening on ' + this._port);
1671
+ }
1672
+ async setConfig(call, callback) {
1673
+ try {
1674
+ if (!this.config) {
1675
+ return callback({
1676
+ code: status.INVALID_ARGUMENT,
1677
+ message: 'Module is not configurable',
1678
+ });
1679
+ }
1680
+ let config = JSON.parse(call.request.newConfig);
1681
+ config = merge(this.config.getProperties(), config);
1682
+ config = await this.preConfig(config);
1683
+ const previousConfig = this.config.getProperties();
1684
+ try {
1685
+ this.config.load(config).validate({
1686
+ allowed: 'warn',
1687
+ });
1688
+ config = this.config.getProperties();
1689
+ callback(null, { updatedConfig: JSON.stringify(config) });
1690
+ }
1691
+ catch (e) {
1692
+ this.config = convict(this.configSchema);
1693
+ this.config.load(previousConfig);
1694
+ return callback({
1695
+ code: status.INVALID_ARGUMENT,
1696
+ message: 'Invalid configuration values: ' + e.message,
1697
+ });
1698
+ }
1699
+ ConfigController.getInstance().config = config;
1700
+ await this.onConfig();
1701
+ this.grpcSdk.bus?.publish(kebabCase(this.name) + ':config:update', JSON.stringify(config));
1702
+ }
1703
+ catch (e) {
1704
+ return callback({ code: status.INTERNAL, message: e.message });
1705
+ }
1706
+ }
1707
+ /** Used to update the module's configuration on initial Redis/DB reconciliation. */
1708
+ async handleConfigSyncUpdate() {
1709
+ if (!this.config)
1710
+ return;
1711
+ this.grpcSdk.bus.subscribe(`${kebabCase(this.name)}:config:update`, async (message) => {
1712
+ ConfigController.getInstance().config = await this.preConfig(JSON.parse(message));
1713
+ await this.onConfig();
1714
+ });
1715
+ }
1716
+ async preRegisterLifecycle() {
1717
+ await this.createGrpcServer();
1718
+ this._moduleState = ModuleLifecycleStage.PRE_SERVER_START;
1719
+ await this.preServerStart();
1720
+ await this.grpcSdk.initializeEventBus();
1721
+ await this.handleConfigSyncUpdate();
1722
+ await this.registerMetrics();
1723
+ await this.startGrpcServer();
1724
+ this._moduleState = ModuleLifecycleStage.SERVER_STARTED;
1725
+ await this.onServerStart();
1726
+ await this.initializeMetrics();
1727
+ this._moduleState = ModuleLifecycleStage.PRE_REGISTER;
1728
+ await this.preRegister();
1729
+ }
1730
+ async postRegisterLifecycle() {
1731
+ this._moduleState = ModuleLifecycleStage.POST_REGISTER;
1732
+ this.registered = true;
1733
+ await this.onRegister();
1734
+ if (this.config) {
1735
+ const configSchema = this.config.getSchema();
1736
+ let config = this.config.getProperties();
1737
+ config = await this.preConfig(config);
1738
+ config = await this.grpcSdk.config.configure(config, convictConfigParser(configSchema), this.configOverride);
1739
+ ConfigController.getInstance();
1740
+ if (config)
1741
+ ConfigController.getInstance().config = config;
1742
+ if (!config || config.active || !config.hasOwnProperty('active'))
1743
+ await this.onConfig();
1744
+ }
1745
+ }
1746
+ }
1747
+
1748
+ export { ConduitActiveSchema, ConduitBoolean, ConduitDate, ConduitJson, ConduitNumber, ConduitObjectId, ConduitRelation, ConduitServiceModule, ConduitString, ConfigController, GrpcServer, ManagedModule, ModuleLifecycleStage, ProxyRouteBuilder, RouteBuilder, RoutingManager, addServiceToServer, createParsedRouterRequest, createServer, initializeSdk, linearBackoffTimeout, linearBackoffTimeoutAsync, merge, wrapFunctionsAsync, wrapGrpcFunctions, wrapRouterGrpcFunction };
1749
+ //# sourceMappingURL=index.esm.js.map