From 948384222eb2dc338c8535ddfd2c5120e4e19ee6 Mon Sep 17 00:00:00 2001 From: huangxd- <649924757@qq.com> Date: Sat, 15 Nov 2025 11:14:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dcloudflare=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98=20(#92)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复cf部署报错的问题 * 增加.cache ignore --- .gitignore | 1 + danmu_api/utils/cache-util.js | 25 +++++++++++++++++-------- danmu_api/worker.js | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 16856f5..03e419d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ package-lock.json node_modules .env +.cache diff --git a/danmu_api/utils/cache-util.js b/danmu_api/utils/cache-util.js index 43c7314..742f83b 100644 --- a/danmu_api/utils/cache-util.js +++ b/danmu_api/utils/cache-util.js @@ -2,8 +2,7 @@ import { globals } from '../configs/globals.js'; import { log } from './log-util.js' import { Anime } from "../models/dandan-model.js"; import { simpleHash } from "./codec-util.js"; -import fs from 'fs'; -import path from 'path'; +let fs, path; // ===================== // cache数据结构处理函数 @@ -409,13 +408,23 @@ export async function updateLocalCaches() { } // 判断是否有效的本地缓存目录 -export function judgeLocalCacheValid(urlPath) { - if (!globals.localCacheValid && urlPath !== "/favicon.ico" && urlPath !== "/robots.txt") { - const cacheDirPath = path.join(getDirname(), '..', '..', '.cache'); +export async function judgeLocalCacheValid(urlPath, deployPlatform) { + if (deployPlatform === 'node') { + try { + fs = await import('fs'); + path = await import('path'); - if (fs.existsSync(cacheDirPath)) { - globals.localCacheValid = true; - } else { + if (!globals.localCacheValid && urlPath !== "/favicon.ico" && urlPath !== "/robots.txt") { + const cacheDirPath = path.join(getDirname(), '..', '..', '.cache'); + + if (fs.existsSync(cacheDirPath)) { + globals.localCacheValid = true; + } else { + globals.localCacheValid = false; + } + } + } catch (error) { + console.warn('Node.js modules not available:', error.message); globals.localCacheValid = false; } } diff --git a/danmu_api/worker.js b/danmu_api/worker.js index 3e4b2cf..4001883 100644 --- a/danmu_api/worker.js +++ b/danmu_api/worker.js @@ -17,7 +17,7 @@ async function handleRequest(req, env, deployPlatform, clientIp) { const method = req.method; if (deployPlatform === "node") { - judgeLocalCacheValid(path); + await judgeLocalCacheValid(path, deployPlatform); } await judgeRedisValid(path);