Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Add scheduler to clean up expired sessions #53

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
"dayjs": "^1.11.6",
"defu": "^6.1.0",
"h3": "^1.0.1",
"node-cron": "^3.0.2",
"unstorage": "^1.0.1"
},
"devDependencies": {
"@nuxt/module-builder": "^0.2.1",
"@nuxt/schema": "^3.0.0",
"@nuxtjs/eslint-config-typescript": "^12.0.0",
"@types/node-cron": "^3.0.6",
"eslint": "8.28",
"nuxt": "^3.0.0"
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/middleware/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const safeSetCookie = (event: H3Event, name: string, value: string, createdAt: D
})
}

const checkSessionExpirationTime = (session: Session, sessionExpiryInSeconds: number) => {
export const checkSessionExpirationTime = (session: Session, sessionExpiryInSeconds: number) => {
const now = dayjs()
if (now.diff(dayjs(session.createdAt), 'seconds') > sessionExpiryInSeconds) {
throw new SessionExpired()
Expand Down
32 changes: 28 additions & 4 deletions src/runtime/server/middleware/session/sessionStorage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import { createStorage, prefixStorage } from 'unstorage'
import { schedule } from 'node-cron'
import { Session, SessionOptions } from '../../../../types'
import { checkSessionExpirationTime } from './index'
// @ts-ignore
import { useRuntimeConfig } from '#imports'
// @ts-ignore
import sessionDriver from '#session-driver'
const sessionConfig = useRuntimeConfig().session.session
const driver = sessionDriver(sessionConfig.storageOptions.options)
const storage = createStorage({ driver }).mount(sessionConfig.storePrefix, driver)
export const sessionStorage = prefixStorage(storage, sessionConfig.storePrefix)

const CLEANUP_INTERVAL = '* * * * *'

const sessionOptions = useRuntimeConfig().session.session as SessionOptions
const driver = sessionDriver(sessionOptions.storageOptions.options)
const storage = createStorage({ driver }).mount(sessionOptions.storePrefix, driver)
const sessionStorage = prefixStorage(storage, sessionOptions.storePrefix)

// Cleanup expired sessions at a fixed interval
schedule(CLEANUP_INTERVAL, async () => {
const keys = await sessionStorage.getKeys()
keys.forEach(async (key) => {
const session = await sessionStorage.getItem(key) as Session
try {
if (sessionOptions.expiryInSeconds) {
checkSessionExpirationTime(session, sessionOptions.expiryInSeconds)
}
} catch {
await sessionStorage.removeItem(key)
}
})
})

export { sessionStorage }