1- import { Request , Router } from "express"
1+ import { Request , Response } from "express"
2+ import * as path from "path"
23import qs from "qs"
34import { HttpCode , HttpError } from "../../common/http"
45import { normalize } from "../../common/util"
56import { authenticated , ensureAuthenticated , redirect } from "../http"
6- import { proxy } from "../proxy"
7- import { Router as WsRouter } from "../wsRouter"
7+ import { proxy as _proxy } from "../proxy"
8+ import { WebsocketRequest } from "../wsRouter"
89
9- export const router = Router ( )
10-
11- const getProxyTarget = ( req : Request , passthroughPath : boolean ) : string => {
10+ const getProxyTarget = ( req : Request , passthroughPath ?: boolean ) : string => {
1211 if ( passthroughPath ) {
1312 return `http://0.0.0.0:${ req . params . port } /${ req . originalUrl } `
1413 }
1514 const query = qs . stringify ( req . query )
1615 return `http://0.0.0.0:${ req . params . port } /${ req . params [ 0 ] || "" } ${ query ? `?${ query } ` : "" } `
1716}
1817
19- router . all ( "/(:port)(/*)?" , ( req , res ) => {
18+ export function proxy (
19+ req : Request ,
20+ res : Response ,
21+ opts ?: {
22+ passthroughPath ?: boolean
23+ } ,
24+ ) : void {
2025 if ( ! authenticated ( req ) ) {
2126 // If visiting the root (/:port only) redirect to the login page.
2227 if ( ! req . params [ 0 ] || req . params [ 0 ] === "/" ) {
@@ -28,22 +33,27 @@ router.all("/(:port)(/*)?", (req, res) => {
2833 throw new HttpError ( "Unauthorized" , HttpCode . Unauthorized )
2934 }
3035
31- if ( ! req . args [ "proxy-path-passthrough" ] ) {
36+ if ( ! opts ?. passthroughPath ) {
3237 // Absolute redirects need to be based on the subpath when rewriting.
33- ; ( req as any ) . base = `${ req . baseUrl } /${ req . params . port } `
38+ // See proxy.ts.
39+ ; ( req as any ) . base = req . path . split ( path . sep ) . slice ( 0 , 3 ) . join ( path . sep )
3440 }
3541
36- proxy . web ( req , res , {
42+ _proxy . web ( req , res , {
3743 ignorePath : true ,
38- target : getProxyTarget ( req , req . args [ "proxy-path-passthrough" ] || false ) ,
44+ target : getProxyTarget ( req , opts ?. passthroughPath ) ,
3945 } )
40- } )
41-
42- export const wsRouter = WsRouter ( )
46+ }
4347
44- wsRouter . ws ( "/(:port)(/*)?" , ensureAuthenticated , ( req ) => {
45- proxy . ws ( req , req . ws , req . head , {
48+ export function wsProxy (
49+ req : WebsocketRequest ,
50+ opts ?: {
51+ passthroughPath ?: boolean
52+ } ,
53+ ) : void {
54+ ensureAuthenticated ( req )
55+ _proxy . ws ( req , req . ws , req . head , {
4656 ignorePath : true ,
47- target : getProxyTarget ( req , req . args [ "proxy-path-passthrough" ] || false ) ,
57+ target : getProxyTarget ( req , opts ?. passthroughPath ) ,
4858 } )
49- } )
59+ }
0 commit comments