Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(async-hooks): mixing async and callback style in preHandler option now returns an error #5069

Merged
merged 11 commits into from
Oct 3, 2023
Merged
7 changes: 6 additions & 1 deletion lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const {
FST_ERR_ROUTE_METHOD_NOT_SUPPORTED,
FST_ERR_ROUTE_METHOD_INVALID,
FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED,
FST_ERR_ROUTE_BODY_LIMIT_OPTION_NOT_INT
FST_ERR_ROUTE_BODY_LIMIT_OPTION_NOT_INT,
FST_ERR_HOOK_INVALID_ASYNC_HANDLER
} = require('./errors')

const {
Expand Down Expand Up @@ -268,6 +269,10 @@ function buildRouting (options) {
if (typeof func !== 'function') {
throw new FST_ERR_HOOK_INVALID_HANDLER(hook, Object.prototype.toString.call(func))
}

if (func.constructor.name === 'AsyncFunction' && func.length === 3) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not all hooks have the same parameters:

'onTimeout' - 3
'onRequest' - 3
'preParsing' - 4
'preValidation' - 3
'preSerialization' - 4
'preHandler' - 3
'onSend' - 4
'onResponse' - 3
'onError' - 4
'onRequestAbort - 2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following your suggestion and the check done here I came up with a small refactor. Do you think it's worth it or should I revert it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does make sense 👍🏼
but I think we should not rewrite too much the code as the additional functions and datastructure may slowdown the start time

throw new FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
}
} else if (opts[hook] !== undefined && typeof opts[hook] !== 'function') {
throw new FST_ERR_HOOK_INVALID_HANDLER(hook, Object.prototype.toString.call(opts[hook]))
Expand Down
45 changes: 35 additions & 10 deletions test/hooks-async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,8 @@ test('Should log a warning if is an async function with `done`', t => {
try {
fastify.addHook('onRequestAbort', async (req, done) => {})
} catch (e) {
t.ok(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.ok(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
t.equal(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.equal(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
}
})

Expand All @@ -757,8 +757,8 @@ test('Should log a warning if is an async function with `done`', t => {
try {
fastify.addHook('onRequest', async (req, reply, done) => {})
} catch (e) {
t.ok(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.ok(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
t.equal(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.equal(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
}
})

Expand All @@ -769,20 +769,20 @@ test('Should log a warning if is an async function with `done`', t => {
try {
fastify.addHook('onSend', async (req, reply, payload, done) => {})
} catch (e) {
t.ok(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.ok(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
t.equal(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.equal(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
}
try {
fastify.addHook('preSerialization', async (req, reply, payload, done) => {})
} catch (e) {
t.ok(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.ok(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
t.equal(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.equal(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
}
try {
fastify.addHook('onError', async (req, reply, payload, done) => {})
} catch (e) {
t.ok(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.ok(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
t.equal(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.equal(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
}
})

Expand Down Expand Up @@ -923,3 +923,28 @@ t.test('nested hooks to do not crash on 404', t => {
t.equal(res.statusCode, 404)
})
})

test('Register an hook after a plugin inside a plugin (with preHandler option) should fail if mixing async and callback style', t => {
giuliowaitforitdavide marked this conversation as resolved.
Show resolved Hide resolved
t.plan(2)
const fastify = Fastify()

try {
fastify.get(
'/',
{
preHandler: [
async (request, reply, done) => {
done()
}
]
},
async (request, reply) => {
return { hello: 'world' }
}
)
t.fail('preHandler mixing async and callback style')
} catch (e) {
t.equal(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.equal(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
}
})