perf: 优化缓存下载

This commit is contained in:
2024-10-11 22:10:30 +08:00
parent 79287d7de9
commit 973b76a5f0
6 changed files with 75 additions and 47 deletions

View File

@@ -7,6 +7,7 @@ import fs from 'fs';
import { useConfig } from '@abearxiong/use-config';
import { redis } from './redis/redis.ts';
import { getContentType } from './get-content-type.ts';
import { sleep } from '@/utils/sleep.ts';
const { api, domain, allowedOrigins } = useConfig<{
api: {
host: string;
@@ -128,30 +129,38 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
let isExist = await userApp.getExist();
if (!isExist) {
try {
const hasApp = await userApp.setCacheData();
if (!hasApp) {
const { code, loading, data } = await userApp.setCacheData();
if (loading) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('Loading App\n');
res.end();
return;
}
if (code !== 200) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write('Not Found App\n');
res.end();
return;
}
await sleep(1000);
isExist = data; // 设置缓存后再次获取
if (!isExist) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write('Not Found App Index Page\n');
res.end();
return;
}
} catch (error) {
console.error('setCacheData error', error);
res.writeHead(500, { 'Content-Type': 'text/html' });
res.write('Server Error\n');
res.end();
return;
}
isExist = await userApp.getExist();
if (!isExist) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write('Not Found App Index Page\n');
res.end();
userApp.setLoaded();
return;
}
}
const indexFile = isExist;
const indexFile = isExist; // 已经必定存在了
let appFileUrl: string;
if (domainApp) {
appFileUrl = (url + '').replace(`/`, '');