Skip to content
Myr

memory.ts

This file

Path

apps/platform/src/auth/accounts/memory.ts

115 lines · 3.2 KiB · TypeScript

Last changed by

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

History of this file

Take 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–2why, one step back
1import { Effect, Layer, Option, Ref } from "effect";
2

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

lines 3–10why, one step back
3import {
4 Accounts,5 EmailTaken,6 emailKey,7 handleKey,8 HandleTaken,9} from "../accounts.ts";10import type { Account, Email } from "../accounts.ts";

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

line 11why, one step back
11

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

lines 12–23why, one step back
12const holderOf = (
13 accounts: ReadonlyMap<string, Account>,14 email: Email15): Option.Option<Account> =>16 Option.fromUndefinedOr(17 [...accounts.values()].find(18 (account) =>19 account.email !== undefined &&20 emailKey(account.email) === emailKey(email)21 )22 );23

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 24–27why, one step back
24export const AccountsMemory = {
25 layer: Layer.effect(26 Accounts,27 Effect.gen(function* () {

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

line 28why, one step back
28 const store = yield* Ref.make<ReadonlyMap<string, Account>>(new Map());

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 29–30why, one step back
29
30 return Accounts.of({

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

lines 31–78why, one step back
31 create: (account) =>
32 Effect.gen(function* () {33 const key = handleKey(account.handle);3435 const refused = yield* Ref.modify(36 store,37 (38 accounts39 ): readonly [40 Option.Option<HandleTaken | EmailTaken>,41 ReadonlyMap<string, Account>,42 ] => {43 if (accounts.has(key)) {44 return [45 Option.some(new HandleTaken({ handle: account.handle })),46 accounts,47 ];48 }4950 if (51 account.email !== undefined &&52 Option.isSome(holderOf(accounts, account.email))53 ) {54 return [55 Option.some(new EmailTaken({ email: account.email })),56 accounts,57 ];58 }5960 return [Option.none(), new Map(accounts).set(key, account)];61 }62 );6364 return yield* Option.isSome(refused)65 ? Effect.fail(refused.value)66 : Effect.void;67 }),68 find: (handle) =>69 Ref.get(store).pipe(70 Effect.map((accounts) =>71 Option.fromUndefinedOr(accounts.get(handleKey(handle)))72 )73 ),74 findByEmail: (email) =>75 Ref.get(store).pipe(76 Effect.map((accounts) => holderOf(accounts, email))77 ),78 setEmail: (handle, email) =>

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 79–81why, one step back
79 Ref.modify(store, (accounts) => {
80 const key = handleKey(handle);81

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

lines 82–94why, one step back
82 const account = accounts.get(key);
8384 const holder = holderOf(accounts, email);8586 // As D1's update, which changes no row for a handle nobody holds.87 if (88 account === undefined ||89 Option.exists(holder, (held) => handleKey(held.handle) !== key)90 ) {91 return [false, accounts];92 }9394 return [true, new Map(accounts).set(key, { ...account, email })];

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 95–96why, one step back
95 }).pipe(
96 Effect.filterOrFail(

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

lines 97–98why, one step back
97 (set) => set,
98 () => new EmailTaken({ email })

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 99–115why, one step back
99 ),
100 Effect.asVoid101 ),102 setPasswordHash: (handle, hash) =>103 Ref.update(store, (accounts) => {104 const key = handleKey(handle);105106 const account = accounts.get(key);107108 return account === undefined109 ? accounts110 : new Map(accounts).set(key, { ...account, hash });111 }),112 });113 })114 ),115};