| … | … | @@ -1,6 +1,7 @@ |
| 1 | 1 | import db from './db.json' with { type: 'json'} |
| 2 | 2 | import { serveDir } from 'https://deno.land/std/http/file_server.ts' |
| 3 | 3 | import { apds } from './apds.js' |
| 4 | import { createGossip } from './gossip.js' |
| 4 | 5 | |
| 5 | 6 | await apds.start('apdsv1') |
| 6 | 7 | |
| … | … | @@ -12,7 +13,19 @@ if (!await apds.pubkey()) { |
| 12 | 13 | console.log('apdsbot started, your pubkey: ' + await apds.pubkey()) |
| 13 | 14 | } |
| 14 | 15 | |
| 16 | const sockets = new Set() |
| 17 | const gossipQueue = createGossip({ |
| 18 | getPeers: () => sockets, |
| 19 | has: async (hash) => !!(await apds.get(hash)), |
| 20 | request: (peer, hash) => { |
| 21 | if (peer.readyState === 1) peer.send(hash) |
| 22 | }, |
| 23 | intervalMs: 10000, |
| 24 | }) |
| 25 | gossipQueue.start() |
| 26 | |
| 15 | 27 | const apdsbot = async (ws) => { |
| 28 | sockets.add(ws) |
| 16 | 29 | ws.onopen = async () => { |
| 17 | 30 | setTimeout(async () => { |
| 18 | 31 | const q = await apds.query() |
| … | … | @@ -38,10 +51,16 @@ const apdsbot = async (ws) => { |
| 38 | 51 | const latest = await apds.getLatest(m.data) |
| 39 | 52 | if (latest) { ws.send(latest.sig) } |
| 40 | 53 | const got = await apds.get(m.data) |
| 41 | | if (got) { ws.send(got) } |
| 54 | if (got) { |
| 55 | ws.send(got) |
| 56 | gossipQueue.resolve(m.data) |
| 57 | } else { |
| 58 | await gossipQueue.enqueue(m.data) |
| 59 | } |
| 42 | 60 | } |
| 43 | 61 | if (m.data.length != 44) { |
| 44 | | await apds.make(m.data) |
| 62 | const storedHash = await apds.make(m.data) |
| 63 | gossipQueue.resolve(storedHash) |
| 45 | 64 | await apds.add(m.data) |
| 46 | 65 | const opened = await apds.open(m.data) |
| 47 | 66 | if (opened) { |
| … | … | @@ -62,6 +81,7 @@ const apdsbot = async (ws) => { |
| 62 | 81 | } |
| 63 | 82 | } |
| 64 | 83 | ws.onclose = () => { |
| 84 | sockets.delete(ws) |
| 65 | 85 | console.log('DISCONNECTED!') |
| 66 | 86 | } |
| 67 | 87 | } |