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

adding type for fastify.routing #3270

Merged
merged 2 commits into from Aug 21, 2021

Conversation

axe-me
Copy link
Contributor

@axe-me axe-me commented Aug 19, 2021

adding missing type declaration for https://www.fastify.io/docs/latest/Server/#routing

@github-actions github-actions bot added the typescript TypeScript related label Aug 19, 2021
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Could you add a test for those types? We use tsd.

@axe-me
Copy link
Contributor Author

axe-me commented Aug 20, 2021

@mcollina Thanks for reviewing this PR. I just added a simple test, please take a look.

@climba03003 climba03003 requested a review from a team August 20, 2021 02:33
Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

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

At first glance, no problem. But I never use this method, I cannot confirm if the type is correct.

Copy link
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

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

Side note: I was unable to find a test for this method in the test/ folder.

This snippet just let the server crash (I think it creates an infinite loop)

const fastify = require('fastify')({ logger: true })
fastify.get('/', async (request, reply) => {
  fastify.routing(request.raw, reply.raw)
  return { hello: 'world' }
})
fastify.listen(8080)

types/instance.d.ts Show resolved Hide resolved
@climba03003
Copy link
Member

climba03003 commented Aug 20, 2021

fastify.routing(request.raw, reply.raw)

@Eomm Yes, it is created an infinite loop. This method found the handler and it will execute the handler.

When you use it inside the handler to pass it's own Request and Response. It will found and execute itself again and again.

@axe-me
Copy link
Contributor Author

axe-me commented Aug 20, 2021

Side note: I was unable to find a test for this method in the test/ folder.

This snippet just let the server crash (I think it creates an infinite loop)

const fastify = require('fastify')({ logger: true })
fastify.get('/', async (request, reply) => {
  fastify.routing(request.raw, reply.raw)
  return { hello: 'world' }
})
fastify.listen(8080)

Hi @Eomm, I think this instance method is for handling requests from other instance or HTTP server. e.g.:

const naviteHttpServer = http.createServer((req, res) => {
  fastify.routing(req, res)
})

this is my use case at least, https://github.com/axe-me/vite-plugin-node/blob/main/src/server/fastify.ts#L8

This method is quite useful for passing raw requests from another frameworks like express/koa to fastify programmatically without using a HTTP proxy.

cc @climba03003

Copy link
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

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

Thanks for sharing your use case.
We could add a safe-check to avoid this crash, but it is not about your PR.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit fb29ece into fastify:main Aug 21, 2021
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
typescript TypeScript related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants