import { AsyncLocalStorage } from 'async_hooks'; import Fiber from 'fibers'; interface IContextStore { getStore(): T | undefined; run(store: T, callback: (...args: any) => void, ...args: any): void; } // This is the default implementation of the context store but there is a bug on Meteor 2.5 that prevents us from using it export class AsyncContextStore extends AsyncLocalStorage implements IContextStore {} export class FibersContextStore implements IContextStore { getStore(): T | undefined { return Fiber.current as unknown as T; } run(store: T, callback: (...args: any) => void, ...args: any): void { // eslint-disable-next-line new-cap return Fiber((...rest: any) => { const fiber = Fiber.current as Record; for (const key in store) { if (store.hasOwnProperty(key)) { fiber[key] = store[key]; } } Fiber.yield(callback(...rest)); }).run(...args); } }