Skip to content

Commit

Permalink
Remove debug logging for URL rewrite (#4754)
Browse files Browse the repository at this point in the history
* Remove debug logging for URL rewrite

* Use url rewrite params object for future compatibility

Amend test to ensure logger param is present

* Change rewriteUrl params to 'this'

* Call rewriteUrl with this bound to the Fastify instance

* Improve docs for rewriteUrl

Co-authored-by: James Sumners <james@sumners.email>

---------

Co-authored-by: James Sumners <james@sumners.email>
  • Loading branch information
brettwillis and jsumners committed May 23, 2023
1 parent ca75d4c commit 410957f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
12 changes: 10 additions & 2 deletions docs/Reference/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,16 @@ URLs.
> Rewriting a URL will modify the `url` property of the `req` object
```js
function rewriteUrl (req) { // req is the Node.js HTTP request
return req.url === '/hi' ? '/hello' : req.url;
// @param {object} req The raw Node.js HTTP request, not the `FastifyRequest` object.
// @this Fastify The root Fastify instance (not an encapsulated instance).
// @returns {string} The path that the request should be mapped to.
function rewriteUrl (req) {
if (req.url === '/hi') {
this.log.debug({ originalUrl: req.url, url: '/hello' }, 'rewrite url');
return '/hello'
} else {
return req.url;
}
}
```
Expand Down
7 changes: 6 additions & 1 deletion fastify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ declare namespace fastify {
req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, TypeProvider>,
res: FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, RequestGeneric, FastifyContextConfig, SchemaCompiler, TypeProvider>
) => void,
rewriteUrl?: (req: RawRequestDefaultExpression<RawServer>) => string,
rewriteUrl?: (
// The RawRequestDefaultExpression, RawReplyDefaultExpression, and FastifyTypeProviderDefault parameters
// should be narrowed further but those generic parameters are not passed to this FastifyServerOptions type
this: FastifyInstance<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, Logger, FastifyTypeProviderDefault>,
req: RawRequestDefaultExpression<RawServer>
) => string,
schemaErrorFormatter?: SchemaErrorFormatter,
/**
* listener to error events emitted by client connections
Expand Down
16 changes: 6 additions & 10 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,16 +794,12 @@ function fastify (options) {
// 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 {
const err = new FST_ERR_ROUTE_REWRITE_NOT_STR(req.url, typeof url)
req.destroy(err)
}
const url = rewriteUrl.call(fastify, req)
if (typeof url === 'string') {
req.url = url
} else {
const err = new FST_ERR_ROUTE_REWRITE_NOT_STR(req.url, typeof url)
req.destroy(err)
}
}
router.routing(req, res, buildAsyncConstraintCallback(isAsync, req, res))
Expand Down
5 changes: 4 additions & 1 deletion test/types/fastify.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ expectAssignable<FastifyInstance>(fastify({
}))
expectAssignable<FastifyInstance>(fastify({ frameworkErrors: () => { } }))
expectAssignable<FastifyInstance>(fastify({
rewriteUrl: (req) => req.url === '/hi' ? '/hello' : req.url!
rewriteUrl: function (req) {
this.log.debug('rewrite url')
return req.url === '/hi' ? '/hello' : req.url!
}
}))
expectAssignable<FastifyInstance>(fastify({
schemaErrorFormatter: (errors, dataVar) => {
Expand Down
3 changes: 3 additions & 0 deletions test/url-rewriting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ test('Should rewrite url', t => {
const fastify = Fastify({
rewriteUrl (req) {
t.equal(req.url, '/this-would-404-without-url-rewrite')
this.log.info('rewriting url')
return '/'
}
})
Expand Down Expand Up @@ -43,6 +44,7 @@ test('Should not rewrite if the url is the same', t => {
const fastify = Fastify({
rewriteUrl (req) {
t.equal(req.url, '/this-would-404-without-url-rewrite')
this.log.info('rewriting url')
return req.url
}
})
Expand Down Expand Up @@ -74,6 +76,7 @@ test('Should throw an error', t => {
const fastify = Fastify({
rewriteUrl (req) {
t.equal(req.url, '/this-would-404-without-url-rewrite')
this.log.info('rewriting url')
return undefined
}
})
Expand Down

0 comments on commit 410957f

Please sign in to comment.