Skip to content

Commit 3ce1bd1

Browse files
committed
Simplify D1 database access and add fallback to LocalStorage for Edge Runtime compatibility
1 parent 62072a5 commit 3ce1bd1

File tree

4 files changed

+48
-67
lines changed

4 files changed

+48
-67
lines changed

src/app/api/debug/env/route.ts

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,22 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { NextRequest, NextResponse } from 'next/server';
2+
import { NextResponse } from 'next/server';
33

44
export const runtime = 'edge';
55

6-
export async function GET(request: NextRequest) {
6+
export async function GET() {
77
try {
8-
const debugInfo = {
8+
return NextResponse.json({
9+
status: 'ok',
910
timestamp: new Date().toISOString(),
10-
environment: {
11-
NODE_ENV: process.env.NODE_ENV,
12-
NEXT_PUBLIC_STORAGE_TYPE: process.env.NEXT_PUBLIC_STORAGE_TYPE,
13-
USERNAME: process.env.USERNAME ? '***' : undefined,
14-
PASSWORD: process.env.PASSWORD ? '***' : undefined,
15-
},
16-
globalThis: {
17-
hasDB: typeof globalThis !== 'undefined' && !!(globalThis as any).DB,
18-
hasProcess: typeof globalThis !== 'undefined' && !!(globalThis as any).process,
19-
hasCloudflare: typeof globalThis !== 'undefined' && !!(globalThis as any).cloudflare,
20-
},
21-
processEnv: {
22-
hasDB: !!(process.env as any).DB,
23-
keys: Object.keys(process.env).filter(key =>
24-
key.startsWith('DB') ||
25-
key.startsWith('NEXT_') ||
26-
key.startsWith('CF_') ||
27-
key.startsWith('CLOUDFLARE_')
28-
),
29-
},
3011
runtime: 'edge',
31-
userAgent: request.headers.get('user-agent')?.slice(0, 100),
32-
};
33-
34-
return NextResponse.json(debugInfo);
12+
storageType: process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage',
13+
hasDB: !!(globalThis as any).DB,
14+
nodeEnv: process.env.NODE_ENV
15+
});
3516
} catch (error) {
3617
return NextResponse.json({
37-
error: 'Debug info collection failed',
38-
message: error instanceof Error ? error.message : 'Unknown error',
39-
timestamp: new Date().toISOString()
18+
error: 'Debug failed',
19+
message: error instanceof Error ? error.message : 'Unknown error'
4020
}, { status: 500 });
4121
}
4222
}

src/app/api/test/simple/route.ts

Whitespace-only changes.

src/lib/d1.db.ts

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,21 @@ interface D1ExecResult {
3939

4040
// 获取全局D1数据库实例
4141
function getD1Database(): D1Database {
42-
// 尝试多种方式访问 D1 数据库
43-
44-
// 1. Cloudflare Pages Functions 中通过 env.DB 访问
45-
if (typeof globalThis !== 'undefined' && (globalThis as any).DB) {
46-
return (globalThis as any).DB as D1Database;
42+
// 在 Cloudflare Pages 环境中,DB 通过全局绑定可用
43+
if (typeof globalThis !== 'undefined') {
44+
// 尝试直接访问全局 DB
45+
const globalDB = (globalThis as any).DB;
46+
if (globalDB) {
47+
return globalDB as D1Database;
48+
}
4749
}
4850

49-
// 2. 通过 process.env.DB 访问(用于本地开发)
50-
if ((process.env as any).DB) {
51+
// 回退到 process.env(用于本地开发)
52+
if (process.env.DB) {
5153
return (process.env as any).DB as D1Database;
5254
}
5355

54-
// 3. 通过 globalThis.process.env.DB 访问
55-
if (typeof globalThis !== 'undefined' && (globalThis as any).process?.env?.DB) {
56-
return (globalThis as any).process.env.DB as D1Database;
57-
}
58-
59-
// 4. 检查 Cloudflare Workers 环境变量
60-
if (typeof globalThis !== 'undefined' && (globalThis as any).cloudflare?.env?.DB) {
61-
return (globalThis as any).cloudflare.env.DB as D1Database;
62-
}
63-
64-
// 最后检查是否在测试环境中,如果是则抛出更详细的错误
65-
const isDev = process.env.NODE_ENV === 'development';
66-
const errorMessage = isDev
67-
? 'D1 database not available in development. Using D1 requires deployment to Cloudflare Pages.'
68-
: 'D1 database binding not configured. Check your Cloudflare Pages settings.';
69-
70-
throw new Error(errorMessage);
56+
throw new Error('D1 database not available');
7157
}
7258

7359
export class D1Storage implements IStorage {

src/lib/db.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,34 @@ const STORAGE_TYPE =
2020

2121
// 创建存储实例
2222
function createStorage(): IStorage {
23-
switch (STORAGE_TYPE) {
24-
case 'redis':
25-
return new RedisStorage();
26-
case 'kvrocks':
27-
return new KvrocksStorage();
28-
case 'upstash':
29-
return new UpstashRedisStorage();
30-
case 'd1':
31-
return new D1Storage();
32-
case 'localstorage':
33-
default:
34-
// 使用 LocalStorage 实现,适用于本地开发和简单部署
35-
return new LocalStorage();
23+
const storageType = STORAGE_TYPE;
24+
25+
try {
26+
switch (storageType) {
27+
case 'redis':
28+
return new RedisStorage();
29+
case 'kvrocks':
30+
return new KvrocksStorage();
31+
case 'upstash':
32+
return new UpstashRedisStorage();
33+
case 'd1':
34+
// 对于 d1,先检查是否可用
35+
if (typeof globalThis !== 'undefined' && (globalThis as any).DB) {
36+
return new D1Storage();
37+
} else if (process.env.DB) {
38+
return new D1Storage();
39+
} else {
40+
// D1 不可用,回退到 LocalStorage
41+
return new LocalStorage();
42+
}
43+
case 'localstorage':
44+
default:
45+
// 使用 LocalStorage 实现,适用于本地开发和简单部署
46+
return new LocalStorage();
47+
}
48+
} catch (error) {
49+
// 创建存储失败,回退到 LocalStorage
50+
return new LocalStorage();
3651
}
3752
}
3853

0 commit comments

Comments
 (0)