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: add support for nested index routes (fixes #281) #282

Closed

Conversation

GlenTiki
Copy link

@GlenTiki GlenTiki commented Dec 9, 2023

Fixes #281. I'll comment on code to clarify reasoning.

Checklist

// This route enables fastify to accept preflight requests on all nested routes, as well as the index route in prefixed setups
// https://github.com/fastify/fastify-cors/issues/281
fastify.options('/*', handlerOptions, handler)
if (fastify.prefix) { fastify.options('/', handlerOptions, handler) }
Copy link
Author

Choose a reason for hiding this comment

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

without doing this if check existing tests break - this literally just fixes the case for the test I added.

I think on root index routes, fastify applies /* to the index route, but on prefixed routes, it doesn't?

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

@GlenTiki GlenTiki changed the title add support for nested index routes (fixes #281) fix: add support for nested index routes (fixes #281) Dec 11, 2023
@Fdawgs Fdawgs requested a review from a team December 12, 2023 20:37
Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

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

I am not at my PC but reading this i think the solution is wrong in case fastify.prefix is set. I mean, yes the test passes but because only this one case was patched.

I assume that you need to do

fastify.options(fastify.prefix + '*', handlerOptions, handler)

To fix it properly.

Blocking the merging.

@GlenTiki
Copy link
Author

@Uzlopak when you’re back at a desk can you take a look again and give further guidance on how to fix this? I tried some variations on your suggestion, but that fails the added test case.

Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

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

I reviewed this again.

Yes, your assumption regarding index seems to be right. But it feels like this is an error in upstream, either find-my-way or fastify itself.

So unfortunately I can not give you more hints.

Something is odd. Well this is my gut feeling.

@mcollina
@ivan-tymoshenko

@GlenTiki wrote;

I think on root index routes, fastify applies /* to the index route, but on prefixed routes, it doesn't?

Can this be a bug in upstream?

@ivan-tymoshenko
Copy link
Member

It looks like a bug in fastify for me.

@GlenTiki
Copy link
Author

GlenTiki commented Dec 14, 2023

I tested fastify with some simple tests which are passing (attached below). Meaning the issue lies in this repo.

I'm now digging in to see if the error lies in the hook this repo adds, but, given this solves the test case, it's an alternate valid solution 😅

Expand to see test
'use strict'

const t = require('tap')
const test = t.test
const Fastify = require('fastify')

test('Routing to the wildcard route works the same for prefixed or root route', async t => {
    t.plan(3)

    const fastify = Fastify()

    const handler = async (req, reply) => {
        return { custom: "body" }
    }

    fastify.route({ method: 'OPTIONS', url: '/*', handler })
    fastify.register(async (scope, opts) => {
        scope.route({ method: 'OPTIONS', url: '/*', handler })
    }, { prefix: '/prefix' })

    await fastify.ready()

    const result = (await fastify.inject({ method: "OPTIONS", path: '/' })).payload

    t.equal(result, JSON.stringify({ custom: "body" }))
    t.equal(result, (await fastify.inject({ method: "OPTIONS", path: '/prefix' })).payload)
    t.equal(result, (await fastify.inject({ method: "OPTIONS", path: '/prefix/' })).payload)
})


test('Routing to the wildcard route works the same for nested prefixed or root route', async t => {
    t.plan(3)

    const fastify = Fastify()

    const handler = async (req, reply) => {
        return { custom: "body" }
    }

    const nestedWildcardPlugin = async (scope, opts) => {
        scope.route({ method: 'OPTIONS', url: '/*', handler })
    }

    fastify.register(nestedWildcardPlugin)
    fastify.register(async (scope, opts) => {
        scope.register(nestedWildcardPlugin)
    }, { prefix: '/prefix' })

    await fastify.ready()

    const result = (await fastify.inject({ method: "OPTIONS", path: '/' })).payload

    t.equal(result, JSON.stringify({ custom: "body" }))
    t.equal(result, (await fastify.inject({ method: "OPTIONS", path: '/prefix' })).payload)
    t.equal(result, (await fastify.inject({ method: "OPTIONS", path: '/prefix/' })).payload)
})

EDIT: Found a failing case. Opening a bug report.

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.

@Uzlopak
Copy link
Contributor

Uzlopak commented Dec 15, 2023

@climba03003

I tested that and it didnt work. Can you provide a suggestion to fix this issue?

@climba03003
Copy link
Member

I would like to revert #278 after some investigation.
We cannot replace * with /* directly without sacrifice the ability to to replace response for options route.

We need another approach to fix the issue mentioned inside #278

@Fdawgs
Copy link
Member

Fdawgs commented Dec 17, 2023

I would like to revert #278 after some investigation.

We cannot replace * with /* directly without sacrifice the ability to to replace response for options route.

We need another approach to fix the issue mentioned inside #278

Sounds good!

@mcollina
Copy link
Member

@climba03003 can you open a PR and add a test to prevent the regression in the future?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

preflight 404 on prefixed index routes in nested/scopted cors setups
6 participants