@principal-ai/control-tower-core 0.1.9 → 0.1.11
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/dist/adapters/websocket/WebSocketClientTransportAdapter.d.ts +78 -0
- package/dist/adapters/websocket/WebSocketClientTransportAdapter.d.ts.map +1 -0
- package/dist/adapters/websocket/WebSocketClientTransportAdapter.js +227 -0
- package/dist/adapters/websocket/WebSocketServerTransportAdapter.d.ts +60 -0
- package/dist/adapters/websocket/WebSocketServerTransportAdapter.d.ts.map +1 -0
- package/dist/adapters/websocket/WebSocketServerTransportAdapter.js +386 -0
- package/dist/adapters/websocket/index.d.ts +2 -1
- package/dist/adapters/websocket/index.d.ts.map +1 -1
- package/dist/adapters/websocket/index.js +5 -3
- package/dist/client/BaseClient.d.ts.map +1 -1
- package/dist/client/BaseClient.js +4 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +6 -5
- package/dist/index.mjs +188 -4
- package/dist/index.mjs.map +6 -5
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -3927,8 +3927,8 @@ var import_sender = __toESM(require_sender(), 1);
|
|
|
3927
3927
|
var import_websocket = __toESM(require_websocket(), 1);
|
|
3928
3928
|
var import_websocket_server = __toESM(require_websocket_server(), 1);
|
|
3929
3929
|
|
|
3930
|
-
// src/adapters/websocket/
|
|
3931
|
-
class
|
|
3930
|
+
// src/adapters/websocket/WebSocketServerTransportAdapter.ts
|
|
3931
|
+
class WebSocketServerTransportAdapter {
|
|
3932
3932
|
state = "disconnected";
|
|
3933
3933
|
messageHandlers = new Set;
|
|
3934
3934
|
errorHandlers = new Set;
|
|
@@ -4275,6 +4275,186 @@ class WebSocketTransportAdapter {
|
|
|
4275
4275
|
return Math.random().toString(36).substring(2) + Date.now().toString(36);
|
|
4276
4276
|
}
|
|
4277
4277
|
}
|
|
4278
|
+
// src/adapters/websocket/WebSocketClientTransportAdapter.ts
|
|
4279
|
+
class WebSocketClientTransportAdapter {
|
|
4280
|
+
ws;
|
|
4281
|
+
state = "disconnected";
|
|
4282
|
+
messageHandlers = new Set;
|
|
4283
|
+
errorHandlers = new Set;
|
|
4284
|
+
closeHandlers = new Set;
|
|
4285
|
+
config;
|
|
4286
|
+
heartbeatTimer;
|
|
4287
|
+
connectionTimer;
|
|
4288
|
+
constructor(config) {
|
|
4289
|
+
this.config = {
|
|
4290
|
+
headers: config?.headers ?? {},
|
|
4291
|
+
connectionTimeout: config?.connectionTimeout ?? 30000,
|
|
4292
|
+
enableHeartbeat: config?.enableHeartbeat ?? true,
|
|
4293
|
+
heartbeatInterval: config?.heartbeatInterval ?? 30000
|
|
4294
|
+
};
|
|
4295
|
+
}
|
|
4296
|
+
async connect(url, options) {
|
|
4297
|
+
if (this.state === "connected" || this.state === "connecting") {
|
|
4298
|
+
throw new Error("Already connected or connecting");
|
|
4299
|
+
}
|
|
4300
|
+
this.state = "connecting";
|
|
4301
|
+
return new Promise((resolve, reject) => {
|
|
4302
|
+
try {
|
|
4303
|
+
const wsOptions = {};
|
|
4304
|
+
if (Object.keys(this.config.headers).length > 0) {
|
|
4305
|
+
wsOptions.headers = this.config.headers;
|
|
4306
|
+
}
|
|
4307
|
+
this.ws = new import_websocket.default(url, wsOptions);
|
|
4308
|
+
this.connectionTimer = setTimeout(() => {
|
|
4309
|
+
if (this.state === "connecting") {
|
|
4310
|
+
this.ws?.terminate();
|
|
4311
|
+
this.state = "disconnected";
|
|
4312
|
+
const error = new Error("Connection timeout");
|
|
4313
|
+
this.errorHandlers.forEach((handler) => handler(error));
|
|
4314
|
+
reject(error);
|
|
4315
|
+
}
|
|
4316
|
+
}, this.config.connectionTimeout);
|
|
4317
|
+
this.ws.on("open", () => {
|
|
4318
|
+
if (this.connectionTimer) {
|
|
4319
|
+
clearTimeout(this.connectionTimer);
|
|
4320
|
+
this.connectionTimer = undefined;
|
|
4321
|
+
}
|
|
4322
|
+
this.state = "connected";
|
|
4323
|
+
if (this.config.enableHeartbeat) {
|
|
4324
|
+
this.startHeartbeat();
|
|
4325
|
+
}
|
|
4326
|
+
resolve();
|
|
4327
|
+
});
|
|
4328
|
+
this.ws.on("message", (data) => {
|
|
4329
|
+
try {
|
|
4330
|
+
const message = JSON.parse(data.toString());
|
|
4331
|
+
this.messageHandlers.forEach((handler) => handler(message));
|
|
4332
|
+
} catch (error) {
|
|
4333
|
+
this.errorHandlers.forEach((handler) => handler(error));
|
|
4334
|
+
}
|
|
4335
|
+
});
|
|
4336
|
+
this.ws.on("error", (error) => {
|
|
4337
|
+
if (this.connectionTimer) {
|
|
4338
|
+
clearTimeout(this.connectionTimer);
|
|
4339
|
+
this.connectionTimer = undefined;
|
|
4340
|
+
}
|
|
4341
|
+
this.errorHandlers.forEach((handler) => handler(error));
|
|
4342
|
+
if (this.state === "connecting") {
|
|
4343
|
+
this.state = "disconnected";
|
|
4344
|
+
reject(error);
|
|
4345
|
+
}
|
|
4346
|
+
});
|
|
4347
|
+
this.ws.on("close", (code, reason) => {
|
|
4348
|
+
if (this.connectionTimer) {
|
|
4349
|
+
clearTimeout(this.connectionTimer);
|
|
4350
|
+
this.connectionTimer = undefined;
|
|
4351
|
+
}
|
|
4352
|
+
this.stopHeartbeat();
|
|
4353
|
+
const wasConnected = this.state === "connected";
|
|
4354
|
+
this.state = "disconnected";
|
|
4355
|
+
if (wasConnected) {
|
|
4356
|
+
this.closeHandlers.forEach((handler) => handler(code, reason.toString()));
|
|
4357
|
+
}
|
|
4358
|
+
});
|
|
4359
|
+
if (this.config.enableHeartbeat) {
|
|
4360
|
+
this.ws.on("pong", () => {});
|
|
4361
|
+
}
|
|
4362
|
+
} catch (error) {
|
|
4363
|
+
if (this.connectionTimer) {
|
|
4364
|
+
clearTimeout(this.connectionTimer);
|
|
4365
|
+
this.connectionTimer = undefined;
|
|
4366
|
+
}
|
|
4367
|
+
this.state = "disconnected";
|
|
4368
|
+
reject(error);
|
|
4369
|
+
}
|
|
4370
|
+
});
|
|
4371
|
+
}
|
|
4372
|
+
async disconnect() {
|
|
4373
|
+
if (this.state === "disconnected") {
|
|
4374
|
+
return;
|
|
4375
|
+
}
|
|
4376
|
+
this.state = "disconnecting";
|
|
4377
|
+
this.stopHeartbeat();
|
|
4378
|
+
if (this.connectionTimer) {
|
|
4379
|
+
clearTimeout(this.connectionTimer);
|
|
4380
|
+
this.connectionTimer = undefined;
|
|
4381
|
+
}
|
|
4382
|
+
if (this.ws) {
|
|
4383
|
+
this.ws.close(1000, "Client disconnecting");
|
|
4384
|
+
await new Promise((resolve) => {
|
|
4385
|
+
const timeout = setTimeout(() => {
|
|
4386
|
+
this.ws?.terminate();
|
|
4387
|
+
resolve();
|
|
4388
|
+
}, 5000);
|
|
4389
|
+
const onClose = () => {
|
|
4390
|
+
clearTimeout(timeout);
|
|
4391
|
+
resolve();
|
|
4392
|
+
};
|
|
4393
|
+
this.ws?.once("close", onClose);
|
|
4394
|
+
});
|
|
4395
|
+
this.ws = undefined;
|
|
4396
|
+
}
|
|
4397
|
+
this.state = "disconnected";
|
|
4398
|
+
}
|
|
4399
|
+
async send(message) {
|
|
4400
|
+
if (this.state !== "connected") {
|
|
4401
|
+
throw new Error("Not connected");
|
|
4402
|
+
}
|
|
4403
|
+
if (!this.ws || this.ws.readyState !== import_websocket.default.OPEN) {
|
|
4404
|
+
throw new Error("WebSocket connection is not open");
|
|
4405
|
+
}
|
|
4406
|
+
try {
|
|
4407
|
+
const data = JSON.stringify(message);
|
|
4408
|
+
this.ws.send(data);
|
|
4409
|
+
} catch (error) {
|
|
4410
|
+
this.errorHandlers.forEach((handler) => handler(error));
|
|
4411
|
+
throw error;
|
|
4412
|
+
}
|
|
4413
|
+
}
|
|
4414
|
+
onMessage(handler) {
|
|
4415
|
+
this.messageHandlers.add(handler);
|
|
4416
|
+
}
|
|
4417
|
+
onError(handler) {
|
|
4418
|
+
this.errorHandlers.add(handler);
|
|
4419
|
+
}
|
|
4420
|
+
onClose(handler) {
|
|
4421
|
+
this.closeHandlers.add(handler);
|
|
4422
|
+
}
|
|
4423
|
+
getState() {
|
|
4424
|
+
return this.state;
|
|
4425
|
+
}
|
|
4426
|
+
isConnected() {
|
|
4427
|
+
return this.state === "connected" && this.ws?.readyState === import_websocket.default.OPEN;
|
|
4428
|
+
}
|
|
4429
|
+
getWebSocket() {
|
|
4430
|
+
return this.ws;
|
|
4431
|
+
}
|
|
4432
|
+
setHeaders(headers) {
|
|
4433
|
+
if (this.state !== "disconnected") {
|
|
4434
|
+
throw new Error("Cannot set headers while connected");
|
|
4435
|
+
}
|
|
4436
|
+
this.config.headers = { ...this.config.headers, ...headers };
|
|
4437
|
+
}
|
|
4438
|
+
setAuthToken(token) {
|
|
4439
|
+
this.setHeaders({ Authorization: `Bearer ${token}` });
|
|
4440
|
+
}
|
|
4441
|
+
startHeartbeat() {
|
|
4442
|
+
if (this.heartbeatTimer) {
|
|
4443
|
+
clearInterval(this.heartbeatTimer);
|
|
4444
|
+
}
|
|
4445
|
+
this.heartbeatTimer = setInterval(() => {
|
|
4446
|
+
if (this.ws && this.ws.readyState === import_websocket.default.OPEN) {
|
|
4447
|
+
this.ws.ping();
|
|
4448
|
+
}
|
|
4449
|
+
}, this.config.heartbeatInterval);
|
|
4450
|
+
}
|
|
4451
|
+
stopHeartbeat() {
|
|
4452
|
+
if (this.heartbeatTimer) {
|
|
4453
|
+
clearInterval(this.heartbeatTimer);
|
|
4454
|
+
this.heartbeatTimer = undefined;
|
|
4455
|
+
}
|
|
4456
|
+
}
|
|
4457
|
+
}
|
|
4278
4458
|
// src/client/BaseClient.ts
|
|
4279
4459
|
class BaseClient extends TypedEventEmitter {
|
|
4280
4460
|
transport;
|
|
@@ -4313,6 +4493,9 @@ class BaseClient extends TypedEventEmitter {
|
|
|
4313
4493
|
if (authResult.success && authResult.token) {
|
|
4314
4494
|
this.authToken = authResult.token;
|
|
4315
4495
|
this.userId = authResult.user?.userId || authResult.userId || "";
|
|
4496
|
+
if (typeof this.transport.setAuthToken === "function") {
|
|
4497
|
+
this.transport.setAuthToken(authResult.token);
|
|
4498
|
+
}
|
|
4316
4499
|
}
|
|
4317
4500
|
}
|
|
4318
4501
|
const options = {
|
|
@@ -5408,7 +5591,8 @@ class ServerBuilder {
|
|
|
5408
5591
|
}
|
|
5409
5592
|
}
|
|
5410
5593
|
export {
|
|
5411
|
-
|
|
5594
|
+
WebSocketServerTransportAdapter,
|
|
5595
|
+
WebSocketClientTransportAdapter,
|
|
5412
5596
|
TypedEventEmitter,
|
|
5413
5597
|
ServerBuilder,
|
|
5414
5598
|
RoomManager,
|
|
@@ -5427,4 +5611,4 @@ export {
|
|
|
5427
5611
|
BaseClient
|
|
5428
5612
|
};
|
|
5429
5613
|
|
|
5430
|
-
//# debugId=
|
|
5614
|
+
//# debugId=FF090C02C3545A4B64756E2164756E21
|