11const middlewareRepository = require ( "./middlewareRepository" ) ;
2+ const proxyConfiguration = require ( "../proxyConfiguration" ) ;
3+
24/**
35 *
46 *
57 * @memberof module:@ui5/server.middleware
68 */
79class MiddlewareManager {
810 constructor ( { tree, resources, options = {
9- sendSAPTargetCSP : false
11+ sendSAPTargetCSP : false ,
12+ useProxy : false
1013 } } ) {
1114 if ( ! tree || ! resources || ! resources . all || ! resources . rootProject || ! resources . dependencies ) {
1215 throw new Error ( "[MiddlewareManager]: One or more mandatory parameters not provided" ) ;
@@ -64,6 +67,13 @@ class MiddlewareManager {
6467 }
6568
6669 async addStandardMiddleware ( ) {
70+ const useProxy = this . options . useProxy ;
71+
72+ let proxyConfig ;
73+ if ( useProxy ) {
74+ proxyConfig = await proxyConfiguration . getConfigurationForProject ( this . tree ) ;
75+ }
76+
6777 await this . addMiddleware ( "csp" , {
6878 wrapperCallback : ( cspModule ) => {
6979 const oCspConfig = {
@@ -105,6 +115,21 @@ class MiddlewareManager {
105115 } ) ;
106116 await this . addMiddleware ( "compression" ) ;
107117 await this . addMiddleware ( "cors" ) ;
118+
119+ if ( useProxy ) {
120+ await this . addMiddleware ( "proxyRewrite" , {
121+ wrapperCallback : ( proxyRewriteModule ) => {
122+ return ( { resources} ) => {
123+ return proxyRewriteModule ( {
124+ resources,
125+ configuration : proxyConfig ,
126+ cdnUrl : this . options . cdnUrl
127+ } ) ;
128+ } ;
129+ }
130+ } ) ;
131+ }
132+
108133 await this . addMiddleware ( "discovery" , {
109134 mountPath : "/discovery"
110135 } ) ;
@@ -121,13 +146,44 @@ class MiddlewareManager {
121146 } ;
122147 }
123148 } ) ;
124- await this . addMiddleware ( "connectUi5Proxy" , {
125- mountPath : "/proxy"
126- } ) ;
149+
150+ if ( this . options . cdnUrl ) {
151+ await this . addMiddleware ( "cdn" , {
152+ wrapperCallback : ( cdn ) => {
153+ return ( { resources} ) => {
154+ return cdn ( {
155+ resources,
156+ cdnUrl : this . options . cdnUrl
157+ } ) ;
158+ } ;
159+ }
160+ } ) ;
161+ }
162+
163+ if ( useProxy ) {
164+ await this . addMiddleware ( "proxy" , {
165+ wrapperCallback : ( proxyModule ) => {
166+ return ( { resources} ) => {
167+ return proxyModule ( {
168+ resources,
169+ configuration : proxyConfig
170+ } ) ;
171+ } ;
172+ }
173+ } ) ;
174+ } else {
175+ await this . addMiddleware ( "connectUi5Proxy" , {
176+ mountPath : "/proxy"
177+ } ) ;
178+ }
179+
127180 // Handle anything but read operations *before* the serveIndex middleware
128181 // as it will reject them with a 405 (Method not allowed) instead of 404 like our old tooling
129182 await this . addMiddleware ( "nonReadRequests" ) ;
130- await this . addMiddleware ( "serveIndex" ) ;
183+ if ( ! useProxy ) {
184+ // Don't do directory listing when using a proxy. High potential for confusion
185+ await this . addMiddleware ( "serveIndex" ) ;
186+ }
131187 }
132188
133189 async addCustomMiddleware ( ) {
0 commit comments