Open History to see the last commit
1import type {
2 AuthAccessSnapshot,
3 AuthAccessStreamEvent,
4 AuthAccessStreamSnapshotEvent,
5 AuthEnvironmentScope,
6 AuthSessionId,
7} from "@t3tools/contracts";
8import { WS_METHODS } from "@t3tools/contracts";
9import * as Stream from "effect/Stream";
10import type { HttpClient } from "effect/unstable/http";
11import { Atom } from "effect/unstable/reactivity";
12
13import type { EnvironmentRegistry } from "../connection/registry.ts";
14import { subscribe } from "../rpc/client.ts";
15export { EnvironmentNotConnectedError } from "./authHttp.ts";
16import {
17 createEnvironmentPairingCredential,
18 fetchEnvironmentSessionState,
19 revokeEnvironmentClientSession,
20 revokeEnvironmentPairingLink,
21 revokeOtherEnvironmentClientSessions,
22} from "./authHttp.ts";
23import {
24 createEnvironmentCommand,
25 createEnvironmentQueryAtomFamily,
26 createEnvironmentSubscriptionAtomFamily,
27} from "./runtime.ts";
28
29export const EMPTY_AUTH_ACCESS_SNAPSHOT: AuthAccessSnapshot = {
30 pairingLinks: [],
31 clientSessions: [],
32};
33
34function upsertByKey<A>(
35 values: ReadonlyArray<A>,
36 next: A,
37 key: (value: A) => string,
38): ReadonlyArray<A> {
39 const nextKey = key(next);
40 return [...values.filter((value) => key(value) !== nextKey), next];
41}
42
43export function applyAuthAccessStreamEvent(
44 current: AuthAccessSnapshot,
45 event: AuthAccessStreamEvent,
46): AuthAccessSnapshot {
47 switch (event.type) {
48 case "snapshot":
49 return event.payload;
50 case "pairingLinkUpserted":
51 return {
52 ...current,
53 pairingLinks: upsertByKey(current.pairingLinks, event.payload, (value) => value.id),
54 };
55 case "pairingLinkRemoved":
56 return {
57 ...current,
58 pairingLinks: current.pairingLinks.filter((value) => value.id !== event.payload.id),
59 };
60 case "clientUpserted":
61 return {
62 ...current,
63 clientSessions: upsertByKey(
64 current.clientSessions,
65 event.payload,
66 (value) => value.sessionId,
67 ),
68 };
69 case "clientRemoved":
70 return {
71 ...current,
72 clientSessions: current.clientSessions.filter(
73 (value) => value.sessionId !== event.payload.sessionId,
74 ),
75 };
76 }
77}
78
79function projectAuthAccessSnapshot(
80 current: AuthAccessSnapshot,
81 event: AuthAccessStreamEvent,
82): readonly [AuthAccessSnapshot, ReadonlyArray<AuthAccessStreamEvent>] {
83 const snapshot = applyAuthAccessStreamEvent(current, event);
84 const projected: AuthAccessStreamSnapshotEvent = {
85 version: 1,
86 revision: event.revision,
87 type: "snapshot",
88 payload: snapshot,
89 };
90 return [snapshot, [projected]];
91}
92
93export function createAuthEnvironmentAtoms<R, E>(
94 runtime: Atom.AtomRuntime<EnvironmentRegistry | HttpClient.HttpClient | R, E>,
95) {
96 return {
97 accessChanges: createEnvironmentSubscriptionAtomFamily(runtime, {
98 label: "environment-data:server:auth-access-changes",
99 subscribe: (_input: null) =>
100 subscribe(WS_METHODS.subscribeAuthAccess, {}).pipe(
101 Stream.mapAccum(() => EMPTY_AUTH_ACCESS_SNAPSHOT, projectAuthAccessSnapshot),
102 ),
103 }),
104 sessionState: createEnvironmentQueryAtomFamily(runtime, {
105 label: "environment-data:server:auth-session-state",
106 execute: (_input: null) => fetchEnvironmentSessionState(),
107 }),
108 createPairingCredential: createEnvironmentCommand(runtime, {
109 label: "environment-command:server:create-pairing-credential",
110 execute: (input: {
111 readonly label?: string;
112 readonly scopes?: ReadonlyArray<AuthEnvironmentScope>;
113 }) => createEnvironmentPairingCredential(input),
114 }),
115 revokePairingLink: createEnvironmentCommand(runtime, {
116 label: "environment-command:server:revoke-pairing-link",
117 execute: (input: { readonly id: string }) => revokeEnvironmentPairingLink(input),
118 }),
119 revokeClientSession: createEnvironmentCommand(runtime, {
120 label: "environment-command:server:revoke-client-session",
121 execute: (input: { readonly sessionId: AuthSessionId }) =>
122 revokeEnvironmentClientSession(input),
123 }),
124 revokeOtherClientSessions: createEnvironmentCommand(runtime, {
125 label: "environment-command:server:revoke-other-client-sessions",
126 execute: (_input: null) => revokeOtherEnvironmentClientSessions(),
127 }),
128 };
129}