Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cache): add duration option #3367

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ jobs:
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- run: env NAME=Deno deno test --coverage=coverage/raw/deno-runtime --allow-read --allow-env --allow-write --allow-net -c runtime-tests/deno/deno.json runtime-tests/deno
- run: deno test -c runtime-tests/deno-jsx/deno.precompile.json --coverage=coverage/raw/deno-precompile-jsx runtime-tests/deno-jsx
- run: deno test -c runtime-tests/deno-jsx/deno.react-jsx.json --coverage=coverage/raw/deno-react-jsx runtime-tests/deno-jsx
- run: env NAME=Deno deno test --coverage=coverage/raw/deno-runtime --allow-read --allow-env --allow-write --allow-net -c runtime_tests/deno/deno.json runtime_tests/deno
- run: deno test -c runtime_tests/deno-jsx/deno.precompile.json --coverage=coverage/raw/deno-precompile-jsx runtime_tests/deno-jsx
- run: deno test -c runtime_tests/deno-jsx/deno.react-jsx.json --coverage=coverage/raw/deno-react-jsx runtime_tests/deno-jsx
- uses: actions/upload-artifact@v4
with:
name: coverage-deno
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hono",
"version": "4.6.2",
"version": "4.6.1",
"description": "Web framework built on Web Standards",
"main": "dist/cjs/index.js",
"type": "module",
Expand All @@ -12,18 +12,18 @@
"scripts": {
"test": "tsc --noEmit && vitest --run && vitest -c .vitest.config/jsx-runtime-default.ts --run && vitest -c .vitest.config/jsx-runtime-dom.ts --run",
"test:watch": "vitest --watch",
"test:deno": "deno test --allow-read --allow-env --allow-write --allow-net -c runtime-tests/deno/deno.json runtime-tests/deno && deno test --no-lock -c runtime-tests/deno-jsx/deno.precompile.json runtime-tests/deno-jsx && deno test --no-lock -c runtime-tests/deno-jsx/deno.react-jsx.json runtime-tests/deno-jsx",
"test:bun": "bun test --jsx-import-source ../../src/jsx runtime-tests/bun/index.test.tsx",
"test:fastly": "vitest --run --config ./runtime-tests/fastly/vitest.config.ts",
"test:node": "vitest --run --config ./runtime-tests/node/vitest.config.ts",
"test:workerd": "vitest --run --config ./runtime-tests/workerd/vitest.config.ts",
"test:lambda": "vitest --run --config ./runtime-tests/lambda/vitest.config.ts",
"test:lambda-edge": "vitest --run --config ./runtime-tests/lambda-edge/vitest.config.ts",
"test:deno": "deno test --allow-read --allow-env --allow-write --allow-net -c runtime_tests/deno/deno.json runtime_tests/deno && deno test --no-lock -c runtime_tests/deno-jsx/deno.precompile.json runtime_tests/deno-jsx && deno test --no-lock -c runtime_tests/deno-jsx/deno.react-jsx.json runtime_tests/deno-jsx",
"test:bun": "bun test --jsx-import-source ../../src/jsx runtime_tests/bun/index.test.tsx",
"test:fastly": "vitest --run --config ./runtime_tests/fastly/vitest.config.ts",
"test:node": "vitest --run --config ./runtime_tests/node/vitest.config.ts",
"test:workerd": "vitest --run --config ./runtime_tests/workerd/vitest.config.ts",
"test:lambda": "vitest --run --config ./runtime_tests/lambda/vitest.config.ts",
"test:lambda-edge": "vitest --run --config ./runtime_tests/lambda-edge/vitest.config.ts",
"test:all": "bun run test && bun test:deno && bun test:bun && bun test:fastly && bun test:node && bun test:workerd && bun test:lambda && bun test:lambda-edge",
"lint": "eslint src runtime-tests",
"lint:fix": "eslint src runtime-tests --fix",
"format": "prettier --check --cache \"src/**/*.{js,ts,tsx}\" \"runtime-tests/**/*.{js,ts,tsx}\"",
"format:fix": "prettier --write --cache --cache-strategy metadata \"src/**/*.{js,ts,tsx}\" \"runtime-tests/**/*.{js,ts,tsx}\"",
"lint": "eslint src runtime_tests",
"lint:fix": "eslint src runtime_tests --fix",
"format": "prettier --check --cache \"src/**/*.{js,ts,tsx}\" \"runtime_tests/**/*.{js,ts,tsx}\"",
"format:fix": "prettier --write --cache --cache-strategy metadata \"src/**/*.{js,ts,tsx}\" \"runtime_tests/**/*.{js,ts,tsx}\"",
"copy:package.cjs.json": "cp ./package.cjs.json ./dist/cjs/package.json && cp ./package.cjs.json ./dist/types/package.json ",
"build": "bun run --shell bun remove-dist && bun ./build.ts && bun run copy:package.cjs.json",
"postbuild": "publint",
Expand Down
34 changes: 0 additions & 34 deletions runtime-tests/deno/deno.lock

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ describe('Basic Auth Middleware', () => {
describe('Serve Static Middleware', () => {
const app = new Hono()
const onNotFound = vi.fn(() => {})
app.all('/favicon.ico', serveStatic({ path: './runtime-tests/bun/favicon.ico' }))
app.all('/favicon.ico', serveStatic({ path: './runtime_tests/bun/favicon.ico' }))
app.all(
'/favicon-notfound.ico',
serveStatic({ path: './runtime-tests/bun/favicon-notfound.ico', onNotFound })
serveStatic({ path: './runtime_tests/bun/favicon-notfound.ico', onNotFound })
)
app.use('/favicon-notfound.ico', async (c, next) => {
await next()
Expand All @@ -95,14 +95,14 @@ describe('Serve Static Middleware', () => {
app.get(
'/static/*',
serveStatic({
root: './runtime-tests/bun/',
root: './runtime_tests/bun/',
onNotFound,
})
)
app.get(
'/dot-static/*',
serveStatic({
root: './runtime-tests/bun/',
root: './runtime_tests/bun/',
rewriteRequestPath: (path) => path.replace(/^\/dot-static/, './.static'),
})
)
Expand All @@ -121,7 +121,7 @@ describe('Serve Static Middleware', () => {
expect(res.status).toBe(404)
expect(res.headers.get('X-Custom')).toBe('Bun')
expect(onNotFound).toHaveBeenCalledWith(
'./runtime-tests/bun/favicon-notfound.ico',
'./runtime_tests/bun/favicon-notfound.ico',
expect.anything()
)
})
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import config from '../../vitest.config'
export default defineConfig({
test: {
globals: true,
include: ['**/runtime-tests/bun/**/*.+(ts|tsx|js)'],
include: ['**/runtime_tests/bun/**/*.+(ts|tsx|js)'],
coverage: {
...config.test?.coverage,
reportsDirectory: './coverage/raw/runtime-bun',
Expand Down
132 changes: 132 additions & 0 deletions runtime_tests/deno/cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { expect } from '@std/expect'
import { FakeTime } from '@std/testing/time'
import { Hono } from '../../src/hono.ts'
import { cache } from '../../src/middleware/cache/index.ts'

Deno.test('Should return cached response', async () => {
const c = await caches.open('my-app')
await c.delete('http://localhost')

let oneTimeWord = 'Hello Hono'

const app = new Hono()
app.use(
'/',
cache({
cacheName: 'my-app',
wait: true,
})
)
app.get('/', (c) => {
const result = oneTimeWord
oneTimeWord = 'Not Found'
return c.text(result)
})

await app.request('http://localhost')
const res = await app.request('http://localhost')
expect(await res.text()).toBe('Hello Hono')
expect(res.headers.get('Hono-Cache-Expires')).toBeNull()
expect(res).not.toBeNull()
expect(res.status).toBe(200)
await c.delete('http://localhost')
})

Deno.test(
{
name: 'Should not return cached response over duration',
sanitizeResources: false,
},
async () => {
const time = new FakeTime()
const c = await caches.open('my-app')
await c.delete('http://localhost')

let oneTimeWord = 'Hello Hono'

const app = new Hono()
app.use(
'/',
cache({
cacheName: 'my-app',
wait: true,
duration: 60,
})
)
app.get('/', (c) => {
const result = oneTimeWord
oneTimeWord = 'Not Found'
return c.text(result)
})

await app.request('http://localhost')
let res = await app.request('http://localhost')

await time.tickAsync(59999)

expect(await res.text()).toBe('Hello Hono')
expect(res.headers.get('Hono-Cache-Expires')).toBeNull()
expect(res).not.toBeNull()
expect(res.status).toBe(200)

await time.tickAsync(1)

res = await app.request('http://localhost')
expect(await res.text()).toBe('Not Found')
expect(res.headers.get('Hono-Cache-Expires')).toBeNull()
expect(res).not.toBeNull()
expect(res.status).toBe(200)
await c.delete('http://localhost')
}
)

Deno.test(
{
name: 'Should return cached response if the response is immutable',
sanitizeResources: false,
},
async () => {
const c = await caches.open('my-app')
await c.delete('http://localhost')

let oneTime = true
let oneTimeWord = 'Hello Hono'

const example = new Hono()
example.get('/', (c) => {
const result = oneTimeWord
oneTimeWord = 'Not Found'
return c.text(result)
})

const app = new Hono()
app.use(
'/',
cache({
cacheName: 'my-app',
wait: true,
})
)
app.get('/', async (c) => {
const result = oneTime ? await fetch(`http://localhost:${server.addr.port}`) : 'Not Found'
oneTime = false
if (result instanceof Response) {
return result
} else {
return c.text('Not Found')
}
})

const server = Deno.serve({ port: 0 }, example.fetch)

await app.request('http://localhost')
const res = await app.request('http://localhost')
expect(await res.text()).toBe('Hello Hono')
expect(res.headers.get('Hono-Cache-Expires')).toBeNull()
expect(res).not.toBeNull()
expect(res.status).toBe(200)

await server.shutdown()
await c.delete('http://localhost')
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.3",
"@std/expect": "jsr:@std/expect@^1.0.2",
"@std/testing": "jsr:@std/testing@^1.0.1",
"hono/jsx/jsx-runtime": "../../src/jsx/jsx-runtime.ts"
}
Expand Down
54 changes: 54 additions & 0 deletions runtime_tests/deno/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ Deno.test('JSX middleware', async () => {
Deno.test('Serve Static middleware', async () => {
const app = new Hono()
const onNotFound = spy(() => {})
app.all('/favicon.ico', serveStatic({ path: './runtime-tests/deno/favicon.ico' }))
app.all('/favicon.ico', serveStatic({ path: './runtime_tests/deno/favicon.ico' }))
app.all(
'/favicon-notfound.ico',
serveStatic({ path: './runtime-tests/deno/favicon-notfound.ico', onNotFound })
serveStatic({ path: './runtime_tests/deno/favicon-notfound.ico', onNotFound })
)
app.use('/favicon-notfound.ico', async (c, next) => {
await next()
Expand All @@ -81,15 +81,15 @@ Deno.test('Serve Static middleware', async () => {
app.get(
'/static/*',
serveStatic({
root: './runtime-tests/deno',
root: './runtime_tests/deno',
onNotFound,
})
)

app.get(
'/dot-static/*',
serveStatic({
root: './runtime-tests/deno',
root: './runtime_tests/deno',
rewriteRequestPath: (path) => path.replace(/^\/dot-static/, './.static'),
})
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export default defineConfig({
plugins: [fastlyCompute()],
test: {
globals: true,
include: ['**/runtime-tests/fastly/**/(*.)+(test).+(ts|tsx)'],
exclude: ['**/runtime-tests/fastly/vitest.config.ts'],
include: ['**/runtime_tests/fastly/**/(*.)+(test).+(ts|tsx)'],
exclude: ['**/runtime_tests/fastly/vitest.config.ts'],
coverage: {
...config.test?.coverage,
reportsDirectory: './coverage/raw/runtime-fastly',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default defineConfig({
NAME: 'Node',
},
globals: true,
include: ['**/runtime-tests/lambda-edge/**/*.+(ts|tsx|js)'],
exclude: ['**/runtime-tests/lambda-edge/vitest.config.ts'],
include: ['**/runtime_tests/lambda-edge/**/*.+(ts|tsx|js)'],
exclude: ['**/runtime_tests/lambda-edge/vitest.config.ts'],
coverage: {
...config.test?.coverage,
reportsDirectory: './coverage/raw/runtime-lambda-edge',
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export default defineConfig({
NAME: 'Node',
},
globals: true,
include: ['**/runtime-tests/lambda/**/*.+(ts|tsx|js)'],
include: ['**/runtime_tests/lambda/**/*.+(ts|tsx|js)'],
exclude: [
'**/runtime-tests/lambda/vitest.config.ts',
'**/runtime-tests/lambda/mock.ts',
'**/runtime-tests/lambda/stream-mock.ts',
'**/runtime_tests/lambda/vitest.config.ts',
'**/runtime_tests/lambda/mock.ts',
'**/runtime_tests/lambda/stream-mock.ts',
],
coverage: {
...config.test?.coverage,
Expand Down
File renamed without changes.
Loading
Loading