Files
Kamil Mysliwiec 15537943ce fix(node): keep the async context of a request across its body (#18302)
The built-in body parsers call next() from the "end" event of the
request, which Node.js emits in the async context of the socket. For
every request with a body, the handler, the interceptors and the
response hook therefore lost an AsyncLocalStorage store that a request
hook or an earlier middleware entered, as tracing and request-context
libraries do. body-parser (through raw-body) and the content type
parser of Fastify bind that callback to the context they were called
in, so the store survives on Express and Fastify.

A parser that has a body to read now binds next() to the context it
was called in. It does so with an AsyncResource and runInAsyncScope(),
as the interceptors of @nestjs/core do, rather than with
AsyncResource.bind(), which is about 25 times slower: it adds about
45 ns to a request with a body, and nothing to one without.
2026-10-27 12:35:45 +01:00
..