Delete proxy-auth.ts, types/index.ts, staff-user.ts+test, local-imports.ts+test (only used by their own tests). Remove unused foundation exports: sanitizeFilename, canonicalizeFormValue, canonicalizeFormData, ConflictError, getRequestStore, getClientIp, elapsed. Remove stale TODO comments from rank-authority.ts and notice.ts. Fix biome lint warnings in catalog-repair.ts.
41 lines
983 B
TypeScript
41 lines
983 B
TypeScript
import { AsyncLocalStorage } from "node:async_hooks";
|
|
import type { IpAddress, RequestId, UserId } from "./types";
|
|
|
|
export interface RequestStore {
|
|
requestId: RequestId;
|
|
ip: IpAddress;
|
|
userId: UserId | null;
|
|
startedAt: number;
|
|
}
|
|
|
|
const als = new AsyncLocalStorage<RequestStore>();
|
|
|
|
let counter = 0;
|
|
|
|
function generateRequestId(): RequestId {
|
|
counter = (counter + 1) & 0xffff;
|
|
return `${Date.now().toString(36)}-${counter.toString(36)}-${crypto.randomUUID().slice(0, 8)}` as RequestId;
|
|
}
|
|
|
|
export function createStore(ip: IpAddress): RequestStore {
|
|
return {
|
|
requestId: generateRequestId(),
|
|
ip,
|
|
userId: null,
|
|
startedAt: Date.now(),
|
|
};
|
|
}
|
|
|
|
export function runWithStore<T>(store: RequestStore, fn: () => T): T {
|
|
return als.run(store, fn);
|
|
}
|
|
|
|
export function getRequestId(): RequestId {
|
|
return als.getStore()?.requestId ?? generateRequestId();
|
|
}
|
|
|
|
export function setContextUserId(userId: UserId): void {
|
|
const store = als.getStore();
|
|
if (store) store.userId = userId;
|
|
}
|