1
- import https = require( 'https' ) ;
2
1
import stream = require( 'stream' ) ;
3
- import ws = require( 'websocket ' ) ;
2
+ import WebSocket = require( 'ws ' ) ;
4
3
5
4
import { V1Status } from './api' ;
6
5
import { KubeConfig } from './config' ;
@@ -13,9 +12,11 @@ const protocols = [
13
12
] ;
14
13
15
14
export 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 > ;
19
20
}
20
21
21
22
export class WebSocketHandler implements WebSocketInterface {
@@ -24,8 +25,10 @@ export class WebSocketHandler implements WebSocketInterface {
24
25
public static readonly StderrStream = 2 ;
25
26
public static readonly StatusStream = 3 ;
26
27
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 {
29
32
if ( buff . length < 1 ) {
30
33
return null ;
31
34
}
@@ -48,8 +51,11 @@ export class WebSocketHandler implements WebSocketInterface {
48
51
return null ;
49
52
}
50
53
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 {
53
59
stdin . on ( 'data' , ( data ) => {
54
60
const buff = Buffer . alloc ( data . length + 1 ) ;
55
61
buff . writeInt8 ( streamNum , 0 ) ;
@@ -58,11 +64,11 @@ export class WebSocketHandler implements WebSocketInterface {
58
64
} else {
59
65
buff . write ( data , 1 ) ;
60
66
}
61
- conn . send ( buff ) ;
67
+ ws . send ( buff ) ;
62
68
} ) ;
63
69
64
70
stdin . on ( 'end' , ( ) => {
65
- conn . close ( ) ;
71
+ ws . close ( ) ;
66
72
} ) ;
67
73
// Keep the stream open
68
74
return true ;
@@ -82,9 +88,11 @@ export class WebSocketHandler implements WebSocketInterface {
82
88
* @param binaryHandler Callback for binary data over the web socket.
83
89
* Returns true if the connection should be kept alive, false to disconnect.
84
90
*/
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 > {
88
96
89
97
const cluster = this . config . getCurrentCluster ( ) ;
90
98
if ( ! cluster ) {
@@ -96,37 +104,38 @@ export class WebSocketHandler implements WebSocketInterface {
96
104
const proto = ssl ? 'wss' : 'ws' ;
97
105
const uri = `${ proto } ://${ target } ${ path } ` ;
98
106
99
- const opts : https . RequestOptions = { } ;
107
+ const opts : WebSocket . ClientOptions = { } ;
100
108
// TODO: This doesn't set insecureSSL if skipTLSVerify is set...
101
109
this . config . applytoHTTPSOptions ( opts ) ;
102
110
103
- const client = new ws . client ( { tlsOptions : opts } ) ;
104
-
105
111
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 ( ) ;
121
136
}
122
- } ) ;
123
- resolve ( connection ) ;
124
- } ) ;
125
-
126
- client . on ( 'connectFailed' , ( err ) => {
127
- reject ( err ) ;
128
- } ) ;
129
- client . connect ( uri , protocols ) ;
137
+ }
138
+ } ;
130
139
} ) ;
131
140
}
132
141
}
0 commit comments