OverviewFilesCommitsIssuesDocsReleasesPeers

apds commits / 674d24c

make a simple terminal/relay combo
674d24c8e7f3483f3ede36818c1eaf532de708e3
by Everett Bogue <ev@evbogue.com> · 2025-10-19T11:53:43.000Z

Changed files (3) +141 -0

cli.js +87 -0
……@@ -0,0 +1,87 @@
1import { apds } from './apds.js'
2
3await apds.start(Deno.args[0] || 'default')
4const pk = await apds.pubkey()
5
6const sockets = new Set()
7
8const send = async (hash) => {
9 const msg = await apds.get(hash)
10 const opened = await apds.open(msg)
11 const blob = await apds.get(opened.substring(13))
12 sockets.forEach(s => s.send(blob))
13 sockets.forEach(s => s.send(msg))
14 sockets.forEach(s => s.send(hash))
15}
16
17const render = async (h) => {
18 const get = await apds.get(h)
19 const opened = await apds.open(get)
20 const blob = await apds.get(opened.substring(13))
21 const yaml = await apds.parseYaml(blob)
22 //console.log(yaml)
23 const handle = yaml.name ? get.substring(0, 10) + ' ' + yaml.name : get.substring(0, 10)
24 console.log(`%c${handle} %c| ${yaml.body} -- %c${opened.substring(0, 13)}`, 'color: magenta', 'color: white', 'color: cyan')
25 //console.log(opened)
26 //console.log(blob)
27}
28
29const commands = async (c) => {
30 if (c.startsWith('nick')) {
31 const ar = c.split(' ')
32 if (ar[1]) {
33 await apds.put('name', ar[1])
34 console.log('You are now known as "' + ar[1] + '"')
35 } else {
36 console.log('ERROR: no nick to set')
37 }
38 }
39 if (c.startsWith('connect')) {
40 const ar = c.split(' ')
41 const s = ar[1]
42 console.log('Connecting to ' + s)
43 const ws = new WebSocket(s)
44 ws.onopen = () => {
45 console.log('Connected to: ' + s)
46 sockets.add(ws)
47 }
48 }
49 if (c.startsWith('clear')) {
50 const cleared = await apds.clear()
51 console.log('ERASING ALL DATA')
52 }
53 if (c.startsWith('exit')) {
54 console.log('EXITING APDS')
55 Deno.exit()
56 }
57}
58
59const flow = async () => {
60 const name = await apds.get('name')
61 const pub = await apds.pubkey()
62 const handle = name ? pub.substring(0, 10) + ' ' + name : pub.substring(0, 10)
63 const command = prompt(handle + '>')
64 if (command.startsWith('/')) {
65 await commands(command.substring(1))
66 await flow()
67 } else if (command.length > 0) {
68 const anmsg = await apds.compose(command)
69 await render(anmsg)
70 await send(anmsg)
71 await flow()
72 } else {
73 await flow()
74 }
75}
76
77if (pk) {
78 console.log('ANPROTO APDS ACTIVATED -- Your pubkey is: ' + pk)
79 await flow()
80} else {
81 const kp = await apds.generate()
82 await apds.put('keypair', kp)
83 console.log('ANPROTO APDS ACTIVATED -- No keypair found!')
84 console.log('New keypair generated! ' + await apds.pubkey())
85 await flow()
86}
87
relay.js +41 -0
……@@ -0,0 +1,41 @@
1import { apds } from './apds.js'
2import { render } from './trender.js'
3
4await apds.start(Deno.args[0] || 'default')
5
6const sockets = new Set()
7
8const handle = async (ws) => {
9 ws.onopen = () => {
10 console.log('Connection opened')
11 }
12
13 ws.onmessage = async (m) => {
14 if (m.data.length === 44) {
15 setTimeout(async () => {
16 try { await render(m.data)} catch (err) { console.log(err)}
17 }, 100)
18 } else {
19 try {
20 await apds.make(m.data)
21 await apds.add(m.data)
22 } catch (err) { console.log(err)}
23 }
24 sockets.forEach(s => s.send(m.data))
25 }
26
27 ws.onclose = () => {
28 console.log('Connection closed')
29 }
30}
31
32Deno.serve(
33 {port: Deno.args[0] || 8080},
34 async (r) => {
35 try {
36 const { socket, response } = Deno.upgradeWebSocket(r)
37 await handle(socket)
38 sockets.add(socket)
39 return response
40 } catch (err) {}
41})
trender.js +13 -0
……@@ -0,0 +1,13 @@
1import { apds } from './apds.js'
2
3export const render = async (h) => {
4 const get = await apds.get(h)
5 const opened = await apds.open(get)
6 const blob = await apds.get(opened.substring(13))
7 const yaml = await apds.parseYaml(blob)
8 //console.log(yaml)
9 const handle = yaml.name ? get.substring(0, 10) + ' ' + yaml.name : get.substring(0, 10)
10 console.log(`%c${handle} %c| ${yaml.body} -- %c${opened.substring(0, 13)}`, 'color: magenta', 'color: white', 'color: cyan')
11 //console.log(opened)
12 //console.log(blob)
13}