import {Inject, Injectable, PlatformContext, PlatformHandler} from "@tsed/common";
import {Logger} from "@tsed/logger";
import {AsyncLocalStorage} from "async_hooks";

@Injectable()
export class PlatformAsyncHookContext {
  @Inject()
  protected logger: Logger;

  @Inject()
  protected platformHandler: PlatformHandler;

  protected store: AsyncLocalStorage<PlatformContext>;

  getContext() {
    return this.store?.getStore();
  }

  $onInit() {
    /* istanbul ignore */
    if (AsyncLocalStorage) {
      this.store = new AsyncLocalStorage();
      // override
      this.platformHandler.run = (ctx: PlatformContext, cb: any) => {
        return PlatformAsyncHookContext.run(ctx, cb, this.store);
      };
    } else {
      this.logger.warn(
        `AsyncLocalStorage is not available for your Node.js version (${process.versions.node}). Please upgrade your version at least to v13.10.0.`
      );
    }
  }

  static run(ctx: PlatformContext, cb: any, store = new AsyncLocalStorage()) {
    return store.run(ctx, cb);
  }
}

import {Inject, Injectable, PlatformContext, PlatformHandler, PlatformTest} from "@tsed/common";
import {Logger} from "@tsed/logger";
import {AsyncLocalStorage} from "async_hooks";

let store: AsyncLocalStorage<PlatformContext>;

@Injectable()
export class PlatformAsyncHookContext {
  @Inject()
  protected logger: Logger;

  @Inject()
  protected platformHandler: PlatformHandler;

  getContext() {
    return store?.getStore();
  }

  static getStore() {
    store = store || new AsyncLocalStorage();
    return store;
  }

  run = (ctx: PlatformContext, cb: any) => {
    return PlatformAsyncHookContext.run(ctx, cb);
  };

  $onInit() {
    /* istanbul ignore */
    if (AsyncLocalStorage) {
      PlatformAsyncHookContext.getStore();
      // override
      this.platformHandler.run = this.run;
    } else {
      this.logger.warn(
        `AsyncLocalStorage is not available for your Node.js version (${process.versions.node}). Please upgrade your version at least to v13.10.0.`
      );
    }
  }

  static run(ctx: PlatformContext, cb: any) {
    return PlatformAsyncHookContext.getStore().run(ctx, cb);
  }
}

Fix asyncHookContext when running unit test
fix(async-hook-context): Fix asyncHookContext when running unit test
