1
1
import https = require( 'https' ) ;
2
2
import stream = require( 'stream' ) ;
3
-
4
3
import ws = require( 'websocket' ) ;
4
+
5
5
import { V1Status } from './api' ;
6
6
import { KubeConfig } from './config' ;
7
7
@@ -12,13 +12,20 @@ const protocols = [
12
12
'channel.k8s.io' ,
13
13
] ;
14
14
15
- export class WebSocketHandler {
15
+ export interface WebSocketInterface {
16
+ connect ( path : string ,
17
+ textHandler : ( ( text : string ) => boolean ) | null ,
18
+ binaryHandler : ( ( stream : number , buff : Buffer ) => boolean ) | null ) : Promise < ws . connection > ;
19
+ }
20
+
21
+ export class WebSocketHandler implements WebSocketInterface {
16
22
public static readonly StdinStream = 0 ;
17
23
public static readonly StdoutStream = 1 ;
18
24
public static readonly StderrStream = 2 ;
19
25
public static readonly StatusStream = 3 ;
20
26
21
- public static handleStandardStreams ( streamNum : number , buff : Buffer , stdout : any , stderr : any ) : V1Status | null {
27
+ public static handleStandardStreams ( streamNum : number , buff : Buffer ,
28
+ stdout : stream . Writable , stderr : stream . Writable ) : V1Status | null {
22
29
if ( buff . length < 1 ) {
23
30
return null ;
24
31
}
@@ -41,10 +48,11 @@ export class WebSocketHandler {
41
48
return null ;
42
49
}
43
50
44
- public static handleStandardInput ( conn : ws . connection , stdin : stream . Readable | any ) {
51
+ public static handleStandardInput ( conn : ws . connection ,
52
+ stdin : stream . Readable | any , streamNum : number = 0 ) : boolean {
45
53
stdin . on ( 'data' , ( data ) => {
46
54
const buff = Buffer . alloc ( data . length + 1 ) ;
47
- buff . writeInt8 ( 0 , 0 ) ;
55
+ buff . writeInt8 ( streamNum , 0 ) ;
48
56
if ( data instanceof Buffer ) {
49
57
data . copy ( buff , 1 ) ;
50
58
} else {
@@ -56,6 +64,8 @@ export class WebSocketHandler {
56
64
stdin . on ( 'end' , ( ) => {
57
65
conn . close ( ) ;
58
66
} ) ;
67
+ // Keep the stream open
68
+ return true ;
59
69
}
60
70
61
71
public 'config' : KubeConfig ;
@@ -64,9 +74,17 @@ export class WebSocketHandler {
64
74
this . config = config ;
65
75
}
66
76
77
+ /**
78
+ * Connect to a web socket endpoint.
79
+ * @param path The HTTP Path to connect to on the server.
80
+ * @param textHandler Callback for text over the web socket.
81
+ * Returns true if the connection should be kept alive, false to disconnect.
82
+ * @param binaryHandler Callback for binary data over the web socket.
83
+ * Returns true if the connection should be kept alive, false to disconnect.
84
+ */
67
85
public connect ( path : string ,
68
- textHandler : ( text : string ) => void ,
69
- binaryHandler : ( stream : number , buff : Buffer ) => void ) : Promise < ws . connection > {
86
+ textHandler : ( ( text : string ) => boolean ) | null ,
87
+ binaryHandler : ( ( stream : number , buff : Buffer ) => boolean ) | null ) : Promise < ws . connection > {
70
88
71
89
const server = this . config . getCurrentCluster ( ) . server ;
72
90
const ssl = server . startsWith ( 'https://' ) ;
@@ -78,19 +96,23 @@ export class WebSocketHandler {
78
96
// TODO: This doesn't set insecureSSL if skipTLSVerify is set...
79
97
this . config . applytoHTTPSOptions ( opts ) ;
80
98
81
- const client = new ws . client ( { tlsOptions : opts } ) ;
99
+ const client = new ws . client ( { tlsOptions : opts } ) ;
82
100
83
101
return new Promise ( ( resolve , reject ) => {
84
102
client . on ( 'connect' , ( connection ) => {
85
103
connection . on ( 'message' , ( message : ws . IMessage ) => {
86
104
if ( message . type === 'utf8' && message . utf8Data ) {
87
105
if ( textHandler ) {
88
- textHandler ( message . utf8Data ) ;
106
+ if ( ! textHandler ( message . utf8Data ) ) {
107
+ connection . close ( ) ;
108
+ }
89
109
}
90
110
} else if ( message . type === 'binary' && message . binaryData ) {
91
111
if ( binaryHandler ) {
92
112
const streamNum = message . binaryData . readInt8 ( 0 ) ;
93
- binaryHandler ( streamNum , message . binaryData . slice ( 1 ) ) ;
113
+ if ( ! binaryHandler ( streamNum , message . binaryData . slice ( 1 ) ) ) {
114
+ connection . close ( ) ;
115
+ }
94
116
}
95
117
}
96
118
} ) ;
0 commit comments