@@ -27,7 +27,7 @@ export class DashboardServer {
2727 private host = '127.0.0.1' ;
2828 private port = 6120 ;
2929 private stevePort = 0 ;
30- private proxies : Record < string , ReturnType < typeof createProxyMiddleware > > = Object . create ( null ) ;
30+ private proxies : Record < ProxyKeys , ReturnType < typeof createProxyMiddleware > > = Object . create ( null ) ;
3131
3232 /**
3333 * Checks for an existing instance of Dashboard server.
@@ -95,15 +95,13 @@ export class DashboardServer {
9595
9696 // Register wrapper functions so that when createProxies() replaces
9797 // this.proxies (on each Steve restart), express and the upgrade
98- // handler automatically use the new instances. The optional
99- // chaining is safe: proxies are always created before the UI is
100- // notified that Kubernetes is ready, and the dashboard button is
101- // disabled until then.
102- //
103- // ?? next() fires only when the proxy is absent: createProxyMiddleware
104- // returns an async function, so the call always yields a Promise (truthy).
98+ // handler automatically use the new instances. The call is safe: proxies
99+ // are always created before the UI is notified that Kubernetes is ready,
100+ // and the dashboard button is disabled until then.
105101 ProxyKeys . forEach ( ( key ) => {
106- this . dashboardServer . use ( key , ( req , res , next ) => this . proxies [ key ] ?.( req , res , next ) ?? next ( ) ) ;
102+ this . dashboardServer . use ( key , ( req , res , next ) => {
103+ return this . proxies [ key ] ? this . proxies [ key ] ( req , res , next ) : next ( ) ;
104+ } ) ;
107105 } ) ;
108106
109107 this . dashboardApp = this . dashboardServer
@@ -133,20 +131,15 @@ export class DashboardServer {
133131 return ;
134132 }
135133
136- // TODO: drive this from ProxyKeys instead of maintaining a parallel list.
137- const key = req . url ?. startsWith ( '/v1' )
138- ? '/v1'
139- : req . url ?. startsWith ( '/v3' )
140- ? '/v3'
141- : req . url ?. startsWith ( '/k8s/' )
142- ? '/k8s'
143- : req . url ?. startsWith ( '/api/' )
144- ? '/api'
145- : undefined ;
146-
147- if ( key ) {
148- return this . proxies [ key ] ?. upgrade ( req , socket , head ) ;
134+ const upgradeKeys = new Set < ProxyKeys > ( [ '/v1' , '/v3' , '/k8s' , '/api' ] ) ;
135+ const key = Array . from ( upgradeKeys ) . find ( ( key ) => {
136+ return req . url === key || req . url ?. startsWith ( key + '/' ) ;
137+ } ) ;
138+
139+ if ( key && this . proxies [ key ] ) {
140+ return this . proxies [ key ] . upgrade ( req , socket , head ) ;
149141 }
142+
150143 console . log ( `Unknown Web socket upgrade request for ${ req . url } ` ) ;
151144 socket . destroy ( ) ;
152145 } ) ;
0 commit comments