@@ -21,6 +21,21 @@ import { messageSchema } from '../schemas/message-schema'
2121import { Settings } from '../@types/settings'
2222import { SocketAddress } from 'net'
2323
24+ ( ( ) => {
25+ ( WebSocket as any ) . Receiver . prototype . _write = function _write ( chunk : any , _encoding : any , cb : any ) {
26+ if ( this . _opcode === 0x08 && this . _state == 0 ) return cb ( )
27+
28+ this . _bufferedBytes += chunk . length
29+ this . _buffers . push ( chunk )
30+ try {
31+ this . startLoop ( cb )
32+ } catch ( error ) {
33+ console . error ( 'what in the world' , error )
34+ cb ( error )
35+ }
36+ }
37+ } ) ( )
38+
2439const debug = createLogger ( 'web-socket-adapter' )
2540const debugHeartbeat = debug . extend ( 'heartbeat' )
2641
@@ -54,16 +69,16 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
5469 } )
5570
5671 this . client
57- . on ( 'message' , this . onClientMessage . bind ( this ) )
58- . on ( 'close' , this . onClientClose . bind ( this ) )
59- . on ( 'pong' , this . onClientPong . bind ( this ) )
6072 . on ( 'error' , ( error ) => {
6173 if ( error . name === 'RangeError' && error . message === 'Max payload size exceeded' ) {
62- debug ( ' client %s from %s sent payload too large' , this . clientId , this . clientAddress )
74+ console . error ( `web-socket-adapter: client ${ this . clientId } ( ${ this . getClientAddress ( ) } ) sent payload too large` )
6375 } else {
64- debug ( ' error' , error )
76+ console . error ( `web-socket-adapter: client error ${ this . clientId } ( ${ this . getClientAddress ( ) } ):` , error )
6577 }
6678 } )
79+ . on ( 'message' , this . onClientMessage . bind ( this ) )
80+ . on ( 'close' , this . onClientClose . bind ( this ) )
81+ . on ( 'pong' , this . onClientPong . bind ( this ) )
6782
6883 this
6984 . on ( WebSocketAdapterEvent . Heartbeat , this . onHeartbeat . bind ( this ) )
@@ -116,12 +131,15 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
116131 }
117132
118133 private sendMessage ( message : OutgoingMessage ) : void {
134+ if ( this . client . readyState !== WebSocket . OPEN ) {
135+ return
136+ }
119137 this . client . send ( JSON . stringify ( message ) )
120138 }
121139
122140 public onHeartbeat ( ) : void {
123141 if ( ! this . alive ) {
124- debug ( 'client %s pong timed out' , this . clientId )
142+ console . error ( `web-socket-adapter: pong timeout for client ${ this . clientId } ( ${ this . getClientAddress ( ) } )` )
125143 this . terminate ( )
126144 return
127145 }
@@ -158,7 +176,7 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
158176
159177 messageHandler = this . createMessageHandler ( [ message , this ] ) as IMessageHandler & IAbortable
160178 if ( ! messageHandler ) {
161- debug ( ' unhandled message: no handler found: %o ', message )
179+ console . error ( 'web-socket-adapter: unhandled message: no handler found:', message )
162180 return
163181 }
164182
@@ -174,17 +192,17 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
174192 } catch ( error ) {
175193 if ( error instanceof Error ) {
176194 if ( error . name === 'AbortError' ) {
177- debug ( 'message handler aborted' )
195+ console . error ( `web-socket-adapter: abort from client ${ this . clientId } ( ${ this . getClientAddress ( ) } )` )
178196 } else if ( error . name === 'SyntaxError' || error . name === 'ValidationError' ) {
179197 if ( typeof ( error as any ) . annotate === 'function' ) {
180- debug ( ' invalid message: %o' , ( error as any ) . annotate ( ) )
198+ console . error ( `web-socket-adapter: invalid message client ${ this . clientId } ( ${ this . getClientAddress ( ) } ):` , ( error as any ) . annotate ( ) )
181199 } else {
182- debug ( ' malformed message: %s' , error . message )
200+ console . error ( `web-socket-adapter: malformed message from client ${ this . clientId } ( ${ this . getClientAddress ( ) } ):` , error . message )
183201 }
184202 this . sendMessage ( createNoticeMessage ( `invalid: ${ error . message } ` ) )
185203 }
186204 } else {
187- console . error ( 'unable to handle message' , error )
205+ console . error ( 'web-socket-adapter: unable to handle message: ' , error )
188206 }
189207 } finally {
190208 if ( abortable && messageHandler ) {
0 commit comments