index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var context_storage_exports = {};
  19. __export(context_storage_exports, {
  20. contextStorage: () => contextStorage,
  21. getContext: () => getContext,
  22. tryGetContext: () => tryGetContext
  23. });
  24. module.exports = __toCommonJS(context_storage_exports);
  25. var import_node_async_hooks = require("node:async_hooks");
  26. const asyncLocalStorage = new import_node_async_hooks.AsyncLocalStorage();
  27. const contextStorage = () => {
  28. return async function contextStorage2(c, next) {
  29. await asyncLocalStorage.run(c, next);
  30. };
  31. };
  32. const tryGetContext = () => {
  33. return asyncLocalStorage.getStore();
  34. };
  35. const getContext = () => {
  36. const context = tryGetContext();
  37. if (!context) {
  38. throw new Error("Context is not available");
  39. }
  40. return context;
  41. };
  42. // Annotate the CommonJS export names for ESM import in node:
  43. 0 && (module.exports = {
  44. contextStorage,
  45. getContext,
  46. tryGetContext
  47. });