From 930236657a6398affb0372802bbeb21235028f97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E5=B0=8F=E9=99=8C?= Date: Tue, 15 Oct 2024 11:31:12 +0800 Subject: [PATCH] 1111 --- index.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index f9cf85e..319b544 100644 --- a/index.js +++ b/index.js @@ -195,13 +195,13 @@ const fetchApiData = (reqPath, sign) => { // 从真实 URL 获取数据并写入缓存 const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => { - https.get(data.realUrl, { timeout: requestTimeout * 10,rejectUnauthorized: false }, (realRes) => { + https.get(data.realUrl, { timeout: requestTimeout * 10, rejectUnauthorized: false }, (realRes) => { const cacheStream = fs.createWriteStream(tempCacheContentFile, { flags: 'w' }); let isVideo = data.path && typeof data.path === 'string' && data.path.includes('.mp4'); data.headers['content-length'] = realRes.headers['content-length']; - res.writeHead(realRes.statusCode, { - ...data.headers, + + const defaultHeaders = { 'Cloud-Type': data.cloudtype, 'Cloud-Expiration': data.expiration, 'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream', @@ -212,7 +212,8 @@ const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => { 'Connection': 'keep-alive', 'Date': new Date().toUTCString(), 'Last-Modified': new Date().toUTCString(), - }); + }; + res.writeHead(realRes.statusCode, Object.assign({}, defaultHeaders, data.headers)); realRes.pipe(cacheStream); realRes.pipe(res); @@ -250,8 +251,8 @@ const serveFromCache = (cacheMetaFile, cacheContentFile, res) => { cacheData.headers['content-length'] = fs.statSync(cacheContentFile).size; readStream.on('open', () => { - res.writeHead(200, { - ...cacheData.headers, + + const defaultHeaders = { 'Cloud-Type': cacheData.cloudtype, 'Cloud-Expiration': cacheData.expiration, 'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream', @@ -262,7 +263,9 @@ const serveFromCache = (cacheMetaFile, cacheContentFile, res) => { 'Connection': 'keep-alive', 'Date': new Date().toUTCString(), 'Last-Modified': new Date().toUTCString(), - }); + } + + res.writeHead(200, Object.assign({}, defaultHeaders, cacheData.headers)); readStream.pipe(res); });