index.js 558 B

1234567891011121314151617181920212223
  1. // src/middleware/context-storage/index.ts
  2. import { AsyncLocalStorage } from "node:async_hooks";
  3. var asyncLocalStorage = new AsyncLocalStorage();
  4. var contextStorage = () => {
  5. return async function contextStorage2(c, next) {
  6. await asyncLocalStorage.run(c, next);
  7. };
  8. };
  9. var tryGetContext = () => {
  10. return asyncLocalStorage.getStore();
  11. };
  12. var getContext = () => {
  13. const context = tryGetContext();
  14. if (!context) {
  15. throw new Error("Context is not available");
  16. }
  17. return context;
  18. };
  19. export {
  20. contextStorage,
  21. getContext,
  22. tryGetContext
  23. };