@@ -8,6 +8,7 @@ import express from 'express';
8
8
import Router from 'express-promise-router' ;
9
9
import { Logger } from 'winston' ;
10
10
import { createProxyMiddleware } from 'http-proxy-middleware' ;
11
+ import { IncomingMessage } from 'http' ;
11
12
12
13
export interface RouterOptions {
13
14
logger : Logger ;
@@ -26,11 +27,20 @@ export async function createRouter(
26
27
27
28
const router = Router ( ) ;
28
29
30
+ // We are filtering the proxy request headers here rather than in
31
+ // `onProxyReq` because when global-agent is enabled then `onProxyReq`
32
+ // fires _after_ the agent has already sent the headers to the proxy
33
+ // target, causing a ERR_HTTP_HEADERS_SENT crash
34
+ const filter = ( _pathname : string , req : IncomingMessage ) : boolean => {
35
+ if ( req . headers [ 'authorization' ] ) delete req . headers [ 'authorization' ] ;
36
+ return req . method === 'GET' ;
37
+ } ;
38
+
29
39
for ( const { host, apiBaseUrl, token } of gitlabIntegrations ) {
30
40
const apiUrl = new URL ( apiBaseUrl ) ;
31
41
router . use (
32
42
`/${ host } ` ,
33
- createProxyMiddleware ( ( _pathname , req ) => req . method === 'GET' , {
43
+ createProxyMiddleware ( filter , {
34
44
target : apiUrl . origin ,
35
45
changeOrigin : true ,
36
46
headers : {
@@ -40,13 +50,6 @@ export async function createRouter(
40
50
pathRewrite : {
41
51
[ `^/api/gitlab/${ host } ` ] : apiUrl . pathname ,
42
52
} ,
43
- onProxyReq : ( proxyReq ) => {
44
- try {
45
- proxyReq . removeHeader ( 'Authorization' ) ;
46
- } catch ( e ) {
47
- console . log ( ( e as Error ) . message ) ;
48
- }
49
- } ,
50
53
} )
51
54
) ;
52
55
}
0 commit comments