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
feat: support async constraint #4323
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ const { | |
const { defaultInitOptions } = getSecuredInitialConfig | ||
|
||
const { | ||
FST_ERR_ASYNC_CONSTRAINT, | ||
FST_ERR_BAD_URL, | ||
FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE | ||
} = errorCodes | ||
|
@@ -182,7 +183,7 @@ function fastify (options) { | |
const fourOhFour = build404(options) | ||
|
||
// HTTP server and its handler | ||
const httpHandler = wrapRouting(router.routing, options) | ||
const httpHandler = wrapRouting(router, options) | ||
|
||
// we need to set this before calling createServer | ||
options.http2SessionTimeout = initialConfig.http2SessionTimeout | ||
|
@@ -666,6 +667,30 @@ function fastify (options) { | |
res.end(body) | ||
} | ||
|
||
function buildAsyncConstraintCallback (isAsync, req, res) { | ||
if (isAsync === false) return undefined | ||
return function onAsyncConstraintError (err) { | ||
if (err) { | ||
if (frameworkErrors) { | ||
const id = genReqId(req) | ||
const childLogger = logger.child({ reqId: id }) | ||
|
||
childLogger.info({ req }, 'incoming request') | ||
|
||
const request = new Request(id, null, req, null, childLogger, onBadUrlContext) | ||
const reply = new Reply(res, request, childLogger) | ||
return frameworkErrors(new FST_ERR_ASYNC_CONSTRAINT(), request, reply) | ||
} | ||
const body = '{"error":"Internal Server Error","message":"Unexpected error from async constraint","statusCode":500}' | ||
res.writeHead(500, { | ||
'Content-Type': 'application/json', | ||
'Content-Length': body.length | ||
}) | ||
res.end(body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you evaluate The only difference is whether or not emit an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not quite understand what you means. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I needed to refresh this
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
} | ||
|
||
function setNotFoundHandler (opts, handler) { | ||
throwIfAlreadyStarted('Cannot call "setNotFoundHandler" when fastify instance is already started!') | ||
|
||
|
@@ -722,6 +747,27 @@ function fastify (options) { | |
opts.includeMeta = opts.includeHooks ? opts.includeMeta ? supportedHooks.concat(opts.includeMeta) : supportedHooks : opts.includeMeta | ||
return router.printRoutes(opts) | ||
} | ||
|
||
function wrapRouting (router, { rewriteUrl, logger }) { | ||
let isAsync | ||
return function preRouting (req, res) { | ||
// only call isAsyncConstraint once | ||
if (isAsync === undefined) isAsync = router.isAsyncConstraint() | ||
if (rewriteUrl) { | ||
const originalUrl = req.url | ||
const url = rewriteUrl(req) | ||
if (originalUrl !== url) { | ||
logger.debug({ originalUrl, url }, 'rewrite url') | ||
if (typeof url === 'string') { | ||
req.url = url | ||
} else { | ||
req.destroy(new Error(`Rewrite url for "${req.url}" needs to be of type "string" but received "${typeof url}"`)) | ||
climba03003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
router.routing(req, res, buildAsyncConstraintCallback(isAsync, req, res)) | ||
} | ||
} | ||
} | ||
|
||
fastify.errorCodes = errorCodes | ||
|
@@ -734,25 +780,6 @@ function validateSchemaErrorFormatter (schemaErrorFormatter) { | |
} | ||
} | ||
|
||
function wrapRouting (httpHandler, { rewriteUrl, logger }) { | ||
if (!rewriteUrl) { | ||
return httpHandler | ||
} | ||
return function preRouting (req, res) { | ||
const originalUrl = req.url | ||
const url = rewriteUrl(req) | ||
if (originalUrl !== url) { | ||
logger.debug({ originalUrl, url }, 'rewrite url') | ||
if (typeof url === 'string') { | ||
req.url = url | ||
} else { | ||
req.destroy(new Error(`Rewrite url for "${req.url}" needs to be of type "string" but received "${typeof url}"`)) | ||
} | ||
} | ||
httpHandler(req, res) | ||
} | ||
} | ||
|
||
/** | ||
* These export configurations enable JS and TS developers | ||
* to consumer fastify in whatever way best suits their needs. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Fdawgs Do you have time to helps me check the document changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @climba03003, missed this. Will take a look next week.