Skip to content

Commit

Permalink
refactor(async-hooks-validation): moved validation in one place and c…
Browse files Browse the repository at this point in the history
…hanged following already present validation and @Eomm suggestions
  • Loading branch information
giuliowaitforitdavide committed Oct 2, 2023
1 parent 1bba51f commit 4a62008
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
19 changes: 3 additions & 16 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const {
} = errorCodes

const { buildErrorHandler } = require('./lib/error-handler.js')
const { validateAsyncHooks } = require('./lib/validation.js')

function defaultBuildPrettyMeta (route) {
// return a shallow copy of route's sanitized context
Expand Down Expand Up @@ -616,22 +617,8 @@ function fastify (options) {
throw new errorCodes.FST_ERR_HOOK_INVALID_HANDLER(name, fn)
}

if (name === 'onSend' || name === 'preSerialization' || name === 'onError' || name === 'preParsing') {
if (fn.constructor.name === 'AsyncFunction' && fn.length === 4) {
throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
} else if (name === 'onReady' || name === 'onListen') {
if (fn.constructor.name === 'AsyncFunction' && fn.length !== 0) {
throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
} else if (name === 'onRequestAbort') {
if (fn.constructor.name === 'AsyncFunction' && fn.length !== 1) {
throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
} else {
if (fn.constructor.name === 'AsyncFunction' && fn.length === 3) {
throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
if (!validateAsyncHooks(name, fn)) {
throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}

if (name === 'onClose') {
Expand Down
5 changes: 3 additions & 2 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const warning = require('./warnings')

const {
compileSchemasForValidation,
compileSchemasForSerialization
compileSchemasForSerialization,
validateAsyncHooks
} = require('./validation')

const {
Expand Down Expand Up @@ -270,7 +271,7 @@ function buildRouting (options) {
throw new FST_ERR_HOOK_INVALID_HANDLER(hook, Object.prototype.toString.call(func))
}

if (func.constructor.name === 'AsyncFunction' && func.length === 3) {
if (!validateAsyncHooks(hook, func)) {
throw new FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
}
Expand Down
23 changes: 22 additions & 1 deletion lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,26 @@ function validateAsyncHeaders (validatePromise, context, request) {
})
}

function validateAsyncHooks (name, fn) {
if (fn.constructor.name !== 'AsyncFunction') {
return true
}

const validateParametersLengthFunction = {
onSend: length => !(length === 4),
preSerialization: length => !(length === 4),
onError: length => !(length === 4),
preParsing: length => !(length === 4),
onReady: length => !(length !== 0),
onListen: length => !(length !== 0),
onRequestAbort: length => !(length !== 1)
}

const defaultValidator = length => !(length === 3)

return (validateParametersLengthFunction[name] ?? defaultValidator)(fn.length)
}

function wrapValidationError (result, dataVar, schemaErrorFormatter) {
if (result instanceof Error) {
result.statusCode = result.statusCode || 400
Expand All @@ -237,5 +257,6 @@ module.exports = {
symbols: { bodySchema, querystringSchema, responseSchema, paramsSchema, headersSchema },
compileSchemasForValidation,
compileSchemasForSerialization,
validate
validate,
validateAsyncHooks
}
2 changes: 1 addition & 1 deletion test/hooks-async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ t.test('nested hooks to do not crash on 404', t => {
})
})

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

Expand Down

0 comments on commit 4a62008

Please sign in to comment.