refactor(缓存处理): 在缓存相关函数中添加req参数传递
为了在缓存处理过程中能够访问请求对象,将req参数添加到processSuccessfulApiData、tryServeFromStaleCacheOrError、fetchAndServe和serveFromCache等函数中
This commit is contained in:
32
source.js
32
source.js
@@ -164,7 +164,7 @@ async function handleApiRedirect(res, apiData) {
|
|||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processSuccessfulApiData(apiData, uniqidhex, reqPath, token, sign, res) {
|
async function processSuccessfulApiData(apiData, uniqidhex, reqPath, token, sign, res, req) {
|
||||||
const { url: realUrl, cloudtype, expiration, path: apiPath, headers, uniqid, thumb } = apiData.data;
|
const { url: realUrl, cloudtype, expiration, path: apiPath, headers, uniqid, thumb } = apiData.data;
|
||||||
const data = { realUrl, cloudtype, expiration: expiration * 1000, path: apiPath, headers, uniqid, thumb };
|
const data = { realUrl, cloudtype, expiration: expiration * 1000, path: apiPath, headers, uniqid, thumb };
|
||||||
|
|
||||||
@@ -186,16 +186,16 @@ async function processSuccessfulApiData(apiData, uniqidhex, reqPath, token, sign
|
|||||||
const contentLength = stats.size;
|
const contentLength = stats.size;
|
||||||
if (contentLength < 2048 && data.headers['content-length'] && parseInt(data.headers['content-length'], 10) !== contentLength) {
|
if (contentLength < 2048 && data.headers['content-length'] && parseInt(data.headers['content-length'], 10) !== contentLength) {
|
||||||
console.warn(`Content length mismatch for ${cacheContentFile}. API: ${data.headers['content-length']}, Cache: ${contentLength}. Re-fetching.`);
|
console.warn(`Content length mismatch for ${cacheContentFile}. API: ${data.headers['content-length']}, Cache: ${contentLength}. Re-fetching.`);
|
||||||
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res);
|
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res, req);
|
||||||
} else {
|
} else {
|
||||||
serveFromCache(data, cacheContentFile, cacheMetaFile, res, reqPath, token, sign, uniqidhex);
|
serveFromCache(data, cacheContentFile, cacheMetaFile, res, reqPath, token, sign, uniqidhex, req);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res);
|
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res, req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function tryServeFromStaleCacheOrError(uniqidhex, res, errorMessage) {
|
async function tryServeFromStaleCacheOrError(uniqidhex, res, errorMessage, req) {
|
||||||
if (pathIndex[uniqidhex]) {
|
if (pathIndex[uniqidhex]) {
|
||||||
const cacheMetaFile = pathModule.join(cacheDir, `${uniqidhex}.meta`);
|
const cacheMetaFile = pathModule.join(cacheDir, `${uniqidhex}.meta`);
|
||||||
const cacheContentFile = pathModule.join(cacheDir, `${pathIndex[uniqidhex].uniqid}.content`);
|
const cacheContentFile = pathModule.join(cacheDir, `${pathIndex[uniqidhex].uniqid}.content`);
|
||||||
@@ -203,7 +203,7 @@ async function tryServeFromStaleCacheOrError(uniqidhex, res, errorMessage) {
|
|||||||
console.warn(`API call failed or returned non-200. Serving stale cache for ${uniqidhex}`);
|
console.warn(`API call failed or returned non-200. Serving stale cache for ${uniqidhex}`);
|
||||||
try {
|
try {
|
||||||
const cacheData = JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8'));
|
const cacheData = JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8'));
|
||||||
serveFromCache(cacheData, cacheContentFile, cacheMetaFile, res, null, null, null, uniqidhex);
|
serveFromCache(cacheData, cacheContentFile, cacheMetaFile, res, null, null, null, uniqidhex, req);
|
||||||
return;
|
return;
|
||||||
} catch (parseError) {
|
} catch (parseError) {
|
||||||
console.error(`Error parsing stale meta file ${cacheMetaFile}:`, parseError);
|
console.error(`Error parsing stale meta file ${cacheMetaFile}:`, parseError);
|
||||||
@@ -263,7 +263,7 @@ async function handleMainRequest(req, res) {
|
|||||||
res.end();
|
res.end();
|
||||||
} else {
|
} else {
|
||||||
viewsInfo.increment('cacheHit');
|
viewsInfo.increment('cacheHit');
|
||||||
serveFromCache(cacheData, cacheContentFile, cacheMetaFile, res, reqPath, token, sign, uniqidhex);
|
serveFromCache(cacheData, cacheContentFile, cacheMetaFile, res, reqPath, token, sign, uniqidhex, req);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
@@ -275,15 +275,15 @@ async function handleMainRequest(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (apiData.code === HTTP_STATUS.OK && apiData.data && apiData.data.url) {
|
if (apiData.code === HTTP_STATUS.OK && apiData.data && apiData.data.url) {
|
||||||
await processSuccessfulApiData(apiData, uniqidhex, reqPath, token, sign, res);
|
await processSuccessfulApiData(apiData, uniqidhex, reqPath, token, sign, res, req);
|
||||||
} else {
|
} else {
|
||||||
viewsInfo.increment('fetchApiWarning');
|
viewsInfo.increment('fetchApiWarning');
|
||||||
await tryServeFromStaleCacheOrError(uniqidhex, res, apiData.message);
|
await tryServeFromStaleCacheOrError(uniqidhex, res, apiData.message, req);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
viewsInfo.increment('fetchApiError');
|
viewsInfo.increment('fetchApiError');
|
||||||
console.error('Error in API call or processing:', error);
|
console.error('Error in API call or processing:', error);
|
||||||
await tryServeFromStaleCacheOrError(uniqidhex, res, `Bad Gateway: API request failed. ${error.message}`);
|
await tryServeFromStaleCacheOrError(uniqidhex, res, `Bad Gateway: API request failed. ${error.message}`, req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -418,7 +418,7 @@ async function fetchApiData(reqPath, token, sign) {
|
|||||||
// 从真实 URL 获取数据并写入缓存
|
// 从真实 URL 获取数据并写入缓存
|
||||||
const REAL_URL_FETCH_TIMEOUT_MS = 0; // 0 means no timeout for the actual file download
|
const REAL_URL_FETCH_TIMEOUT_MS = 0; // 0 means no timeout for the actual file download
|
||||||
|
|
||||||
const fetchAndServe = async (data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res) => {
|
const fetchAndServe = async (data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res, req) => {
|
||||||
const protocol = data.realUrl.startsWith('https:') ? https : http;
|
const protocol = data.realUrl.startsWith('https:') ? https : http;
|
||||||
|
|
||||||
protocol.get(data.realUrl, { timeout: REAL_URL_FETCH_TIMEOUT_MS, rejectUnauthorized: false }, (realRes) => {
|
protocol.get(data.realUrl, { timeout: REAL_URL_FETCH_TIMEOUT_MS, rejectUnauthorized: false }, (realRes) => {
|
||||||
@@ -502,7 +502,7 @@ const fetchAndServe = async (data, tempCacheContentFile, cacheContentFile, cache
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 从缓存中读取数据并返回
|
// 从缓存中读取数据并返回
|
||||||
async function serveFromCache(cacheData, cacheContentFile, cacheMetaFile, res, reqPath, token, sign, uniqidhex) {
|
async function serveFromCache(cacheData, cacheContentFile, cacheMetaFile, res, reqPath, token, sign, uniqidhex, req) {
|
||||||
if (!cacheData) { // 缓存数据不可用,尝试重新获取
|
if (!cacheData) { // 缓存数据不可用,尝试重新获取
|
||||||
console.warn(`Cache metadata unavailable for ${cacheContentFile}, attempting to fetch fresh data`);
|
console.warn(`Cache metadata unavailable for ${cacheContentFile}, attempting to fetch fresh data`);
|
||||||
|
|
||||||
@@ -531,7 +531,7 @@ async function serveFromCache(cacheData, cacheContentFile, cacheMetaFile, res, r
|
|||||||
|
|
||||||
// 获取并提供新数据
|
// 获取并提供新数据
|
||||||
const tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);
|
const tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);
|
||||||
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res);
|
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res, req);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
viewsInfo.increment('fetchApiWarning');
|
viewsInfo.increment('fetchApiWarning');
|
||||||
@@ -616,7 +616,7 @@ async function serveFromCache(cacheData, cacheContentFile, cacheMetaFile, res, r
|
|||||||
|
|
||||||
// 获取并提供新数据
|
// 获取并提供新数据
|
||||||
const tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);
|
const tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);
|
||||||
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res);
|
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res, req);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
sendErrorResponse(res, HTTP_STATUS.BAD_GATEWAY, apiData.message || 'Failed to fetch data from API');
|
sendErrorResponse(res, HTTP_STATUS.BAD_GATEWAY, apiData.message || 'Failed to fetch data from API');
|
||||||
@@ -692,7 +692,7 @@ async function serveFromCache(cacheData, cacheContentFile, cacheMetaFile, res, r
|
|||||||
|
|
||||||
// 获取并提供新数据
|
// 获取并提供新数据
|
||||||
const tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);
|
const tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);
|
||||||
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res);
|
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res, req);
|
||||||
} else {
|
} else {
|
||||||
viewsInfo.increment('fetchApiWarning');
|
viewsInfo.increment('fetchApiWarning');
|
||||||
sendErrorResponse(res, HTTP_STATUS.BAD_GATEWAY, apiData.message || 'Failed to fetch data from API');
|
sendErrorResponse(res, HTTP_STATUS.BAD_GATEWAY, apiData.message || 'Failed to fetch data from API');
|
||||||
@@ -763,7 +763,7 @@ const handleCacheReadError = (res, filePath, reqPath, token, sign, uniqidhex) =>
|
|||||||
fs.promises.mkdir(pathModule.dirname(cacheMetaFile), { recursive: true })
|
fs.promises.mkdir(pathModule.dirname(cacheMetaFile), { recursive: true })
|
||||||
.then(() => fs.promises.writeFile(cacheMetaFile, JSON.stringify(data)))
|
.then(() => fs.promises.writeFile(cacheMetaFile, JSON.stringify(data)))
|
||||||
.then(() => {
|
.then(() => {
|
||||||
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res);
|
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res, req);
|
||||||
})
|
})
|
||||||
.catch(writeError => {
|
.catch(writeError => {
|
||||||
console.error(`Error writing meta file after cache read error: ${writeError.message}`);
|
console.error(`Error writing meta file after cache read error: ${writeError.message}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user