@kinotic-ai/core 5.0.0-beta.1 → 5.0.0-beta.10
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/README.md +2 -2
- package/dist/index.cjs +525 -293
- package/dist/index.d.cts +197 -68
- package/dist/index.d.ts +197 -68
- package/dist/index.js +519 -287
- package/dist/node/index.cjs +79 -0
- package/dist/node/index.d.cts +9 -0
- package/dist/node/index.d.ts +9 -0
- package/dist/node/index.js +14 -0
- package/package.json +28 -19
package/dist/index.cjs
CHANGED
|
@@ -66,17 +66,22 @@ var exports_src = {};
|
|
|
66
66
|
__export(exports_src, {
|
|
67
67
|
validateZone: () => validateZone,
|
|
68
68
|
validateLabel: () => validateLabel,
|
|
69
|
+
scopeOptionalMethodNames: () => scopeOptionalMethodNames,
|
|
69
70
|
receivesContext: () => receivesContext,
|
|
70
71
|
isAdvertised: () => isAdvertised,
|
|
71
72
|
createCRI: () => createCRI,
|
|
72
|
-
|
|
73
|
+
checkUiVersion: () => checkUiVersion,
|
|
74
|
+
buildServerUrl: () => buildServerUrl,
|
|
75
|
+
buildBrokerUrl: () => buildBrokerUrl,
|
|
73
76
|
Zone: () => Zone,
|
|
74
77
|
Version: () => Version,
|
|
75
78
|
TextEventFactory: () => TextEventFactory,
|
|
76
79
|
Sort: () => Sort,
|
|
77
80
|
SessionKeepAliveMode: () => SessionKeepAliveMode,
|
|
81
|
+
SessionCredentialsResolver: () => SessionCredentialsResolver,
|
|
78
82
|
ServiceRegistry: () => ServiceRegistry,
|
|
79
83
|
ServerInfo: () => ServerInfo,
|
|
84
|
+
ScopeOptional: () => ScopeOptional,
|
|
80
85
|
Scope: () => Scope,
|
|
81
86
|
Publish: () => Publish,
|
|
82
87
|
ParticipantConstants: () => ParticipantConstants,
|
|
@@ -91,6 +96,7 @@ __export(exports_src, {
|
|
|
91
96
|
EventConstants: () => EventConstants,
|
|
92
97
|
EventBus: () => EventBus,
|
|
93
98
|
Event: () => Event,
|
|
99
|
+
EnvCredentialsResolver: () => EnvCredentialsResolver,
|
|
94
100
|
Direction: () => Direction,
|
|
95
101
|
DefaultCRI: () => DefaultCRI,
|
|
96
102
|
DataSourceUtils: () => DataSourceUtils,
|
|
@@ -98,32 +104,32 @@ __export(exports_src, {
|
|
|
98
104
|
CrudServiceProxyFactory: () => CrudServiceProxyFactory,
|
|
99
105
|
CrudServiceProxy: () => CrudServiceProxy,
|
|
100
106
|
Context: () => Context,
|
|
101
|
-
ConnectionInfo: () => ConnectionInfo,
|
|
102
107
|
ConnectedInfo: () => ConnectedInfo,
|
|
103
|
-
|
|
104
|
-
|
|
108
|
+
ChainedCredentialsResolver: () => ChainedCredentialsResolver,
|
|
109
|
+
BearerCredentialsResolver: () => BearerCredentialsResolver,
|
|
110
|
+
BasicCredentialsResolver: () => BasicCredentialsResolver,
|
|
105
111
|
AbstractIterablePage: () => AbstractIterablePage
|
|
106
112
|
});
|
|
107
113
|
module.exports = __toCommonJS(exports_src);
|
|
108
114
|
|
|
109
|
-
// packages/core/src/api/
|
|
115
|
+
// packages/core/src/api/ConnectOptions.ts
|
|
110
116
|
class ServerInfo {
|
|
111
117
|
host;
|
|
112
118
|
port;
|
|
113
119
|
useSSL;
|
|
114
120
|
}
|
|
121
|
+
function buildServerUrl(server, scheme) {
|
|
122
|
+
return scheme + (server.useSSL ? "s" : "") + "://" + server.host + (server.port ? ":" + server.port : "");
|
|
123
|
+
}
|
|
124
|
+
function buildBrokerUrl(server) {
|
|
125
|
+
return buildServerUrl(server, "ws") + "/v1";
|
|
126
|
+
}
|
|
115
127
|
var SessionKeepAliveMode;
|
|
116
128
|
((SessionKeepAliveMode2) => {
|
|
117
129
|
SessionKeepAliveMode2["NONE"] = "NONE";
|
|
118
130
|
SessionKeepAliveMode2["ACTIVITY"] = "ACTIVITY";
|
|
119
131
|
SessionKeepAliveMode2["CONNECTION"] = "CONNECTION";
|
|
120
132
|
})(SessionKeepAliveMode ||= {});
|
|
121
|
-
|
|
122
|
-
class ConnectionInfo extends ServerInfo {
|
|
123
|
-
webSocketFactory;
|
|
124
|
-
maxConnectionAttempts;
|
|
125
|
-
sessionKeepAlive = "ACTIVITY" /* ACTIVITY */;
|
|
126
|
-
}
|
|
127
133
|
// packages/core/src/api/event/IEventBus.ts
|
|
128
134
|
var EventConstants;
|
|
129
135
|
((EventConstants2) => {
|
|
@@ -154,39 +160,25 @@ var EventConstants;
|
|
|
154
160
|
EventConstants2["TRACESTATE_HEADER"] = "tracestate";
|
|
155
161
|
})(EventConstants ||= {});
|
|
156
162
|
|
|
157
|
-
// packages/core/src/internal/api/
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
for (const [key, value] of incomingHeaders) {
|
|
169
|
-
if (key.startsWith("__")) {
|
|
170
|
-
newHeaders.set(key, value);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
if (headers) {
|
|
174
|
-
for (const [key, value] of headers) {
|
|
175
|
-
newHeaders.set(key, value);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
return new Event(replyCRI, newHeaders, body || undefined);
|
|
179
|
-
}
|
|
180
|
-
static buildBrokerUrl(serverInfo) {
|
|
181
|
-
return "ws" + (serverInfo.useSSL ? "s" : "") + "://" + serverInfo.host + (serverInfo.port ? ":" + serverInfo.port : "") + "/v1";
|
|
182
|
-
}
|
|
163
|
+
// packages/core/src/internal/api/Logger.ts
|
|
164
|
+
var import_debug = __toESM(require("debug"));
|
|
165
|
+
function createDebugLogger(namespace) {
|
|
166
|
+
const debugLogger = import_debug.default(namespace);
|
|
167
|
+
return {
|
|
168
|
+
trace: (...args) => debugLogger("TRACE", ...args),
|
|
169
|
+
debug: (...args) => debugLogger("DEBUG", ...args),
|
|
170
|
+
info: (...args) => debugLogger("INFO", ...args),
|
|
171
|
+
warn: (...args) => debugLogger("WARN", ...args),
|
|
172
|
+
error: (...args) => debugLogger("ERROR", ...args)
|
|
173
|
+
};
|
|
183
174
|
}
|
|
184
175
|
|
|
185
176
|
// packages/core/src/internal/api/StompConnectionManager.ts
|
|
186
177
|
var import_rx_stomp = require("@stomp/rx-stomp");
|
|
187
178
|
var import_stompjs = require("@stomp/stompjs");
|
|
188
|
-
var
|
|
179
|
+
var import_debug2 = __toESM(require("debug"));
|
|
189
180
|
var import_rxjs = require("rxjs");
|
|
181
|
+
var import_operators = require("rxjs/operators");
|
|
190
182
|
var import_uuid = require("uuid");
|
|
191
183
|
var SESSION_CHECK_PATH = "/api/auth/me";
|
|
192
184
|
|
|
@@ -194,15 +186,20 @@ class StompConnectionManager {
|
|
|
194
186
|
lastWebsocketError = null;
|
|
195
187
|
maxConnectionAttemptsReached = false;
|
|
196
188
|
replyToCriChangedHandler = null;
|
|
197
|
-
rxStomp =
|
|
189
|
+
rxStomp = new import_rx_stomp.RxStomp;
|
|
190
|
+
isActive = false;
|
|
191
|
+
initialConnectedSubscription = null;
|
|
192
|
+
initialFailureSubscription = null;
|
|
193
|
+
webSocketErrorsSubscription = null;
|
|
198
194
|
INITIAL_RECONNECT_DELAY = 2000;
|
|
199
195
|
JITTER_MAX = 5000;
|
|
200
196
|
MAX_RECONNECT_DELAY = 120000;
|
|
201
197
|
connectionAttempts = 0;
|
|
202
|
-
debugLogger =
|
|
198
|
+
debugLogger = import_debug2.default("kinotic:stomp");
|
|
203
199
|
fatalErrorsSubject = new import_rxjs.Subject;
|
|
204
200
|
_fatalErrors = this.fatalErrorsSubject.asObservable();
|
|
205
201
|
initialConnectionSuccessful = false;
|
|
202
|
+
rxStompHasConnected = false;
|
|
206
203
|
serverHeadersSubscription = null;
|
|
207
204
|
stompErrorsSubscription = null;
|
|
208
205
|
uuidv4 = import_uuid.v4();
|
|
@@ -211,61 +208,70 @@ class StompConnectionManager {
|
|
|
211
208
|
return this._replyToCri;
|
|
212
209
|
}
|
|
213
210
|
get active() {
|
|
214
|
-
return
|
|
211
|
+
return this.isActive;
|
|
215
212
|
}
|
|
216
213
|
get connected() {
|
|
217
|
-
return this.
|
|
214
|
+
return this.isActive && this.rxStomp.connected();
|
|
218
215
|
}
|
|
219
216
|
get fatalErrors() {
|
|
220
217
|
return this._fatalErrors;
|
|
221
218
|
}
|
|
222
|
-
activate(
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
219
|
+
async activate(options) {
|
|
220
|
+
if (!options?.server?.host) {
|
|
221
|
+
throw new Error("No host provided");
|
|
222
|
+
}
|
|
223
|
+
if (this.isActive) {
|
|
224
|
+
throw new Error("Stomp connection already active");
|
|
225
|
+
}
|
|
226
|
+
const server = options.server;
|
|
227
|
+
const credentialsResolver = options.credentials;
|
|
228
|
+
let headerAuth = false;
|
|
229
|
+
if (!options.webSocketFactory) {
|
|
230
|
+
if (!credentialsResolver) {
|
|
231
|
+
throw new Error("No credentials supplied and no default resolution applied");
|
|
231
232
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
233
|
+
const resolved = await credentialsResolver.resolve(server);
|
|
234
|
+
if (resolved === null) {
|
|
235
|
+
throw new Error("No Kinotic credentials found; consulted: " + credentialsResolver.name);
|
|
235
236
|
}
|
|
237
|
+
headerAuth = resolved.authHeaders != null;
|
|
238
|
+
}
|
|
239
|
+
return new Promise((resolve, reject) => {
|
|
236
240
|
this.connectionAttempts = 0;
|
|
237
241
|
this.initialConnectionSuccessful = false;
|
|
238
242
|
this.lastWebsocketError = null;
|
|
239
243
|
this.maxConnectionAttemptsReached = false;
|
|
240
|
-
const url =
|
|
241
|
-
this.rxStomp = new import_rx_stomp.RxStomp;
|
|
244
|
+
const url = buildBrokerUrl(server);
|
|
242
245
|
let preparedSocket = null;
|
|
243
|
-
const userWebSocketFactory =
|
|
246
|
+
const userWebSocketFactory = options.webSocketFactory;
|
|
247
|
+
const usesPreparedSocket = userWebSocketFactory != null || headerAuth;
|
|
244
248
|
const stompConfig = {
|
|
245
249
|
brokerURL: url,
|
|
246
250
|
connectHeaders: {
|
|
247
|
-
["session-keep-alive" /* SESSION_KEEP_ALIVE_HEADER */]:
|
|
251
|
+
["session-keep-alive" /* SESSION_KEEP_ALIVE_HEADER */]: options.sessionKeepAlive ?? "ACTIVITY" /* ACTIVITY */
|
|
248
252
|
},
|
|
249
253
|
heartbeatIncoming: 120000,
|
|
250
254
|
heartbeatOutgoing: 30000,
|
|
251
255
|
reconnectDelay: this.INITIAL_RECONNECT_DELAY,
|
|
252
256
|
maxReconnectDelay: this.MAX_RECONNECT_DELAY,
|
|
253
257
|
reconnectTimeMode: import_stompjs.ReconnectionTimeMode.EXPONENTIAL,
|
|
254
|
-
webSocketFactory:
|
|
258
|
+
webSocketFactory: usesPreparedSocket ? () => preparedSocket : undefined,
|
|
255
259
|
beforeConnect: async () => {
|
|
256
|
-
if (!
|
|
257
|
-
const sessionCheckUrl =
|
|
260
|
+
if (!usesPreparedSocket) {
|
|
261
|
+
const sessionCheckUrl = buildServerUrl(server, "http") + SESSION_CHECK_PATH;
|
|
258
262
|
try {
|
|
259
263
|
const res = await fetch(sessionCheckUrl, { credentials: "include" });
|
|
260
264
|
if (res.status === 401) {
|
|
261
265
|
await this.signalFatal(new Error("Authentication required"));
|
|
262
266
|
return;
|
|
263
267
|
}
|
|
264
|
-
} catch {
|
|
268
|
+
} catch (e) {
|
|
269
|
+
this.debugLogger("Session check at %s failed: %O", sessionCheckUrl, e);
|
|
270
|
+
}
|
|
265
271
|
}
|
|
266
|
-
if (
|
|
272
|
+
if (options?.maxConnectionAttempts) {
|
|
267
273
|
this.connectionAttempts++;
|
|
268
|
-
if (this.connectionAttempts >
|
|
274
|
+
if (this.connectionAttempts > options.maxConnectionAttempts) {
|
|
269
275
|
this.maxConnectionAttemptsReached = true;
|
|
270
276
|
await this.signalFatal(new Error("Max number of reconnection attempts reached", { cause: this.lastWebsocketError ?? undefined }));
|
|
271
277
|
return;
|
|
@@ -281,6 +287,18 @@ class StompConnectionManager {
|
|
|
281
287
|
} catch (e) {
|
|
282
288
|
await this.signalFatal(new Error("WebSocket factory failed", { cause: e }));
|
|
283
289
|
}
|
|
290
|
+
} else if (headerAuth) {
|
|
291
|
+
try {
|
|
292
|
+
const resolved = await credentialsResolver.resolve(server);
|
|
293
|
+
if (resolved?.authHeaders == null) {
|
|
294
|
+
await this.signalFatal(new Error("Credentials resolver no longer supplies auth headers: " + credentialsResolver.name));
|
|
295
|
+
} else {
|
|
296
|
+
const WS = WebSocket;
|
|
297
|
+
preparedSocket = new WS(url, { headers: resolved.authHeaders });
|
|
298
|
+
}
|
|
299
|
+
} catch (e) {
|
|
300
|
+
await this.signalFatal(new Error("Credential resolution failed", { cause: e }));
|
|
301
|
+
}
|
|
284
302
|
}
|
|
285
303
|
}
|
|
286
304
|
};
|
|
@@ -290,26 +308,31 @@ class StompConnectionManager {
|
|
|
290
308
|
};
|
|
291
309
|
}
|
|
292
310
|
this.rxStomp.configure(stompConfig);
|
|
293
|
-
this.rxStomp.webSocketErrors$.subscribe((value) => {
|
|
311
|
+
this.webSocketErrorsSubscription = this.rxStomp.webSocketErrors$.subscribe(async (value) => {
|
|
294
312
|
this.lastWebsocketError = value;
|
|
313
|
+
if (options?.maxConnectionAttempts && this.connectionAttempts >= options.maxConnectionAttempts) {
|
|
314
|
+
this.maxConnectionAttemptsReached = true;
|
|
315
|
+
await this.signalFatal(new Error("Max number of reconnection attempts reached", { cause: value ?? undefined }));
|
|
316
|
+
}
|
|
295
317
|
});
|
|
296
318
|
this.stompErrorsSubscription = this.rxStomp.stompErrors$.subscribe(async (frame) => {
|
|
297
319
|
const stompError = new Error(frame.headers["message"], { cause: frame });
|
|
298
320
|
await this.signalFatal(new Error("STOMP connection error", { cause: stompError }));
|
|
299
321
|
});
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
initialFailureSubscription
|
|
322
|
+
this.initialConnectedSubscription = this.rxStomp.connected$.subscribe(() => {
|
|
323
|
+
this.initialConnectedSubscription?.unsubscribe();
|
|
324
|
+
this.initialFailureSubscription?.unsubscribe();
|
|
303
325
|
if (!this.initialConnectionSuccessful) {
|
|
304
326
|
this.initialConnectionSuccessful = true;
|
|
305
327
|
}
|
|
306
328
|
});
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
initialFailureSubscription
|
|
329
|
+
this.initialFailureSubscription = this.fatalErrorsSubject.subscribe((err) => {
|
|
330
|
+
this.initialConnectedSubscription?.unsubscribe();
|
|
331
|
+
this.initialFailureSubscription?.unsubscribe();
|
|
310
332
|
reject(err.message);
|
|
311
333
|
});
|
|
312
|
-
this.serverHeadersSubscription = this.rxStomp.serverHeaders$.subscribe(async (value) => {
|
|
334
|
+
this.serverHeadersSubscription = this.rxStomp.serverHeaders$.pipe(import_operators.skip(this.rxStompHasConnected ? 1 : 0)).subscribe(async (value) => {
|
|
335
|
+
this.rxStompHasConnected = true;
|
|
313
336
|
const connectedInfoJson = value["connected-info" /* CONNECTED_INFO_HEADER */];
|
|
314
337
|
if (connectedInfoJson == null) {
|
|
315
338
|
if (!this.initialConnectionSuccessful) {
|
|
@@ -326,7 +349,7 @@ class StompConnectionManager {
|
|
|
326
349
|
}
|
|
327
350
|
return;
|
|
328
351
|
}
|
|
329
|
-
const newReplyToCri = "reply://" /* REPLY_DESTINATION_PREFIX */ + connectedInfo.replyToId + ":" + this.uuidv4 + "@
|
|
352
|
+
const newReplyToCri = "reply://" /* REPLY_DESTINATION_PREFIX */ + connectedInfo.replyToId + ":" + this.uuidv4 + "@kinotic.js.EventBus/replyHandler";
|
|
330
353
|
if (!this.initialConnectionSuccessful) {
|
|
331
354
|
this._replyToCri = newReplyToCri;
|
|
332
355
|
resolve(connectedInfo);
|
|
@@ -335,21 +358,23 @@ class StompConnectionManager {
|
|
|
335
358
|
this.replyToCriChangedHandler?.(newReplyToCri);
|
|
336
359
|
}
|
|
337
360
|
});
|
|
361
|
+
this.isActive = true;
|
|
338
362
|
this.rxStomp.activate();
|
|
339
363
|
});
|
|
340
364
|
}
|
|
341
365
|
async deactivate(force) {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
await rxStomp.deactivate({ force });
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
366
|
+
if (this.isActive) {
|
|
367
|
+
this.isActive = false;
|
|
368
|
+
await this.rxStomp.deactivate({ force });
|
|
369
|
+
this.initialConnectedSubscription?.unsubscribe();
|
|
370
|
+
this.initialConnectedSubscription = null;
|
|
371
|
+
this.webSocketErrorsSubscription?.unsubscribe();
|
|
372
|
+
this.webSocketErrorsSubscription = null;
|
|
373
|
+
this.serverHeadersSubscription?.unsubscribe();
|
|
374
|
+
this.serverHeadersSubscription = null;
|
|
375
|
+
this.stompErrorsSubscription?.unsubscribe();
|
|
376
|
+
this.stompErrorsSubscription = null;
|
|
377
|
+
this._replyToCri = null;
|
|
353
378
|
}
|
|
354
379
|
return;
|
|
355
380
|
}
|
|
@@ -370,7 +395,7 @@ class StompConnectionManager {
|
|
|
370
395
|
// packages/core/src/api/event/EventBus.ts
|
|
371
396
|
var import_api = require("@opentelemetry/api");
|
|
372
397
|
var import_rxjs2 = require("rxjs");
|
|
373
|
-
var
|
|
398
|
+
var import_operators2 = require("rxjs/operators");
|
|
374
399
|
var import_typescript_optional = require("typescript-optional");
|
|
375
400
|
var import_uuid2 = require("uuid");
|
|
376
401
|
class Event {
|
|
@@ -411,6 +436,7 @@ class Event {
|
|
|
411
436
|
|
|
412
437
|
class EventBus {
|
|
413
438
|
serverInfo = null;
|
|
439
|
+
log = createDebugLogger("kinotic:EventBus");
|
|
414
440
|
stompConnectionManager = new StompConnectionManager;
|
|
415
441
|
connectionLifecycle = Promise.resolve();
|
|
416
442
|
replyToCri = null;
|
|
@@ -436,15 +462,12 @@ class EventBus {
|
|
|
436
462
|
isConnected() {
|
|
437
463
|
return this.stompConnectionManager.connected;
|
|
438
464
|
}
|
|
439
|
-
connect(
|
|
465
|
+
connect(options) {
|
|
440
466
|
return this.serializeLifecycle(async () => {
|
|
441
467
|
if (!this.stompConnectionManager.active) {
|
|
442
468
|
this.cleanup();
|
|
443
|
-
const connectedInfo = await this.stompConnectionManager.activate(
|
|
444
|
-
this.serverInfo =
|
|
445
|
-
this.serverInfo.host = connectionInfo.host;
|
|
446
|
-
this.serverInfo.port = connectionInfo.port;
|
|
447
|
-
this.serverInfo.useSSL = connectionInfo.useSSL;
|
|
469
|
+
const connectedInfo = await this.stompConnectionManager.activate(options);
|
|
470
|
+
this.serverInfo = { ...options.server };
|
|
448
471
|
this.replyToCri = this.stompConnectionManager.replyToCri;
|
|
449
472
|
return connectedInfo;
|
|
450
473
|
} else {
|
|
@@ -464,7 +487,7 @@ class EventBus {
|
|
|
464
487
|
return result;
|
|
465
488
|
}
|
|
466
489
|
send(event) {
|
|
467
|
-
if (this.stompConnectionManager.
|
|
490
|
+
if (this.stompConnectionManager.connected) {
|
|
468
491
|
const headers = {};
|
|
469
492
|
for (const [key, value] of event.headers.entries()) {
|
|
470
493
|
headers[key] = value;
|
|
@@ -480,7 +503,8 @@ class EventBus {
|
|
|
480
503
|
this.stompConnectionManager.rxStomp.publish({
|
|
481
504
|
destination: event.cri,
|
|
482
505
|
headers,
|
|
483
|
-
binaryBody: event.data.orUndefined()
|
|
506
|
+
binaryBody: event.data.orUndefined(),
|
|
507
|
+
retryIfDisconnected: false
|
|
484
508
|
});
|
|
485
509
|
} else {
|
|
486
510
|
throw this.createSendUnavailableError();
|
|
@@ -490,17 +514,18 @@ class EventBus {
|
|
|
490
514
|
return import_rxjs2.firstValueFrom(this.requestStream(event, false));
|
|
491
515
|
}
|
|
492
516
|
requestStream(event, sendControlEvents = true) {
|
|
493
|
-
if (this.stompConnectionManager
|
|
517
|
+
if (this.stompConnectionManager.active) {
|
|
494
518
|
return new import_rxjs2.Observable((subscriber) => {
|
|
495
519
|
if (this.requestRepliesObservable == null) {
|
|
496
520
|
this.requestRepliesSubject = new import_rxjs2.Subject;
|
|
497
|
-
this.requestRepliesObservable = this._observe(this.replyToCri).pipe(
|
|
521
|
+
this.requestRepliesObservable = this._observe(this.replyToCri).pipe(import_operators2.tap((value) => this.cancelIfUnexpected(value)), import_operators2.multicast(this.requestRepliesSubject));
|
|
498
522
|
this.requestRepliesSubscription = this.requestRepliesObservable.connect();
|
|
499
523
|
}
|
|
500
524
|
let serverSignaledCompletion = false;
|
|
501
525
|
const correlationId = import_uuid2.v4();
|
|
502
526
|
this.activeCorrelationIds.add(correlationId);
|
|
503
|
-
|
|
527
|
+
subscriber.add(() => this.activeCorrelationIds.delete(correlationId));
|
|
528
|
+
const defaultMessagesSubscription = this.requestRepliesObservable.pipe(import_operators2.filter((value) => {
|
|
504
529
|
return value.headers.get("__correlation-id" /* CORRELATION_ID_HEADER */) === correlationId;
|
|
505
530
|
})).subscribe({
|
|
506
531
|
next(value) {
|
|
@@ -529,15 +554,15 @@ class EventBus {
|
|
|
529
554
|
event.setHeader("reply-to" /* REPLY_TO_HEADER */, this.replyToCri);
|
|
530
555
|
event.setHeader("__correlation-id" /* CORRELATION_ID_HEADER */, correlationId);
|
|
531
556
|
this.send(event);
|
|
532
|
-
|
|
533
|
-
this.activeCorrelationIds.delete(correlationId);
|
|
557
|
+
subscriber.add(() => {
|
|
534
558
|
if (sendControlEvents && !serverSignaledCompletion) {
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
559
|
+
try {
|
|
560
|
+
this.sendCancel(event.cri, correlationId);
|
|
561
|
+
} catch (e) {
|
|
562
|
+
this.log.warn(`Could not cancel stream ${correlationId} on ${event.cri}`, e);
|
|
563
|
+
}
|
|
539
564
|
}
|
|
540
|
-
};
|
|
565
|
+
});
|
|
541
566
|
});
|
|
542
567
|
} else {
|
|
543
568
|
return import_rxjs2.throwError(() => this.createSendUnavailableError());
|
|
@@ -563,13 +588,19 @@ class EventBus {
|
|
|
563
588
|
return;
|
|
564
589
|
}
|
|
565
590
|
try {
|
|
566
|
-
|
|
567
|
-
cancelEvent.setHeader("control" /* CONTROL_HEADER */, "cancel" /* CONTROL_VALUE_CANCEL */);
|
|
568
|
-
cancelEvent.setHeader("__correlation-id" /* CORRELATION_ID_HEADER */, correlationId);
|
|
569
|
-
this.send(cancelEvent);
|
|
591
|
+
this.sendCancel(originCri, correlationId);
|
|
570
592
|
this.recentlyReaped.add(correlationId);
|
|
571
593
|
setTimeout(() => this.recentlyReaped.delete(correlationId), EventBus.REAP_DEBOUNCE_MS);
|
|
572
|
-
} catch {
|
|
594
|
+
} catch (e) {
|
|
595
|
+
this.log.warn(`Could not cancel orphaned stream ${correlationId} on ${originCri}`, e);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
sendCancel(cri, correlationId) {
|
|
599
|
+
const cancelEvent = new Event(cri);
|
|
600
|
+
cancelEvent.setHeader("control" /* CONTROL_HEADER */, "cancel" /* CONTROL_VALUE_CANCEL */);
|
|
601
|
+
cancelEvent.setHeader("__correlation-id" /* CORRELATION_ID_HEADER */, correlationId);
|
|
602
|
+
cancelEvent.setHeader("reply-to" /* REPLY_TO_HEADER */, this.replyToCri);
|
|
603
|
+
this.send(cancelEvent);
|
|
573
604
|
}
|
|
574
605
|
resetRequestReplies(reason) {
|
|
575
606
|
if (this.requestRepliesSubject != null) {
|
|
@@ -584,29 +615,167 @@ class EventBus {
|
|
|
584
615
|
}
|
|
585
616
|
}
|
|
586
617
|
createSendUnavailableError() {
|
|
587
|
-
let ret
|
|
618
|
+
let ret;
|
|
588
619
|
if (this.stompConnectionManager.maxConnectionAttemptsReached) {
|
|
589
620
|
ret = "Max connection attempts reached event bus is not available";
|
|
621
|
+
} else if (this.stompConnectionManager.active) {
|
|
622
|
+
ret = "The event bus is not connected to the server";
|
|
623
|
+
} else {
|
|
624
|
+
ret = "You must call connect on the event bus before sending any request";
|
|
590
625
|
}
|
|
591
626
|
return new Error(ret);
|
|
592
627
|
}
|
|
593
628
|
_observe(cri) {
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
headers.set(prop, message.headers[prop]);
|
|
603
|
-
}
|
|
629
|
+
return this.stompConnectionManager.rxStomp.watch(cri).pipe(import_operators2.map((message) => {
|
|
630
|
+
const headers = new Map;
|
|
631
|
+
let destination = "";
|
|
632
|
+
for (const prop of Object.keys(message.headers)) {
|
|
633
|
+
if (prop === "destination") {
|
|
634
|
+
destination = message.headers[prop];
|
|
635
|
+
} else {
|
|
636
|
+
headers.set(prop, message.headers[prop]);
|
|
604
637
|
}
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
}
|
|
608
|
-
|
|
638
|
+
}
|
|
639
|
+
return new Event(destination, headers, message.binaryBody);
|
|
640
|
+
}));
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// packages/core/src/api/security/ChainedCredentialsResolver.ts
|
|
645
|
+
class ChainedCredentialsResolver {
|
|
646
|
+
name;
|
|
647
|
+
resolvers;
|
|
648
|
+
constructor(...resolvers) {
|
|
649
|
+
this.resolvers = resolvers;
|
|
650
|
+
this.name = `ChainedCredentialsResolver(${resolvers.map((r) => r.name).join(" -> ")})`;
|
|
651
|
+
}
|
|
652
|
+
async resolve(server) {
|
|
653
|
+
for (const resolver of this.resolvers) {
|
|
654
|
+
const resolved = await resolver.resolve(server);
|
|
655
|
+
if (resolved !== null) {
|
|
656
|
+
return resolved;
|
|
657
|
+
}
|
|
609
658
|
}
|
|
659
|
+
return null;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// packages/core/src/api/security/BasicCredentialsResolver.ts
|
|
664
|
+
class BasicCredentialsResolver {
|
|
665
|
+
name = "BasicCredentialsResolver";
|
|
666
|
+
credentials;
|
|
667
|
+
constructor(clientId, clientSecret, organizationId, applicationId) {
|
|
668
|
+
const authHeaders = { clientId, clientSecret };
|
|
669
|
+
if (organizationId != null)
|
|
670
|
+
authHeaders.organizationId = organizationId;
|
|
671
|
+
if (applicationId != null)
|
|
672
|
+
authHeaders.applicationId = applicationId;
|
|
673
|
+
this.credentials = { authHeaders, organizationId, applicationId };
|
|
674
|
+
}
|
|
675
|
+
async resolve(_server) {
|
|
676
|
+
return this.credentials;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// packages/core/src/api/security/BearerCredentialsResolver.ts
|
|
681
|
+
class BearerCredentialsResolver {
|
|
682
|
+
name = "BearerCredentialsResolver";
|
|
683
|
+
token;
|
|
684
|
+
constructor(token) {
|
|
685
|
+
this.token = token;
|
|
686
|
+
}
|
|
687
|
+
async resolve(_server) {
|
|
688
|
+
const token = typeof this.token === "function" ? await this.token() : this.token;
|
|
689
|
+
return { authHeaders: { Authorization: `Bearer ${token}` } };
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// packages/core/src/api/security/EnvCredentialsResolver.ts
|
|
694
|
+
class EnvCredentialsResolver {
|
|
695
|
+
name = "EnvCredentialsResolver";
|
|
696
|
+
async resolve(server) {
|
|
697
|
+
const env = typeof process !== "undefined" ? process.env : undefined;
|
|
698
|
+
let ret = null;
|
|
699
|
+
if (env?.KINOTIC_CLIENT_ID && env?.KINOTIC_CLIENT_SECRET) {
|
|
700
|
+
ret = await new BasicCredentialsResolver(env.KINOTIC_CLIENT_ID, env.KINOTIC_CLIENT_SECRET, env.KINOTIC_ORGANIZATION_ID || undefined, env.KINOTIC_APPLICATION_ID || undefined).resolve(server);
|
|
701
|
+
} else if (env?.KINOTIC_TOKEN) {
|
|
702
|
+
ret = await new BearerCredentialsResolver(env.KINOTIC_TOKEN).resolve(server);
|
|
703
|
+
}
|
|
704
|
+
return ret;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
// packages/core/src/api/security/SessionCredentialsResolver.ts
|
|
709
|
+
class SessionCredentialsResolver {
|
|
710
|
+
name = "SessionCredentialsResolver";
|
|
711
|
+
async resolve(_server) {
|
|
712
|
+
return typeof document !== "undefined" ? {} : null;
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
// packages/core/src/internal/api/Util.ts
|
|
717
|
+
var DEFAULT_PORT = 58503;
|
|
718
|
+
var DEFAULT_HOST = "api.kinotic.ai";
|
|
719
|
+
|
|
720
|
+
class Util {
|
|
721
|
+
static createReplyEvent(incomingHeaders, headers, body) {
|
|
722
|
+
if (!incomingHeaders) {
|
|
723
|
+
throw new Error("incomingHeaders cannot be null");
|
|
724
|
+
}
|
|
725
|
+
const replyCRI = incomingHeaders.get("reply-to" /* REPLY_TO_HEADER */);
|
|
726
|
+
if (!replyCRI || replyCRI.trim() === "") {
|
|
727
|
+
throw new Error("No reply-to header found, cannot create outgoing message");
|
|
728
|
+
}
|
|
729
|
+
const newHeaders = new Map;
|
|
730
|
+
for (const [key, value] of incomingHeaders) {
|
|
731
|
+
if (key.startsWith("__")) {
|
|
732
|
+
newHeaders.set(key, value);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
if (headers) {
|
|
736
|
+
for (const [key, value] of headers) {
|
|
737
|
+
newHeaders.set(key, value);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
return new Event(replyCRI, newHeaders, body || undefined);
|
|
741
|
+
}
|
|
742
|
+
static resolveConnectOptions(options) {
|
|
743
|
+
const opts = options ?? {};
|
|
744
|
+
const server = opts.server ?? {};
|
|
745
|
+
const env = typeof process !== "undefined" ? process.env : undefined;
|
|
746
|
+
const location = typeof window !== "undefined" ? window.location : undefined;
|
|
747
|
+
let host = server.host ?? env?.KINOTIC_SERVER_HOST;
|
|
748
|
+
const hostFromLocation = host == null && location != null;
|
|
749
|
+
const hostDefaulted = host == null && location == null;
|
|
750
|
+
if (host == null) {
|
|
751
|
+
host = location?.hostname ?? DEFAULT_HOST;
|
|
752
|
+
}
|
|
753
|
+
let useSSL = server.useSSL;
|
|
754
|
+
if (useSSL == null) {
|
|
755
|
+
if (env?.KINOTIC_SERVER_USE_SSL != null) {
|
|
756
|
+
useSSL = env.KINOTIC_SERVER_USE_SSL === "true";
|
|
757
|
+
} else if (hostFromLocation) {
|
|
758
|
+
useSSL = location.protocol === "https:";
|
|
759
|
+
} else {
|
|
760
|
+
useSSL = hostDefaulted;
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
let port = server.port;
|
|
764
|
+
if (port === undefined) {
|
|
765
|
+
if (env?.KINOTIC_SERVER_PORT != null) {
|
|
766
|
+
port = Number(env.KINOTIC_SERVER_PORT);
|
|
767
|
+
} else if (hostFromLocation) {
|
|
768
|
+
port = location.port ? Number(location.port) : null;
|
|
769
|
+
} else {
|
|
770
|
+
port = useSSL ? null : DEFAULT_PORT;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
return {
|
|
774
|
+
...opts,
|
|
775
|
+
server: { host, port, useSSL },
|
|
776
|
+
sessionKeepAlive: opts.sessionKeepAlive ?? "ACTIVITY" /* ACTIVITY */,
|
|
777
|
+
credentials: opts.credentials ?? new ChainedCredentialsResolver(new EnvCredentialsResolver, new SessionCredentialsResolver)
|
|
778
|
+
};
|
|
610
779
|
}
|
|
611
780
|
}
|
|
612
781
|
|
|
@@ -799,23 +968,11 @@ class JsonArgumentResolver {
|
|
|
799
968
|
// packages/core/src/internal/api/ReturnValueConverter.ts
|
|
800
969
|
class BasicReturnValueConverter {
|
|
801
970
|
convert(incomingMetadata, returnValue) {
|
|
802
|
-
|
|
971
|
+
const json = returnValue === undefined ? "null" : JSON.stringify(returnValue);
|
|
972
|
+
return Util.createReplyEvent(incomingMetadata, new Map([["content-type" /* CONTENT_TYPE_HEADER */, "application/json"]]), new TextEncoder().encode(json));
|
|
803
973
|
}
|
|
804
974
|
}
|
|
805
975
|
|
|
806
|
-
// packages/core/src/internal/api/Logger.ts
|
|
807
|
-
var import_debug2 = __toESM(require("debug"));
|
|
808
|
-
function createDebugLogger(namespace) {
|
|
809
|
-
const debugLogger = import_debug2.default(namespace);
|
|
810
|
-
return {
|
|
811
|
-
trace: (...args) => debugLogger("TRACE", ...args),
|
|
812
|
-
debug: (...args) => debugLogger("DEBUG", ...args),
|
|
813
|
-
info: (...args) => debugLogger("INFO", ...args),
|
|
814
|
-
warn: (...args) => debugLogger("WARN", ...args),
|
|
815
|
-
error: (...args) => debugLogger("ERROR", ...args)
|
|
816
|
-
};
|
|
817
|
-
}
|
|
818
|
-
|
|
819
976
|
// packages/core/src/api/ServiceIdentifier.ts
|
|
820
977
|
class ServiceIdentifier {
|
|
821
978
|
namespace;
|
|
@@ -851,6 +1008,9 @@ class ServiceIdentifier {
|
|
|
851
1008
|
}
|
|
852
1009
|
return this._cri;
|
|
853
1010
|
}
|
|
1011
|
+
unscopedCri() {
|
|
1012
|
+
return createCRI("srv" /* SERVICE_DESTINATION_SCHEME */, null, this.qualifiedName(), null, this.version || null);
|
|
1013
|
+
}
|
|
854
1014
|
}
|
|
855
1015
|
|
|
856
1016
|
// packages/core/src/api/ZoneUtil.ts
|
|
@@ -870,6 +1030,7 @@ function validateLabel(label) {
|
|
|
870
1030
|
|
|
871
1031
|
// packages/core/src/api/KinoticDecorators.ts
|
|
872
1032
|
var scopeFunctions = new WeakSet;
|
|
1033
|
+
var scopeOptionalFunctions = new WeakSet;
|
|
873
1034
|
var versionRegistry = new WeakMap;
|
|
874
1035
|
var zonesRegistry = new WeakMap;
|
|
875
1036
|
var advertisedRegistry = new WeakMap;
|
|
@@ -907,6 +1068,23 @@ function resolveScope(instance) {
|
|
|
907
1068
|
function Scope(value, _context) {
|
|
908
1069
|
scopeFunctions.add(value);
|
|
909
1070
|
}
|
|
1071
|
+
function ScopeOptional(value, _context) {
|
|
1072
|
+
scopeOptionalFunctions.add(value);
|
|
1073
|
+
}
|
|
1074
|
+
function scopeOptionalMethodNames(instance) {
|
|
1075
|
+
const ret = new Set;
|
|
1076
|
+
let proto = Object.getPrototypeOf(instance);
|
|
1077
|
+
while (proto && proto !== Object.prototype) {
|
|
1078
|
+
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
1079
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, key);
|
|
1080
|
+
if (typeof descriptor?.value === "function" && scopeOptionalFunctions.has(descriptor.value)) {
|
|
1081
|
+
ret.add(key);
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
proto = Object.getPrototypeOf(proto);
|
|
1085
|
+
}
|
|
1086
|
+
return ret;
|
|
1087
|
+
}
|
|
910
1088
|
function Version(version) {
|
|
911
1089
|
if (!/^\d+\.\d+\.\d+(-[a-zA-Z0-9]+)?$/.test(version)) {
|
|
912
1090
|
throw new Error(`Invalid semantic version: ${version}. Must follow X.Y.Z[-optional] format.`);
|
|
@@ -969,6 +1147,93 @@ function Publish(namespace, name, advertise = false) {
|
|
|
969
1147
|
};
|
|
970
1148
|
}
|
|
971
1149
|
|
|
1150
|
+
// packages/core/src/internal/api/ServiceInvocationSupervisor.ts
|
|
1151
|
+
var import_api2 = __toESM(require("@opentelemetry/api"));
|
|
1152
|
+
// packages/core/package.json
|
|
1153
|
+
var package_default = {
|
|
1154
|
+
name: "@kinotic-ai/core",
|
|
1155
|
+
version: "5.0.0-beta.10",
|
|
1156
|
+
type: "module",
|
|
1157
|
+
files: [
|
|
1158
|
+
"dist"
|
|
1159
|
+
],
|
|
1160
|
+
module: "./dist/index.js",
|
|
1161
|
+
main: "./dist/index.cjs",
|
|
1162
|
+
types: "./dist/index.d.ts",
|
|
1163
|
+
exports: {
|
|
1164
|
+
".": {
|
|
1165
|
+
import: {
|
|
1166
|
+
types: "./dist/index.d.ts",
|
|
1167
|
+
default: "./dist/index.js"
|
|
1168
|
+
},
|
|
1169
|
+
require: {
|
|
1170
|
+
types: "./dist/index.d.cts",
|
|
1171
|
+
default: "./dist/index.cjs"
|
|
1172
|
+
}
|
|
1173
|
+
},
|
|
1174
|
+
"./node": {
|
|
1175
|
+
import: {
|
|
1176
|
+
types: "./dist/node/index.d.ts",
|
|
1177
|
+
default: "./dist/node/index.js"
|
|
1178
|
+
},
|
|
1179
|
+
require: {
|
|
1180
|
+
types: "./dist/node/index.d.cts",
|
|
1181
|
+
default: "./dist/node/index.cjs"
|
|
1182
|
+
}
|
|
1183
|
+
},
|
|
1184
|
+
"./package.json": "./package.json"
|
|
1185
|
+
},
|
|
1186
|
+
homepage: "https://github.com/kinotic-ai/kinotic",
|
|
1187
|
+
repository: {
|
|
1188
|
+
type: "git",
|
|
1189
|
+
url: "git+https://github.com/kinotic-ai/kinotic.git",
|
|
1190
|
+
directory: "kinotic-js/workspace/packages/core"
|
|
1191
|
+
},
|
|
1192
|
+
license: "Elastic License 2.0",
|
|
1193
|
+
publishConfig: {
|
|
1194
|
+
access: "public"
|
|
1195
|
+
},
|
|
1196
|
+
scripts: {
|
|
1197
|
+
"type-check": "tsc --noEmit",
|
|
1198
|
+
test: "vitest run",
|
|
1199
|
+
coverage: "vitest run --coverage",
|
|
1200
|
+
"ui-test": "vitest --ui --coverage.enabled=true --mode development"
|
|
1201
|
+
},
|
|
1202
|
+
devDependencies: {
|
|
1203
|
+
"@opentelemetry/exporter-trace-otlp-grpc": "^0.221.0",
|
|
1204
|
+
"@opentelemetry/resources": "^2.10.0",
|
|
1205
|
+
"@opentelemetry/sdk-metrics": "^2.10.0",
|
|
1206
|
+
"@opentelemetry/sdk-node": "^0.221.0",
|
|
1207
|
+
"@opentelemetry/sdk-trace-node": "^2.10.0",
|
|
1208
|
+
"@types/debug": "^4.1.13",
|
|
1209
|
+
"@types/node": "^25.6.0",
|
|
1210
|
+
"@types/ws": "^8.18.0",
|
|
1211
|
+
"@vitest/coverage-v8": "^4.1.4",
|
|
1212
|
+
"@vitest/runner": "^4.1.4",
|
|
1213
|
+
"@vitest/ui": "^4.1.4",
|
|
1214
|
+
allure: "^3.14.3",
|
|
1215
|
+
"allure-vitest": "^3.10.2",
|
|
1216
|
+
dotenv: "^17.4.1",
|
|
1217
|
+
"properties-file": "^4.0.0",
|
|
1218
|
+
testcontainers: "^11.14.0",
|
|
1219
|
+
tslib: "^2.8.1",
|
|
1220
|
+
typescript: "^6.0.2",
|
|
1221
|
+
vitest: "^4.1.4"
|
|
1222
|
+
},
|
|
1223
|
+
dependencies: {
|
|
1224
|
+
"@opentelemetry/api": "^1.9.1",
|
|
1225
|
+
"@opentelemetry/semantic-conventions": "^1.40.0",
|
|
1226
|
+
"@stomp/rx-stomp": "^2.4.0",
|
|
1227
|
+
"@stomp/stompjs": "^7.3.0",
|
|
1228
|
+
debug: "^4.4.3",
|
|
1229
|
+
"p-tap": "^4.0.0",
|
|
1230
|
+
rxjs: "^7.8.2",
|
|
1231
|
+
"typescript-optional": "3.0.0-alpha.3",
|
|
1232
|
+
uuid: "^13.0.0",
|
|
1233
|
+
ws: "^8.21.0"
|
|
1234
|
+
}
|
|
1235
|
+
};
|
|
1236
|
+
|
|
972
1237
|
// packages/core/src/internal/api/ServiceInvocationSupervisor.ts
|
|
973
1238
|
class ServiceInvocationSupervisor {
|
|
974
1239
|
log;
|
|
@@ -980,7 +1245,10 @@ class ServiceInvocationSupervisor {
|
|
|
980
1245
|
serviceIdentifier;
|
|
981
1246
|
serviceInstance;
|
|
982
1247
|
methodSubscription = null;
|
|
1248
|
+
unscopedSubscription = null;
|
|
983
1249
|
methodMap;
|
|
1250
|
+
scopeOptionalMethods;
|
|
1251
|
+
tracer;
|
|
984
1252
|
constructor(serviceIdentifier, serviceInstance, eventBusService, interceptorProvider, options = {}) {
|
|
985
1253
|
if (!serviceIdentifier)
|
|
986
1254
|
throw new Error("ServiceIdentifier must not be null");
|
|
@@ -994,10 +1262,12 @@ class ServiceInvocationSupervisor {
|
|
|
994
1262
|
this.serviceInstance = serviceInstance;
|
|
995
1263
|
this._eventBus = eventBusService;
|
|
996
1264
|
this.interceptorProvider = interceptorProvider;
|
|
997
|
-
this.log = options.logger || createDebugLogger("
|
|
1265
|
+
this.log = options.logger || createDebugLogger("kinotic:ServiceInvocationSupervisor");
|
|
998
1266
|
this.argumentResolver = options.argumentResolver || new JsonArgumentResolver;
|
|
999
1267
|
this.returnValueConverter = options.returnValueConverter || new BasicReturnValueConverter;
|
|
1000
1268
|
this.methodMap = this.buildMethodMap(serviceInstance);
|
|
1269
|
+
this.scopeOptionalMethods = scopeOptionalMethodNames(serviceInstance);
|
|
1270
|
+
this.tracer = import_api2.default.trace.getTracer(package_default.name, package_default.version);
|
|
1001
1271
|
}
|
|
1002
1272
|
isActive() {
|
|
1003
1273
|
return this.active;
|
|
@@ -1017,7 +1287,16 @@ class ServiceInvocationSupervisor {
|
|
|
1017
1287
|
}
|
|
1018
1288
|
this.active = true;
|
|
1019
1289
|
const criBase = this.serviceIdentifier.cri().baseResource();
|
|
1020
|
-
this.methodSubscription = this.
|
|
1290
|
+
this.methodSubscription = this.subscribeAt(criBase);
|
|
1291
|
+
this.log.info(`ServiceInvocationSupervisor started for ${criBase}`);
|
|
1292
|
+
if (this.serviceIdentifier.scope && this.scopeOptionalMethods.size > 0) {
|
|
1293
|
+
const unscopedBase = this.serviceIdentifier.unscopedCri().baseResource();
|
|
1294
|
+
this.unscopedSubscription = this.subscribeAt(unscopedBase);
|
|
1295
|
+
this.log.info(`ServiceInvocationSupervisor also listening for ScopeOptional methods at ${unscopedBase}`);
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
subscribeAt(criBase) {
|
|
1299
|
+
return this._eventBus.observe(criBase).subscribe({
|
|
1021
1300
|
next: async (event) => {
|
|
1022
1301
|
await this.processEvent(event);
|
|
1023
1302
|
},
|
|
@@ -1030,7 +1309,6 @@ class ServiceInvocationSupervisor {
|
|
|
1030
1309
|
this.active = false;
|
|
1031
1310
|
}
|
|
1032
1311
|
});
|
|
1033
|
-
this.log.info(`ServiceInvocationSupervisor started for ${criBase}`);
|
|
1034
1312
|
}
|
|
1035
1313
|
stop() {
|
|
1036
1314
|
if (!this.active) {
|
|
@@ -1039,6 +1317,8 @@ class ServiceInvocationSupervisor {
|
|
|
1039
1317
|
this.active = false;
|
|
1040
1318
|
this.methodSubscription?.unsubscribe();
|
|
1041
1319
|
this.methodSubscription = null;
|
|
1320
|
+
this.unscopedSubscription?.unsubscribe();
|
|
1321
|
+
this.unscopedSubscription = null;
|
|
1042
1322
|
this.log.info("ServiceInvocationSupervisor stopped");
|
|
1043
1323
|
}
|
|
1044
1324
|
buildMethodMap(serviceInstance) {
|
|
@@ -1081,7 +1361,8 @@ class ServiceInvocationSupervisor {
|
|
|
1081
1361
|
this.log.trace(`Processing control event for correlationId: ${correlationId}`);
|
|
1082
1362
|
}
|
|
1083
1363
|
async processInvocationRequest(event) {
|
|
1084
|
-
const
|
|
1364
|
+
const incomingCri = createCRI(event.cri);
|
|
1365
|
+
const path = incomingCri.path();
|
|
1085
1366
|
if (!path) {
|
|
1086
1367
|
throw new Error("The methodId must not be blank");
|
|
1087
1368
|
}
|
|
@@ -1089,49 +1370,94 @@ class ServiceInvocationSupervisor {
|
|
|
1089
1370
|
if (!handlerMethod) {
|
|
1090
1371
|
throw new Error(`No method resolved for methodId ${path}`);
|
|
1091
1372
|
}
|
|
1373
|
+
if (this.serviceIdentifier.scope && !incomingCri.hasScope() && !this.scopeOptionalMethods.has(path)) {
|
|
1374
|
+
throw new Error(`Method ${path} of ${this.serviceIdentifier.qualifiedName()} requires a scoped invocation naming the service instance`);
|
|
1375
|
+
}
|
|
1092
1376
|
const methodName = path;
|
|
1093
|
-
const
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1377
|
+
const span = this.startInvocationSpan(event, methodName);
|
|
1378
|
+
try {
|
|
1379
|
+
const args = this.argumentResolver.resolveArguments(event);
|
|
1380
|
+
const injectContext = receivesContext(this.serviceInstance, methodName);
|
|
1381
|
+
let serviceContext = {};
|
|
1382
|
+
const interceptor = this.interceptorProvider();
|
|
1383
|
+
if (interceptor) {
|
|
1384
|
+
try {
|
|
1385
|
+
serviceContext = await interceptor.intercept(event, serviceContext);
|
|
1386
|
+
} catch (e) {
|
|
1387
|
+
this.log.error(`Interceptor failed to create context for event: ${JSON.stringify(event)}`, e);
|
|
1388
|
+
this.handleException(event, new Error("Internal server error"));
|
|
1389
|
+
this.failSpan(span, e);
|
|
1390
|
+
return;
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
if (injectContext) {
|
|
1394
|
+
args.push(serviceContext);
|
|
1395
|
+
}
|
|
1396
|
+
const expectedArgsCount = handlerMethod.length;
|
|
1397
|
+
if (args.length !== expectedArgsCount) {
|
|
1398
|
+
throw new Error(`Argument count mismatch for method ${path}: expected ${expectedArgsCount}, got ${args.length}`);
|
|
1399
|
+
}
|
|
1400
|
+
let result;
|
|
1098
1401
|
try {
|
|
1099
|
-
|
|
1402
|
+
result = import_api2.context.with(import_api2.trace.setSpan(import_api2.context.active(), span), () => handlerMethod(...args));
|
|
1403
|
+
if (result instanceof Promise) {
|
|
1404
|
+
result.then((resolved) => {
|
|
1405
|
+
this.processMethodInvocationResult(event, resolved);
|
|
1406
|
+
span.end();
|
|
1407
|
+
}, (error) => {
|
|
1408
|
+
this.handleException(event, error);
|
|
1409
|
+
this.failSpan(span, error);
|
|
1410
|
+
});
|
|
1411
|
+
} else {
|
|
1412
|
+
this.processMethodInvocationResult(event, result);
|
|
1413
|
+
span.end();
|
|
1414
|
+
}
|
|
1100
1415
|
} catch (e) {
|
|
1101
|
-
this.
|
|
1102
|
-
this.
|
|
1103
|
-
return;
|
|
1416
|
+
this.handleException(event, e);
|
|
1417
|
+
this.failSpan(span, e);
|
|
1104
1418
|
}
|
|
1419
|
+
} catch (e) {
|
|
1420
|
+
this.failSpan(span, e);
|
|
1421
|
+
throw e;
|
|
1105
1422
|
}
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
}
|
|
1109
|
-
const
|
|
1110
|
-
|
|
1111
|
-
throw new Error(`Argument count mismatch for method ${path}: expected ${expectedArgsCount}, got ${args.length}`);
|
|
1423
|
+
}
|
|
1424
|
+
startInvocationSpan(event, methodName) {
|
|
1425
|
+
const carrier = {};
|
|
1426
|
+
for (const [key, value] of event.headers.entries()) {
|
|
1427
|
+
carrier[key] = value;
|
|
1112
1428
|
}
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1429
|
+
const serviceName = this.serviceIdentifier.qualifiedName();
|
|
1430
|
+
return this.tracer.startSpan(`${this.serviceIdentifier.name}/${methodName}`, {
|
|
1431
|
+
kind: import_api2.SpanKind.SERVER,
|
|
1432
|
+
attributes: {
|
|
1433
|
+
"rpc.system": "kinotic",
|
|
1434
|
+
"rpc.service": serviceName,
|
|
1435
|
+
"rpc.method": methodName
|
|
1120
1436
|
}
|
|
1121
|
-
}
|
|
1122
|
-
|
|
1123
|
-
|
|
1437
|
+
}, import_api2.propagation.extract(import_api2.context.active(), carrier));
|
|
1438
|
+
}
|
|
1439
|
+
failSpan(span, error) {
|
|
1440
|
+
span.recordException(error);
|
|
1441
|
+
span.setStatus({ code: import_api2.SpanStatusCode.ERROR });
|
|
1442
|
+
span.end();
|
|
1124
1443
|
}
|
|
1125
1444
|
processMethodInvocationResult(event, result) {
|
|
1126
1445
|
const outgoingEvent = this.returnValueConverter.convert(event.headers, result);
|
|
1127
|
-
this.
|
|
1446
|
+
this.sendReply(outgoingEvent);
|
|
1447
|
+
}
|
|
1448
|
+
sendReply(event) {
|
|
1449
|
+
try {
|
|
1450
|
+
this._eventBus.send(event);
|
|
1451
|
+
} catch (e) {
|
|
1452
|
+
this.log.warn(`Could not send reply to ${event.cri}`, e);
|
|
1453
|
+
}
|
|
1128
1454
|
}
|
|
1129
1455
|
handleException(event, error) {
|
|
1130
1456
|
const errorEvent = Util.createReplyEvent(event.headers, new Map([
|
|
1131
1457
|
["error" /* ERROR_HEADER */, error.message || "Unknown error"],
|
|
1132
1458
|
["content-type" /* CONTENT_TYPE_HEADER */, "application/json"]
|
|
1133
1459
|
]), new TextEncoder().encode(JSON.stringify({ message: error.message })));
|
|
1134
|
-
this.
|
|
1460
|
+
this.sendReply(errorEvent);
|
|
1135
1461
|
}
|
|
1136
1462
|
validateReplyTo(event) {
|
|
1137
1463
|
const replyTo = event.getHeader("reply-to" /* REPLY_TO_HEADER */);
|
|
@@ -1152,86 +1478,9 @@ class ServiceInvocationSupervisor {
|
|
|
1152
1478
|
}
|
|
1153
1479
|
|
|
1154
1480
|
// packages/core/src/api/ServiceRegistry.ts
|
|
1155
|
-
var
|
|
1481
|
+
var import_api3 = __toESM(require("@opentelemetry/api"));
|
|
1156
1482
|
var import_semantic_conventions = require("@opentelemetry/semantic-conventions");
|
|
1157
|
-
var
|
|
1158
|
-
// packages/core/package.json
|
|
1159
|
-
var package_default = {
|
|
1160
|
-
name: "@kinotic-ai/core",
|
|
1161
|
-
version: "5.0.0-beta.1",
|
|
1162
|
-
type: "module",
|
|
1163
|
-
files: [
|
|
1164
|
-
"dist"
|
|
1165
|
-
],
|
|
1166
|
-
module: "./dist/index.js",
|
|
1167
|
-
main: "./dist/index.cjs",
|
|
1168
|
-
types: "./dist/index.d.ts",
|
|
1169
|
-
exports: {
|
|
1170
|
-
".": {
|
|
1171
|
-
import: {
|
|
1172
|
-
types: "./dist/index.d.ts",
|
|
1173
|
-
default: "./dist/index.js"
|
|
1174
|
-
},
|
|
1175
|
-
require: {
|
|
1176
|
-
types: "./dist/index.d.cts",
|
|
1177
|
-
default: "./dist/index.cjs"
|
|
1178
|
-
}
|
|
1179
|
-
},
|
|
1180
|
-
"./package.json": "./package.json"
|
|
1181
|
-
},
|
|
1182
|
-
homepage: "https://github.com/kinotic-ai/kinotic",
|
|
1183
|
-
license: "Elastic License 2.0",
|
|
1184
|
-
publishConfig: {
|
|
1185
|
-
access: "public"
|
|
1186
|
-
},
|
|
1187
|
-
scripts: {
|
|
1188
|
-
"type-check": "tsc --noEmit",
|
|
1189
|
-
test: "vitest run",
|
|
1190
|
-
coverage: "vitest run --coverage",
|
|
1191
|
-
"ui-test": "vitest --ui --coverage.enabled=true --mode development"
|
|
1192
|
-
},
|
|
1193
|
-
devDependencies: {
|
|
1194
|
-
"@opentelemetry/exporter-trace-otlp-grpc": "^0.214.0",
|
|
1195
|
-
"@opentelemetry/resources": "^2.6.1",
|
|
1196
|
-
"@opentelemetry/sdk-metrics": "^2.6.1",
|
|
1197
|
-
"@opentelemetry/sdk-node": "^0.214.0",
|
|
1198
|
-
"@opentelemetry/sdk-trace-node": "^2.6.1",
|
|
1199
|
-
"@types/node": "^25.6.0",
|
|
1200
|
-
"@types/uuid": "^11.0.0",
|
|
1201
|
-
"@types/ws": "^8.18.1",
|
|
1202
|
-
"@vitest/coverage-v8": "^4.1.4",
|
|
1203
|
-
"@vitest/runner": "^4.1.4",
|
|
1204
|
-
"@vitest/ui": "^4.1.4",
|
|
1205
|
-
allure: "^3.4.1",
|
|
1206
|
-
"allure-vitest": "^3.7.1",
|
|
1207
|
-
dotenv: "^17.4.1",
|
|
1208
|
-
"properties-file": "^4.0.0",
|
|
1209
|
-
testcontainers: "^11.14.0",
|
|
1210
|
-
tslib: "^2.8.1",
|
|
1211
|
-
typescript: "^6.0.2",
|
|
1212
|
-
vitest: "^4.1.4",
|
|
1213
|
-
ws: "^8.20.0"
|
|
1214
|
-
},
|
|
1215
|
-
dependencies: {
|
|
1216
|
-
"@opentelemetry/api": "^1.9.1",
|
|
1217
|
-
"@opentelemetry/semantic-conventions": "^1.40.0",
|
|
1218
|
-
"@stomp/rx-stomp": "^2.4.0",
|
|
1219
|
-
"@stomp/stompjs": "^7.3.0",
|
|
1220
|
-
"@types/debug": "^4.1.13",
|
|
1221
|
-
debug: "^4.4.3",
|
|
1222
|
-
"p-tap": "^4.0.0",
|
|
1223
|
-
rxjs: "^7.8.2",
|
|
1224
|
-
"typescript-optional": "3.0.0-alpha.3",
|
|
1225
|
-
uuid: "^13.0.0"
|
|
1226
|
-
},
|
|
1227
|
-
peerDependenciesMeta: {
|
|
1228
|
-
typescript: {
|
|
1229
|
-
optional: true
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
};
|
|
1233
|
-
|
|
1234
|
-
// packages/core/src/api/ServiceRegistry.ts
|
|
1483
|
+
var import_operators3 = require("rxjs/operators");
|
|
1235
1484
|
class JsonEventFactory {
|
|
1236
1485
|
create(cri, args) {
|
|
1237
1486
|
const event = new Event(cri);
|
|
@@ -1270,7 +1519,7 @@ class ServiceRegistry {
|
|
|
1270
1519
|
_eventBus;
|
|
1271
1520
|
supervisors = new Map;
|
|
1272
1521
|
contextInterceptor = null;
|
|
1273
|
-
debugLogger = import_debug3.default("
|
|
1522
|
+
debugLogger = import_debug3.default("kinotic:serviceRegistry");
|
|
1274
1523
|
constructor(eventBus) {
|
|
1275
1524
|
this._eventBus = eventBus;
|
|
1276
1525
|
}
|
|
@@ -1320,24 +1569,24 @@ class ServiceProxy {
|
|
|
1320
1569
|
}
|
|
1321
1570
|
this.serviceIdentifier = serviceIdentifier;
|
|
1322
1571
|
this.serviceRegistry = serviceRegistry;
|
|
1323
|
-
this.tracer =
|
|
1572
|
+
this.tracer = import_api3.default.trace.getTracer(package_default.name, package_default.version);
|
|
1324
1573
|
}
|
|
1325
1574
|
invoke(methodIdentifier, args, scope, eventFactory) {
|
|
1326
1575
|
return this.tracer.startActiveSpan(`${this.serviceIdentifier}/${methodIdentifier}`, {
|
|
1327
|
-
kind:
|
|
1576
|
+
kind: import_api3.SpanKind.CLIENT
|
|
1328
1577
|
}, async (span) => {
|
|
1329
1578
|
if (scope) {
|
|
1330
|
-
span.setAttribute("
|
|
1579
|
+
span.setAttribute("kinotic.scope", scope);
|
|
1331
1580
|
}
|
|
1332
|
-
span.setAttribute("rpc.system", "
|
|
1581
|
+
span.setAttribute("rpc.system", "kinotic");
|
|
1333
1582
|
span.setAttribute("rpc.service", this.serviceIdentifier);
|
|
1334
1583
|
span.setAttribute("rpc.method", methodIdentifier);
|
|
1335
|
-
return this.__invokeStream(false, methodIdentifier, args, scope, eventFactory).pipe(
|
|
1584
|
+
return this.__invokeStream(false, methodIdentifier, args, scope, eventFactory).pipe(import_operators3.first()).toPromise().then(async (value) => {
|
|
1336
1585
|
span.end();
|
|
1337
1586
|
return value;
|
|
1338
1587
|
}, async (ex) => {
|
|
1339
1588
|
span.recordException(ex);
|
|
1340
|
-
span.setStatus({ code:
|
|
1589
|
+
span.setStatus({ code: import_api3.SpanStatusCode.ERROR });
|
|
1341
1590
|
span.end();
|
|
1342
1591
|
throw ex;
|
|
1343
1592
|
});
|
|
@@ -1353,13 +1602,13 @@ class ServiceProxy {
|
|
|
1353
1602
|
eventFactoryToUse = eventFactory;
|
|
1354
1603
|
}
|
|
1355
1604
|
let eventBusToUse = this.serviceRegistry.eventBus;
|
|
1356
|
-
const span =
|
|
1605
|
+
const span = import_api3.default.trace.getActiveSpan();
|
|
1357
1606
|
if (span) {
|
|
1358
1607
|
span.setAttribute(import_semantic_conventions.ATTR_SERVER_ADDRESS, eventBusToUse.serverInfo?.host || "unknown");
|
|
1359
1608
|
span.setAttribute(import_semantic_conventions.ATTR_SERVER_PORT, eventBusToUse.serverInfo?.port || "unknown");
|
|
1360
1609
|
}
|
|
1361
1610
|
let event = eventFactoryToUse.create(cri, args);
|
|
1362
|
-
return eventBusToUse.requestStream(event, sendControlEvents).pipe(
|
|
1611
|
+
return eventBusToUse.requestStream(event, sendControlEvents).pipe(import_operators3.map((value) => {
|
|
1363
1612
|
const contentType = value.getHeader("content-type" /* CONTENT_TYPE_HEADER */);
|
|
1364
1613
|
if (contentType !== undefined) {
|
|
1365
1614
|
if (contentType === "application/json" /* CONTENT_JSON */) {
|
|
@@ -1395,8 +1644,8 @@ class KinoticSingleton {
|
|
|
1395
1644
|
this._eventBus = eventBus;
|
|
1396
1645
|
this.serviceRegistry.eventBus = eventBus;
|
|
1397
1646
|
}
|
|
1398
|
-
connect(
|
|
1399
|
-
return this._eventBus.connect(
|
|
1647
|
+
connect(options) {
|
|
1648
|
+
return this._eventBus.connect(Util.resolveConnectOptions(options));
|
|
1400
1649
|
}
|
|
1401
1650
|
disconnect(force) {
|
|
1402
1651
|
return this._eventBus.disconnect(force);
|
|
@@ -1410,6 +1659,21 @@ class KinoticSingleton {
|
|
|
1410
1659
|
}
|
|
1411
1660
|
}
|
|
1412
1661
|
var Kinotic = new KinoticSingleton;
|
|
1662
|
+
// packages/core/src/api/UiVersionUtil.ts
|
|
1663
|
+
async function checkUiVersion(builtCommit, versionUrl = "/version.json") {
|
|
1664
|
+
if (!builtCommit) {
|
|
1665
|
+
throw new Error("builtCommit is required");
|
|
1666
|
+
}
|
|
1667
|
+
let servedCommit = null;
|
|
1668
|
+
try {
|
|
1669
|
+
const response = await fetch(versionUrl, { cache: "no-store" });
|
|
1670
|
+
if (response.ok) {
|
|
1671
|
+
const body = await response.json();
|
|
1672
|
+
servedCommit = typeof body.commitSha === "string" && body.commitSha.length > 0 ? body.commitSha : null;
|
|
1673
|
+
}
|
|
1674
|
+
} catch {}
|
|
1675
|
+
return { stale: servedCommit !== null && servedCommit !== builtCommit, servedCommit };
|
|
1676
|
+
}
|
|
1413
1677
|
// packages/core/src/api/crud/AbstractIterablePage.ts
|
|
1414
1678
|
class AbstractIterablePage {
|
|
1415
1679
|
pageable;
|
|
@@ -1517,6 +1781,9 @@ class CrudServiceProxy {
|
|
|
1517
1781
|
deleteById(id) {
|
|
1518
1782
|
return this.serviceProxy.invoke("deleteById", [id]);
|
|
1519
1783
|
}
|
|
1784
|
+
deleteByIdSync(id) {
|
|
1785
|
+
return this.serviceProxy.invoke("deleteByIdSync", [id]);
|
|
1786
|
+
}
|
|
1520
1787
|
async findAll(pageable) {
|
|
1521
1788
|
const page = await this.findAllSinglePage(pageable);
|
|
1522
1789
|
return new FindAllIterablePage(pageable, page, this);
|
|
@@ -1639,41 +1906,6 @@ class Order {
|
|
|
1639
1906
|
class Sort {
|
|
1640
1907
|
orders = [];
|
|
1641
1908
|
}
|
|
1642
|
-
// packages/core/src/api/security/AuthenticatedWebSocketFactory.ts
|
|
1643
|
-
function createAuthenticatedWebSocketFactory(serverInfo, authProvider) {
|
|
1644
|
-
const brokerUrl = Util.buildBrokerUrl(serverInfo);
|
|
1645
|
-
return async () => {
|
|
1646
|
-
const headers = await authProvider.getAuthHeaders();
|
|
1647
|
-
const WS = WebSocket;
|
|
1648
|
-
return new WS(brokerUrl, { headers });
|
|
1649
|
-
};
|
|
1650
|
-
}
|
|
1651
|
-
// packages/core/src/api/security/BasicAuthProvider.ts
|
|
1652
|
-
class BasicAuthProvider {
|
|
1653
|
-
authHeader;
|
|
1654
|
-
constructor(login, passcode) {
|
|
1655
|
-
const bytes = new TextEncoder().encode(`${login}:${passcode}`);
|
|
1656
|
-
let binary = "";
|
|
1657
|
-
for (const byte of bytes) {
|
|
1658
|
-
binary += String.fromCharCode(byte);
|
|
1659
|
-
}
|
|
1660
|
-
this.authHeader = `Basic ${btoa(binary)}`;
|
|
1661
|
-
}
|
|
1662
|
-
getAuthHeaders() {
|
|
1663
|
-
return { Authorization: this.authHeader };
|
|
1664
|
-
}
|
|
1665
|
-
}
|
|
1666
|
-
// packages/core/src/api/security/BearerTokenAuthProvider.ts
|
|
1667
|
-
class BearerTokenAuthProvider {
|
|
1668
|
-
token;
|
|
1669
|
-
constructor(token) {
|
|
1670
|
-
this.token = token;
|
|
1671
|
-
}
|
|
1672
|
-
async getAuthHeaders() {
|
|
1673
|
-
const token = typeof this.token === "function" ? await this.token() : this.token;
|
|
1674
|
-
return { Authorization: `Bearer ${token}` };
|
|
1675
|
-
}
|
|
1676
|
-
}
|
|
1677
1909
|
// packages/core/src/api/security/ConnectedInfo.ts
|
|
1678
1910
|
class ConnectedInfo {
|
|
1679
1911
|
replyToId;
|