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

Deno kv #4

Merged
merged 2 commits into from
Oct 16, 2023
Merged
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
20 changes: 10 additions & 10 deletions dev/deno.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @jsx jsx */
import { Hono } from 'https://deno.land/x/hono/mod.ts'
import { logger, jsx } from 'https://deno.land/x/hono/middleware.ts'
import { kvClient } from 'npm:hono-kv-session/bun';
import { kvClient } from '../src/kv/denokv.js';
import { SessionManager, createSession, deleteSession, renewSession, regenerateSession } from 'npm:hono-kv-session';

const app = new Hono()
Expand All @@ -16,20 +16,20 @@ app.use('*', SessionManager({
}))

app.get('/', async (c) => {
const url = new URL(c.req.url)
const { value: user } = c.session

const keys = await c.kv.keys('*');
const entries = await c.kv.list({ prefix: ['session', url.hostname] })

const kvLists = await Promise.all(
keys.map(async (name) => {
const [ expiration, value ] = await c.kv.multi().ttl(name).get(name).exec()
const kvLists = Array.from(entries).map((key) => {
console.log(key)
const [ expiration, value ] = entry

const now = Date.now()
const date = new Date(now + (expiration * 1000)).toISOString()
const now = Date.now()
const date = new Date(now + (expiration * 1000)).toISOString()

return [ name, value, date, expiration ]
})
)
return [ name, value, date, expiration ]
})

return c.html(
<div>
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hono-kv-session",
"version": "0.3.3",
"version": "0.4.0",
"description": "Stateful session middleware for Hono. Works with Cloudflare KV or node-redis.",
"files": [
"dist"
Expand All @@ -20,6 +20,10 @@
"./cloudflare": {
"import": "./dist/kv/cloudflare.js",
"require": "./dist/cjs/kv/cloudflare.js"
},
"./denokv": {
"import": "./dist/kv/denokv.js",
"require": "./dist/cjs/kv/denokv.js"
}
},
"scripts": {
Expand All @@ -35,7 +39,7 @@
"dev": "wrangler dev dev/worker.js",
"dev:bun": "bun run --hot dev/index.js",
"dev:node": "npm run build:node && node run.cjs",
"dev:deno": "deno run --allow-net --watch ./dev/deno.tsx",
"dev:deno": "deno run --allow-net --watch --unstable ./dev/deno.tsx",
"deploy": "wrangler deploy --minify dev/worker.js"
},
"repository": {
Expand Down
8 changes: 8 additions & 0 deletions src/kv/denokv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const kvClient = (options = {}) => {
return async (c, next) => {
c.kv = await Deno.openKv()
c.kvType = 'denokv'

await next()
}
}
36 changes: 27 additions & 9 deletions src/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@ export const SessionManager = (options = {}) => {

c.session.ttl = ttl
c.session.key = (secret) ? await getSignedCookie(c, secret, name) : getCookie(c, name)
c.session.value = (c.session.key) && await kv.get(`session:${url.hostname}:${c.session.key}`)
if (c.session.key) {
switch (c.kvType) {
case 'cloudflare':
case 'redis':
c.session.value = await kv.get(`session:${url.hostname}:${c.session.key}`)
break

case 'denokv':
c.session.value = await kv.get(['session', url.hostname, c.session.key])
break

default:
throw new Error('Invalid kvType')
}
}

c.session.status = (!c.session.value) ? false : true;

Expand Down Expand Up @@ -60,19 +74,21 @@ export const createSession = async (c, value, options = {}) => {
const { kv } = c
const url = new URL(c.req.url)

const key = `session:${url.hostname}:${session}`;

if (c.session.ttl < 60) {
c.session.ttl = 60
}

switch (c.kvType) {
case 'cloudflare':
await kv.put(key, value, { expirationTtl: c.session.ttl })
await kv.put(`session:${url.hostname}:${session}`, value, { expirationTtl: c.session.ttl })
break

case 'redis':
await kv.set(key, value, { EX: c.session.ttl })
await kv.set(`session:${url.hostname}:${session}`, value, { EX: c.session.ttl })
break

case 'denokv':
await kv.set(['session', url.hostname, session], value, { expireIn: c.session.ttl * 1000 })
break

default:
Expand Down Expand Up @@ -103,15 +119,17 @@ export const deleteSession = async (c) => {
const { name, key } = c.session
const url = new URL(c.req.url)

const kvKey = `session:${url.hostname}:${key}`;

switch (c.kvType) {
case 'cloudflare':
await kv.delete(kvKey)
await kv.delete(`session:${url.hostname}:${key}`)
break

case 'redis':
await kv.del(kvKey)
await kv.del(`session:${url.hostname}:${key}`)
break

case 'denokv':
await kv.delete(['session', url.hostname, key])
break

default:
Expand Down