diff --git a/dev/deno.tsx b/dev/deno.tsx index a578ded..d716031 100644 --- a/dev/deno.tsx +++ b/dev/deno.tsx @@ -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() @@ -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(
diff --git a/package.json b/package.json index bb4127d..a6c864e 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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": { diff --git a/src/kv/denokv.js b/src/kv/denokv.js new file mode 100644 index 0000000..f3a275b --- /dev/null +++ b/src/kv/denokv.js @@ -0,0 +1,8 @@ +export const kvClient = (options = {}) => { + return async (c, next) => { + c.kv = await Deno.openKv() + c.kvType = 'denokv' + + await next() + } +} diff --git a/src/session.js b/src/session.js index 7225971..62d4495 100644 --- a/src/session.js +++ b/src/session.js @@ -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; @@ -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: @@ -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: