feat: improved serveLiveDirectory

allow disabling etag and lastModified headers if required
This commit is contained in:
Bobby Wibowo 2022-07-21 23:56:57 +07:00
parent 1b4b73b67c
commit d9fd98f7de
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -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)
}