@conduitplatform/module-tools 0.16.26 → 0.17.0-alpha.2
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.
- package/LICENSE +21 -0
- package/dist/index.cjs +2059 -1775
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +490 -0
- package/dist/index.d.ts +490 -7
- package/dist/index.esm.js +2033 -1746
- package/dist/index.esm.js.map +1 -1
- package/dist/module.proto +25 -0
- package/package.json +27 -31
- package/dist/ManagedModule.d.ts +0 -80
- package/dist/classes/ConduitActiveSchema.d.ts +0 -19
- package/dist/classes/ConduitServiceModule.d.ts +0 -27
- package/dist/classes/ConfigController.d.ts +0 -7
- package/dist/classes/GrpcServer.d.ts +0 -23
- package/dist/classes/ModuleError.d.ts +0 -5
- package/dist/classes/index.d.ts +0 -4
- package/dist/helpers/GrpcHelper.d.ts +0 -8
- package/dist/helpers/GrpcToParsedRouterRequest.d.ts +0 -2
- package/dist/helpers/TypeHelpers.d.ts +0 -70
- package/dist/helpers/index.d.ts +0 -4
- package/dist/helpers/wrapGrpcFunctions.d.ts +0 -5
- package/dist/interfaces/ConduitService.d.ts +0 -8
- package/dist/interfaces/ModuleLifecycleStage.d.ts +0 -7
- package/dist/interfaces/index.d.ts +0 -2
- package/dist/logging/index.d.ts +0 -16
- package/dist/metrics/MetricsServer.d.ts +0 -11
- package/dist/metrics/clientMiddleware.d.ts +0 -2
- package/dist/metrics/config/defaults.d.ts +0 -91
- package/dist/metrics/index.d.ts +0 -23
- package/dist/routing/ProxyRouteBuilder.d.ts +0 -21
- package/dist/routing/RouteBuilder.d.ts +0 -23
- package/dist/routing/RoutingController.d.ts +0 -8
- package/dist/routing/RoutingManager.d.ts +0 -35
- package/dist/routing/RoutingUtilities.d.ts +0 -7
- package/dist/routing/index.d.ts +0 -5
- package/dist/routing/wrapRouterFunctions.d.ts +0 -13
- package/dist/utilities/convictConfigParser.d.ts +0 -2
- package/dist/utilities/index.d.ts +0 -3
- package/dist/utilities/initializeSdk.d.ts +0 -2
- package/dist/utilities/linearBackoffTimeout.d.ts +0 -9
- package/dist/utilities/merge.d.ts +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,1856 +1,2140 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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 =
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
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
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
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
|
-
|
|
373
|
-
|
|
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
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
this.
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
this.
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
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
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
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
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
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
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
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
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
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
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
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
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
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
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
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
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
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
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
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
|
-
|
|
753
|
-
|
|
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
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
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
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
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
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
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
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
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
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
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
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
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
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
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
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
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
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
|
|
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
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
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(
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
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
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
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
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
}
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
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
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
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
|
-
//
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
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
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
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
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1551
|
+
for await (const item of iterable) {
|
|
1552
|
+
counter.inc();
|
|
1553
|
+
yield item;
|
|
1554
|
+
}
|
|
1393
1555
|
}
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
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
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
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
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
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
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
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
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
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
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
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
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
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
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
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
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
*/
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
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
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
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
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
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
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
}
|
|
1598
|
-
|
|
1599
|
-
|
|
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
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
return this.
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
}
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
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
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
this.
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
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
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
this.
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
}
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
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
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
exports
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
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
|