From 6ec230f58f74c13e8c40af005cdef5455806bbfd Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 29 Sep 2020 18:53:24 +0800 Subject: [PATCH] feat: support etag --- lib/app.js | 9 +++++---- lib/middleware/debug.js | 4 ++++ lib/middleware/header.js | 24 +++++++++++++----------- lib/routes/index.js | 8 ++++++-- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/lib/app.js b/lib/app.js index 904adf3001..61ad7129e6 100644 --- a/lib/app.js +++ b/lib/app.js @@ -39,15 +39,13 @@ app.use(favicon(__dirname + '/favicon.png')); // global error handing app.use(onerror); -// 1 set header -app.use(header); - app.use(accessControl); -// 6 debug +// 7 debug app.context.debug = { hitCache: 0, request: 0, + etag: 0, paths: [], routes: [], ips: [], @@ -56,6 +54,9 @@ app.context.debug = { }; app.use(debug); +// 6 set header +app.use(header); + // 5 fix incorrect `utf-8` characters app.use(utf8); diff --git a/lib/middleware/debug.js b/lib/middleware/debug.js index e30854fe8f..d3c41ae9dc 100644 --- a/lib/middleware/debug.js +++ b/lib/middleware/debug.js @@ -23,4 +23,8 @@ module.exports = async (ctx, next) => { } ctx.state.debuged = true; + + if (ctx.status === 304) { + ctx.debug.etag++; + } }; diff --git a/lib/middleware/header.js b/lib/middleware/header.js index e1118af539..ff4272cbf4 100644 --- a/lib/middleware/header.js +++ b/lib/middleware/header.js @@ -19,8 +19,7 @@ module.exports = async (ctx, next) => { await next(); - let body = ctx.body; - if (!body || typeof body !== 'string' || ctx.response.get('ETag')) { + if (!ctx.body || typeof ctx.body !== 'string' || ctx.response.get('ETag')) { return; } @@ -29,14 +28,17 @@ module.exports = async (ctx, next) => { return; } - const match = body.match(/(.*)<\/lastBuildDate>/); - if (match) { - ctx.set({ - 'Last-Modified': body.match(/(.*)<\/lastBuildDate>/)[1], - }); + ctx.set('ETag', etagCalculate(ctx.body.replace(/(.*)<\/lastBuildDate>/, '').replace(//, ''))); + + if (ctx.fresh) { + ctx.status = 304; + ctx.body = null; + } else { + const match = ctx.body.match(/(.*)<\/lastBuildDate>/); + if (match) { + ctx.set({ + 'Last-Modified': match[1], + }); + } } - - body = body.replace(/(.*)<\/lastBuildDate>/, ''); - - ctx.response.etag = etagCalculate(body); }; diff --git a/lib/routes/index.js b/lib/routes/index.js index 3688cbb719..b8cccbd846 100644 --- a/lib/routes/index.js +++ b/lib/routes/index.js @@ -85,12 +85,16 @@ module.exports = async (ctx) => { }, { name: 'request frequency', - value: ((ctx.debug.request / (stats.elapsed / 1000)) * 60).toFixed(3) + ' 次/分钟', + value: ((ctx.debug.request / (stats.elapsed / 1000)) * 60).toFixed(3) + ' times/minute', }, { name: 'cache hit ratio', value: ctx.debug.request ? (ctx.debug.hitCache / ctx.debug.request).toFixed(3) : 0, }, + { + name: 'ETag Matched', + value: ctx.debug.etag, + }, { name: 'memory usage', value: stats.memory / 1000000 + ' MB', @@ -101,7 +105,7 @@ module.exports = async (ctx) => { }, { name: 'run time', - value: (stats.elapsed / 3600000).toFixed(2) + ' 小时', + value: (stats.elapsed / 3600000).toFixed(2) + ' hour(s)', }, { name: 'hot routes',