Jeff-Lewis / cls-hooked

cls-hooked : CLS using AsynWrap or async_hooks instead of async-listener for node 4.7+

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CLS Hooks with Node v10.0.0

davidadas opened this issue · comments

I'm currently running into an issue where my previous method for handing-off information via CLS no longer works in Node v10.0.0 (although it did before):

const bindRequestContext = async (ctx, next) => new Promise(
    namespace.bind(async (resolve, reject) => {
        try {
            await next();
            resolve();
        } catch (err) {
            reject(err);
        }
    })
);

const appLoggingMiddleware = async (ctx, next) => bindRequestContext(ctx, async () => {
    const id = cuid();
    namespace.set("requestId", requestId);
    await next();
});

const userLoggingMiddleware = async (ctx, next) => bindRequestContext(ctx, async () => {
    const userId = ctx.getUserId();
    namespace.set("userId", userId);
    await next();
});

When I attach these to KOA routers, the second call fails saying that I need to run bind or run on the context first.

Not sure if this has anything to do with the async_hooks addition or not. I was unable to find a connection myself.

Any guidance would be appreciated.

Closing as it only appears to be an issue in 10.0.0 -> 10.2.0. Those aren't going to be LTS versions anyway.