fix: 根本修复 getClientIP tree-shaking 问题

- audit.ts: 直接定义 getClientIP 函数,而非从 shared 库 re-export
- 原因: Next.js standalone 构建的 tree-shaking 会丢弃 re-export 的函数
- 影响: 修复所有使用 getClientIP 的 API 路由(scan-emails、scan-status、auth/login)
This commit is contained in:
gitadmin 2026-07-10 13:09:47 +08:00
parent 73b442717e
commit 75ca749db7
3 changed files with 12 additions and 18 deletions

View File

@ -8,16 +8,9 @@ import { getMonitorConfig } from '@/lib/monitor/settings-manager'
import { ImapFlow } from 'imapflow'
import { simpleParser } from 'mailparser'
import { formatBeijingTime } from '@/lib/monitor/types'
import { writeAuditLog } from '@/lib/audit'
import { writeAuditLog, getClientIP } from '@/lib/audit'
import { getScanState, setScanState, isCancelRequested, resetCancelFlag } from '@/lib/monitor/scan-state'
// 直接定义,避免 Next.js tree-shaking 导致模块导出缺失
function getClientIP(request: NextRequest): string | null {
const forwarded = request.headers.get('x-forwarded-for')
if (forwarded) return forwarded.split(',')[0].trim()
return request.headers.get('x-real-ip') ?? null
}
interface ScanResult {
status: 'running' | 'completed' | 'error' | 'cancelling'
startedAt: string

View File

@ -3,16 +3,9 @@ import { NextRequest, NextResponse } from 'next/server'
import { initDatabase } from '@/lib/db-schema'
import { getCurrentUser } from '@/lib/auth'
import { hasPermission } from '@/lib/permissions'
import { writeAuditLog } from '@/lib/audit'
import { getClientIP, writeAuditLog } from '@/lib/audit'
import { getScanState, requestCancel } from '@/lib/monitor/scan-state'
// 直接定义,避免 Next.js tree-shaking 导致模块导出缺失
function getClientIP(request: NextRequest): string | null {
const forwarded = request.headers.get('x-forwarded-for')
if (forwarded) return forwarded.split(',')[0].trim()
return request.headers.get('x-real-ip') ?? null
}
export async function GET(request: NextRequest) {
initDatabase()
const user = await getCurrentUser()

View File

@ -1,6 +1,6 @@
// issue-ai/src/lib/audit.ts — 站点审计包装器(引用共享库)
import { getDb } from './db'
import { writeAuditLog as sharedWriteAuditLog, diffObjects, getClientIP } from '@shared/lib/audit/write-audit-log'
import { writeAuditLog as sharedWriteAuditLog, diffObjects } from '@shared/lib/audit/write-audit-log'
import type { AuditLogEntry, AuditStore } from '@shared/lib/audit/write-audit-log'
// 创建 issue-ai 的 store 适配器
@ -19,5 +19,13 @@ export function writeAuditLog(opts: AuditLogEntry): void {
sharedWriteAuditLog(store, opts)
}
// 直接定义 getClientIP避免 Next.js tree-shaking 导致 re-export 丢失
// shared 库的 re-export 在 standalone 构建中可能被优化掉)
export function getClientIP(request: Request): string | null {
const forwarded = request.headers.get('x-forwarded-for')
if (forwarded) return forwarded.split(',')[0].trim()
return request.headers.get('x-real-ip') ?? null
}
// re-export 共享工具函数
export { diffObjects, getClientIP }
export { diffObjects }