From 30e9227a781bf99170c82665dcd4e2d911af2e4c Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Fri, 22 Jul 2022 01:09:17 +0700 Subject: [PATCH] feat: custom pages use ServeLiveDirectory they now have conditional GET suppor too --- controllers/middlewares/serveLiveDirectory.js | 26 +++++++++++-------- lolisafe.js | 13 +++------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/controllers/middlewares/serveLiveDirectory.js b/controllers/middlewares/serveLiveDirectory.js index 763c597..2985023 100644 --- a/controllers/middlewares/serveLiveDirectory.js +++ b/controllers/middlewares/serveLiveDirectory.js @@ -38,17 +38,7 @@ class ServeLiveDirectory { * MIT Licensed */ - #middleware (req, res, next) { - // Only process GET and HEAD requests - if (req.method !== 'GET' && req.method !== 'HEAD') { - return next() - } - - const file = this.instance.get(req.path) - if (file === undefined) { - return next() - } - + handler (req, res, file) { // set header fields this.#setHeaders(req, res, file) @@ -74,6 +64,20 @@ class ServeLiveDirectory { return res.send(file.buffer) } + #middleware (req, res, next) { + // Only process GET and HEAD requests + if (req.method !== 'GET' && req.method !== 'HEAD') { + return next() + } + + const file = this.instance.get(req.path) + if (file === undefined) { + return next() + } + + return this.handler(req, res, file) + } + #setHeaders (req, res, file) { // Always do external setHeaders function first, // in case it will overwrite the following default headers anyways diff --git a/lolisafe.js b/lolisafe.js index 8629a31..2300f63 100644 --- a/lolisafe.js +++ b/lolisafe.js @@ -232,13 +232,9 @@ safe.use('/api', api) } } - const liveDirectoryCustomPages = new LiveDirectory({ + const serveLiveDirectoryCustomPagesInstance = new ServeLiveDirectory({ path: paths.customPages, - keep: ['.html'], - ignore: path => { - // ignore dot files - return path.startsWith('.') - } + keep: ['.html'] }) // Cookie Policy @@ -254,10 +250,9 @@ safe.use('/api', api) safe.use((req, res, next) => { if (req.method === 'GET' || req.method === 'HEAD') { const page = req.path === '/' ? 'home' : req.path.substring(1) - const customPage = liveDirectoryCustomPages.get(`${page}.html`) + const customPage = serveLiveDirectoryCustomPagesInstance.instance.get(`${page}.html`) if (customPage) { - // TODO: Conditional GETs? (e.g. Last-Modified, etag, etc.) - return res.type('html').send(customPage.buffer) + return serveLiveDirectoryCustomPagesInstance.handler(req, res, customPage) } else if (config.pages.includes(page)) { // These rendered pages are persistently cached during production return res.render(page, {