Skip to content

Commit 62a22ae

Browse files
committed
Upgrade to Express 5
Now async routes are handled!
1 parent 7779039 commit 62a22ae

File tree

4 files changed

+54
-50
lines changed

4 files changed

+54
-50
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"body-parser": "^1.19.0",
7373
"cookie-parser": "^1.4.5",
7474
"env-paths": "^2.2.0",
75-
"express": "^4.17.1",
75+
"express": "^5.0.0-alpha.8",
7676
"fs-extra": "^9.0.1",
7777
"http-proxy": "^1.18.0",
7878
"httpolyglot": "^0.1.2",

src/node/http.ts

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -191,22 +191,19 @@ export const handleUpgrade = (app: express.Express, server: http.Server): void =
191191
}
192192

193193
/**
194-
* Patch Express routers to handle web sockets and async routes (since we have
195-
* to patch `get` anyway).
194+
* Patch Express routers to handle web sockets.
196195
*
197-
* Not using express-ws since the ws-wrapped sockets don't work with the proxy
198-
* and wildcards don't work correctly.
196+
* Not using express-ws since the ws-wrapped sockets don't work with the proxy.
199197
*/
200198
function patchRouter(): void {
201-
// Apparently this all works because Router is also the prototype assigned to
202-
// the routers it returns.
199+
// This works because Router is also the prototype assigned to the routers it
200+
// returns.
203201

204-
// Store these since the original methods will be overridden.
205-
const originalGet = (express.Router as any).get
206-
const originalPost = (express.Router as any).post
202+
// Store this since the original method will be overridden.
203+
const originalGet = (express.Router as any).prototype.get
207204

208205
// Inject the `ws` method.
209-
;(express.Router as any).ws = function ws(
206+
;(express.Router as any).prototype.ws = function ws(
210207
route: expressCore.PathParams,
211208
...handlers: express.WebSocketRequestHandler[]
212209
) {
@@ -216,41 +213,26 @@ function patchRouter(): void {
216213
const wrapped: express.Handler = (req, res, next) => {
217214
if (isWebSocketRequest(req)) {
218215
req._ws_handled = true
219-
Promise.resolve(handler(req, res, next)).catch(next)
220-
} else {
221-
next()
216+
return handler(req, res, next)
222217
}
218+
next()
223219
}
224220
return wrapped
225221
}),
226222
])
227223
return this
228224
}
229225
// Overwrite `get` so we can distinguish between websocket and non-websocket
230-
// routes. While we're at it handle async responses.
231-
;(express.Router as any).get = function get(route: expressCore.PathParams, ...handlers: express.Handler[]) {
226+
// routes.
227+
;(express.Router as any).prototype.get = function get(route: expressCore.PathParams, ...handlers: express.Handler[]) {
232228
originalGet.apply(this, [
233229
route,
234230
...handlers.map((handler) => {
235231
const wrapped: express.Handler = (req, res, next) => {
236232
if (!isWebSocketRequest(req)) {
237-
Promise.resolve(handler(req, res, next)).catch(next)
238-
} else {
239-
next()
233+
return handler(req, res, next)
240234
}
241-
}
242-
return wrapped
243-
}),
244-
])
245-
return this
246-
}
247-
// Handle async responses for `post` as well since we're in here anyway.
248-
;(express.Router as any).post = function post(route: expressCore.PathParams, ...handlers: express.Handler[]) {
249-
originalPost.apply(this, [
250-
route,
251-
...handlers.map((handler) => {
252-
const wrapped: express.Handler = (req, res, next) => {
253-
Promise.resolve(handler(req, res, next)).catch(next)
235+
next()
254236
}
255237
return wrapped
256238
}),
@@ -259,5 +241,5 @@ function patchRouter(): void {
259241
}
260242
}
261243

262-
// This needs to happen before anything uses the router.
244+
// This needs to happen before anything creates a router.
263245
patchRouter()

src/node/routes/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ export const register = async (app: Express, server: http.Server, args: Defaulte
5555
app.use(bodyParser.json())
5656
app.use(bodyParser.urlencoded({ extended: true }))
5757

58-
server.on("upgrade", () => {
59-
heart.beat()
60-
})
61-
6258
app.use(async (req, res, next) => {
6359
heart.beat()
6460

yarn.lock

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,10 +1421,10 @@ array-equal@^1.0.0:
14211421
resolved "https://registry.yarnpkg.com/array-equal/-/array-equal-1.0.0.tgz#8c2a5ef2472fd9ea742b04c77a75093ba2757c93"
14221422
integrity sha1-jCpe8kcv2ep0KwTHenUJO6J1fJM=
14231423

1424-
array-flatten@1.1.1:
1425-
version "1.1.1"
1426-
resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2"
1427-
integrity sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=
1424+
array-flatten@2.1.1:
1425+
version "2.1.1"
1426+
resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-2.1.1.tgz#426bb9da84090c1838d812c8150af20a8331e296"
1427+
integrity sha1-Qmu52oQJDBg42BLIFQryCoMx4pY=
14281428

14291429
array-includes@^3.1.1:
14301430
version "3.1.1"
@@ -2585,6 +2585,13 @@ [email protected], debug@^2.1.3, debug@^2.2.0, debug@^2.3.3, debug@^2.6.9:
25852585
dependencies:
25862586
ms "2.0.0"
25872587

2588+
2589+
version "3.1.0"
2590+
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
2591+
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
2592+
dependencies:
2593+
ms "2.0.0"
2594+
25882595
25892596
version "4.1.1"
25902597
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
@@ -3209,19 +3216,19 @@ expand-brackets@^2.1.4:
32093216
snapdragon "^0.8.1"
32103217
to-regex "^3.0.1"
32113218

3212-
express@^4.17.1:
3213-
version "4.17.1"
3214-
resolved "https://registry.yarnpkg.com/express/-/express-4.17.1.tgz#4491fc38605cf51f8629d39c2b5d026f98a4c134"
3215-
integrity sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==
3219+
express@^5.0.0-alpha.8:
3220+
version "5.0.0-alpha.8"
3221+
resolved "https://registry.yarnpkg.com/express/-/express-5.0.0-alpha.8.tgz#b9dd3a568eab791e3391db47f9e6ab91e61b13fe"
3222+
integrity sha512-PL8wTLgaNOiq7GpXt187/yWHkrNSfbr4H0yy+V0fpqJt5wpUzBi9DprAkwGKBFOqWHylJ8EyPy34V5u9YArfng==
32163223
dependencies:
32173224
accepts "~1.3.7"
3218-
array-flatten "1.1.1"
3225+
array-flatten "2.1.1"
32193226
body-parser "1.19.0"
32203227
content-disposition "0.5.3"
32213228
content-type "~1.0.4"
32223229
cookie "0.4.0"
32233230
cookie-signature "1.0.6"
3224-
debug "2.6.9"
3231+
debug "3.1.0"
32253232
depd "~1.1.2"
32263233
encodeurl "~1.0.2"
32273234
escape-html "~1.0.3"
@@ -3232,10 +3239,11 @@ express@^4.17.1:
32323239
methods "~1.1.2"
32333240
on-finished "~2.3.0"
32343241
parseurl "~1.3.3"
3235-
path-to-regexp "0.1.7"
3242+
path-is-absolute "1.0.1"
32363243
proxy-addr "~2.0.5"
32373244
qs "6.7.0"
32383245
range-parser "~1.2.1"
3246+
router "2.0.0-alpha.1"
32393247
safe-buffer "5.1.2"
32403248
send "0.17.1"
32413249
serve-static "1.14.1"
@@ -5489,7 +5497,7 @@ [email protected]:
54895497
resolved "https://registry.yarnpkg.com/parse5/-/parse5-5.1.0.tgz#c59341c9723f414c452975564c7c00a68d58acd2"
54905498
integrity sha512-fxNG2sQjHvlVAYmzBZS9YlDp6PTSSDwa98vkD4QgVDDCAo84z5X1t5XyJQ62ImdLXx5NdIIfihey6xpum9/gRQ==
54915499

5492-
parseurl@~1.3.3:
5500+
parseurl@~1.3.2, parseurl@~1.3.3:
54935501
version "1.3.3"
54945502
resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4"
54955503
integrity sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==
@@ -5519,7 +5527,7 @@ path-exists@^4.0.0:
55195527
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3"
55205528
integrity sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==
55215529

5522-
path-is-absolute@^1.0.0:
5530+
path-is-absolute@1.0.1, path-is-absolute@^1.0.0:
55235531
version "1.0.1"
55245532
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
55255533
integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=
@@ -6619,6 +6627,19 @@ rotating-file-stream@^2.1.1:
66196627
resolved "https://registry.yarnpkg.com/rotating-file-stream/-/rotating-file-stream-2.1.3.tgz#4b3cc8f56ae70b3e30ccdb4ee6b14d95e66b02bb"
66206628
integrity sha512-zZ4Tkngxispo7DgiTqX0s4ChLtM3qET6iYsDA9tmgDEqJ3BFgRq/ZotsKEDAYQt9pAn9JwwqT27CSwQt3CTxNg==
66216629

6630+
6631+
version "2.0.0-alpha.1"
6632+
resolved "https://registry.yarnpkg.com/router/-/router-2.0.0-alpha.1.tgz#9188213b972215e03ef830e0ac77837870085f6d"
6633+
integrity sha512-fz/T/qLkJM6RTtbqGqA1+uZ88ejqJoPyKeJAeXPYjebA7HzV/UyflH4gXWqW/Y6SERnp4kDwNARjqy6se3PcOw==
6634+
dependencies:
6635+
array-flatten "2.1.1"
6636+
debug "3.1.0"
6637+
methods "~1.1.2"
6638+
parseurl "~1.3.2"
6639+
path-to-regexp "0.1.7"
6640+
setprototypeof "1.1.0"
6641+
utils-merge "1.0.1"
6642+
66226643
run-parallel@^1.1.9:
66236644
version "1.1.9"
66246645
resolved "https://registry.yarnpkg.com/run-parallel/-/run-parallel-1.1.9.tgz#c9dd3a7cf9f4b2c4b6244e173a6ed866e61dd679"
@@ -6736,6 +6757,11 @@ setimmediate@^1.0.4:
67366757
resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.5.tgz#290cbb232e306942d7d7ea9b83732ab7856f8285"
67376758
integrity sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=
67386759

6760+
6761+
version "1.1.0"
6762+
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.1.0.tgz#d0bd85536887b6fe7c0d818cb962d9d91c54e656"
6763+
integrity sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ==
6764+
67396765
67406766
version "1.1.1"
67416767
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.1.1.tgz#7e95acb24aa92f5885e0abef5ba131330d4ae683"

0 commit comments

Comments
 (0)