fix(types,client,server): improve type safety and harden client/server message handling

This commit is contained in:
2026-05-01 11:22:06 +00:00
parent 496fd9a81a
commit 3ac4c2e708
22 changed files with 9276 additions and 3895 deletions
+39 -49
View File
@@ -1,5 +1,4 @@
import * as plugins from './smartuniverse.plugins.js';
import * as pluginsTyped from './smartuniverse.pluginstyped.js';
import { UniverseCache, UniverseChannel, UniverseMessage } from './index.js';
@@ -9,7 +8,6 @@ import { logger } from './smartuniverse.logging.js';
export interface ISmartUniverseConstructorOptions {
messageExpiryInMilliseconds: number;
externalServer?: pluginsTyped.typedserver.servertools.Server;
}
/**
@@ -22,15 +20,10 @@ export class Universe {
// options
private options: ISmartUniverseConstructorOptions;
/**
* the smartexpress server used
*/
private server: pluginsTyped.typedserver.servertools.Server;
/**
* the smartsocket used
*/
private smartsocket: plugins.smartsocket.Smartsocket;
private smartsocket?: plugins.smartsocket.Smartsocket;
constructor(optionsArg: ISmartUniverseConstructorOptions) {
this.options = optionsArg;
@@ -41,7 +34,7 @@ export class Universe {
* stores the version of the universe server running
* this is done since the version is exposed through the api and multiple fs actions are avoided this way.
*/
private universeVersionStore: string;
private universeVersionStore = '';
/**
* get the currently running version of smartuniverse
@@ -77,25 +70,16 @@ export class Universe {
* initiates a server
*/
public async start(portArg?: number) {
if (!this.options.externalServer && !portArg) {
throw new Error(`You supplied an external error. You need to specify a portArg to start on.`);
if (!portArg) {
throw new Error(`You need to specify a portArg to start on.`);
}
portArg = portArg || 3000; // TODO: remove
// add websocket upgrade
this.smartsocket = new plugins.smartsocket.Smartsocket({
alias: 'smartuniverse',
port: portArg,
});
// lets create the base smartexpress server
if (this.options.externalServer) {
console.log('Universe is using externally supplied server');
this.smartsocket.setExternalServer('smartexpress', this.options.externalServer);
}
const socketFunctionSubscription =
new plugins.smartsocket.SocketFunction<interfaces.ISocketRequest_SubscribeChannel>({
funcName: 'subscribeChannel',
@@ -112,36 +96,42 @@ export class Universe {
},
});
const socketFunctionProcessMessage = new plugins.smartsocket.SocketFunction<any>({
// TODO proper ITypedRequest here instead of any
funcName: 'processMessage',
funcDef: async (messageDataArg: interfaces.IUniverseMessage, socketConnectionArg) => {
const universeConnection = UniverseConnection.findUniverseConnectionBySocketConnection(
this.universeCache,
socketConnectionArg
);
if (universeConnection) {
logger.log('ok', 'found UniverseConnection for socket for incoming message');
} else {
logger.log('warn', 'found no Authorized channel for incoming message');
const socketFunctionProcessMessage =
new plugins.smartsocket.SocketFunction<interfaces.ISocketRequest_ProcessMessage>({
funcName: 'processMessage',
funcDef: async (messageDataArg: interfaces.IUniverseMessage, socketConnectionArg) => {
const universeConnection = UniverseConnection.findUniverseConnectionBySocketConnection(
this.universeCache,
socketConnectionArg
);
if (universeConnection) {
logger.log('ok', 'found UniverseConnection for socket for incoming message');
} else {
logger.log('warn', 'found no Authorized channel for incoming message');
return {
messageStatus: 'authentication required',
};
}
const unauthenticatedMessage = UniverseMessage.createMessageFromPayload(
socketConnectionArg,
messageDataArg
);
const foundChannel = await UniverseChannel.authorizeAMessageForAChannel(
this.universeCache,
unauthenticatedMessage
);
if (foundChannel && unauthenticatedMessage.authenticated) {
const authenticatedMessage = unauthenticatedMessage;
await this.universeCache.addMessage(authenticatedMessage);
return {
messageStatus: 'ok',
};
}
return {
error: 'You need to authenticate for a channel',
messageStatus: 'channel not found',
};
}
const unauthenticatedMessage = UniverseMessage.createMessageFromPayload(
socketConnectionArg,
messageDataArg
);
const foundChannel = await UniverseChannel.authorizeAMessageForAChannel(
this.universeCache,
unauthenticatedMessage
);
if (foundChannel && unauthenticatedMessage.authenticated) {
const authenticatedMessage = unauthenticatedMessage;
await this.universeCache.addMessage(authenticatedMessage);
}
},
});
},
});
// add socket functions
this.smartsocket.addSocketFunction(socketFunctionSubscription);
@@ -156,6 +146,6 @@ export class Universe {
* stop everything
*/
public async stopServer() {
await this.smartsocket.stop();
await this.smartsocket?.stop();
}
}