mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2024-12-14 00:16:21 +00:00
feat: improved serveLiveDirectory
allow disabling etag and lastModified headers if required
This commit is contained in:
parent
1b4b73b67c
commit
d9fd98f7de
@ -16,6 +16,14 @@ class ServeLiveDirectory {
|
||||
|
||||
this.instance = new LiveDirectory(instanceOptions)
|
||||
|
||||
if (options.etag === undefined) {
|
||||
options.etag = true
|
||||
}
|
||||
|
||||
if (options.lastModified === undefined) {
|
||||
options.lastModified = true
|
||||
}
|
||||
|
||||
if (options.setHeaders && typeof options.setHeaders !== 'function') {
|
||||
throw new TypeError('Middleware option setHeaders must be a function')
|
||||
}
|
||||
@ -23,6 +31,13 @@ class ServeLiveDirectory {
|
||||
this.#options = options
|
||||
}
|
||||
|
||||
/*
|
||||
* Based on https://github.com/pillarjs/send/blob/0.18.0/index.js
|
||||
* Copyright(c) 2012 TJ Holowaychuk
|
||||
* Copyright(c) 2014-2022 Douglas Christopher Wilson
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
#middleware (req, res, next) {
|
||||
// Only process GET and HEAD requests
|
||||
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
||||
@ -66,12 +81,12 @@ class ServeLiveDirectory {
|
||||
this.#options.setHeaders(req, res)
|
||||
}
|
||||
|
||||
if (!res.get('Last-Modified')) {
|
||||
if (this.#options.lastModified && !res.get('Last-Modified')) {
|
||||
const modified = new Date(file.last_update).toUTCString()
|
||||
res.header('Last-Modified', modified)
|
||||
}
|
||||
|
||||
if (!res.get('ETag')) {
|
||||
if (this.#options.etag && !res.get('ETag')) {
|
||||
const val = file.etag
|
||||
res.header('ETag', val)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user