Skip to content

Commit

Permalink
fix: reconverted body into a stream
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomuso committed Aug 29, 2023
1 parent f80ca18 commit e1c4b8b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/gitlab-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@backstage/config": "^1.0.8",
"@backstage/integration": "^1.5.0",
"@types/express": "*",
"body-parser": "^1.20.2",
"express": "^4.17.3",
"express-promise-router": "^4.1.0",
"http-proxy-middleware": "^2.0.6",
Expand Down
20 changes: 15 additions & 5 deletions packages/gitlab-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ export async function createRouter(

const graphqlFilter = (_pathname: string, req: Request): boolean => {
if (req.headers['authorization']) delete req.headers['authorization'];

return (
req.method === 'POST' &&
!JSON.stringify(req.body).includes('mutation')
);
return req.method === 'POST' && !req.body.query.includes('mutation');
};

for (const { host, apiBaseUrl, token } of gitlabIntegrations) {
Expand All @@ -73,6 +69,20 @@ export async function createRouter(
...(token ? { 'PRIVATE-TOKEN': token } : {}),
},
secure,
onProxyReq: (proxyReq, req) => {
// Here we have to convert body into a stream to avoid to break middleware
if (req.body) {
const bodyData = JSON.stringify(req.body);
// incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
proxyReq.setHeader('Content-Type', 'application/json');
proxyReq.setHeader(
'Content-Length',
Buffer.byteLength(bodyData)
);
// stream the content
proxyReq.write(bodyData);
}
},
logProvider: () => logger,
pathRewrite: {
[`^${basePath}/api/gitlab/graphql/${host}`]: `/api/graphql`,
Expand Down
25 changes: 23 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4227,6 +4227,7 @@ __metadata:
"@backstage/plugin-catalog-node": ^1.3.7
"@types/express": "*"
"@types/supertest": ^2.0.12
body-parser: ^1.20.2
express: ^4.17.3
express-promise-router: ^4.1.0
http-proxy-middleware: ^2.0.6
Expand Down Expand Up @@ -9757,6 +9758,26 @@ __metadata:
languageName: node
linkType: hard

"body-parser@npm:^1.20.2":
version: 1.20.2
resolution: "body-parser@npm:1.20.2"
dependencies:
bytes: 3.1.2
content-type: ~1.0.5
debug: 2.6.9
depd: 2.0.0
destroy: 1.2.0
http-errors: 2.0.0
iconv-lite: 0.4.24
on-finished: 2.4.1
qs: 6.11.0
raw-body: 2.5.2
type-is: ~1.6.18
unpipe: 1.0.0
checksum: 14d37ec638ab5c93f6099ecaed7f28f890d222c650c69306872e00b9efa081ff6c596cd9afb9930656aae4d6c4e1c17537bea12bb73c87a217cb3cfea8896737
languageName: node
linkType: hard

"bonjour-service@npm:^1.0.11":
version: 1.1.1
resolution: "bonjour-service@npm:1.1.1"
Expand Down Expand Up @@ -10983,7 +11004,7 @@ __metadata:
languageName: node
linkType: hard

"content-type@npm:~1.0.4":
"content-type@npm:~1.0.4, content-type@npm:~1.0.5":
version: 1.0.5
resolution: "content-type@npm:1.0.5"
checksum: 566271e0a251642254cde0f845f9dd4f9856e52d988f4eb0d0dcffbb7a1f8ec98de7a5215fc628f3bce30fe2fb6fd2bc064b562d721658c59b544e2d34ea2766
Expand Down Expand Up @@ -22508,7 +22529,7 @@ __metadata:
languageName: node
linkType: hard

"raw-body@npm:^2.4.1":
"raw-body@npm:2.5.2, raw-body@npm:^2.4.1":
version: 2.5.2
resolution: "raw-body@npm:2.5.2"
dependencies:
Expand Down

0 comments on commit e1c4b8b

Please sign in to comment.