Record usernames for manual provisioning requests

This commit is contained in:
2026-10-08 00:15:19 -04:00
parent 6a897df716
commit 2f67482521
12 changed files with 122 additions and 5 deletions
+17
View File
@@ -0,0 +1,17 @@
import { AsyncLocalStorage } from 'node:async_hooks';
const requester = new AsyncLocalStorage<{ username: string | null; active: boolean }>();
/** Use only the authenticated session, and stop attribution when the request finishes. */
export async function withRequestUsername<T>(username: string | null, work: () => T | Promise<T>): Promise<T> {
const context = { username, active: true };
return requester.run(context, async () => {
try { return await work(); }
finally { context.active = false; }
});
}
export function currentRequestUsername(): string | null {
const context = requester.getStore();
return context?.active ? context.username : null;
}