-
-
Notifications
You must be signed in to change notification settings - Fork 2.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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure that fastify Request is encapsulated #2598
Comments
It is not possible to achieve this reliably and in a performant way (I already stumbled on this a few times unfortunately). I think we should document this for v3 and maybe add a better API in v4. |
We could support a
|
That'd slow things down considerably. Moreover, we have hooks for this kind of work and it's as is as adding an |
IMO this issue is really important. The circumstance that the instance is shared across requests is very confusing. This provides room for memory-leaks as shown above. I'd always prefer correctness over absolute speed. @mcollina do you mean that |
i'd assume that : fastify.decorateRequest('array', function() { return [] }, { factory: true }) will result in subsequent const fn = function() { return [] };
function onRequest (request, reply, done) {
reply.array = fn()
done()
} |
The problem is that we can't know how to creat a new instance identical to the value as can be of any custom type or class... and the only solution would be an unreliable (and slow) deep copy. I think we should deprecate We could potentially add a new API that allows to set a factory for the instance value, but this is already covered by hooks. |
Defaulting to an empty string is totally valid. Really, any primitive is valid. Why do you say it isn't? |
Right! All value types are good, including functions. It's really objects, arrays, set and maps that are problematic. |
Agree on emitting a warning per these types |
Hello, I want to attempt to tackle this issue. Please let me know if this issue no longer needs assistance. |
Sure! Could you target the How do you plan to solve this? |
What's left here? We now emit a warning if an object is decorated, and others have said complete encapsulation will hurt performance Though I must admit I like the idea of knowing for sure that a completed request is gotten rid from memory, feels safer 馃槃 |
馃殌 Feature Proposal
Fastify
Request
object is bound to the HTTP request lifecycle.As a developer, I would expect that fastify creates a new
Request
object for every new request. While this is the case, fastify keeps object references across requests. This is due to the nature of prototype inheritance here.This makes the code vulnerable to memory leaks: fastify/fastify-multipart#158 (comment)
We need to ensure that all properties are reset.
Motivation
As in other HTTP frameworks, the
Request
object is a new instance on every request. The developer shouldn't need to care about it. I propose to rethink that strategy because it's confusing and error-prone.Example
The text was updated successfully, but these errors were encountered: