@@ -4,18 +4,11 @@ import { debugLogger } from '@/utils/debug';
44import { TokenId } from '@/utils/tokenId' ;
55
66import { POLLER_INTERVAL_IN_MS } from './auth/SessionCookiePoller' ;
7+ import { createKeyResolver , type TokenCacheKeyJSON } from './keyResolver' ;
78import { Token } from './resources/internal' ;
89import { pickFreshestJwt } from './tokenFreshness' ;
910import { createTokenStore } from './tokenStore' ;
1011
11- /**
12- * Identifies a cached token entry by tokenId and optional audience.
13- */
14- interface TokenCacheKeyJSON {
15- audience ?: string ;
16- tokenId : string ;
17- }
18-
1912/**
2013 * Cache entry containing token metadata and resolver.
2114 * Extends TokenCacheKeyJSON with additional properties for expiration tracking and token retrieval.
@@ -105,9 +98,6 @@ export interface TokenCache {
10598 size ( ) : number ;
10699}
107100
108- const KEY_PREFIX = 'clerk' ;
109- const DELIMITER = '::' ;
110-
111101/**
112102 * Default seconds before token expiration to trigger background refresh.
113103 * This threshold accounts for timer jitter, SafeLock contention (~5s), network latency,
@@ -122,36 +112,6 @@ const BACKGROUND_REFRESH_THRESHOLD_IN_SECONDS = 15;
122112const BROADCAST = { broadcast : true } ;
123113const NO_BROADCAST = { broadcast : false } ;
124114
125- /**
126- * Converts between cache key objects and string representations.
127- * Format: `prefix::tokenId::audience`
128- */
129- export class TokenCacheKey {
130- /**
131- * Parses a cache key string into a TokenCacheKey instance.
132- */
133- static fromKey ( key : string ) : TokenCacheKey {
134- const [ prefix , tokenId , audience = '' ] = key . split ( DELIMITER ) ;
135- return new TokenCacheKey ( prefix , { audience, tokenId } ) ;
136- }
137-
138- constructor (
139- public prefix : string ,
140- public data : TokenCacheKeyJSON ,
141- ) {
142- this . prefix = prefix ;
143- this . data = data ;
144- }
145-
146- /**
147- * Converts the key to its string representation for Map storage.
148- */
149- toKey ( ) : string {
150- const { tokenId, audience } = this . data ;
151- return [ this . prefix , tokenId , audience || '' ] . join ( DELIMITER ) ;
152- }
153- }
154-
155115/**
156116 * Message format for BroadcastChannel token synchronization between tabs.
157117 */
@@ -173,8 +133,9 @@ const generateTabId = (): string => {
173133 * Automatically manages token expiration and cleanup via scheduled timeouts.
174134 * BroadcastChannel support is enabled whenever the environment provides it.
175135 */
176- const MemoryTokenCache = ( prefix = KEY_PREFIX ) : TokenCache => {
136+ const MemoryTokenCache = ( prefix ?: string ) : TokenCache => {
177137 const store = createTokenStore < TokenCacheValue > ( ) ;
138+ const keyResolver = createKeyResolver ( prefix ) ;
178139 const tabId = generateTabId ( ) ;
179140
180141 let broadcastChannel : BroadcastChannel | null = null ;
@@ -213,8 +174,8 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
213174 const get = ( cacheKeyJSON : TokenCacheKeyJSON ) : TokenCacheGetResult | undefined => {
214175 ensureBroadcastChannel ( ) ;
215176
216- const cacheKey = new TokenCacheKey ( prefix , cacheKeyJSON ) ;
217- const value = store . get ( cacheKey . toKey ( ) ) ;
177+ const key = keyResolver . toKey ( cacheKeyJSON ) ;
178+ const value = store . get ( key ) ;
218179
219180 if ( ! value ) {
220181 return ;
@@ -233,7 +194,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
233194 if ( value . refreshTimeoutId !== undefined ) {
234195 clearTimeout ( value . refreshTimeoutId ) ;
235196 }
236- store . delete ( cacheKey . toKey ( ) ) ;
197+ store . delete ( key ) ;
237198 return ;
238199 }
239200
@@ -344,13 +305,11 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
344305 * @param options - Configuration for cache behavior; broadcast controls whether to notify other tabs
345306 */
346307 const setInternal = ( entry : TokenCacheEntry , options : { broadcast : boolean } = BROADCAST ) => {
347- const cacheKey = new TokenCacheKey ( prefix , {
308+ const key = keyResolver . toKey ( {
348309 audience : entry . audience ,
349310 tokenId : entry . tokenId ,
350311 } ) ;
351312
352- const key = cacheKey . toKey ( ) ;
353-
354313 // Clear timers from any existing entry for this key to prevent orphaned
355314 // refresh timers from accumulating across set() calls (e.g., from
356315 // #hydrateCache during _updateClient AND #refreshTokenInBackground).
0 commit comments