memory.ts
This file
Path
apps/platform/src/auth/attempts/memory.ts
44 lines · 1.2 KiB · TypeScript
Last changed by
Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–44why, one step back1import { Clock, Duration, Effect, Layer, Ref } from "effect";23import { AttemptPolicy, SignInAttempts } from "../attempts.ts";45interface Window {6 readonly count: number;7 readonly startedAt: number;8}910export const SignInAttemptsMemory = {11 layer: Layer.effect(12 SignInAttempts,13 Effect.gen(function* () {14 const windows = yield* Ref.make(new Map<string, Window>());1516 const period = Duration.toMillis(AttemptPolicy.period);1718 return SignInAttempts.of({19 admit: (key) =>20 Effect.gen(function* () {21 const now = yield* Clock.currentTimeMillis;2223 return yield* Ref.modify(windows, (all) => {24 const open = all.get(key);2526 if (open === undefined || now - open.startedAt >= period) {27 return [28 true,29 new Map(all).set(key, { count: 1, startedAt: now }),30 ];31 }3233 return open.count < AttemptPolicy.limit34 ? [35 true,36 new Map(all).set(key, { ...open, count: open.count + 1 }),37 ]38 : [false, all];39 });40 }),41 });42 })43 ),44};