thiagobustamante / typescript-rest

This is a lightweight annotation-based expressjs extension for typescript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any request with body parser does not correctly use middleware for cls-hooked

jeroen-plug opened this issue · comments

When using cls-hooked to store a request context, it does not work if the body parser is used.
I made a small example project to reproduce the issue: mwe.zip

Ignoring the body works just fine:
curl -X POST http://localhost:3000/nobody -H 'Content-Type: application/json' -d '{"key": "value"}'

If the body is parsed, cls-hooked throws an exception:
curl -X POST http://localhost:3000/body -H 'Content-Type: application/json' -d '{"key": "value"}'

If the body-parser is disabled it works again, but then the body can't be used
curl -X POST http://localhost:3000/noparse -H 'Content-Type: application/json' -d '{"key": "value"}'

Middleware code:

const namespace = cls.createNamespace('context');

app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
    namespace.run(() => next());
});

/body endpoint code:

@POST
@Path("body")
withBody(body: any): string {
    namespace.set("test", "value");
    return "Hello " + JSON.stringify(body);
}

While using cls-hooked we have also observed that somehow the namespace.active is null on some code path.

Interestingly it is available for all functions running inside "most requests", but it is not available when running in services / methods protected by @Security (i.e. we have set up our own authenticator).

To fix it, for now, we add another middleware in the initialize of our ServiceAuthenticator implementation ... But I'd be interested in knowing the actual root cause.