-
-
Notifications
You must be signed in to change notification settings - Fork 850
feat(request): return new Request with cached body in raw getter #4425
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
base: main
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Bundle size check
Compiler Diagnostics (tsc)
Compiler Diagnostics (typescript-go)
Reported by octocov |
HTTP Performance Benchmark
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4425 +/- ##
==========================================
+ Coverage 91.25% 91.28% +0.02%
==========================================
Files 171 171
Lines 10913 10933 +20
Branches 3144 3149 +5
==========================================
+ Hits 9959 9980 +21
+ Misses 953 952 -1
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
kamaal111
left a comment
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.
In HonoRequest, where we don't need to access the raw body, can we use the private _raw instead?
This might improve the performance slightly
|
|
||
| // Second call to req.raw.json() - should work with cached body | ||
| const secondResult = await req.raw.json() | ||
| expect(secondResult).toEqual(data) |
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.
Can you also verify that it still works as expected when try to get the body for the third time?
To switch it up a bit we can also use .text which would still result in getting the cached body and constructing a new Request object
This PR will fix the problem that we wanted to resolve with #4382.
Problem
The following code will cause the error:
This is because when you do
c.req.raw, the body to be used is already consumed. This is explained in #4382 (comment).Solution
In this PR, it will create a new Request object with a cached body when
c.req.rawis called. The reason why it does not use getter/setter and usesObject.definePropertyis that if it were to use getter/setter, it would fail with the spread syntax{...c.req}.Caveat
The code size will be increased.
The performance may degrade, but it's small.
The author should do the following, if applicable
bun run format:fix && bun run lint:fixto format the code