Skip to content

Commit

Permalink
Stop the server from crashing
Browse files Browse the repository at this point in the history
When upgrading the http-proxy-middleware library I accidentally caused the app to create a new proxy server on every request (whether for the proxy or not), rather than just on startup. This led to a memory leak, and the Node process crashing.

This change creates it once, and uses the express-async-handler to get around triggering the no-misused-promises linting rule.

Refs #1636, chimurai/http-proxy-middleware#108 (comment), https://github.com/chimurai/http-proxy-middleware/tree/v3.0.0?tab=readme-ov-file#express-server-example, https://typescript-eslint.io/rules/no-misused-promises/
  • Loading branch information
thewilkybarkid committed Jun 12, 2024
1 parent 37ca9bf commit a61986a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"datacite-ts": "^0.1.4",
"doi-ts": "^0.1.9",
"express": "^4.19.2",
"express-async-handler": "^1.2.0",
"fetch-fp-ts": "^0.1.6",
"fp-ts": "^2.16.6",
"fp-ts-routing": "^0.6.0",
Expand Down
61 changes: 32 additions & 29 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import slashes from 'connect-slashes'
import express from 'express'
import asyncHandler from 'express-async-handler'
import type { Json } from 'fp-ts/Json'
import * as R from 'fp-ts/Reader'
import * as RTE from 'fp-ts/ReaderTaskEither'
Expand Down Expand Up @@ -222,39 +223,41 @@ export const app = (config: ConfigEnv) => {
},
}),
)
.use((req, res, next) => {
createProxyMiddleware({
target: config.legacyPrereviewApi.url,
changeOrigin: true,
pathFilter: '/api/v2/',
on: {
proxyReq: (proxyReq, req) => {
const payload = {
url: `${proxyReq.protocol}//${proxyReq.host}${proxyReq.path}`,
method: proxyReq.method,
requestId: req.headers['fly-request-id'] ?? null,
}
.use(
asyncHandler(
createProxyMiddleware({
target: config.legacyPrereviewApi.url,
changeOrigin: true,
pathFilter: '/api/v2/',
on: {
proxyReq: (proxyReq, req) => {
const payload = {
url: `${proxyReq.protocol}//${proxyReq.host}${proxyReq.path}`,
method: proxyReq.method,
requestId: req.headers['fly-request-id'] ?? null,
}

L.debugP('Sending proxy HTTP request')(payload)(config)()
L.debugP('Sending proxy HTTP request')(payload)(config)()

proxyReq.once('response', response => {
L.debugP('Received proxy HTTP response')({
...payload,
status: response.statusCode as Json,
headers: response.headers as Json,
})(config)()
})
proxyReq.once('response', response => {
L.debugP('Received proxy HTTP response')({
...payload,
status: response.statusCode as Json,
headers: response.headers as Json,
})(config)()
})

proxyReq.once('error', error => {
L.warnP('Did not receive a proxy HTTP response')({
...payload,
error: error.message,
})(config)()
})
proxyReq.once('error', error => {
L.warnP('Did not receive a proxy HTTP response')({
...payload,
error: error.message,
})(config)()
})
},
},
},
})(req, res, next)?.catch(() => next())
})
}),
),
)
.use(slashes(false))
.use(express.urlencoded({ extended: true }))
.use((req, res, next) => {
Expand Down

0 comments on commit a61986a

Please sign in to comment.