1- import  https  =  require( 'https' ) ; 
21import  stream  =  require( 'stream' ) ; 
3- import  ws  =  require( 'websocket ' ) ; 
2+ import  WebSocket  =  require( 'ws ' ) ; 
43
54import  {  V1Status  }  from  './api' ; 
65import  {  KubeConfig  }  from  './config' ; 
@@ -13,9 +12,11 @@ const protocols = [
1312] ; 
1413
1514export  interface  WebSocketInterface  { 
16-     connect ( path : string , 
17-             textHandler : ( ( text : string )  =>  boolean )  |  null , 
18-             binaryHandler : ( ( stream : number ,  buff : Buffer )  =>  boolean )  |  null ) : Promise < ws . connection > ; 
15+     connect ( 
16+         path : string , 
17+         textHandler : ( ( text : string )  =>  boolean )  |  null , 
18+         binaryHandler : ( ( stream : number ,  buff : Buffer )  =>  boolean )  |  null , 
19+     ) : Promise < WebSocket > ; 
1920} 
2021
2122export  class  WebSocketHandler  implements  WebSocketInterface  { 
@@ -24,8 +25,10 @@ export class WebSocketHandler implements WebSocketInterface {
2425    public  static  readonly  StderrStream  =  2 ; 
2526    public  static  readonly  StatusStream  =  3 ; 
2627
27-     public  static  handleStandardStreams ( streamNum : number ,  buff : Buffer , 
28-                                         stdout : stream . Writable ,  stderr : stream . Writable ) : V1Status  |  null  { 
28+     public  static  handleStandardStreams ( 
29+         streamNum : number ,  buff : Buffer , 
30+         stdout : stream . Writable ,  stderr : stream . Writable , 
31+     ) : V1Status  |  null  { 
2932        if  ( buff . length  <  1 )  { 
3033            return  null ; 
3134        } 
@@ -48,8 +51,11 @@ export class WebSocketHandler implements WebSocketInterface {
4851        return  null ; 
4952    } 
5053
51-     public  static  handleStandardInput ( conn : ws . connection , 
52-                                       stdin : stream . Readable  |  any ,  streamNum : number  =  0 ) : boolean  { 
54+     public  static  handleStandardInput ( 
55+         ws : WebSocket , 
56+         stdin : stream . Readable  |  any , 
57+         streamNum : number  =  0 , 
58+     ) : boolean  { 
5359        stdin . on ( 'data' ,  ( data )  =>  { 
5460            const  buff  =  Buffer . alloc ( data . length  +  1 ) ; 
5561            buff . writeInt8 ( streamNum ,  0 ) ; 
@@ -58,11 +64,11 @@ export class WebSocketHandler implements WebSocketInterface {
5864            }  else  { 
5965                buff . write ( data ,  1 ) ; 
6066            } 
61-             conn . send ( buff ) ; 
67+             ws . send ( buff ) ; 
6268        } ) ; 
6369
6470        stdin . on ( 'end' ,  ( )  =>  { 
65-             conn . close ( ) ; 
71+             ws . close ( ) ; 
6672        } ) ; 
6773        // Keep the stream open 
6874        return  true ; 
@@ -82,9 +88,11 @@ export class WebSocketHandler implements WebSocketInterface {
8288     * @param  binaryHandler Callback for binary data over the web socket. 
8389     *      Returns true if the connection should be kept alive, false to disconnect. 
8490     */ 
85-     public  connect ( path : string , 
86-                    textHandler : ( ( text : string )  =>  boolean )  |  null , 
87-                    binaryHandler : ( ( stream : number ,  buff : Buffer )  =>  boolean )  |  null ) : Promise < ws . connection >  { 
91+     public  connect ( 
92+         path : string , 
93+         textHandler : ( ( text : string )  =>  boolean )  |  null , 
94+         binaryHandler : ( ( stream : number ,  buff : Buffer )  =>  boolean )  |  null , 
95+     ) : Promise < WebSocket >  { 
8896
8997        const  cluster  =  this . config . getCurrentCluster ( ) ; 
9098        if  ( ! cluster )  { 
@@ -96,37 +104,38 @@ export class WebSocketHandler implements WebSocketInterface {
96104        const  proto  =  ssl  ? 'wss'  : 'ws' ; 
97105        const  uri  =  `${ proto } ${ target } ${ path }  ; 
98106
99-         const  opts : https . RequestOptions  =  { } ; 
107+         const  opts : WebSocket . ClientOptions  =  { } ; 
100108        // TODO: This doesn't set insecureSSL if skipTLSVerify is set... 
101109        this . config . applytoHTTPSOptions ( opts ) ; 
102110
103-         const  client  =  new  ws . client ( {  tlsOptions : opts  } ) ; 
104- 
105111        return  new  Promise ( ( resolve ,  reject )  =>  { 
106-             client . on ( 'connect' ,  ( connection )  =>  { 
107-                 connection . on ( 'message' ,  ( message : ws . IMessage )  =>  { 
108-                     if  ( message . type  ===  'utf8'  &&  message . utf8Data )  { 
109-                         if  ( textHandler )  { 
110-                             if  ( ! textHandler ( message . utf8Data ) )  { 
111-                                 connection . close ( ) ; 
112-                             } 
113-                         } 
114-                     }  else  if  ( message . type  ===  'binary'  &&  message . binaryData )  { 
115-                         if  ( binaryHandler )  { 
116-                             const  streamNum  =  message . binaryData . readInt8 ( 0 ) ; 
117-                             if  ( ! binaryHandler ( streamNum ,  message . binaryData . slice ( 1 ) ) )  { 
118-                                 connection . close ( ) ; 
119-                             } 
120-                         } 
112+             const  client  =  new  WebSocket ( uri ,  opts ) ; 
113+             let  resolved  =  false ; 
114+ 
115+             client . onopen  =  ( )  =>  { 
116+                 resolved  =  true ; 
117+                 resolve ( client ) ; 
118+             } ; 
119+ 
120+             client . onerror  =  ( err )  =>  { 
121+                 if  ( ! resolved )  { 
122+                     reject ( err ) ; 
123+                 } 
124+             } ; 
125+ 
126+             client . onmessage  =  ( {  data } : {  data : WebSocket . Data  } )  =>  { 
127+                 // TODO: support ArrayBuffer and Buffer[] data types? 
128+                 if  ( typeof  data  ===  'string' )  { 
129+                     if  ( textHandler  &&  ! textHandler ( data ) )  { 
130+                         client . close ( ) ; 
131+                     } 
132+                 }  else  if  ( data  instanceof  Buffer )  { 
133+                     const  streamNum  =  data . readInt8 ( 0 ) ; 
134+                     if  ( binaryHandler  &&  ! binaryHandler ( streamNum ,  data . slice ( 1 ) ) )  { 
135+                         client . close ( ) ; 
121136                    } 
122-                 } ) ; 
123-                 resolve ( connection ) ; 
124-             } ) ; 
125- 
126-             client . on ( 'connectFailed' ,  ( err )  =>  { 
127-                 reject ( err ) ; 
128-             } ) ; 
129-             client . connect ( uri ,  protocols ) ; 
137+                 } 
138+             } ; 
130139        } ) ; 
131140    } 
132141} 
0 commit comments