Line data Source code
1 : /**
2 : * The scope port: the one thing the billing core needs to know about the
3 : * identity it runs as — which user, which workspace, which role — and when
4 : * that changes. A host that holds a browser session client adapts it with
5 : * `sessionBillingScopeSource`; a host whose credentials live on its own
6 : * server reads the scope out of its bootstrap payload instead, and never
7 : * hands the core a session client it does not have.
8 : */
9 : import type { BillingSession } from './billingContracts'
10 : import type { AccountCredential } from '#/core/sessionContracts'
11 :
12 : export interface BillingScope {
13 : readonly userId: string
14 : readonly workspaceId: string
15 : readonly role: AccountCredential['role']
16 : }
17 :
18 : export interface BillingScopeContext {
19 : readonly scope: BillingScope
20 : readonly generation: number
21 : }
22 :
23 : export interface BillingScopeTracker {
24 : capture: () => BillingScopeContext | undefined
25 : isCurrent: (context: BillingScopeContext) => boolean
26 : dispose: () => void
27 : }
28 :
29 : /**
30 : * Where the core learns its scope. `getScope` is undefined whenever no scope
31 : * is established — signed out, or not settled yet — and `subscribe` reports
32 : * that anything about it may have changed.
33 : */
34 : export interface BillingScopeSource {
35 : readonly getScope: () => BillingScope | undefined
36 : readonly subscribe: (listener: () => void) => () => void
37 : }
38 :
39 0 : export function sameBillingScope(a: BillingScope, b: BillingScope): boolean {
40 0 : return (
41 : a.userId === b.userId &&
42 : a.workspaceId === b.workspaceId &&
43 : a.role === b.role
44 : )
45 : }
46 :
47 : type BillingScopeSession = Pick<BillingSession, 'getSnapshot' | 'subscribe'>
48 :
49 : /** The scope a workspace session client is currently minted for. */
50 0 : export function sessionBillingScopeSource(
51 : session: BillingScopeSession
52 : ): BillingScopeSource {
53 0 : return {
54 : getScope: () => readSessionScope(session),
55 : subscribe: (listener) => session.subscribe(listener)
56 : }
57 : }
58 :
59 0 : export function createBillingScopeTracker(
60 : source: BillingScopeSource,
61 : onChange: () => void
62 : ): BillingScopeTracker {
63 0 : let scope = source.getScope()
64 0 : let generation = 0
65 :
66 : const unsubscribe = source.subscribe(() => {
67 0 : const next = source.getScope()
68 0 : if (
69 : (scope === undefined && next === undefined) ||
70 : (scope !== undefined &&
71 : next !== undefined &&
72 : sameBillingScope(scope, next))
73 : ) {
74 0 : return
75 : }
76 :
77 0 : scope = next
78 0 : generation++
79 0 : onChange()
80 : })
81 :
82 0 : return {
83 : capture: () => (scope === undefined ? undefined : { scope, generation }),
84 : isCurrent: (context) =>
85 : context.generation === generation &&
86 : scope !== undefined &&
87 : sameBillingScope(context.scope, scope),
88 : dispose: unsubscribe
89 : }
90 : }
91 :
92 0 : function readSessionScope(
93 : session: BillingScopeSession
94 : ): BillingScope | undefined {
95 0 : const state = session.getSnapshot()
96 0 : if (state.user === null || state.session === undefined) return undefined
97 0 : return {
98 : userId: state.user.uid,
99 : workspaceId: state.session.workspace.id,
100 : role: state.session.role
101 : }
102 : }
|