diff --git a/index.js b/index.js index 319b544..3544c98 100644 --- a/index.js +++ b/index.js @@ -199,7 +199,14 @@ const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => { 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']; + + // 确保 content-length 是有效的 + const contentLength = realRes.headers['content-length']; + if (contentLength) { + data.headers['content-length'] = contentLength; + } else { + console.warn('Warning: content-length is undefined for the response from:', data.realUrl); + } const defaultHeaders = { 'Cloud-Type': data.cloudtype, @@ -248,7 +255,13 @@ const serveFromCache = (cacheMetaFile, cacheContentFile, res) => { const readStream = fs.createReadStream(cacheContentFile); let isVideo = cacheData.path && typeof cacheData.path === 'string' && cacheData.path.includes('.mp4'); - cacheData.headers['content-length'] = fs.statSync(cacheContentFile).size; + + const contentLength = fs.statSync(cacheContentFile).size; + if (contentLength) { + cacheData.headers['content-length'] = contentLength; + } else { + console.warn('Warning: content-length is undefined for cached content file:', cacheContentFile); + } readStream.on('open', () => { @@ -310,4 +323,4 @@ process.on('SIGINT', () => { console.error('Forcing shutdown...'); process.exit(1); }, 10000); -}); +}); \ No newline at end of file