Skip to content

Commit 04921d6

Browse files
authored
Merge pull request #185 from immobiliare/next
5.0.2
2 parents e113331 + c171ec1 commit 04921d6

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

packages/gitlab-backend/src/service/router.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import express from 'express';
88
import Router from 'express-promise-router';
99
import { Logger } from 'winston';
1010
import { createProxyMiddleware } from 'http-proxy-middleware';
11+
import { IncomingMessage } from 'http';
1112

1213
export interface RouterOptions {
1314
logger: Logger;
@@ -26,11 +27,20 @@ export async function createRouter(
2627

2728
const router = Router();
2829

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+
2939
for (const { host, apiBaseUrl, token } of gitlabIntegrations) {
3040
const apiUrl = new URL(apiBaseUrl);
3141
router.use(
3242
`/${host}`,
33-
createProxyMiddleware((_pathname, req) => req.method === 'GET', {
43+
createProxyMiddleware(filter, {
3444
target: apiUrl.origin,
3545
changeOrigin: true,
3646
headers: {
@@ -40,13 +50,6 @@ export async function createRouter(
4050
pathRewrite: {
4151
[`^/api/gitlab/${host}`]: apiUrl.pathname,
4252
},
43-
onProxyReq: (proxyReq) => {
44-
try {
45-
proxyReq.removeHeader('Authorization');
46-
} catch (e) {
47-
console.log((e as Error).message);
48-
}
49-
},
5053
})
5154
);
5255
}

0 commit comments

Comments
 (0)