22// The ws package is required and provides the WebSocket implementation
33
44import { EventEmitter } from 'events' ;
5- import { DataStreamResp } from './types' ;
5+ import { DataStreamResp , DataStreamBaseReq } from './types' ;
66import { Logger } from '../logger' ;
7+ import type WebSocket from 'ws' ;
78
89// Dynamic imports to avoid webpack issues
910const createWebSocket = ( ) => {
@@ -37,12 +38,12 @@ const MAX_RECONNECT_DELAY = 30 * 1000; // 30 seconds
3738 * MessageHandlerFunc is a function type for handling incoming datastream messages
3839 * Expects parsed DataStreamResp messages
3940 */
40- export type MessageHandlerFunc = ( ctx : any , message : DataStreamResp ) => Promise < void > ;
41+ export type MessageHandlerFunc = ( message : DataStreamResp ) => Promise < void > ;
4142
4243/**
4344 * ConnectionReadyHandlerFunc is a function type for functions that need to be called before connection is considered ready
4445 */
45- export type ConnectionReadyHandlerFunc = ( ctx : any ) => Promise < void > ;
46+ export type ConnectionReadyHandlerFunc = ( ) => Promise < void > ;
4647
4748/**
4849 * ClientOptions contains configuration for the datastream client
@@ -74,7 +75,7 @@ export interface ClientOptions {
7475 * https://custom.example.com -> wss://custom.example.com/datastream
7576 * http://localhost:8080 -> ws://localhost:8080/datastream
7677 */
77- function convertAPIURLToWebSocketURL ( apiURL : string ) : any {
78+ function convertAPIURLToWebSocketURL ( apiURL : string ) : string {
7879 const URLClass = createURL ( ) ;
7980 const parsedURL = new URLClass ( apiURL ) ;
8081
@@ -110,7 +111,7 @@ function convertAPIURLToWebSocketURL(apiURL: string): any {
110111 */
111112export class DatastreamWSClient extends EventEmitter {
112113 // Configuration
113- private readonly url : any ;
114+ private readonly url : string ;
114115 private readonly headers : Record < string , string > ;
115116 private readonly logger : Logger ;
116117 private readonly messageHandler : MessageHandlerFunc ;
@@ -120,7 +121,7 @@ export class DatastreamWSClient extends EventEmitter {
120121 private readonly maxReconnectDelay : number ;
121122
122123 // Connection state
123- private ws ?: any ;
124+ private ws ?: WebSocket ;
124125 private connected : boolean = false ;
125126 private ready : boolean = false ;
126127
@@ -131,9 +132,6 @@ export class DatastreamWSClient extends EventEmitter {
131132 private pongTimeout ?: NodeJS . Timeout ;
132133 private reconnectTimeout ?: NodeJS . Timeout ;
133134
134- // Context
135- private ctx : any = { } ;
136-
137135 constructor ( options : ClientOptions ) {
138136 super ( ) ;
139137
@@ -153,8 +151,7 @@ export class DatastreamWSClient extends EventEmitter {
153151 if ( options . url . startsWith ( 'http://' ) || options . url . startsWith ( 'https://' ) ) {
154152 this . url = convertAPIURLToWebSocketURL ( options . url ) ;
155153 } else {
156- const URLClass = createURL ( ) ;
157- this . url = new URLClass ( options . url ) ;
154+ this . url = options . url ;
158155 }
159156
160157 // Create headers with API key
@@ -198,7 +195,7 @@ export class DatastreamWSClient extends EventEmitter {
198195 /**
199196 * SendMessage sends a message through the WebSocket connection
200197 */
201- public async sendMessage ( message : any ) : Promise < void > {
198+ public async sendMessage ( message : DataStreamBaseReq ) : Promise < void > {
202199 if ( ! this . isConnected ( ) || ! this . ws ) {
203200 throw new Error ( 'WebSocket connection is not available!' ) ;
204201 }
@@ -280,7 +277,7 @@ export class DatastreamWSClient extends EventEmitter {
280277 // Call connection ready handler if provided
281278 if ( this . connectionReadyHandler ) {
282279 try {
283- await this . connectionReadyHandler ( this . ctx ) ;
280+ await this . connectionReadyHandler ( ) ;
284281 this . logger . debug ( 'Connection ready handler completed successfully' ) ;
285282 } catch ( err ) {
286283 this . logger . error ( `Connection ready handler failed: ${ err } ` ) ;
@@ -354,10 +351,10 @@ export class DatastreamWSClient extends EventEmitter {
354351 */
355352 private connect ( ) : Promise < any > {
356353 return new Promise ( ( resolve , reject ) => {
357- this . logger . debug ( `Connecting to WebSocket: ${ this . url . toString ( ) } ` ) ;
354+ this . logger . debug ( `Connecting to WebSocket: ${ this . url } ` ) ;
358355
359356 const WebSocketClass = createWebSocket ( ) ;
360- const ws = new WebSocketClass ( this . url . toString ( ) , {
357+ const ws = new WebSocketClass ( this . url , {
361358 headers : this . headers ,
362359 handshakeTimeout : 30000 , // 30 seconds
363360 } ) ;
@@ -382,8 +379,8 @@ export class DatastreamWSClient extends EventEmitter {
382379 /**
383380 * setupWebSocketHandlers sets up message and error handlers for the WebSocket
384381 */
385- private setupWebSocketHandlers ( ws : any ) : void {
386- ws . on ( 'message' , ( data : any ) => {
382+ private setupWebSocketHandlers ( ws : WebSocket ) : void {
383+ ws . on ( 'message' , ( data : Buffer | Buffer [ ] ) => {
387384 this . handleMessage ( data ) ;
388385 } ) ;
389386
@@ -403,7 +400,7 @@ export class DatastreamWSClient extends EventEmitter {
403400 /**
404401 * handleMessage processes incoming WebSocket messages
405402 */
406- private async handleMessage ( data : any ) : Promise < void > {
403+ private async handleMessage ( data : Buffer | Buffer [ ] ) : Promise < void > {
407404 try {
408405
409406 let messageStr : string ;
@@ -412,7 +409,8 @@ export class DatastreamWSClient extends EventEmitter {
412409 } else if ( Array . isArray ( data ) ) {
413410 messageStr = Buffer . concat ( data ) . toString ( ) ;
414411 } else {
415- messageStr = data . toString ( ) ;
412+ // Fallback for other string-like types
413+ messageStr = String ( data ) ;
416414 }
417415
418416 // Parse the datastream message
@@ -427,7 +425,7 @@ export class DatastreamWSClient extends EventEmitter {
427425
428426 // Handle the parsed message using the provided handler
429427 try {
430- await this . messageHandler ( this . ctx , message ) ;
428+ await this . messageHandler ( message ) ;
431429 } catch ( err ) {
432430 this . emit ( 'error' , new Error ( `Message handler error: ${ err } ` ) ) ;
433431 }
0 commit comments