-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
124 lines (107 loc) · 3.29 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Hono } from 'hono'
import { getRuntimeKey } from 'hono/adapter'
import { SessionManager, createSession, deleteSession, renewSession, regenerateSession } from '../src'
const app = new Hono()
app.use('*', SessionManager({
ttl: 60,
renew: false,
// regenerate: true,
}))
app.get('/', async (c) => {
const { value: user } = c.session
const runtime = getRuntimeKey()
let keys, kvLists;
if (runtime === 'workerd') {
if ('list' in c.kv) {
keys = (await c.kv.list()).keys;
kvLists = await Promise.all(
keys.map(async ({ name, expiration }) => {
const value = await c.kv.get(name)
const timestamp = expiration * 1000
const date = new Date(timestamp)
const now = Date.now()
return [ name, value, date.toISOString(), (date - now) / 1000 ]
})
)
} else {
const { results } = await c.kv.prepare('SELECT * FROM session').all()
keys = results.map(r => r.key)
kvLists = results.map(r => {
const date = new Date(r.ttl * 1000)
return [r.key, r.value, date.toISOString(), (date.getTime() - Date.now()) / 1000]
})
}
} else {
keys = await c.kv.keys('*');
kvLists = await Promise.all(
keys.map(async (name) => {
const [ expiration, value ] = await c.kv.multi().ttl(name).get(name).exec()
const now = Date.now()
const date = new Date(now + (expiration * 1000)).toISOString()
return [ name, value, date, expiration ]
})
)
}
return c.html(
<div>
<h1>hono-kv-session Demo</h1>
<ul>
<li>Source Code: <a href="https://github.com/shinosaki/hono-kv-session">Github</a></li>
<li>Author: <a href="https://shinosaki.com">@shinosaki</a></li>
</ul>
<form method="POST">
<input name="user" placeholder="username" />
<button>Get Session</button>
<p>{(user) ? `User: ${user}` : 'Session not found'}</p>
<hr />
<h2>KV DATA</h2>
<table>
<thead>
<tr>
<th>KV Key</th>
<td>KV Value</td>
<td>Expiration Date (after n sec)</td>
</tr>
</thead>
<tbody>
{kvLists.map(([ key, data, expire, diff ]) =>
<tr>
<th>{key}</th>
<td>{data}</td>
<td>{expire} ({diff})</td>
</tr>
)}
</tbody>
</table>
</form>
<hr />
<form action="/delete" method="POST">
<button>Delete Session</button>
</form>
<form action="/renew" method="POST">
<button>Renew Session</button>
</form>
<form action="/regen" method="POST">
<button>Regenerate Session</button>
</form>
</div>
)
})
app.post('/', async (c) => {
const { user } = await c.req.parseBody()
await createSession(c, user)
return c.redirect('/')
})
app.post('/delete', async (c) => {
await deleteSession(c)
return c.redirect('/')
})
app.post('/renew', async (c) => {
await renewSession(c)
return c.redirect('/')
})
app.post('/regen', async (c) => {
await regenerateSession(c)
return c.redirect('/')
})
export default app