const { AsyncLocalStorage } = require("async_hooks")

const storage = new AsyncLocalStorage()

function requestContextMiddleware(req, res, next) {
  const context = {
    method: req.method,
    path: req.originalUrl || req.url,
  }
  storage.run(context, () => next())
}

function getRequestContext() {
  return storage.getStore() || null
}

module.exports = {
  requestContextMiddleware,
  getRequestContext,
}
