@@ -27,7 +27,13 @@ export interface ProxyOptions extends HttpProxy.ServerOptions {
27
27
/** undefined for WebSocket upgrade requests */
28
28
res : http . ServerResponse | undefined ,
29
29
options : ProxyOptions ,
30
- ) => void | null | undefined | false | string
30
+ ) =>
31
+ | void
32
+ | null
33
+ | undefined
34
+ | false
35
+ | string
36
+ | Promise < void | null | undefined | boolean | string >
31
37
/**
32
38
* rewrite the Origin header of a WebSocket request to match the target
33
39
*
@@ -158,7 +164,7 @@ export function proxyMiddleware(
158
164
} )
159
165
160
166
if ( httpServer ) {
161
- httpServer . on ( 'upgrade' , ( req , socket , head ) => {
167
+ httpServer . on ( 'upgrade' , async ( req , socket , head ) => {
162
168
const url = req . url !
163
169
for ( const context in proxies ) {
164
170
if ( doesProxyContextMatchUrl ( context , url ) ) {
@@ -169,14 +175,26 @@ export function proxyMiddleware(
169
175
opts . target ?. toString ( ) . startsWith ( 'wss:' )
170
176
) {
171
177
if ( opts . bypass ) {
172
- const bypassResult = opts . bypass ( req , undefined , opts )
173
- if ( typeof bypassResult === 'string' ) {
174
- req . url = bypassResult
175
- debug ?.( `bypass: ${ req . url } -> ${ bypassResult } ` )
176
- return
177
- } else if ( bypassResult === false ) {
178
- debug ?.( `bypass: ${ req . url } -> 404` )
179
- socket . end ( 'HTTP/1.1 404 Not Found\r\n\r\n' , '' )
178
+ try {
179
+ const bypassResult = await opts . bypass ( req , undefined , opts )
180
+ if ( typeof bypassResult === 'string' ) {
181
+ debug ?.( `bypass: ${ req . url } -> ${ bypassResult } ` )
182
+ req . url = bypassResult
183
+ return
184
+ }
185
+ if ( bypassResult === false ) {
186
+ debug ?.( `bypass: ${ req . url } -> 404` )
187
+ socket . end ( 'HTTP/1.1 404 Not Found\r\n\r\n' , '' )
188
+ return
189
+ }
190
+ } catch ( err ) {
191
+ config . logger . error (
192
+ `${ colors . red ( `ws proxy bypass error:` ) } \n${ err . stack } ` ,
193
+ {
194
+ timestamp : true ,
195
+ error : err ,
196
+ } ,
197
+ )
180
198
return
181
199
}
182
200
}
@@ -194,23 +212,29 @@ export function proxyMiddleware(
194
212
}
195
213
196
214
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
197
- return function viteProxyMiddleware ( req , res , next ) {
215
+ return async function viteProxyMiddleware ( req , res , next ) {
198
216
const url = req . url !
199
217
for ( const context in proxies ) {
200
218
if ( doesProxyContextMatchUrl ( context , url ) ) {
201
219
const [ proxy , opts ] = proxies [ context ]
202
220
const options : HttpProxy . ServerOptions = { }
203
221
204
222
if ( opts . bypass ) {
205
- const bypassResult = opts . bypass ( req , res , opts )
206
- if ( typeof bypassResult === 'string' ) {
207
- req . url = bypassResult
208
- debug ?.( `bypass: ${ req . url } -> ${ bypassResult } ` )
209
- return next ( )
210
- } else if ( bypassResult === false ) {
211
- debug ?.( `bypass: ${ req . url } -> 404` )
212
- res . statusCode = 404
213
- return res . end ( )
223
+ try {
224
+ const bypassResult = await opts . bypass ( req , res , opts )
225
+ if ( typeof bypassResult === 'string' ) {
226
+ debug ?.( `bypass: ${ req . url } -> ${ bypassResult } ` )
227
+ req . url = bypassResult
228
+ return next ( )
229
+ }
230
+ if ( bypassResult === false ) {
231
+ debug ?.( `bypass: ${ req . url } -> 404` )
232
+ res . statusCode = 404
233
+ return res . end ( )
234
+ }
235
+ } catch ( e ) {
236
+ debug ?.( `bypass: ${ req . url } -> ${ e } ` )
237
+ return next ( e )
214
238
}
215
239
}
216
240
0 commit comments