OverviewFilesCommitsIssuesDocsReleasesPeers

apds commits / ddff762

pass an appId to cachekv and rely on bogbot to get and save everything in the db
ddff7622098f3a2916431761f4d2b43bdd3c7048
by Ev Bogue <ev@evbogue.com> · 2025-01-04T20:43:20.000Z

Changed files (4) +100 -87

bogbot.js +64 -51
……@@ -4,17 +4,68 @@ import { cachekv } from './lib/cachekv.js'
44import { human } from './lib/human.js'
55import { vb } from './lib/vb.js'
66
7let db
8let log = []
9let newMessages = false
10let sort = true
11
712export const bogbot = {}
813
14bogbot.start = async (appId) => {
15 console.log(appId)
16
17 db = await cachekv(appId)
18
19 setInterval(async () => {
20 if (newMessages) {
21 await db.put('log', JSON.stringify(log))
22 newMessages = false
23 }
24 }, 1000)
25
26 const getLog = await db.get('log')
27 if (getLog) {
28 log = JSON.parse(getLog)
29 }
30
31
32 setInterval(async () => {
33 if (sort) {
34 const newArray = []
35
36 await Promise.all(log.map(async (hash) => {
37 const obj = {
38 hash,
39 sig: await bogbot.find(hash)
40 }
41 obj.opened = await bogbot.open(obj.sig)
42 obj.ts = obj.opened.substring(0, 13)
43 newArray.push(obj)
44 }))
45
46 await newArray.sort((a,b) => a.ts - b.ts)
47
48 const newLog = []
49
50 await newArray.forEach(msg => {
51 newLog.push(msg.hash)
52 })
53
54 log = newLog
55 newMessages = true
56 sort = false
57 }
58 }, 20000)
59}
60
961bogbot.generate = async () => {
1062 const genkey = nacl.sign.keyPair()
1163 const keygen = encode(genkey.publicKey) + encode(genkey.secretKey)
12 //await localStorage.setItem('keypair', keygen)
1364 return keygen
1465}
1566
1667bogbot.keypair = async () => {
17 const keypair = await localStorage.getItem('keypair')
68 const keypair = await db.get('keypair')
1869 if (keypair) {
1970 return keypair
2071 }
……@@ -35,7 +86,7 @@ bogbot.privkey = async () => {
3586}
3687
3788bogbot.deletekey = async () => {
38 localStorage.removeItem('keypair')
89 db.rm('keypair')
3990}
4091
4192bogbot.hash = async (data) => {
……@@ -56,7 +107,7 @@ bogbot.sign = async (data) => {
56107 const sig = encode(nacl.sign(new TextEncoder().encode(timestamp + hash), decode(await bogbot.privkey())))
57108 await bogbot.add(await bogbot.pubkey() + sig)
58109 const protocolMsg = await bogbot.make(await bogbot.pubkey() + sig)
59 localStorage.setItem('previous', protocolMsg)
110 db.put('previous', protocolMsg)
60111 return protocolMsg
61112}
62113
……@@ -86,9 +137,9 @@ bogbot.createYaml = async (obj, content) => {
86137bogbot.compose = async (content) => {
87138 const obj = {}
88139
89 const name = localStorage.getItem('name')
90 const image = localStorage.getItem('image')
91 const previous = localStorage.getItem('previous')
140 const name = await db.get('name')
141 const image = await db.get('image')
142 const previous = await db.get('previous')
92143
93144 if (name) { obj.name = name}
94145 if (image) { obj.image = image}
……@@ -105,30 +156,21 @@ bogbot.compose = async (content) => {
105156bogbot.make = async (data) => {
106157 const hash = await bogbot.hash(data)
107158
108 await cachekv.put(hash, data)
159 await db.put(hash, data)
109160
110161 return hash
111162}
112163
113164bogbot.find = async (hash) => {
114 const blob = await cachekv.get(hash)
165 const blob = await db.get(hash)
115166
116167 return blob
117168}
118169
119let newMessages = false
120
121setInterval(async () => {
122 if (newMessages) {
123 await cachekv.put('log', JSON.stringify(log))
124 newMessages = false
125 }
126}, 1000)
127
128let log = []
129const getLog = await cachekv.get('log')
130if (getLog) {
131 log = JSON.parse(getLog)
170bogbot.save = async (key, value) => {
171 console.log(key)
172 console.log(value)
173 await db.put(key, value)
132174}
133175
134176bogbot.add = async (msg) => {
……@@ -155,32 +197,3 @@ bogbot.visual = async (pubkey) => {
155197 return vb(decode(pubkey), 256)
156198}
157199
158let sort = true
159
160setInterval(async () => {
161 if (sort) {
162 const newArray = []
163
164 await Promise.all(log.map(async (hash) => {
165 const obj = {
166 hash,
167 sig: await bogbot.find(hash)
168 }
169 obj.opened = await bogbot.open(obj.sig)
170 obj.ts = obj.opened.substring(0, 13)
171 newArray.push(obj)
172 }))
173
174 await newArray.sort((a,b) => a.ts - b.ts)
175
176 const newLog = []
177
178 await newArray.forEach(msg => {
179 newLog.push(msg.hash)
180 })
181
182 log = newLog
183 newMessages = true
184 sort = false
185 }
186}, 20000)
example.js +3 -1
……@@ -3,9 +3,11 @@ import { profile } from './profile.js'
33import { composer } from './composer.js'
44import { render } from './render.js'
55
6await bogbot.start('bog5example')
7
68if (!await bogbot.pubkey()) {
79 const keypair = await bogbot.generate()
8 await localStorage.setItem('keypair', keypair)
10 await bogbot.save('keypair', keypair)
911}
1012
1113document.body.appendChild(await profile())
lib/cachekv.js +28 -30
……@@ -1,38 +1,36 @@
11const url = 'http://localhost:8000/'
22
3const db = 'bog5db'
3export const cachekv = async (appId) => {
4 if ('caches' in globalThis) {
5 const obj = {}
6 const cache = await caches.open(appId)
47
5export const cachekv = {}
6
7let cache
8
9if ('caches' in globalThis) {
10 cache = await caches.open(db)
11
12 cachekv.get = async function (key) {
13 const file = await cache.match(url + key)
14 try {
15 const string = await file.text()
16
17 return string
18 } catch {
19 return undefined
8 obj.get = async function (key) {
9 const file = await cache.match(url + key)
10 try {
11 const string = await file.text()
12
13 return string
14 } catch {
15 return undefined
16 }
17 }
18
19 obj.put = async function (key, string) {
20 await cache.delete(url + key)
21 await cache.put(url + key, new Response(string))
22 }
23
24 obj.rm = async function (key) {
25 await cache.delete(url + key)
2026 }
21 }
22
23 cachekv.put = async function (key, string) {
24 await cache.delete(url + key)
25 await cache.put(url + key, new Response(string))
26 }
2727
28 cachekv.rm = async function (key) {
29 await cache.delete(url + key)
30 }
31
32 cachekv.clear = async function () {
33 await caches.delete(db)
28 obj.clear = async function () {
29 await caches.delete(appId)
30 }
31 return obj
32 } else {
33 console.log('No Cache API available')
3434 }
35} else {
36 console.log('No Cache API available')
3735}
3836
profile.js +5 -5
……@@ -6,7 +6,7 @@ export const profile = async () => {
66
77 const avatarImg = await bogbot.visual(await bogbot.pubkey())
88
9 const existingImage = await localStorage.getItem('image')
9 const existingImage = await bogbot.find('image')
1010
1111 if (existingImage) { avatarImg.src = await bogbot.find(existingImage)}
1212
……@@ -47,12 +47,12 @@ export const profile = async () => {
4747 const croppedImage = canvas.toDataURL()
4848 avatarImg.src = croppedImage
4949 const hash = await bogbot.make(croppedImage)
50 localStorage.setItem('image', hash)
50 await bogbot.save('image', hash)
5151 } else {
5252 const croppedImage = canvas.toDataURL()
5353 avatarImg.src = img.src
5454 const hash = await bogbot.make(img.src)
55 localStorage.setItem('image', hash)
55 await bogbot.save('image', hash)
5656 }
5757 }
5858 img.src = e.target.result
……@@ -67,7 +67,7 @@ export const profile = async () => {
6767
6868 div.appendChild(h('div', [await bogbot.pubkey()]))
6969
70 const name = await localStorage.getItem('name')
70 const name = await bogbot.find('name')
7171
7272 const namer = h('input', {
7373 placeholder: name || 'Name yourself'
……@@ -79,7 +79,7 @@ export const profile = async () => {
7979 h('button', {onclick: async () => {
8080 if (namer.value) {
8181 namer.placeholder = namer.value
82 localStorage.setItem('name', namer.value)
82 await bogbot.save('name', namer.value)
8383 namer.value = ''
8484 }
8585 }}, ['Save'])