Skip to content

Commit bf472be

Browse files
snomiaoclaude
andcommitted
fix: remove top-level await from ghc.ts to resolve Vercel build errors
- Wrap directory creation and Keyv initialization in async functions - Initialize Keyv lazily when first needed - Fix top-level await in test section by wrapping in async function - This resolves build issues in environments that don't support top-level await 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent ebbebf8 commit bf472be

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

src/ghc.ts

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@ const DEFAULT_TTL = process.env.LOCAL_DEV
2222
? 30 * 60 * 1000 // cache 30 minutes when local dev
2323
: 1 * 60 * 1000; // cache 1 minute in production
2424

25-
await fs.mkdir(CACHE_DIR, { recursive: true });
25+
async function ensureCacheDir() {
26+
await fs.mkdir(CACHE_DIR, { recursive: true });
27+
}
2628

27-
const keyv = new Keyv({
28-
store: new KeyvSqlite(CACHE_FILE),
29-
ttl: DEFAULT_TTL,
30-
});
29+
let keyv: Keyv | null = null;
30+
31+
async function getKeyv() {
32+
if (!keyv) {
33+
await ensureCacheDir();
34+
keyv = new Keyv({
35+
store: new KeyvSqlite(CACHE_FILE),
36+
ttl: DEFAULT_TTL,
37+
});
38+
}
39+
return keyv;
40+
}
3141

3242
function createCacheKey(basePath: string[], prop: string | symbol, args: any[]): string {
3343
// Create a deterministic key from the path and arguments
@@ -58,9 +68,10 @@ function createCachedProxy(target: any, basePath: string[] = []): any {
5868
if (typeof value === "function") {
5969
return async function (...args: any[]) {
6070
const cacheKey = createCacheKey(basePath, prop, args);
71+
const keyvInstance = await getKeyv();
6172

6273
// Try to get from cache first
63-
const cached = await keyv.get(cacheKey);
74+
const cached = await keyvInstance.get(cacheKey);
6475
if (cached !== undefined) {
6576
// console.log(`HIT|${cacheKey}`); // cache hit info for debug
6677
return cached;
@@ -70,7 +81,7 @@ function createCachedProxy(target: any, basePath: string[] = []): any {
7081
const result = await value.apply(obj, args);
7182

7283
// Cache the result
73-
await keyv.set(cacheKey, result);
84+
await keyvInstance.set(cacheKey, result);
7485

7586
return result;
7687
};
@@ -95,7 +106,8 @@ type DeepAsyncWrapper<T> = {
95106
};
96107

97108
export async function clearGhCache(): Promise<void> {
98-
await keyv.clear();
109+
const keyvInstance = await getKeyv();
110+
await keyvInstance.clear();
99111
}
100112

101113
export async function getGhCacheStats(): Promise<{ size: number; keys: string[] }> {
@@ -108,22 +120,26 @@ export const ghc = createCachedProxy(gh) as DeepAsyncWrapper<typeof gh>;
108120

109121
// manual test with real api
110122
if (import.meta.main) {
111-
// Test the cached client
112-
console.log("Testing cached GitHub client...");
113-
114-
// This should make a real API call
115-
const result1 = await ghc.repos.get({
116-
owner: "octocat",
117-
repo: "Hello-World",
118-
});
119-
console.log("First call result:", result1.data.name);
120-
121-
// This should use cache
122-
const result2 = await ghc.repos.get({
123-
owner: "octocat",
124-
repo: "Hello-World",
125-
});
126-
console.log("Second call result (cached):", result2.data.name);
123+
async function runTest() {
124+
// Test the cached client
125+
console.log("Testing cached GitHub client...");
126+
127+
// This should make a real API call
128+
const result1 = await ghc.repos.get({
129+
owner: "octocat",
130+
repo: "Hello-World",
131+
});
132+
console.log("First call result:", result1.data.name);
133+
134+
// This should use cache
135+
const result2 = await ghc.repos.get({
136+
owner: "octocat",
137+
repo: "Hello-World",
138+
});
139+
console.log("Second call result (cached):", result2.data.name);
140+
141+
console.log("Cache test complete!");
142+
}
127143

128-
console.log("Cache test complete!");
144+
runTest().catch(console.error);
129145
}

0 commit comments

Comments
 (0)