-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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(server): prevent 404 error in fetch adapter when endpoint is set to '/' #4893
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@maddsua is attempting to deploy a commit to the trpc Team on Vercel. A member of the Team first needs to authorize it. |
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.
Please add a regression test 🙏
I have one more thing in mind to check, but at the same time I'm not sure how to add a regression test in this project |
9b82c5242a250d832549e9f424099a03901be67c Ensures that this change won't have any effect in case user has defined a procedure that starts with forward slash, like this: '/getUserById': t.procedure.input(z.string()).query((opts) => {
return users[opts.input]; // input type is string
}), |
It looks like it won't be possible to just add another test inside https://github.com/trpc/trpc/blob/main/packages/tests/server/adapters/fetch.test.ts, as the server there starts once and it only uses empty endpoint path |
Although, I should probably revert this one, as it actually breaks things. |
This reverts commit 9b82c52.
Sorry for too many messages, the tests in this project are a bit overwhelming for me. I've done basic testing locally, but still not sure how to do this properly |
I can't push to your branch, here's a patch: But I tried with that patch and I couldn't get the test to fail when I reverted your change: |
you can run the tests with pnm vitest fetch.test |
The test is not gonna fail unless there is a procedure with a URL-like name. Thanks for the patch, I'll try to bring it to my branch |
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 for the slow response on this - could you join our Discord and @ me there and we'll push this over the line?
Had a bit too many open stale PRs and my GitHub notifications are a mess so I just forgot about this
@@ -20,7 +20,7 @@ export async function fetchRequestHandler<TRouter extends AnyRouter>( | |||
}; | |||
|
|||
const url = new URL(opts.req.url); | |||
const path = url.pathname.slice(opts.endpoint.length + 1); | |||
const path = url.pathname.slice(opts.endpoint === '/' ? 1 : opts.endpoint.length + 1); |
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.
I feel like this is a bit too implicit - isn't it possible to look at the url
defined above? Is the problem that .pathname
starts with a /
?
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.
I've joined discord, no response there yet. Anyway. The problem is that without this change trpc assumes that if endpoint path is not empty it starts with forward slash and has characters after it. Which doesn't work if endpoint path is set to a single slash with no other characters after. It's a slight oversight in algorithm, as this length + 1
in fact implies that endpoint path has two slashes needed to be removed and here he have just one, so it just "eats" first character of procedure name instead
# Conflicts: # packages/server/src/adapters/fetch/fetchRequestHandler.ts
This reverts commit 74b72e6.
# Conflicts: # packages/tests/server/adapters/fetch.test.ts
Extended this pr with a fix for trailing slash: #5067. |
Head branch was pushed to by a user without write access
🎯 Changes
Fixed:
Setting
endpoint
property to '/' infetchRequestHandler
function call will result in url path being trimmed and in a "NOT_FOUND" error in all request attemptsworker.ts:
Issue
Expected behavior: request succeeds
Actual behavior (error log):
Got this error while trying to setup trpc on cloudflare workers. Actually it's my first interaction with trpc, so please don't judge me too much
Upd: having a trailing slash in endpoing path also causes 404 error