From ad2228566191443d01a02b5a90f235dbddcf9f08 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Thu, 21 Jul 2022 20:28:10 +0700 Subject: [PATCH] refactor: res.set -> res.header res.set() is an expressjs-compat function with unnecessary checks for our use case --- controllers/errorsController.js | 4 ++-- controllers/middlewares/rateLimiter.js | 8 ++++---- lolisafe.js | 12 ++++++------ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/controllers/errorsController.js b/controllers/errorsController.js index 7a52fd8..c602cee 100644 --- a/controllers/errorsController.js +++ b/controllers/errorsController.js @@ -44,7 +44,7 @@ self.handleError = (req, res, error) => { json.code = error.code } - res.setHeader('Cache-Control', 'no-store') + res.header('Cache-Control', 'no-store') if (Object.keys(json).length) { json.success = false @@ -59,7 +59,7 @@ self.handleError = (req, res, error) => { } self.handleNotFound = (req, res) => { - res.setHeader('Cache-Control', 'no-store') + res.header('Cache-Control', 'no-store') return res.status(404).sendFile(path.join(paths.errorRoot, config.errorPages[404])) } diff --git a/controllers/middlewares/rateLimiter.js b/controllers/middlewares/rateLimiter.js index ac85961..0b1a713 100644 --- a/controllers/middlewares/rateLimiter.js +++ b/controllers/middlewares/rateLimiter.js @@ -38,10 +38,10 @@ class RateLimiter { await this.rateLimiterMemory.consume(key, 1) .then(result => { res.locals.rateLimit = result - res.set('Retry-After', String(result.msBeforeNext / 1000)) - res.set('X-RateLimit-Limit', String(this.rateLimiterMemory._points)) - res.set('X-RateLimit-Remaining', String(result.remainingPoints)) - res.set('X-RateLimit-Reset', String(new Date(Date.now() + result.msBeforeNext))) + res.header('Retry-After', String(result.msBeforeNext / 1000)) + res.header('X-RateLimit-Limit', String(this.rateLimiterMemory._points)) + res.header('X-RateLimit-Remaining', String(result.remainingPoints)) + res.header('X-RateLimit-Reset', String(new Date(Date.now() + result.msBeforeNext))) }) .catch(reject => { // Re-throw with ClientError diff --git a/lolisafe.js b/lolisafe.js index 622a31b..066118b 100644 --- a/lolisafe.js +++ b/lolisafe.js @@ -105,7 +105,7 @@ if (config.accessControlAllowOrigin) { config.accessControlAllowOrigin = '*' } safe.use((req, res, next) => { - res.set('Access-Control-Allow-Origin', config.accessControlAllowOrigin) + res.header('Access-Control-Allow-Origin', config.accessControlAllowOrigin) if (config.accessControlAllowOrigin !== '*') { res.vary('Origin') } @@ -149,7 +149,7 @@ if (config.cacheControl) { // By default soft cache everything safe.use('/', (req, res, next) => { - res.set('Cache-Control', cacheControls.validate) + res.header('Cache-Control', cacheControls.validate) return next() }) @@ -162,7 +162,7 @@ if (config.cacheControl) { if (req.method === 'GET' || req.method === 'HEAD') { const page = req.path === '/' ? 'home' : req.path.substring(1) if (cdnPages.includes(page)) { - res.set('Cache-Control', cacheControls.cdn) + res.header('Cache-Control', cacheControls.cdn) } } return next() @@ -174,16 +174,16 @@ if (config.cacheControl) { // This requires the assets to use version in their query string, // as they will be cached by clients for a very long time. setHeadersForStaticAssets = (req, res) => { - res.set('Cache-Control', cacheControls.static) + res.header('Cache-Control', cacheControls.static) } // Consider album ZIPs static as well, since they use version in their query string safe.use('/api/album/zip', (req, res, next) => { const versionString = parseInt(req.query.v) if (versionString > 0) { - res.set('Cache-Control', cacheControls.static) + res.header('Cache-Control', cacheControls.static) } else { - res.set('Cache-Control', cacheControls.disable) + res.header('Cache-Control', cacheControls.disable) } return next() })