mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-02-23 21:59:05 +00:00
refactor: res.set -> res.header
res.set() is an expressjs-compat function with unnecessary checks for our use case
This commit is contained in:
parent
05448842b8
commit
ad22285661
@ -44,7 +44,7 @@ self.handleError = (req, res, error) => {
|
|||||||
json.code = error.code
|
json.code = error.code
|
||||||
}
|
}
|
||||||
|
|
||||||
res.setHeader('Cache-Control', 'no-store')
|
res.header('Cache-Control', 'no-store')
|
||||||
|
|
||||||
if (Object.keys(json).length) {
|
if (Object.keys(json).length) {
|
||||||
json.success = false
|
json.success = false
|
||||||
@ -59,7 +59,7 @@ self.handleError = (req, res, error) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.handleNotFound = (req, res) => {
|
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]))
|
return res.status(404).sendFile(path.join(paths.errorRoot, config.errorPages[404]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,10 +38,10 @@ class RateLimiter {
|
|||||||
await this.rateLimiterMemory.consume(key, 1)
|
await this.rateLimiterMemory.consume(key, 1)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
res.locals.rateLimit = result
|
res.locals.rateLimit = result
|
||||||
res.set('Retry-After', String(result.msBeforeNext / 1000))
|
res.header('Retry-After', String(result.msBeforeNext / 1000))
|
||||||
res.set('X-RateLimit-Limit', String(this.rateLimiterMemory._points))
|
res.header('X-RateLimit-Limit', String(this.rateLimiterMemory._points))
|
||||||
res.set('X-RateLimit-Remaining', String(result.remainingPoints))
|
res.header('X-RateLimit-Remaining', String(result.remainingPoints))
|
||||||
res.set('X-RateLimit-Reset', String(new Date(Date.now() + result.msBeforeNext)))
|
res.header('X-RateLimit-Reset', String(new Date(Date.now() + result.msBeforeNext)))
|
||||||
})
|
})
|
||||||
.catch(reject => {
|
.catch(reject => {
|
||||||
// Re-throw with ClientError
|
// Re-throw with ClientError
|
||||||
|
12
lolisafe.js
12
lolisafe.js
@ -105,7 +105,7 @@ if (config.accessControlAllowOrigin) {
|
|||||||
config.accessControlAllowOrigin = '*'
|
config.accessControlAllowOrigin = '*'
|
||||||
}
|
}
|
||||||
safe.use((req, res, next) => {
|
safe.use((req, res, next) => {
|
||||||
res.set('Access-Control-Allow-Origin', config.accessControlAllowOrigin)
|
res.header('Access-Control-Allow-Origin', config.accessControlAllowOrigin)
|
||||||
if (config.accessControlAllowOrigin !== '*') {
|
if (config.accessControlAllowOrigin !== '*') {
|
||||||
res.vary('Origin')
|
res.vary('Origin')
|
||||||
}
|
}
|
||||||
@ -149,7 +149,7 @@ if (config.cacheControl) {
|
|||||||
|
|
||||||
// By default soft cache everything
|
// By default soft cache everything
|
||||||
safe.use('/', (req, res, next) => {
|
safe.use('/', (req, res, next) => {
|
||||||
res.set('Cache-Control', cacheControls.validate)
|
res.header('Cache-Control', cacheControls.validate)
|
||||||
return next()
|
return next()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -162,7 +162,7 @@ if (config.cacheControl) {
|
|||||||
if (req.method === 'GET' || req.method === 'HEAD') {
|
if (req.method === 'GET' || req.method === 'HEAD') {
|
||||||
const page = req.path === '/' ? 'home' : req.path.substring(1)
|
const page = req.path === '/' ? 'home' : req.path.substring(1)
|
||||||
if (cdnPages.includes(page)) {
|
if (cdnPages.includes(page)) {
|
||||||
res.set('Cache-Control', cacheControls.cdn)
|
res.header('Cache-Control', cacheControls.cdn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return next()
|
return next()
|
||||||
@ -174,16 +174,16 @@ if (config.cacheControl) {
|
|||||||
// This requires the assets to use version in their query string,
|
// This requires the assets to use version in their query string,
|
||||||
// as they will be cached by clients for a very long time.
|
// as they will be cached by clients for a very long time.
|
||||||
setHeadersForStaticAssets = (req, res) => {
|
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
|
// Consider album ZIPs static as well, since they use version in their query string
|
||||||
safe.use('/api/album/zip', (req, res, next) => {
|
safe.use('/api/album/zip', (req, res, next) => {
|
||||||
const versionString = parseInt(req.query.v)
|
const versionString = parseInt(req.query.v)
|
||||||
if (versionString > 0) {
|
if (versionString > 0) {
|
||||||
res.set('Cache-Control', cacheControls.static)
|
res.header('Cache-Control', cacheControls.static)
|
||||||
} else {
|
} else {
|
||||||
res.set('Cache-Control', cacheControls.disable)
|
res.header('Cache-Control', cacheControls.disable)
|
||||||
}
|
}
|
||||||
return next()
|
return next()
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user