Skip to content

Commit

Permalink
feat: add maxAge support to exposeFolder
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbonnet committed Aug 20, 2020
1 parent e497428 commit 045b2ee
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions demo/files/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ <h1>Test page</h1>
<p>
This test page imports a script that prints something into the console.
</p>
<img src="./photo.jpeg" />
<button onclick="sendJson()">Send</button>
<button onclick="sendJson()">Post a JSON value</button>
<br />
<img src="./photo.jpeg" width="600" />
<form></form>
</body>
</html>
8 changes: 7 additions & 1 deletion src/exposeFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getAcceptedEncodingList } from './getAcceptedEncodingList'
export function exposeFolder({
path: folderPath,
cache = false,
maxAge = 1 * YEARS,
lastModified = true,
}) {
return async function (request, next) {
Expand All @@ -21,7 +22,6 @@ export function exposeFolder({
if (!isChildPath(folderPath, pathname)) {
return next(request)
}
const response = request.respond()
let stats
try {
stats = await stat(pathname)
Expand All @@ -34,6 +34,10 @@ export function exposeFolder({
if (!stats.isFile()) {
return next(request)
}
const response = request.respond()
if (maxAge) {
response.setHeader('Cache-Control', `public, max-age=${maxAge}`)
}
if (lastModified && stats.mtime) {
response.setHeader('Last-Modified', stats.mtime.toUTCString())
}
Expand Down Expand Up @@ -98,3 +102,5 @@ const COMPRESSIBLE_CONTENT_TYPES = {
'application/rss+xml': true,
'application/atom_xml': true,
}

const YEARS = 60 * 60 * 24 * 365
6 changes: 3 additions & 3 deletions src/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export function session({
key = await generateKey()
setCookie(response, name, key, {
httpOnly: true,
maxAge,
expires: new Date(getNow() + maxAge * 1000),
maxAge: (maxAge / 1000) | 0,
expires: new Date(getNow() + maxAge),
secure,
sameSite,
domain,
Expand Down Expand Up @@ -60,7 +60,7 @@ export function session({
}
}

const DAYS = 24 * 60 * 60
const DAYS = 24 * 60 * 60 * 1000

async function defaultGenerateKey() {
return (await randomBytes(48)).toString('hex')
Expand Down
Binary file modified src/tests/snapshots/exposeFolder.js.md
Binary file not shown.
Binary file modified src/tests/snapshots/exposeFolder.js.snap
Binary file not shown.

0 comments on commit 045b2ee

Please sign in to comment.