-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapds.js
More file actions
366 lines (314 loc) · 8.87 KB
/
apds.js
File metadata and controls
366 lines (314 loc) · 8.87 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { decode } from './lib/base64.js'
import { cachekv } from './lib/cachekv.js'
import { idbkv } from './lib/idbkv.js'
import { human } from './lib/human.js'
import { vb } from './lib/vb.js'
import { an } from 'https://esm.sh/gh/evbogue/anproto@ddc040c/an.js'
let db
let hashLog = []
let openedLog = []
let newMessages = false
let sort = true
export const apds = {}
const rebuildOpenedLog = async () => {
const newArray = []
await Promise.all(hashLog.map(async (hash) => {
try {
const obj = {
hash,
sig: await apds.get(hash)
}
if (!obj.sig) { return }
const sigHash = await apds.hash(obj.sig)
if (sigHash !== hash) {
console.warn('hashlog integrity: sig hash mismatch', hash)
await db.rm(hash)
return
}
obj.author = obj.sig.substring(0, 44)
obj.opened = await apds.open(obj.sig)
if (!obj.opened || obj.opened.length < 14) {
console.warn('hashlog integrity: open failed', hash)
await db.rm(hash)
return
}
const contentHash = obj.opened.substring(13)
obj.text = await apds.get(contentHash)
if (obj.text) {
const contentSig = await apds.hash(obj.text)
if (contentSig !== contentHash) {
console.warn('hashlog integrity: content hash mismatch', hash)
await db.rm(contentHash)
return
}
}
obj.ts = obj.opened.substring(0, 13)
newArray.push(obj)
} catch (err) { /* ignore per-entry */ }
}))
await newArray.sort((a, b) => a.ts - b.ts)
const newLog = []
await newArray.forEach(msg => {
newLog.push(msg.hash)
})
hashLog = newLog
openedLog = newArray
newMessages = true
sort = false
}
apds.ensureOpenLog = async () => {
if (sort || !openedLog.length) {
await rebuildOpenedLog()
}
}
apds.start = async (appId) => {
if ('indexedDB' in globalThis) {
try {
db = await idbkv(appId)
const migrationFlag = 'cachekv_migrated_v1'
const migrationDone = await db.get(migrationFlag)
if (!migrationDone) {
const legacy = await cachekv(appId)
const migrated = []
if (legacy && legacy.keys) {
const keys = await legacy.keys()
for (const key of keys) {
if (key === migrationFlag) continue
const value = await legacy.get(key)
if (value === undefined) continue
const existing = await db.get(key)
if (existing === undefined) {
await db.put(key, value)
}
await legacy.rm(key)
migrated.push(key)
}
}
await db.put(migrationFlag, new Date().toISOString())
if (migrated.length) {
console.log('apds: migrated cachekv to IndexedDB', migrated.join(', '))
}
}
} catch (err) {
console.warn('IndexedDB unavailable, falling back to Cache API', err)
}
}
if (!db) {
db = await cachekv(appId)
}
setInterval(async () => {
if (newMessages) {
await db.put('hashlog', JSON.stringify(hashLog))
await db.put('openedlog', JSON.stringify(openedLog))
newMessages = false
}
}, 1000)
const getHashLog = await db.get('hashlog')
const getOpenedLog = await db.get('openedlog')
if (getHashLog) {
hashLog = JSON.parse(getHashLog)
}
if (getOpenedLog) {
openedLog = JSON.parse(getOpenedLog)
}
setInterval(async () => {
if (sort) {
await rebuildOpenedLog()
}
}, 20000)
}
apds.generate = async () => {
const genkey = await an.gen()
return genkey
}
apds.keypair = async () => {
const keypair = await db.get('keypair')
if (keypair) {
return keypair
}
}
apds.pubkey = async () => {
const keypair = await apds.keypair()
if (keypair) {
return keypair.substring(0, 44)
}
}
apds.privkey = async () => {
const keypair = await apds.keypair()
if (keypair) {
return keypair.substring(44)
}
}
apds.deletekey = async () => {
db.rm('keypair')
}
apds.clear = async () => {
db.clear()
}
apds.hash = async (data) => { return await an.hash(data) }
apds.sign = async (data) => {
const hash = await apds.make(data)
const sig = await an.sign(hash, await apds.keypair())
await apds.add(sig)
const protocolMsg = await apds.make(sig)
db.put('previous', protocolMsg)
return protocolMsg
}
apds.open = async (msg) => {
try {
if (msg.endsWith('==')) {
return await an.open(msg)
} //else {
//console.log('NOT A VALID SIGNATURE ' + msg)
//}
} catch (err) {
//console.log('Not a valid ANProto message')
}
}
import { yaml } from './lib/yaml.js'
apds.parseYaml = async (doc) => {
return await yaml.parse(doc)
}
apds.createYaml = async (obj, content) => {
return await yaml.create(obj, content)
}
const isHash = (value) => typeof value === 'string' && value.length === 44
const extractImagesFromBody = (body) => {
if (!body) { return [] }
const matches = body.match(/!\[.*?\]\((.*?)\)/g)
if (!matches) { return [] }
const hashes = []
for (const image of matches) {
const src = image.match(/!\[.*?\]\((.*?)\)/)?.[1]
if (isHash(src)) { hashes.push(src) }
}
return hashes
}
apds.compose = async (content, prev) => {
let obj = {}
if (prev) { obj = prev }
const name = await db.get('name')
const image = await db.get('image')
const previous = await db.get('previous')
if (name) { obj.name = name}
if (image) { obj.image = image}
if (previous) { obj.previous = previous}
if (Object.keys(obj).length > 0) {
const yaml = await apds.createYaml(obj, content)
return await apds.sign(yaml)
} else {
return await apds.sign(content)
}
}
apds.make = async (data) => {
const hash = await apds.hash(data)
await db.put(hash, data)
return hash
}
apds.get = async (hash) => {
const blob = await db.get(hash)
return blob
}
apds.put = async (key, value) => {
await db.put(key, value)
}
apds.rm = async (key) => {
await db.rm(key)
}
apds.add = async (msg) => {
const opened = await apds.open(msg)
if (opened) {
const hash = await apds.make(msg)
if (!hashLog.includes(hash)) {
hashLog.push(hash)
const obj = {
hash,
sig: msg
}
obj.author = obj.sig.substring(0, 44)
obj.opened = opened
obj.text = await apds.get(obj.opened.substring(13))
obj.ts = obj.opened.substring(0, 13)
openedLog.push(obj)
newMessages = true
sort = true
return true
}
}
}
apds.purgeAuthor = async (author) => {
if (!author || author.length !== 44) {
return { removed: 0, blobs: 0 }
}
const targets = openedLog.filter(msg => msg.author === author)
if (!targets.length) {
return { removed: 0, blobs: 0 }
}
const hashesToRemove = new Set()
const blobsToRemove = new Set()
for (const msg of targets) {
if (!msg || !msg.hash) { continue }
hashesToRemove.add(msg.hash)
const opened = msg.opened || (msg.sig ? await apds.open(msg.sig) : null)
const contentHash = opened && opened.length > 13 ? opened.substring(13) : null
const content = msg.text || (contentHash ? await apds.get(contentHash) : null)
if (contentHash && isHash(contentHash)) {
blobsToRemove.add(contentHash)
}
if (content) {
const parsed = await apds.parseYaml(content)
if (parsed?.image && isHash(parsed.image)) {
blobsToRemove.add(parsed.image)
}
const bodyImages = extractImagesFromBody(parsed?.body)
bodyImages.forEach(hash => blobsToRemove.add(hash))
}
}
for (const hash of blobsToRemove) {
await db.rm(hash)
}
for (const hash of hashesToRemove) {
await db.rm(hash)
}
if (hashesToRemove.size) {
hashLog = hashLog.filter(hash => !hashesToRemove.has(hash))
openedLog = openedLog.filter(msg => msg.author !== author)
newMessages = true
sort = false
await db.put('hashlog', JSON.stringify(hashLog))
await db.put('openedlog', JSON.stringify(openedLog))
}
return { removed: hashesToRemove.size, blobs: blobsToRemove.size }
}
apds.getHashLog = async () => { return hashLog }
apds.getOpenedLog = async () => { return openedLog }
apds.query = async (query) => {
if (!openedLog[0]) { return [] }
if (!query) { return openedLog }
if (query.startsWith('?')) {
const search = query.substring(1).replace(/%20/g, ' ').toUpperCase()
const result = openedLog.filter(msg => msg.text && msg.text.toUpperCase().includes(search))
return result
}
const result = openedLog.filter(msg => msg.author == query || msg.hash == query)
return result
}
apds.getPubkeys = async () => {
const arr = await apds.query()
const newSet = new Set()
for (const msg of arr) {
newSet.add(msg.author)
}
const newArr = [...newSet]
return newArr
}
apds.getLatest = async (pubkey) => {
const q = openedLog.filter(msg => msg.author === pubkey)
return q[q.length -1]
}
apds.human = async (ts) => {
return await human(new Date(parseInt(ts)))
}
apds.visual = async (pubkey) => {
return vb(decode(pubkey), 256)
}