Skip to content

Ensure that fastify Request is encapsulated #2598

@StarpTech

Description

@StarpTech

🚀 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

fastify.decorateRequest('array', [])

fastify.post('/', async function (req, reply) {
    req.array.push(1)
    reply.code(200).send()
})

http.request(...) // array = [1]
http.request(...) // array = [1, 1]
.....

[ 1 ]
[ 1, 1 ]
[ 1, 1, 1 ]
[ 1, 1, 1, 1 ]
[ 1, 1, 1, 1, 1 ]
[ 1, 1, 1, 1, 1, 1 ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestNew feature to be addedv5.xIssue or pr related to Fastify v5

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions