feat(auth): clarify login identity and speed up mobile entry
This commit is contained in:
+22
-5
@@ -13,6 +13,8 @@
|
||||
const http = require('http');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
const zlib = require('zlib');
|
||||
|
||||
const PORT = Number(process.env.PORT) || 8080;
|
||||
// 只绑回环:公网入口是 nginx(1me.xajuzi.com/state/),8080 不再对公网裸奔;需局域网调试时 HOST=0.0.0.0
|
||||
@@ -84,11 +86,26 @@ function serveStatic(req, res, pathname) {
|
||||
fs.readFile(file, (err, data) => {
|
||||
if (err) { res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' }); return res.end('Not Found'); }
|
||||
const ext = path.extname(file);
|
||||
// html/css/js 不缓存(迭代期样式/脚本改动必须即时到端;图片/字体/音频走默认)
|
||||
const headers = { 'Content-Type': MIME[ext] || 'application/octet-stream' };
|
||||
if (['.html', '.css', '.js'].includes(ext)) headers['Cache-Control'] = 'no-cache';
|
||||
res.writeHead(200, headers);
|
||||
res.end(data);
|
||||
// Revalidate every time: updates remain immediate; unchanged resources use
|
||||
// 304 instead of downloading again. APIs/private reports never enter here.
|
||||
const headers = { 'Content-Type': MIME[ext] || 'application/octet-stream',
|
||||
'Cache-Control': 'no-cache', ETag: 'W/"' + crypto.createHash('sha256').update(data).digest('hex') + '"' };
|
||||
const compressible = /\.(html|css|js|json|svg)$/.test(file);
|
||||
if (compressible) headers.Vary = 'Accept-Encoding';
|
||||
if ((req.headers['if-none-match'] || '').split(/\s*,\s*/).includes(headers.ETag)) {
|
||||
res.writeHead(304, headers); return res.end();
|
||||
}
|
||||
// Compress static public text only, not authenticated JSON or photographs.
|
||||
const gzipAccepted = (req.headers['accept-encoding'] || '').split(',').some(value => {
|
||||
const parts = value.trim().split(';');
|
||||
return parts[0] === 'gzip' && !parts.slice(1).some(p => /^\s*q=0(?:\.0*)?\s*$/.test(p));
|
||||
});
|
||||
if (compressible && data.length > 1024 && gzipAccepted) {
|
||||
zlib.gzip(data, (error, compressed) => {
|
||||
if (!error) headers['Content-Encoding'] = 'gzip';
|
||||
res.writeHead(200, headers); res.end(error ? data : compressed);
|
||||
});
|
||||
} else { res.writeHead(200, headers); res.end(data); }
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user