1+ /* eslint-disable @typescript-eslint/no-unused-vars */
12/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
23import { EventEmitter } from 'events' ;
34import WebSocket from 'isomorphic-ws' ;
@@ -10,7 +11,6 @@ import {
1011 WsChannelSubUnSubRequestArg ,
1112 WSClientConfigurableOptions ,
1213 WsDataEvent ,
13- WsEvent ,
1414 WsSubRequest ,
1515 WsUnsubRequest ,
1616} from './types' ;
@@ -29,6 +29,7 @@ import {
2929 PUBLIC_WS_KEYS ,
3030 WS_KEY_MAP ,
3131} from './util' ;
32+ import { WSClientEventMap } from './util/BaseWSClient' ;
3233import { signMessage } from './util/webCryptoAPI' ;
3334import {
3435 getWsKeyForMarket ,
@@ -41,42 +42,20 @@ import {
4142import WsStore from './util/WsStore' ;
4243import { WsConnectionStateEnum } from './util/WsStore.types' ;
4344
44- type WsKeyObject = { wsKey : WsKey } ;
45-
46- interface WebsocketClientEvents {
47- /** Connection opened. If this connection was previously opened and reconnected, expect the reconnected event instead */
48- open : ( evt : { event : any } & WsKeyObject ) => void ;
49- /** Reconnecting a dropped connection */
50- reconnect : ( evt : { event : any } & WsKeyObject ) => void ;
51- /** Successfully reconnected a connection that dropped */
52- reconnected : ( evt : { event : any } & WsKeyObject ) => void ;
53- /** Connection closed */
54- close : ( evt : { event : any } & WsKeyObject ) => void ;
55- /** Received reply to websocket command (e.g. after subscribing to topics or authenticating) */
56- response : ( response : WsEvent & WsKeyObject ) => void ;
57- /** Received data for a topic/channel */
58- update : ( response : WsDataEvent & WsKeyObject ) => void ;
59- /** Exception from ws client OR custom listeners */
60- error : ( response : any ) => void ;
61- }
62-
6345// Type safety for on and emit handlers: https://stackoverflow.com/a/61609010/880837
64- export declare interface WebsocketClientLegacy {
65- on < U extends keyof WebsocketClientEvents > (
46+ export declare interface WebsocketClient {
47+ on < U extends keyof WSClientEventMap < WsKey , WsDataEvent > > (
6648 event : U ,
67- listener : WebsocketClientEvents [ U ] ,
49+ listener : WSClientEventMap < WsKey > [ U ] ,
6850 ) : this;
6951
70- emit < U extends keyof WebsocketClientEvents > (
52+ emit < U extends keyof WSClientEventMap < WsKey , WsDataEvent > > (
7153 event : U ,
72- ...args : Parameters < WebsocketClientEvents [ U ] >
54+ ...args : Parameters < WSClientEventMap < WsKey > [ U ] >
7355 ) : boolean ;
7456}
7557
76- /**
77- * @deprecated Previous generation WS Client will be removed soon. Use the new WebsocketClient instead.
78- */
79- export class WebsocketClientLegacy extends EventEmitter {
58+ export class WebsocketClient extends EventEmitter {
8059 private logger : typeof DefaultLogger ;
8160
8261 private options : WebsocketClientOptions ;
@@ -102,6 +81,7 @@ export class WebsocketClientLegacy extends EventEmitter {
10281 authPrivateConnectionsOnConnect : true ,
10382 // Individual requests do not require a signature, so this is disabled.
10483 authPrivateRequests : false ,
84+
10585 ...options ,
10686 } ;
10787
@@ -110,9 +90,6 @@ export class WebsocketClientLegacy extends EventEmitter {
11090 'ERROR: to use demo trading, set the "demoTrading: true" flag in the constructor' ,
11191 ) ;
11292 }
113-
114- // add default error handling so this doesn't crash node (if the user didn't set a handler)
115- this . on ( 'error' , ( ) => { } ) ;
11693 }
11794
11895 /**
@@ -287,7 +264,7 @@ export class WebsocketClientLegacy extends EventEmitter {
287264 private parseWsError ( context : string , error : any , wsKey : WsKey ) {
288265 if ( ! error . message ) {
289266 this . logger . error ( `${ context } due to unexpected error: ` , error ) ;
290- this . emit ( 'error ' , error ) ;
267+ this . emit ( 'exception ' , error ) ;
291268 return ;
292269 }
293270
@@ -311,7 +288,7 @@ export class WebsocketClientLegacy extends EventEmitter {
311288 }
312289 break ;
313290 }
314- this . emit ( 'error ' , error ) ;
291+ this . emit ( 'exception ' , error ) ;
315292 }
316293
317294 /**
@@ -668,16 +645,21 @@ export class WebsocketClientLegacy extends EventEmitter {
668645 const { protocols = [ ] , ...wsOptions } = this . options . wsOptions || { } ;
669646 const ws = new WebSocket ( url , protocols , wsOptions ) ;
670647
671- ws . onopen = ( event ) => this . onWsOpen ( event , wsKey ) ;
672- ws . onmessage = ( event ) => this . onWsMessage ( event , wsKey ) ;
648+ ws . onopen = ( event ) => this . onWsOpen ( event , wsKey , url , ws ) ;
649+ ws . onmessage = ( event ) => this . onWsMessage ( event , wsKey , ws ) ;
673650 ws . onerror = ( event ) =>
674651 this . parseWsError ( 'Websocket onWsError' , event , wsKey ) ;
675652 ws . onclose = ( event ) => this . onWsClose ( event , wsKey ) ;
676653
677654 return ws ;
678655 }
679656
680- private async onWsOpen ( event : unknown , wsKey : WsKey ) {
657+ private async onWsOpen (
658+ event : WebSocket . Event ,
659+ wsKey : WsKey ,
660+ url : string ,
661+ ws : WebSocket ,
662+ ) {
681663 if (
682664 this . wsStore . isConnectionState ( wsKey , WsConnectionStateEnum . CONNECTING )
683665 ) {
@@ -686,15 +668,15 @@ export class WebsocketClientLegacy extends EventEmitter {
686668 wsKey,
687669 market : this . options . market ,
688670 } ) ;
689- this . emit ( 'open' , { wsKey, event } ) ;
671+ this . emit ( 'open' , { wsKey, event, wsUrl : url , ws } ) ;
690672 } else if (
691673 this . wsStore . isConnectionState ( wsKey , WsConnectionStateEnum . RECONNECTING )
692674 ) {
693675 this . logger . info ( 'Websocket reconnected' , {
694676 ...WS_LOGGER_CATEGORY ,
695677 wsKey,
696678 } ) ;
697- this . emit ( 'reconnected' , { wsKey, event } ) ;
679+ this . emit ( 'reconnected' , { wsKey, event, wsUrl : url , ws } ) ;
698680 }
699681
700682 this . wsStore . setConnectionState ( wsKey , WsConnectionStateEnum . CONNECTED ) ;
@@ -721,7 +703,7 @@ export class WebsocketClientLegacy extends EventEmitter {
721703 this . requestSubscribeTopics ( wsKey , topics ) ;
722704 }
723705
724- private onWsMessage ( event : any , wsKey : WsKey ) {
706+ private onWsMessage ( event : any , wsKey : WsKey , ws : WebSocket ) {
725707 const logContext = { ...WS_LOGGER_CATEGORY , wsKey, method : 'onWsMessage' } ;
726708
727709 try {
@@ -737,7 +719,7 @@ export class WebsocketClientLegacy extends EventEmitter {
737719
738720 if ( isWsErrorEvent ( msg ) ) {
739721 this . logger . error ( 'WS error event: ' , { ...msg , wsKey } ) ;
740- return this . emit ( 'error ' , { ...msg , wsKey } ) ;
722+ return this . emit ( 'exception ' , { ...msg , wsKey } ) ;
741723 }
742724
743725 if ( isWsDataEvent ( msg ) ) {
@@ -768,7 +750,7 @@ export class WebsocketClientLegacy extends EventEmitter {
768750 ...msg ,
769751 wsKey,
770752 } ) ;
771- return this . emit ( 'error ' , { ...msg , wsKey } ) ;
753+ return this . emit ( 'exception ' , { ...msg , wsKey } ) ;
772754 }
773755
774756 if ( isWsSubscribeEvent ( msg ) || isWsUnsubscribeEvent ( msg ) ) {
0 commit comments