Files
overleaf-cep/services/web/app/src/infrastructure/AsyncLocalStorage.js
T
Jessica Lawshe a9a28a13f5 Merge pull request #28402 from overleaf/jel-link-logged-in-async-local-storage
[web] Extend `AsyncLocalStorage` to SAML linking in route, clear `AsyncLocalStorage` on email updates, await analytics helper on email updates,

GitOrigin-RevId: 86a51e6800a4b954ff81a2d977edf1401064dda4
2025-09-17 08:05:30 +00:00

39 lines
829 B
JavaScript

// @ts-check
const { AsyncLocalStorage } = require('node:async_hooks')
/**
* @typedef {Object} RequestContext
* @property {Object.<string, array>} [userFullEmails] - Dictionary mapping userId to an array of full emails
*/
/** @type {AsyncLocalStorage<RequestContext>} */
const asyncLocalStorage = new AsyncLocalStorage()
/**
* @param {import("express").Request} req
* @param {import("express").Response} res
* @param {import("express").NextFunction} next
*/
function middleware(req, res, next) {
asyncLocalStorage.run({}, next)
}
/**
* Remove a key from the AsyncLocalStorage cache
*
* @param {string} key
*/
function removeItem(key) {
const store = asyncLocalStorage.getStore()
if (store?.[key]) {
delete store[key]
}
}
module.exports = {
middleware,
storage: asyncLocalStorage,
removeItem,
}