typescript-rest icon indicating copy to clipboard operation
typescript-rest copied to clipboard

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

Open jeroen-plug opened this issue 6 years ago • 1 comments
trafficstars

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);
}

jeroen-plug avatar Sep 26 '19 16:09 jeroen-plug

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.

tsimbalar avatar May 14 '20 06:05 tsimbalar