{"version":3,"sources":["../../src/stores/memory.ts"],"sourcesContent":["import type { DPoPNonceStore } from \"./types.js\";\n\nexport interface MemoryNonceStoreOptions {\n\t/**\n\t * Maximum entries before FIFO eviction of the oldest entry. Default: 100_000.\n\t * Set to a higher value if your peak QPS × jtiTtl exceeds this.\n\t */\n\tmaxSize?: number;\n\t/** Minimum interval between background sweeps, in milliseconds (default: 60_000). */\n\tsweepInterval?: number;\n}\n\nexport interface MemoryNonceStore extends DPoPNonceStore {\n\t/** Number of entries currently held (including expired but not yet swept). */\n\treadonly size: number;\n}\n\nconst DEFAULT_SWEEP_INTERVAL = 60_000;\nconst DEFAULT_MAX_SIZE = 100_000;\n\nexport function memoryNonceStore(options: MemoryNonceStoreOptions = {}): MemoryNonceStore {\n\tconst maxSize = options.maxSize ?? DEFAULT_MAX_SIZE;\n\tconst sweepInterval = options.sweepInterval ?? DEFAULT_SWEEP_INTERVAL;\n\tconst map = new Map<string, number>();\n\tlet lastSweep = Number.NEGATIVE_INFINITY;\n\n\tconst sweepIfDue = (now: number): void => {\n\t\tif (now - lastSweep < sweepInterval) return;\n\t\tlastSweep = now;\n\t\tfor (const [jti, exp] of map) {\n\t\t\tif (exp <= now) map.delete(jti);\n\t\t}\n\t};\n\n\treturn {\n\t\tget size() {\n\t\t\treturn map.size;\n\t\t},\n\n\t\tasync check(jti, expiresAt) {\n\t\t\tconst now = Date.now();\n\t\t\tsweepIfDue(now);\n\n\t\t\tconst existingExp = map.get(jti);\n\t\t\tif (existingExp !== undefined && existingExp > now) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tmap.set(jti, expiresAt);\n\t\t\tif (map.size > maxSize) {\n\t\t\t\tconst oldest = map.keys().next().value;\n\t\t\t\tif (oldest !== undefined && oldest !== jti) map.delete(oldest);\n\t\t\t}\n\t\t\treturn true;\n\t\t},\n\n\t\tasync purge() {\n\t\t\tconst now = Date.now();\n\t\t\tlet count = 0;\n\t\t\tfor (const [jti, exp] of map) {\n\t\t\t\tif (exp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