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
+38
View File
@@ -0,0 +1,38 @@
{
"@git.zone/cli": {
"projectType": "npm",
"module": {
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "smartuniverse",
"description": "A messaging service enabling secure, reactive communication between microservices.",
"npmPackagename": "@push.rocks/smartuniverse",
"license": "MIT",
"projectDomain": "push.rocks"
},
"release": {
"registries": [
"https://verdaccio.lossless.digital",
"https://registry.npmjs.org"
],
"accessLevel": "public"
}
},
"@git.zone/tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
},
"@git.zone/tsbundle": {
"bundles": [
{
"from": "./ts/index.ts",
"to": "./dist_bundle/bundle.js",
"outputMode": "bundle",
"bundler": "esbuild"
}
]
},
"@ship.zone/szci": {
"npmGlobalTools": [],
"npmRegistryUrl": "registry.npmjs.org"
}
}
+140
View File
@@ -0,0 +1,140 @@
# Changelog
## 2026-05-01 - 1.0.109 - fix(types,client,server)
improve type safety and harden client/server message handling
- add explicit unsubscribe socket request typing and typed processMessage responses for authenticated, missing-channel, and unauthenticated cases
- guard client channel subscription and message posting when no socket connection is available
- tighten generic message typing and optional state handling across universe, client message, and reaction request classes
- modernize package and tooling configuration, including renamed dependencies, pnpm workspace metadata, and updated node-based tests
## 2024-05-29 - 1.0.108 - maintenance
Repository metadata and TypeScript configuration were updated across the latest release cycle.
- Updated project description
- Updated TypeScript configuration
- Updated `npmextra.json` githost settings
## 2023-07-25 - 1.0.106 - 1.0.107 - core
This version range contains repeated trivial core update fixes with no further detail.
- Applied minor `core` update fixes across versions `1.0.106` and `1.0.107`
## 2020-09-24 - 1.0.98 - 1.0.105 - core
This version range contains repeated trivial core update fixes with no further detail.
- Applied minor `core` update fixes across versions `1.0.98` through `1.0.105`
## 2019-08-13 - 1.0.57 - 1.0.97 - core
This version range contains repeated trivial core update fixes with no further detail.
- Applied minor `core` update fixes across versions `1.0.57` through `1.0.97`
## 2019-06-06 - 1.0.47 - 1.0.56 - core
This version range contains repeated trivial core update fixes with no further detail.
- Applied minor `core` update fixes across versions `1.0.47` through `1.0.56`
## 2019-04-11 - 1.0.25 - 1.0.46 - core
This version range contains repeated trivial core update fixes with no further detail.
- Applied minor `core` update fixes across versions `1.0.25` through `1.0.46`
## 2019-01-30 - 1.0.23 - dependencies
Dependency maintenance updates were made in this release cycle.
- Updated project dependencies
## 2018-05-30 - 1.0.22 - websocket
The websocket infrastructure was reworked.
- Switched to the complete websocket infrastructure
## 2018-05-28 - 1.0.21 - core
Core behavior was updated to support a transparent universe model.
- Updated core to transparent universe handling
## 2018-05-26 - 1.0.20 - UniverseMessage / UniverseChannel
Authentication handling for channel-based messages was improved.
- Improved authentication for messages associated with channels
## 2018-05-24 - 1.0.19 - dependencies
Dependency maintenance updates were made.
- Updated project dependencies
## 2018-05-24 - 1.0.18 - UniverseChannel
Channel handling was improved.
- Improved `UniverseChannel` handling
## 2018-05-23 - 1.0.17 - core
Core channel handling was improved across the release.
- Improved channel handling in core
## 2018-05-23 - 1.0.16 - structure
Project structure and formatting were cleaned up.
- Formatted the TypeScript codebase
## 2018-05-19 - 1.0.15 - core
Release preparation work was completed.
- Prepared core for release
## 2018-05-07 - 1.0.14 - message
The message layer was refactored.
- Refactored message-related functionality
## 2018-04-29 - 1.0.9 - 1.0.13 - maintenance
This version range mainly contains standards, CI, and general maintenance updates.
- Moved the project toward TypeScript predominance
- Updated the project to latest standards
- Updated CI and CI configuration
- Included general maintenance updates with limited detail
## 2018-04-05 - 1.0.8 - documentation
Documentation was improved.
- Added a better README
## 2018-04-05 - 1.0.7 - package
Package publishing configuration was updated.
- Added npm access level configuration
## 2018-04-04 - 1.0.5 - scope
Project package scope was updated.
- Migrated to the new pushrocks scope
## 2018-03-27 - 1.0.4 - networking
Connection lifecycle handling was improved.
- Fixed opening and closing behavior on both server and client sides
## 2018-03-17 - 1.0.3 - messaging
Message delivery and compatibility issues were addressed.
- Fixed RxJS incompatibility
- Corrected message sending behavior
## 2018-03-15 - 1.0.2 - core
Initial runtime functionality was established.
- Got the project running
## 2018-03-08 - 1.0.1 - testing
Initial test setup was updated.
- Updated the initial test
## 2018-03-07 - 1.0.1 - project
Initial project setup.
- Initial commit
+4 -2
View File
@@ -1,4 +1,6 @@
Copyright (c) 2018 Lossless GmbH (hello@lossless.com)
The MIT License (MIT)
Copyright (c) 2026 Task Venture Capital GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -16,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
+14 -7
View File
@@ -1,5 +1,5 @@
{
"gitzone": {
"@git.zone/cli": {
"projectType": "npm",
"module": {
"githost": "code.foss.global",
@@ -21,13 +21,20 @@
"websocket",
"scalability"
]
},
"release": {
"registries": [
"https://verdaccio.lossless.digital",
"https://registry.npmjs.org"
],
"accessLevel": "public"
}
},
"npmci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
},
"tsdoc": {
"@git.zone/tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
},
"@ship.zone/szci": {
"npmGlobalTools": [],
"npmRegistryUrl": "registry.npmjs.org"
}
}
}
+24 -20
View File
@@ -5,35 +5,36 @@
"description": "A messaging service enabling secure, reactive communication between microservices.",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"author": "Lossless GmbH",
"author": "Task Venture Capital GmbH <hello@task.vc>",
"license": "MIT",
"scripts": {
"test": "(tstest test/)",
"testManual": "(tsrun test/test.ts)",
"build": "(tsbuild --allowimplicitany && tsbundle --from ./ts/index.ts --to dist_bundle/bundle.js)",
"format": "(gitzone format)",
"test": "tstest test/ --verbose",
"testManual": "tsrun test/",
"build": "tsbuild && tsbundle",
"format": "gitzone format",
"buildDocs": "tsdoc"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.66",
"@gitzone/tsbundle": "^2.0.8",
"@gitzone/tsrun": "^1.2.44",
"@gitzone/tstest": "^1.0.77",
"@push.rocks/tapbundle": "^5.0.12",
"@types/node": "^20.4.4"
"@git.zone/tsbuild": "^4.4.0",
"@git.zone/tsbundle": "^2.10.1",
"@git.zone/tsrun": "^2.0.3",
"@git.zone/tstest": "^3.6.3",
"@types/lodash.clonedeep": "^4.5.9",
"@types/node": "^25.6.0",
"@types/picomatch": "^4.0.3"
},
"dependencies": {
"@apiglobal/typedrequest-interfaces": "^2.0.1",
"@apiglobal/typedserver": "^2.0.65",
"@api.global/typedrequest-interfaces": "^3.0.19",
"@api.global/typedserver": "^8.4.6",
"@push.rocks/isohash": "^2.0.1",
"@push.rocks/isounique": "^1.0.5",
"@push.rocks/lik": "^6.0.3",
"@push.rocks/lik": "^6.4.1",
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartlog": "^3.0.3",
"@push.rocks/smartpromise": "^4.0.2",
"@push.rocks/smartlog": "^3.2.2",
"@push.rocks/smartpromise": "^4.2.3",
"@push.rocks/smartrx": "^3.0.6",
"@push.rocks/smartsocket": "^2.0.20",
"@push.rocks/smarttime": "^4.0.4",
"@push.rocks/smartsocket": "^4.0.1",
"@push.rocks/smarttime": "^4.2.3",
"@push.rocks/smarturl": "^3.0.7"
},
"files": [
@@ -45,6 +46,8 @@
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
".smartconfig.json",
"license",
"npmextra.json",
"readme.md"
],
@@ -68,5 +71,6 @@
"repository": {
"type": "git",
"url": "https://code.foss.global/push.rocks/smartuniverse.git"
}
}
},
"packageManager": "pnpm@10.28.2"
}
+8935 -3758
View File
File diff suppressed because it is too large Load Diff
+2
View File
@@ -0,0 +1,2 @@
onlyBuiltDependencies:
- esbuild
+2 -3
View File
@@ -1,5 +1,4 @@
// tslint:disable-next-line:no-implicit-dependencies
import { expect, tap } from '@push.rocks/tapbundle';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as smartuniverse from '../ts/index.js';
let testUniverse: smartuniverse.Universe;
@@ -131,4 +130,4 @@ tap.test('should end the server correctly', async (tools) => {
await testUniverse.stopServer();
});
tap.start();
export default tap.start();
+3 -3
View File
@@ -1,8 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
* autocreated commitinfo by @push.rocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartuniverse',
version: '1.0.108',
description: 'messaging service for your micro services'
version: '1.0.109',
description: 'A messaging service enabling secure, reactive communication between microservices.'
}
+7 -1
View File
@@ -15,6 +15,12 @@ export interface ISocketRequest_ProcessMessage {
method: 'processMessage';
request: interfaces.IUniverseMessage;
response: {
messageStatus: 'ok' | 'channel not found';
messageStatus: 'ok' | 'channel not found' | 'authentication required';
};
}
export interface ISocketRequest_Unsubscribe {
method: 'unsubscribe';
request: interfaces.IServerUnsubscribeActionPayload;
response: {};
}
+3 -3
View File
@@ -1,12 +1,12 @@
export interface IMessageCreator {
export interface IMessageCreator<T = any> {
messageText: string;
payload?: string | number | any;
payload?: T;
}
/**
* A universe
*/
export interface IUniverseMessage extends IMessageCreator {
export interface IUniverseMessage<T = any> extends IMessageCreator<T> {
id: string;
/**
* time of creation
+17 -16
View File
@@ -1,5 +1,5 @@
import * as plugins from './smartuniverse.plugins.js';
import { Smartsocket, SmartsocketClient } from '@push.rocks/smartsocket';
import { SmartsocketClient } from '@push.rocks/smartsocket';
import * as interfaces from './interfaces/index.js';
@@ -18,7 +18,7 @@ export interface IClientOptions {
*/
export class ClientUniverse {
public options: IClientOptions;
public smartsocketClient: plugins.smartsocket.SmartsocketClient;
public smartsocketClient?: plugins.smartsocket.SmartsocketClient;
public messageRxjsSubject = new plugins.smartrx.rxjs.Subject<ClientUniverseMessage<any>>();
public clientUniverseCache = new ClientUniverseCache();
@@ -64,7 +64,7 @@ export class ClientUniverse {
* remove a a achannel
* @param messageArg
*/
public removeChannel(channelNameArg, notifyServer = true) {
public removeChannel(channelNameArg: string, notifyServer = true) {
const clientUniverseChannel = this.clientUniverseCache.channelMap.findOneAndRemoveSync(
(channelItemArg) => {
return channelItemArg.name === channelNameArg;
@@ -110,18 +110,19 @@ export class ClientUniverse {
/**
* should handle a forced unsubscription by the server
*/
const socketFunctionUnsubscribe = new plugins.smartsocket.SocketFunction({
funcName: 'unsubscribe',
funcDef: async (dataArg: interfaces.IServerUnsubscribeActionPayload) => {
const channel = this.clientUniverseCache.channelMap.findSync((channelArg) => {
return channelArg.name === dataArg.name;
});
if (channel) {
channel.unsubscribe();
}
return {};
},
});
const socketFunctionUnsubscribe =
new plugins.smartsocket.SocketFunction<interfaces.ISocketRequest_Unsubscribe>({
funcName: 'unsubscribe',
funcDef: async (dataArg) => {
const channel = this.clientUniverseCache.channelMap.findSync((channelArg) => {
return channelArg.name === dataArg.name;
});
if (channel) {
channel.unsubscribe();
}
return {};
},
});
/**
* handles message reception
@@ -169,7 +170,7 @@ export class ClientUniverse {
const instructDisconnect = async () => {
if (this.smartsocketClient) {
const smartsocketToDisconnect = this.smartsocketClient;
this.smartsocketClient = null; // making sure the upstreamEvent does not interfere
this.smartsocketClient = undefined; // making sure the upstreamEvent does not interfere
await smartsocketToDisconnect.disconnect();
}
};
@@ -69,8 +69,12 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
public async populateSubscriptionToServer() {
// lets make sure the channel is connected
if (this.status === 'unsubscribed') {
const smartsocketClient = this.clientUniverseRef.smartsocketClient;
if (!smartsocketClient) {
throw new Error('Cannot subscribe channel before the smartuniverse client is connected.');
}
const response =
await this.clientUniverseRef.smartsocketClient.serverCall<interfaces.ISocketRequest_SubscribeChannel>(
await smartsocketClient.serverCall<interfaces.ISocketRequest_SubscribeChannel>(
'subscribeChannel',
{
name: this.name,
@@ -91,6 +95,10 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
*/
public async postMessage(messageArg: interfaces.IMessageCreator) {
await this.clientUniverseRef.start(); // its ok to call this multiple times
const smartsocketClient = this.clientUniverseRef.smartsocketClient;
if (!smartsocketClient) {
throw new Error('Cannot post message before the smartuniverse client is connected.');
}
const universeMessageToSend: interfaces.IUniverseMessage = {
id: plugins.isounique.uni(),
timestamp: Date.now(),
@@ -99,7 +107,7 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
messageText: messageArg.messageText,
payload: messageArg.payload,
};
await this.clientUniverseRef.smartsocketClient.serverCall(
await smartsocketClient.serverCall<interfaces.ISocketRequest_ProcessMessage>(
'processMessage',
universeMessageToSend
);
@@ -2,12 +2,12 @@ import * as plugins from './smartuniverse.plugins.js';
import * as interfaces from './interfaces/index.js';
export class ClientUniverseMessage<T> implements interfaces.IUniverseMessage {
export class ClientUniverseMessage<T = any> implements interfaces.IUniverseMessage<T> {
// ======
// STATIC
// ======
public static createMessageFromMessageDescriptor(messageDescriptor: interfaces.IUniverseMessage) {
const clientuniverseMessage = new ClientUniverseMessage(messageDescriptor);
public static createMessageFromMessageDescriptor<T = any>(messageDescriptor: interfaces.IUniverseMessage<T>) {
const clientuniverseMessage = new ClientUniverseMessage<T>(messageDescriptor);
return clientuniverseMessage;
}
@@ -25,10 +25,14 @@ export class ClientUniverseMessage<T> implements interfaces.IUniverseMessage {
public payload: T;
public targetChannelName: string;
constructor(messageArg: interfaces.IUniverseMessage) {
for (const key of Object.keys(messageArg)) {
this[key] = messageArg[key];
}
constructor(messageArg: interfaces.IUniverseMessage<T>) {
this.id = messageArg.id;
this.timestamp = messageArg.timestamp;
this.smartTimestamp = new plugins.smarttime.TimeStamp(this.timestamp);
this.messageText = messageArg.messageText;
this.passphrase = messageArg.passphrase;
this.payload = messageArg.payload as T;
this.targetChannelName = messageArg.targetChannelName;
}
/**
@@ -19,7 +19,7 @@ export interface ICombinatorPayload<T extends plugins.typedrequestInterfaces.ITy
typedRequestPayload: {
method: T['method'];
request: T['request'];
response: T['response'];
response: T['response'] | null;
};
}
@@ -54,7 +54,9 @@ export class ReactionRequest<T extends plugins.typedrequestInterfaces.ITypedRequ
if (payload.id !== requestId) {
return;
}
reactionResult.pushReactionResponse(payload.typedRequestPayload.response);
if (payload.typedRequestPayload.response !== null) {
reactionResult.pushReactionResponse(payload.typedRequestPayload.response);
}
}
}
)
+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();
}
}
+1 -1
View File
@@ -54,7 +54,7 @@ export class UniverseChannel {
public static authorizeAMessageForAChannel(
universeCacheArg: UniverseCache,
universeMessageArg: UniverseMessage<any>
): UniverseChannel {
): UniverseChannel | null {
const foundChannel = universeCacheArg.channelMap.findSync((universeChannel) => {
const result = universeChannel.authenticate(universeMessageArg);
return result;
@@ -36,7 +36,7 @@ export class UniverseConnection {
universeCache: UniverseCache,
universeConnectionArg: UniverseConnection
): Promise<UniverseConnection> {
let connectionToReturn: UniverseConnection;
let connectionToReturn: UniverseConnection | undefined;
universeCache.connectionMap.forEach(async (existingConnection) => {
if (existingConnection.socketConnection === universeConnectionArg.socketConnection) {
connectionToReturn = await this.mergeUniverseConnections(
@@ -63,7 +63,7 @@ export class UniverseConnection {
universeRef,
authenticationRequest.name
);
if (universeChannelToAuthenticateAgainst.passphrase === authenticationRequest.passphrase) {
if (universeChannelToAuthenticateAgainst?.passphrase === authenticationRequest.passphrase) {
universeConnectionArg.authenticatedChannels.push(universeChannelToAuthenticateAgainst);
}
}
@@ -86,7 +86,7 @@ export class UniverseConnection {
public static findUniverseConnectionBySocketConnection(
universeCache: UniverseCache,
socketConnectionArg: plugins.smartsocket.SocketConnection
): UniverseConnection {
): UniverseConnection | undefined {
const universeConnection = universeCache.connectionMap.findSync((universeConnectionArg) => {
return universeConnectionArg.socketConnection === socketConnectionArg;
});
+12 -11
View File
@@ -1,6 +1,5 @@
import * as plugins from './smartuniverse.plugins.js';
import * as interfaces from './interfaces/index.js';
import { Universe } from './smartuniverse.classes.universe.js';
import { UniverseChannel } from './smartuniverse.classes.universechannel.js';
import { UniverseCache } from './smartuniverse.classes.universecache.js';
import { SocketConnection } from '@push.rocks/smartsocket';
@@ -10,12 +9,12 @@ import { logger } from './smartuniverse.logging.js';
* represents a message within a universe
* acts as a container to save message states like authentication status
*/
export class UniverseMessage<T> implements interfaces.IUniverseMessage {
public static createMessageFromPayload(
export class UniverseMessage<T = any> implements interfaces.IUniverseMessage<T> {
public static createMessageFromPayload<T = any>(
socketConnectionArg: SocketConnection,
dataArg: interfaces.IUniverseMessage
dataArg: interfaces.IUniverseMessage<T>
) {
const universeMessageInstance = new UniverseMessage(dataArg);
const universeMessageInstance = new UniverseMessage<T>(dataArg);
universeMessageInstance.socketConnection = socketConnectionArg;
return universeMessageInstance;
}
@@ -27,12 +26,12 @@ export class UniverseMessage<T> implements interfaces.IUniverseMessage {
public passphrase: string;
public payload: T;
public targetChannelName: string;
public socketConnection: SocketConnection;
public socketConnection?: SocketConnection;
/**
* the UniverseCache the message is attached to
*/
public universeCache: UniverseCache;
public universeCache?: UniverseCache;
/**
* enables unprotected grouping of messages for efficiency purposes.
@@ -47,19 +46,21 @@ export class UniverseMessage<T> implements interfaces.IUniverseMessage {
/**
* a destruction timer for this message
*/
public destructionTimer: plugins.smarttime.Timer; // a timer to take care of message destruction
public destructionTimer?: plugins.smarttime.Timer; // a timer to take care of message destruction
/**
* the constructor to create a universe message
* @param messageArg
* @param attachedPayloadArg
*/
constructor(messageDescriptor: interfaces.IUniverseMessage) {
constructor(messageDescriptor: interfaces.IUniverseMessage<T>) {
this.id = messageDescriptor.id;
this.timestamp = messageDescriptor.timestamp;
this.smartTimestamp = new plugins.smarttime.TimeStamp(this.timestamp);
this.messageText = messageDescriptor.messageText;
this.targetChannelName = messageDescriptor.targetChannelName;
this.passphrase = messageDescriptor.passphrase;
this.payload = messageDescriptor.payload;
this.payload = messageDescriptor.payload as T;
// prevent memory issues
this.setDestructionTimer();
}
@@ -77,7 +78,7 @@ export class UniverseMessage<T> implements interfaces.IUniverseMessage {
// set up self destruction by removing this from the parent messageCache
this.destructionTimer.completed
.then(async () => {
this.universeCache.messageMap.remove(this);
this.universeCache?.messageMap.remove(this);
})
.catch((err) => {
console.log(err);
+1 -1
View File
@@ -1,5 +1,5 @@
// apiglobal scope
import * as typedrequestInterfaces from '@apiglobal/typedrequest-interfaces';
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
export { typedrequestInterfaces };
+2 -2
View File
@@ -1,5 +1,5 @@
import type * as typedserver from '@apiglobal/typedserver';
import type * as typedserver from '@api.global/typedserver';
export type {
typedserver
};
};
+4 -4
View File
@@ -5,10 +5,10 @@
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noImplicitAny": true,
"esModuleInterop": true,
"verbatimModuleSyntax": true
"verbatimModuleSyntax": true,
"types": ["node"]
},
"exclude": [
"dist_*/**/*.d.ts"
]
"exclude": ["dist_*/**/*.d.ts"]
}