@@ -6,35 +6,26 @@ import { promises as fs } from "fs"
66import http from "http"
77import * as path from "path"
88import * as tls from "tls"
9+ import * as pluginapi from "../../../typings/pluginapi"
910import { HttpCode , HttpError } from "../../common/http"
1011import { plural } from "../../common/util"
1112import { AuthType , DefaultedArgs } from "../cli"
1213import { rootPath } from "../constants"
1314import { Heart } from "../heart"
14- import { replaceTemplates , redirect } from "../http"
15+ import { redirect , replaceTemplates } from "../http"
1516import { PluginAPI } from "../plugin"
1617import { getMediaMime , paths } from "../util"
17- import { WebsocketRequest } from "../wsRouter "
18+ import { wrapper } from "../wrapper "
1819import * as apps from "./apps"
1920import * as domainProxy from "./domainProxy"
2021import * as health from "./health"
2122import * as login from "./login"
22- import * as proxy from "./pathProxy"
23+ import * as pathProxy from "./pathProxy"
2324// static is a reserved keyword.
2425import * as _static from "./static"
2526import * as update from "./update"
2627import * as vscode from "./vscode"
2728
28- declare global {
29- // eslint-disable-next-line @typescript-eslint/no-namespace
30- namespace Express {
31- export interface Request {
32- args : DefaultedArgs
33- heart : Heart
34- }
35- }
36- }
37-
3829/**
3930 * Register all routes and middleware.
4031 */
@@ -104,25 +95,34 @@ export const register = async (
10495 wsApp . use ( "/" , domainProxy . wsRouter . router )
10596
10697 app . all ( "/proxy/(:port)(/*)?" , ( req , res ) => {
107- proxy . proxy ( req , res )
98+ pathProxy . proxy ( req , res )
10899 } )
109- wsApp . get ( "/proxy/(:port)(/*)?" , ( req , res ) => {
110- proxy . wsProxy ( req as WebsocketRequest )
100+ wsApp . get ( "/proxy/(:port)(/*)?" , ( req ) => {
101+ pathProxy . wsProxy ( req as pluginapi . WebsocketRequest )
111102 } )
112103 // These two routes pass through the path directly.
113104 // So the proxied app must be aware it is running
114105 // under /absproxy/<someport>/
115106 app . all ( "/absproxy/(:port)(/*)?" , ( req , res ) => {
116- proxy . proxy ( req , res , {
107+ pathProxy . proxy ( req , res , {
117108 passthroughPath : true ,
118109 } )
119110 } )
120- wsApp . get ( "/absproxy/(:port)(/*)?" , ( req , res ) => {
121- proxy . wsProxy ( req as WebsocketRequest , {
111+ wsApp . get ( "/absproxy/(:port)(/*)?" , ( req ) => {
112+ pathProxy . wsProxy ( req as pluginapi . WebsocketRequest , {
122113 passthroughPath : true ,
123114 } )
124115 } )
125116
117+ if ( ! process . env . CS_DISABLE_PLUGINS ) {
118+ const workingDir = args . _ && args . _ . length > 0 ? path . resolve ( args . _ [ args . _ . length - 1 ] ) : undefined
119+ const pluginApi = new PluginAPI ( logger , process . env . CS_PLUGIN , process . env . CS_PLUGIN_PATH , workingDir )
120+ await pluginApi . loadPlugins ( )
121+ pluginApi . mount ( app , wsApp )
122+ app . use ( "/api/applications" , apps . router ( pluginApi ) )
123+ wrapper . onDispose ( ( ) => pluginApi . dispose ( ) )
124+ }
125+
126126 app . use ( bodyParser . json ( ) )
127127 app . use ( bodyParser . urlencoded ( { extended : true } ) )
128128
@@ -132,6 +132,7 @@ export const register = async (
132132 wsApp . use ( "/vscode" , vscode . wsRouter . router )
133133
134134 app . use ( "/healthz" , health . router )
135+ wsApp . use ( "/healthz" , health . wsRouter . router )
135136
136137 if ( args . auth === AuthType . Password ) {
137138 app . use ( "/login" , login . router )
@@ -144,11 +145,6 @@ export const register = async (
144145 app . use ( "/static" , _static . router )
145146 app . use ( "/update" , update . router )
146147
147- const papi = new PluginAPI ( logger , process . env . CS_PLUGIN , process . env . CS_PLUGIN_PATH )
148- await papi . loadPlugins ( )
149- papi . mount ( app )
150- app . use ( "/api/applications" , apps . router ( papi ) )
151-
152148 app . use ( ( ) => {
153149 throw new HttpError ( "Not Found" , HttpCode . NotFound )
154150 } )
@@ -187,7 +183,7 @@ export const register = async (
187183
188184 const wsErrorHandler : express . ErrorRequestHandler = async ( err , req , res , next ) => {
189185 logger . error ( `${ err . message } ${ err . stack } ` )
190- ; ( req as WebsocketRequest ) . ws . end ( )
186+ ; ( req as pluginapi . WebsocketRequest ) . ws . end ( )
191187 }
192188
193189 wsApp . use ( wsErrorHandler )
0 commit comments