Skip to content

Commit

Permalink
🎉 feat: release 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Apr 24, 2024
1 parent edb2931 commit 49b5e67
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

# 1.0.3 - 24 Apr 2024
Improvement:
- [#26](https://github.com/elysiajs/elysia-static/pull/26) support system paths (windows)
- [#25](https://github.com/elysiajs/elysia-static/pull/24) add maxAge param
- add cache-control directive

# 1.0.0 - 16 Mar 2024
Change:
- Add support for Elysia 1.0
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elysiajs/static",
"version": "1.0.2",
"version": "1.0.3",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
22 changes: 6 additions & 16 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,22 @@ export async function isCached(
if (
headers['cache-control'] &&
headers['cache-control'].indexOf('no-cache') !== -1
) {
)
return false
}

// if-none-match
if ('if-none-match' in headers) {
const ifNoneMatch = headers['if-none-match']

if (ifNoneMatch === '*') {
return true
}
if (ifNoneMatch === '*') return true

if (ifNoneMatch === null) {
return false
}
if (ifNoneMatch === null) return false

if (typeof etag !== 'string') {
return false
}
if (typeof etag !== 'string') return false

const isMatching = ifNoneMatch === etag

if (isMatching) {
return true
}
if (isMatching) return true

/**
* A recipient MUST ignore If-Modified-Since if the request contains an
Expand All @@ -63,9 +54,8 @@ export async function isCached(
if (
lastModified !== undefined &&
lastModified.getTime() <= Date.parse(ifModifiedSince)
) {
)
return true
}
}

return false
Expand Down
63 changes: 50 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { Elysia, NotFoundError } from 'elysia'

import { readdir, stat } from 'fs/promises'
import { resolve, resolve as resolveFn, join, sep} from 'path'
import { resolve, resolve as resolveFn, join, sep } from 'path'
import Cache from 'node-cache'

import { generateETag, isCached } from './cache'
import { Stats } from 'fs'

const URL_PATH_SEP = '/'
const fileExists = (path: string) => stat(path).then(() => true, () => false)
const fileExists = (path: string) =>
stat(path).then(
() => true,
() => false
)

const statCache = new Cache({
useClones: false,
Expand Down Expand Up @@ -60,7 +64,8 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
resolve = resolveFn,
headers = {},
noCache = false,
maxAge = 0,
maxAge = 86400,
directive = 'public',
indexHTML = true
}: {
/**
Expand Down Expand Up @@ -125,14 +130,33 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
*/
noCache?: boolean
/**
* @default 0
* @default public
*
* directive for Cache-Control header
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#directives
*/
directive?:
| 'public'
| 'private'
| 'must-revalidate'
| 'no-cache'
| 'no-store'
| 'no-transform'
| 'proxy-revalidate'
| 'immutable'
/**
* @default 86400
*
* Specifies the maximum amount of time in seconds, a resource will be considered fresh.
* This freshness lifetime is calculated relative to the time of the request.
* This setting helps control browser caching behavior.
* A `maxAge` of 0 will prevent caching, requiring requests to validate with the server before use.
*/
maxAge?: number
maxAge?: number | null
/**
*
*/
/**
* @default true
*
Expand Down Expand Up @@ -176,9 +200,12 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
alwaysStatic,
ignorePatterns,
noExtension,
enableDecodeURI,
resolve: resolve.toString(),
headers,
noCache,
maxAge,
directive,
indexHTML
}
})
Expand All @@ -205,9 +232,10 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
const file = Bun.file(filePath)
const etag = await generateETag(file)

const pathName = isFSSepUnsafe ?
prefix + fileName.split(sep).join(URL_PATH_SEP) :
join(prefix, fileName)
const pathName = isFSSepUnsafe
? prefix + fileName.split(sep).join(URL_PATH_SEP)
: join(prefix, fileName)

app.get(
pathName,
noCache
Expand All @@ -223,7 +251,9 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
}

headers['Etag'] = etag
headers['Cache-Control'] = `public, max-age=${maxAge}`
headers['Cache-Control'] = directive
if (maxAge)
headers['Cache-Control'] += `, max-age=${maxAge}`

return new Response(file, {
headers
Expand All @@ -247,8 +277,11 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
}

headers['Etag'] = etag
headers['Cache-Control'] =
`public, max-age=${maxAge}`
headers['Cache-Control'] = directive
if (maxAge)
headers[
'Cache-Control'
] += `, max-age=${maxAge}`

return new Response(file, {
headers
Expand Down Expand Up @@ -298,7 +331,9 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
htmlCache.get<boolean>(
`${path}${sep}index.html`
) ??
(await fileExists(`${path}${sep}index.html`)))
(await fileExists(
`${path}${sep}index.html`
)))
) {
if (hasCache === undefined)
htmlCache.set(
Expand Down Expand Up @@ -335,7 +370,9 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
})

headers['Etag'] = etag
headers['Cache-Control'] = `public, max-age=${maxAge}`
headers['Cache-Control'] = directive
if (maxAge)
headers['Cache-Control'] += `, max-age=${maxAge}`

return new Response(file, {
headers
Expand Down

0 comments on commit 49b5e67

Please sign in to comment.