Repository navigation
Expand file tree
/
Copy pathdevCache.ts
More file actions
109 lines (93 loc) · 3.21 KB
/
Copy pathdevCache.ts
File metadata and controls
109 lines (93 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { isDevelopmentEnvironment } from '../utils/runtimeEnvironment';
import type { AccountlessApplication, PublicKeylessApplication } from './types';
// 10 minutes in milliseconds
const THROTTLE_DURATION_MS = 10 * 60 * 1000;
export interface ClerkDevCache {
__cache: Map<string, { expiresAt: number; data?: unknown }>;
/**
* Log a message with throttling to prevent spam.
*/
log: (params: { cacheKey: string; msg: string }) => void;
/**
* Run an async callback with caching.
*/
run: <T>(
callback: () => Promise<T>,
options: {
cacheKey: string;
onSuccessStale?: number;
onErrorStale?: number;
},
) => Promise<T | undefined>;
}
declare global {
var __clerk_internal_keyless_logger: ClerkDevCache | undefined;
}
/**
* Creates a development-only cache for keyless mode logging and API calls.
* This prevents console spam and duplicate API requests.
*
* @returns The cache instance or undefined in non-development environments
*/
export function createClerkDevCache(): ClerkDevCache | undefined {
if (!isDevelopmentEnvironment()) {
return undefined;
}
if (!globalThis.__clerk_internal_keyless_logger) {
globalThis.__clerk_internal_keyless_logger = {
__cache: new Map<string, { expiresAt: number; data?: unknown }>(),
log: function ({ cacheKey, msg }) {
if (this.__cache.has(cacheKey) && Date.now() < (this.__cache.get(cacheKey)?.expiresAt || 0)) {
return;
}
console.log(msg);
this.__cache.set(cacheKey, {
expiresAt: Date.now() + THROTTLE_DURATION_MS,
});
},
run: async function (
callback,
{ cacheKey, onSuccessStale = THROTTLE_DURATION_MS, onErrorStale = THROTTLE_DURATION_MS },
) {
if (this.__cache.has(cacheKey) && Date.now() < (this.__cache.get(cacheKey)?.expiresAt || 0)) {
return this.__cache.get(cacheKey)?.data as ReturnType<typeof callback>;
}
try {
const result = await callback();
this.__cache.set(cacheKey, {
expiresAt: Date.now() + onSuccessStale,
data: result,
});
return result;
} catch (e) {
this.__cache.set(cacheKey, {
expiresAt: Date.now() + onErrorStale,
});
throw e;
}
},
};
}
return globalThis.__clerk_internal_keyless_logger;
}
/**
* Creates the console message shown when running in keyless mode.
*
* @param keys - The keyless application keys
* @returns Formatted console message
*/
export function createKeylessModeMessage(keys: AccountlessApplication | PublicKeylessApplication): string {
return `\n\x1b[35m\n[Clerk]:\x1b[0m You are running in keyless mode.\nYou can \x1b[35mclaim your keys\x1b[0m by visiting ${keys.claimUrl}\n`;
}
/**
* Creates the console message shown when keys have been claimed.
*
* @returns Formatted console message
*/
export function createConfirmationMessage(): string {
return `\n\x1b[35m\n[Clerk]:\x1b[0m Your application is running with your claimed keys.\nYou can safely remove the \x1b[35m.clerk/\x1b[0m from your project.\n`;
}
/**
* Shared singleton instance of the development cache.
*/
export const clerkDevelopmentCache = createClerkDevCache();