Skip to content

Commit 4b4886b

Browse files
committed
Add missing TSDocs
1 parent dc2d1f0 commit 4b4886b

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

packages/clerk-js/src/core/tokenCache.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { debugLogger } from '@/utils/debug';
44

55
import { Token } from './resources/internal';
66

7+
/**
8+
* Identifies a cached token entry by tokenId and optional audience.
9+
*/
710
interface TokenCacheKeyJSON {
811
audience?: string;
912
tokenId: string;
@@ -52,11 +55,14 @@ export interface TokenBroadcastMetadata {
5255

5356
type Seconds = number;
5457

58+
/**
59+
* Internal cache value containing the entry, expiration metadata, and cleanup timer.
60+
*/
5561
interface TokenCacheValue {
5662
createdAt: Seconds;
5763
entry: TokenCacheEntry;
58-
expiresIn?: Seconds;
5964
expiresAt?: Seconds;
65+
expiresIn?: Seconds;
6066
timeoutId?: ReturnType<typeof setTimeout>;
6167
}
6268

@@ -105,7 +111,14 @@ const LEEWAY = 10;
105111
// This value should have the same value as the INTERVAL_IN_MS in SessionCookiePoller
106112
const SYNC_LEEWAY = 5;
107113

114+
/**
115+
* Converts between cache key objects and string representations.
116+
* Format: `prefix::tokenId::audience`
117+
*/
108118
export class TokenCacheKey {
119+
/**
120+
* Parses a cache key string into a TokenCacheKey instance.
121+
*/
109122
static fromKey(key: string): TokenCacheKey {
110123
const [prefix, tokenId, audience = ''] = key.split(DELIMITER);
111124
return new TokenCacheKey(prefix, { audience, tokenId });
@@ -119,16 +132,26 @@ export class TokenCacheKey {
119132
this.data = data;
120133
}
121134

135+
/**
136+
* Converts the key to its string representation for Map storage.
137+
*/
122138
toKey(): string {
123139
const { tokenId, audience } = this.data;
124140
return [this.prefix, tokenId, audience || ''].join(DELIMITER);
125141
}
126142
}
127143

144+
/**
145+
* Generates a unique token identifier from session context.
146+
* Format: `sessionId-template-organizationId` (omitting falsy values).
147+
*/
128148
const computeTokenId = (sessionId: string, template?: string, organizationId?: string | null): string => {
129149
return [sessionId, template, organizationId].filter(Boolean).join('-');
130150
};
131151

152+
/**
153+
* Message format for BroadcastChannel token synchronization between tabs.
154+
*/
132155
interface SessionTokenEvent {
133156
organizationId?: string | null;
134157
sessionId: string;
@@ -138,6 +161,10 @@ interface SessionTokenEvent {
138161
traceId: string;
139162
}
140163

164+
/**
165+
* Creates an in-memory token cache with BroadcastChannel synchronization across tabs.
166+
* Automatically manages token expiration and cleanup via scheduled timeouts.
167+
*/
141168
const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
142169
const cache = new Map<string, TokenCacheValue>();
143170

@@ -199,6 +226,10 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
199226
return value.entry;
200227
};
201228

229+
/**
230+
* Processes token updates from other tabs via BroadcastChannel.
231+
* Validates token ID, parses JWT, and updates cache if token is newer than existing entry.
232+
*/
202233
const handleBroadcastMessage = async ({ data }: MessageEvent<SessionTokenEvent>) => {
203234
const expectedTokenId = computeTokenId(data.sessionId, data.template, data.organizationId);
204235
if (data.tokenId !== expectedTokenId) {
@@ -320,6 +351,10 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
320351
}
321352
};
322353

354+
/**
355+
* Internal cache setter that stores an entry and schedules expiration cleanup.
356+
* Resolves the token promise to extract expiration claims and set a deletion timeout.
357+
*/
323358
const setInternal = (entry: TokenCacheEntry) => {
324359
const cacheKey = new TokenCacheKey(prefix, {
325360
audience: entry.audience,

0 commit comments

Comments
 (0)