11const middlewareRepository = require ( "./middlewareRepository" ) ;
2+ const proxyConfiguration = require ( "../proxyConfiguration" ) ;
23
34/**
45 *
@@ -7,7 +8,8 @@ const middlewareRepository = require("./middlewareRepository");
78 */
89class MiddlewareManager {
910 constructor ( { tree, resources, options = {
10- sendSAPTargetCSP : false
11+ sendSAPTargetCSP : false ,
12+ useProxy : false
1113 } } ) {
1214 if ( ! tree || ! resources || ! resources . all || ! resources . rootProject || ! resources . dependencies ) {
1315 throw new Error ( "[MiddlewareManager]: One or more mandatory parameters not provided" ) ;
@@ -76,6 +78,13 @@ class MiddlewareManager {
7678 }
7779
7880 async addStandardMiddleware ( ) {
81+ const useProxy = this . options . useProxy ;
82+
83+ let proxyConfig ;
84+ if ( useProxy ) {
85+ proxyConfig = await proxyConfiguration . getConfigurationForProject ( this . tree ) ;
86+ }
87+
7988 await this . addMiddleware ( "csp" , {
8089 wrapperCallback : ( cspModule ) => {
8190 const oCspConfig = {
@@ -117,6 +126,21 @@ class MiddlewareManager {
117126 } ) ;
118127 await this . addMiddleware ( "compression" ) ;
119128 await this . addMiddleware ( "cors" ) ;
129+
130+ if ( useProxy ) {
131+ await this . addMiddleware ( "proxyRewrite" , {
132+ wrapperCallback : ( proxyRewriteModule ) => {
133+ return ( { resources} ) => {
134+ return proxyRewriteModule ( {
135+ resources,
136+ configuration : proxyConfig ,
137+ cdnUrl : this . options . cdnUrl
138+ } ) ;
139+ } ;
140+ }
141+ } ) ;
142+ }
143+
120144 await this . addMiddleware ( "discovery" , {
121145 mountPath : "/discovery"
122146 } ) ;
@@ -133,20 +157,52 @@ class MiddlewareManager {
133157 } ;
134158 }
135159 } ) ;
136- await this . addMiddleware ( "connectUi5Proxy" , {
137- mountPath : "/proxy"
138- } ) ;
160+
161+ if ( this . options . cdnUrl ) {
162+ await this . addMiddleware ( "cdn" , {
163+ wrapperCallback : ( cdn ) => {
164+ return ( { resources} ) => {
165+ return cdn ( {
166+ resources,
167+ cdnUrl : this . options . cdnUrl
168+ } ) ;
169+ } ;
170+ }
171+ } ) ;
172+ }
173+
174+ if ( useProxy ) {
175+ await this . addMiddleware ( "proxy" , {
176+ wrapperCallback : ( proxyModule ) => {
177+ return ( { resources} ) => {
178+ return proxyModule ( {
179+ resources,
180+ configuration : proxyConfig
181+ } ) ;
182+ } ;
183+ }
184+ } ) ;
185+ } else {
186+ await this . addMiddleware ( "connectUi5Proxy" , {
187+ mountPath : "/proxy"
188+ } ) ;
189+ }
190+
139191 // Handle anything but read operations *before* the serveIndex middleware
140192 // as it will reject them with a 405 (Method not allowed) instead of 404 like our old tooling
141193 await this . addMiddleware ( "nonReadRequests" ) ;
142- await this . addMiddleware ( "serveIndex" , {
143- wrapperCallback : ( middleware ) => {
144- return ( { resources} ) => middleware ( {
145- resources,
146- simpleIndex : this . options . simpleIndex
147- } ) ;
148- }
149- } ) ;
194+
195+ if ( ! useProxy ) {
196+ // Don't do directory listing when using a proxy. High potential for confusion
197+ await this . addMiddleware ( "serveIndex" , {
198+ wrapperCallback : ( middleware ) => {
199+ return ( { resources} ) => middleware ( {
200+ resources,
201+ simpleIndex : this . options . simpleIndex
202+ } ) ;
203+ }
204+ } ) ;
205+ }
150206 }
151207
152208 async addCustomMiddleware ( ) {
0 commit comments