fix(build): tighten TypeScript compatibility and update project build configuration
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartsocket',
|
||||
version: '4.0.0',
|
||||
version: '4.0.1',
|
||||
description: 'Provides easy and secure websocket communication mechanisms, including server and client implementation, function call routing, connection management, and tagging.'
|
||||
}
|
||||
|
||||
@@ -28,10 +28,10 @@ export class SmartsocketClient {
|
||||
public shortId = plugins.isounique.uni();
|
||||
|
||||
// the shortId of the remote we connect to
|
||||
public remoteShortId: string = null;
|
||||
public remoteShortId: string | null = null;
|
||||
|
||||
public alias: string;
|
||||
public socketConnection: SocketConnection;
|
||||
public socketConnection?: SocketConnection;
|
||||
public serverUrl: string;
|
||||
public serverPort: number;
|
||||
public autoReconnect: boolean;
|
||||
@@ -50,7 +50,7 @@ export class SmartsocketClient {
|
||||
|
||||
// tagStore
|
||||
private tagStore: { [key: string]: interfaces.ITag } = {};
|
||||
private tagStoreSubscription: plugins.smartrx.rxjs.Subscription;
|
||||
private tagStoreSubscription?: plugins.smartrx.rxjs.Subscription;
|
||||
|
||||
/**
|
||||
* adds a tag to a connection
|
||||
@@ -86,7 +86,7 @@ export class SmartsocketClient {
|
||||
this.alias = optionsArg.alias;
|
||||
this.serverUrl = optionsArg.url;
|
||||
this.serverPort = optionsArg.port;
|
||||
this.autoReconnect = optionsArg.autoReconnect;
|
||||
this.autoReconnect = optionsArg.autoReconnect ?? false;
|
||||
this.maxRetries = optionsArg.maxRetries ?? 100; // Default to 100 retries
|
||||
this.initialBackoffDelay = optionsArg.initialBackoffDelay ?? 1000; // Default to 1 second
|
||||
this.maxBackoffDelay = optionsArg.maxBackoffDelay ?? 60000; // Default to 1 minute
|
||||
@@ -148,6 +148,7 @@ export class SmartsocketClient {
|
||||
smartsocketHost: this,
|
||||
socket: socket as any,
|
||||
});
|
||||
const socketConnection = this.socketConnection;
|
||||
|
||||
// Increment attempt ID to invalidate any pending timers from previous attempts
|
||||
this.connectionAttemptId++;
|
||||
@@ -183,7 +184,7 @@ export class SmartsocketClient {
|
||||
this.remoteShortId = authRequestPayload.serverAlias;
|
||||
|
||||
// Send authentication data
|
||||
this.socketConnection.sendMessage({
|
||||
socketConnection.sendMessage({
|
||||
type: 'auth',
|
||||
payload: { alias: this.alias },
|
||||
});
|
||||
@@ -193,7 +194,7 @@ export class SmartsocketClient {
|
||||
const authResponse = message.payload as interfaces.IAuthResponsePayload;
|
||||
if (authResponse.success) {
|
||||
logger.log('info', 'client is authenticated');
|
||||
this.socketConnection.authenticated = true;
|
||||
socketConnection.authenticated = true;
|
||||
} else {
|
||||
logger.log('warn', `authentication failed: ${authResponse.error}`);
|
||||
await this.disconnect();
|
||||
@@ -202,15 +203,15 @@ export class SmartsocketClient {
|
||||
|
||||
case 'serverReady':
|
||||
// Set up function request listening
|
||||
await this.socketConnection.listenToFunctionRequests();
|
||||
await socketConnection.listenToFunctionRequests();
|
||||
|
||||
// Handle retagging
|
||||
const oldTagStore = this.tagStore;
|
||||
this.tagStoreSubscription?.unsubscribe();
|
||||
for (const keyArg of Object.keys(this.tagStore)) {
|
||||
this.socketConnection.addTag(this.tagStore[keyArg]);
|
||||
socketConnection.addTag(this.tagStore[keyArg]);
|
||||
}
|
||||
this.tagStoreSubscription = this.socketConnection.tagStoreObservable.subscribe(
|
||||
this.tagStoreSubscription = socketConnection.tagStoreObservable.subscribe(
|
||||
(tagStoreArg) => {
|
||||
this.tagStore = tagStoreArg;
|
||||
}
|
||||
@@ -226,7 +227,7 @@ export class SmartsocketClient {
|
||||
|
||||
default:
|
||||
// Other messages are handled by SocketConnection
|
||||
this.socketConnection.handleMessage(message);
|
||||
socketConnection.handleMessage(message);
|
||||
break;
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -341,6 +342,9 @@ export class SmartsocketClient {
|
||||
functionNameArg: T['method'],
|
||||
dataArg: T['request']
|
||||
): Promise<T['response']> {
|
||||
if (!this.socketConnection) {
|
||||
throw new Error('Cannot call server without an active socket connection');
|
||||
}
|
||||
const socketRequest = new SocketRequest<T>(this, {
|
||||
side: 'requesting',
|
||||
originSocketConnection: this.socketConnection,
|
||||
|
||||
@@ -22,7 +22,7 @@ export type TSocketConnectionSide = 'server' | 'client';
|
||||
* interface for constructor of class SocketConnection
|
||||
*/
|
||||
export interface ISocketConnectionConstructorOptions {
|
||||
alias: string;
|
||||
alias?: string;
|
||||
authenticated: boolean;
|
||||
side: TSocketConnectionSide;
|
||||
smartsocketHost: Smartsocket | SmartsocketClient;
|
||||
@@ -43,7 +43,7 @@ export let allSocketConnections = new plugins.lik.ObjectMap<SocketConnection>();
|
||||
* class SocketConnection represents a websocket connection
|
||||
*/
|
||||
export class SocketConnection {
|
||||
public alias: string;
|
||||
public alias?: string;
|
||||
public side: TSocketConnectionSide;
|
||||
public authenticated: boolean = false;
|
||||
public smartsocketRef: Smartsocket | SmartsocketClient;
|
||||
@@ -97,6 +97,10 @@ export class SocketConnection {
|
||||
}
|
||||
|
||||
private handleFunctionCall(messageData: interfaces.ISocketMessage<interfaces.IFunctionCallPayload>): void {
|
||||
if (!messageData.id) {
|
||||
logger.log('warn', 'received function call without request id');
|
||||
return;
|
||||
}
|
||||
const requestData: ISocketRequestDataObject<any> = {
|
||||
funcCallData: {
|
||||
funcName: messageData.payload.funcName,
|
||||
@@ -124,6 +128,10 @@ export class SocketConnection {
|
||||
}
|
||||
|
||||
private handleFunctionResponse(messageData: interfaces.ISocketMessage<interfaces.IFunctionCallPayload>): void {
|
||||
if (!messageData.id) {
|
||||
logger.log('warn', 'received function response without request id');
|
||||
return;
|
||||
}
|
||||
const responseData: ISocketRequestDataObject<any> = {
|
||||
funcCallData: {
|
||||
funcName: messageData.payload.funcName,
|
||||
|
||||
@@ -27,7 +27,7 @@ export interface ISocketRequestConstructorOptions<
|
||||
side: TSocketRequestSide;
|
||||
originSocketConnection: SocketConnection;
|
||||
shortId: string;
|
||||
funcCallData?: ISocketFunctionCallDataRequest<T>;
|
||||
funcCallData: ISocketFunctionCallDataRequest<T>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,8 +11,8 @@ import { logger } from './smartsocket.logging.js';
|
||||
*/
|
||||
export class SocketServer {
|
||||
private smartsocket: Smartsocket;
|
||||
private httpServer: pluginsTyped.http.Server | pluginsTyped.https.Server;
|
||||
private wsServer: pluginsTyped.ws.WebSocketServer;
|
||||
private httpServer: pluginsTyped.http.Server | pluginsTyped.https.Server | null = null;
|
||||
private wsServer: pluginsTyped.ws.WebSocketServer | null = null;
|
||||
|
||||
/**
|
||||
* whether httpServer is standalone (created by us)
|
||||
@@ -38,17 +38,19 @@ export class SocketServer {
|
||||
const httpModule = await this.smartsocket.smartenv.getSafeNodeModule('http');
|
||||
const wsModule = await this.smartsocket.smartenv.getSafeNodeModule('ws');
|
||||
|
||||
this.httpServer = httpModule.createServer();
|
||||
const httpServer = httpModule.createServer();
|
||||
this.httpServer = httpServer;
|
||||
this.standaloneServer = true;
|
||||
|
||||
// Create WebSocket server attached to HTTP server
|
||||
this.wsServer = new wsModule.WebSocketServer({ server: this.httpServer });
|
||||
const wsServer = new wsModule.WebSocketServer({ server: httpServer });
|
||||
this.wsServer = wsServer;
|
||||
|
||||
this.wsServer.on('connection', (ws: pluginsTyped.ws.WebSocket) => {
|
||||
wsServer.on('connection', (ws: pluginsTyped.ws.WebSocket) => {
|
||||
this.smartsocket.handleNewConnection(ws);
|
||||
});
|
||||
|
||||
this.httpServer.listen(this.smartsocket.options.port, () => {
|
||||
httpServer.listen(this.smartsocket.options.port, () => {
|
||||
logger.log(
|
||||
'success',
|
||||
`Server started in standalone mode on port ${this.smartsocket.options.port}`
|
||||
|
||||
Reference in New Issue
Block a user