|
| 1 | +/* eslint-disable node/no-extraneous-import */ |
| 2 | +/* eslint-disable no-process-exit */ |
| 3 | +/* eslint-disable unicorn/no-process-exit */ |
| 4 | +import {create} from '@dstack-js/ipfs' |
| 5 | +import {Stack} from '@dstack-js/lib' |
| 6 | +import {PubSub} from '@dstack-js/lib/src/pubsub' |
| 7 | +const wrtc = require('wrtc') |
| 8 | +import * as blessed from 'blessed' |
| 9 | + |
| 10 | +interface Message { |
| 11 | + nickname?: string; |
| 12 | + message: string; |
| 13 | +} |
| 14 | + |
| 15 | +export const run = async (room: string, nickname?: string) => { |
| 16 | + const ipfs = await create({ |
| 17 | + repo: process.env.IPFS_REPO, |
| 18 | + relay: { |
| 19 | + enabled: true, // enable relay dialer/listener (STOP) |
| 20 | + hop: { |
| 21 | + enabled: true, // make this node a relay (HOP) |
| 22 | + }, |
| 23 | + }, |
| 24 | + config: { |
| 25 | + Addresses: { |
| 26 | + Swarm: ['/ip4/0.0.0.0/tcp/0', '/dns4/dstack-relay.herokuapp.com/tcp/443/wss/p2p-webrtc-star'], |
| 27 | + }, |
| 28 | + Discovery: { |
| 29 | + MDNS: { |
| 30 | + Enabled: true, |
| 31 | + Interval: 1, |
| 32 | + }, |
| 33 | + webRTCStar: { |
| 34 | + Enabled: true, |
| 35 | + }, |
| 36 | + }, |
| 37 | + Bootstrap: ['/dns4/dstack-relay.herokuapp.com/tcp/443/wss/p2p-webrtc-star/p2p/QmV2uXBKbii29iJKHKVy8sx5m49qdDTBYNybVoa5uLJtrf'], |
| 38 | + }, |
| 39 | + }, wrtc) |
| 40 | + |
| 41 | + const stack = await Stack.create('dstack-chat', ipfs) |
| 42 | + const pubsub = stack.pubsub as PubSub<Message> |
| 43 | + |
| 44 | + const screen = blessed.screen({ |
| 45 | + smartCSR: true, |
| 46 | + title: `#${room}`, |
| 47 | + }) |
| 48 | + |
| 49 | + const messageList = blessed.list({ |
| 50 | + align: 'left', |
| 51 | + mouse: true, |
| 52 | + keys: true, |
| 53 | + width: '100%', |
| 54 | + height: '90%', |
| 55 | + border: 'line', |
| 56 | + top: 0, |
| 57 | + left: 0, |
| 58 | + items: [ |
| 59 | + 'dstack: connected', |
| 60 | + `dstack: your id is ${stack.id.slice(-5)}`, |
| 61 | + 'dstack: use /help to see commands', |
| 62 | + ], |
| 63 | + }) |
| 64 | + |
| 65 | + // Append our box to the screen. |
| 66 | + const input = blessed.textarea({ |
| 67 | + bottom: 0, |
| 68 | + height: '10%', |
| 69 | + inputOnFocus: true, |
| 70 | + clickable: true, |
| 71 | + label: ` ID: ${stack.id.slice(-5)} `, |
| 72 | + padding: { |
| 73 | + top: 1, |
| 74 | + left: 2, |
| 75 | + }, |
| 76 | + style: { |
| 77 | + fg: '#787878', |
| 78 | + bg: '#454545', |
| 79 | + |
| 80 | + focus: { |
| 81 | + fg: '#f6f6f6', |
| 82 | + bg: '#353535', |
| 83 | + }, |
| 84 | + }, |
| 85 | + }) |
| 86 | + |
| 87 | + input.key('enter', async function () { |
| 88 | + const message = input.getValue() |
| 89 | + if (message.length === 0) return |
| 90 | + |
| 91 | + if (message.startsWith('/connect ')) { |
| 92 | + await stack.ipfs.swarm.connect(message.split('/connect ')[1]) |
| 93 | + input.clearValue() |
| 94 | + screen.render() |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + if (message.startsWith('/addr')) { |
| 99 | + const {addresses} = await ipfs.id() |
| 100 | + |
| 101 | + for (const addr of addresses) { |
| 102 | + messageList.addItem(`dstack: ${addr.toString()}`) |
| 103 | + } |
| 104 | + |
| 105 | + input.clearValue() |
| 106 | + screen.render() |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + if (message.startsWith('/help')) { |
| 111 | + messageList.addItem('dstack: /addr - your addresses') |
| 112 | + messageList.addItem('dstack: /connect <addr> - manually connect to peer') |
| 113 | + messageList.addItem('dstack: /peers - peers list') |
| 114 | + input.clearValue() |
| 115 | + screen.render() |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + if (message.startsWith('/peers')) { |
| 120 | + const peers = await stack.peers() |
| 121 | + |
| 122 | + for (const peer of peers) { |
| 123 | + messageList.addItem(`dstack: ${peer.id}`) |
| 124 | + } |
| 125 | + |
| 126 | + input.clearValue() |
| 127 | + screen.render() |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + try { |
| 132 | + await pubsub.publish('chat', {nickname, message}) |
| 133 | + } catch { |
| 134 | + // error handling |
| 135 | + } finally { |
| 136 | + input.clearValue() |
| 137 | + screen.render() |
| 138 | + } |
| 139 | + }) |
| 140 | + |
| 141 | + screen.key(['escape', 'q', 'C-c'], function () { |
| 142 | + return process.exit(0) |
| 143 | + }) |
| 144 | + |
| 145 | + screen.append(messageList) |
| 146 | + screen.append(input) |
| 147 | + input.focus() |
| 148 | + |
| 149 | + await pubsub.subscribe('chat', event => { |
| 150 | + messageList.addItem(`${event.data.nickname ? `${event.data.nickname} (${event.from.slice(-5)})` : event.from.slice(-5)}: ${event.data.message}`) |
| 151 | + messageList.scrollTo(100) |
| 152 | + screen.render() |
| 153 | + }) |
| 154 | + |
| 155 | + stack.onPeerConnect(async peer => { |
| 156 | + messageList.setLabel(` Messages #${room} - Peers: ${await pubsub.peers('chat')} `) |
| 157 | + messageList.addItem(`dstack: peer connected ${peer.id.slice(-5)}`) |
| 158 | + messageList.scrollTo(100) |
| 159 | + screen.render() |
| 160 | + }) |
| 161 | + |
| 162 | + setInterval(async () => { |
| 163 | + messageList.setLabel(` Messages #${room} - Peers: ${await pubsub.peers('chat')} `) |
| 164 | + screen.render() |
| 165 | + }, 1000) |
| 166 | + |
| 167 | + messageList.scrollTo(100) |
| 168 | + screen.render() |
| 169 | +} |
0 commit comments