From 87117868ab0cdc331fddac9c7a35ff816e98d947 Mon Sep 17 00:00:00 2001 From: c0sx Date: Sat, 21 Sep 2024 20:58:52 +0400 Subject: [PATCH 1/6] fix: handle missing route options --- index.js | 4 +++- package.json | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 3e9b888..0942bcc 100644 --- a/index.js +++ b/index.js @@ -221,7 +221,9 @@ async function fastifyUnderPressure (fastify, opts) { } function onRequest (req, reply, next) { - const _pressureHandler = req.routeOptions.config.pressureHandler || pressureHandler + const options = req.routeOptions ? req.routeOptions : req.context + const reqPressureHandler = options.config.pressureHandler + const _pressureHandler = reqPressureHandler || pressureHandler if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) { handlePressure(_pressureHandler, req, reply, next, TYPE_EVENT_LOOP_DELAY, eventLoopDelay) return diff --git a/package.json b/package.json index 7ca94d4..c473bf5 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "devDependencies": { "@fastify/pre-commit": "^2.0.2", "@types/node": "^20.1.0", - "fastify": "^4.0.0-rc.2", + "fastify": "^4.0.0", "semver": "^7.3.2", "simple-get": "^4.0.0", "sinon": "^18.0.0", From de94f8df51f868e496cb39e03a4d7febbf503801 Mon Sep 17 00:00:00 2001 From: c0sx Date: Sat, 21 Sep 2024 21:37:15 +0400 Subject: [PATCH 2/6] fix: support 4.0 version without routeOptions at all --- index.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 0942bcc..23c52d1 100644 --- a/index.js +++ b/index.js @@ -220,10 +220,17 @@ async function fastifyUnderPressure (fastify, opts) { return false } + function getReqConfig(req) { + if (req.routeOptions && req.routeOptions.config) { + return req.routeOptions.config + } + + return req.context.config + } + function onRequest (req, reply, next) { - const options = req.routeOptions ? req.routeOptions : req.context - const reqPressureHandler = options.config.pressureHandler - const _pressureHandler = reqPressureHandler || pressureHandler + const config = getReqConfig(req) + const _pressureHandler = config.pressureHandler || pressureHandler if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) { handlePressure(_pressureHandler, req, reply, next, TYPE_EVENT_LOOP_DELAY, eventLoopDelay) return From 18e963a8c8882e8d67b250b8e51a810556cd98ba Mon Sep 17 00:00:00 2001 From: c0sx Date: Sun, 22 Sep 2024 10:53:35 +0400 Subject: [PATCH 3/6] chore: return fastify version back --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c473bf5..7ca94d4 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "devDependencies": { "@fastify/pre-commit": "^2.0.2", "@types/node": "^20.1.0", - "fastify": "^4.0.0", + "fastify": "^4.0.0-rc.2", "semver": "^7.3.2", "simple-get": "^4.0.0", "sinon": "^18.0.0", From a6019173defc205561ee97150318ec47090eeb33 Mon Sep 17 00:00:00 2001 From: c0sx Date: Sun, 22 Sep 2024 12:01:51 +0400 Subject: [PATCH 4/6] refactor: get req config w/o function --- index.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/index.js b/index.js index 23c52d1..131fb11 100644 --- a/index.js +++ b/index.js @@ -220,16 +220,8 @@ async function fastifyUnderPressure (fastify, opts) { return false } - function getReqConfig(req) { - if (req.routeOptions && req.routeOptions.config) { - return req.routeOptions.config - } - - return req.context.config - } - function onRequest (req, reply, next) { - const config = getReqConfig(req) + const config = req.routeOptions && req.routeOptions.config ? req.routeOptions.config : req.context.config const _pressureHandler = config.pressureHandler || pressureHandler if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) { handlePressure(_pressureHandler, req, reply, next, TYPE_EVENT_LOOP_DELAY, eventLoopDelay) From 9d6a8cff96ad3038b63d02e5b4db2205e6416d4b Mon Sep 17 00:00:00 2001 From: c0sx Date: Sun, 22 Sep 2024 12:07:44 +0400 Subject: [PATCH 5/6] refactor: use optional chaining to get config --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 131fb11..fe44d83 100644 --- a/index.js +++ b/index.js @@ -221,7 +221,7 @@ async function fastifyUnderPressure (fastify, opts) { } function onRequest (req, reply, next) { - const config = req.routeOptions && req.routeOptions.config ? req.routeOptions.config : req.context.config + const config = req?.routeOptions?.config ?? req.context.config const _pressureHandler = config.pressureHandler || pressureHandler if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) { handlePressure(_pressureHandler, req, reply, next, TYPE_EVENT_LOOP_DELAY, eventLoopDelay) From a31eb9a68d1552435433c8674c4f63f9ab410423 Mon Sep 17 00:00:00 2001 From: c0sx Date: Sun, 22 Sep 2024 12:10:06 +0400 Subject: [PATCH 6/6] refactor: remove extra ?. operator --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index fe44d83..9fe80cd 100644 --- a/index.js +++ b/index.js @@ -221,7 +221,7 @@ async function fastifyUnderPressure (fastify, opts) { } function onRequest (req, reply, next) { - const config = req?.routeOptions?.config ?? req.context.config + const config = req.routeOptions?.config ?? req.context.config const _pressureHandler = config.pressureHandler || pressureHandler if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) { handlePressure(_pressureHandler, req, reply, next, TYPE_EVENT_LOOP_DELAY, eventLoopDelay)