Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit 1b0ef0c

Browse files
committed
chore(dstack): upgrade to v0.2.33
1 parent 353aa0f commit 1b0ef0c

File tree

6 files changed

+1103
-409
lines changed

6 files changed

+1103
-409
lines changed

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
"peerchat": "./bin/run",
88
"pchat": "./bin/run"
99
},
10-
"resolutions": {
11-
"wrtc": "git+https://github.com/dstack-js/node-webrtc.git"
12-
},
1310
"bugs": "https://github.com/dstack-js/chat/issues",
1411
"dependencies": {
15-
"@dstack-js/ipfs": "^0.2.29",
16-
"@dstack-js/lib": "^0.2.25",
12+
"@dstack-js/ipfs": "^0.2.33",
13+
"@dstack-js/lib": "^0.2.33",
1714
"@oclif/command": "^1",
1815
"@oclif/config": "^1",
1916
"blessed": "^0.1.81",
2017
"discord.js": "^13.6.0",
18+
"graphql": "^16.3.0",
19+
"graphql-request": "^4.0.0",
20+
"libp2p-webrtc-star": "0.20.1",
21+
"libp2p-websockets": "0.15.0",
2122
"tslib": "^1",
2223
"wrtc": "^0.4.7"
2324
},

src/commands/discord/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class DiscordRelay extends Command {
2626
}
2727

2828
const { stack } = await getStack()
29-
const { pubsub } = stack
29+
const pubsub = stack.pubsub.create(`chat-${args.room}`)
3030

3131
const client = new Discord.Client({ intents: [Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES] })
3232
await client.login(process.env.DISCORD_KEY)

src/services/bootstrap.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { request, gql } from 'graphql-request'
2+
import { URL } from 'node:url'
3+
4+
export const getBootstrapData = async () => {
5+
const endpoint = process.env.RELAY_GRAPHQL_ENDPOINT || 'https://relay.dstack.dev:443/graphql'
6+
const url = new URL(endpoint)
7+
8+
const query = gql`
9+
query Bootstrap($protocol: Protocol!, $hostname: String!, $port: Int!) {
10+
listen(protocol: $protocol, hostname: $hostname, port: $port)
11+
peers(randomize: true)
12+
}
13+
`
14+
15+
return request<{ listen: string[]; peers: string[] }>(endpoint, query, { protocol: url.protocol.replace(':', ''), hostname: url.hostname, port: Number(url.port || '443') })
16+
}

src/services/chat.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { getStack } from './stack'
33

44
export const run = async (room: string, nickname?: string) => {
55
const { ipfs, stack } = await getStack()
6-
const { id, pubsub } = stack
6+
const { id } = await stack.id()
77

8+
const pubsub = stack.pubsub.create<{ nickname?: string; message: string }>(`chat-${room}`)
89
const screen = blessed.screen({
910
smartCSR: true,
1011
title: `#${room}`
@@ -93,7 +94,7 @@ export const run = async (room: string, nickname?: string) => {
9394
}
9495

9596
try {
96-
await pubsub.publish(room, { nickname, message })
97+
await pubsub.publish('message', { nickname, message })
9798
} catch {
9899
// error handling
99100
} finally {
@@ -114,14 +115,14 @@ export const run = async (room: string, nickname?: string) => {
114115
screen.append(input)
115116
input.focus()
116117

117-
await pubsub.subscribe(room, event => {
118+
await pubsub.subscribe('message', event => {
118119
messageList.addItem(`${event.data.nickname ? `${event.data.nickname} (${event.from.slice(-5)})` : event.from.slice(-5)}: ${event.data.message}`)
119120
messageList.scrollTo(100)
120121
screen.render()
121122
})
122123

123124
setInterval(async () => {
124-
messageList.setLabel(` Messages #${room} - Peers: ${await pubsub.peers(room)} `)
125+
messageList.setLabel(` Messages #${room} - Peers: ${await pubsub.peers('message')} `)
125126
screen.render()
126127
}, 1000)
127128

src/services/stack.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { create } from '@dstack-js/ipfs'
22
import { Stack } from '@dstack-js/lib'
3+
// @ts-expect-error: no types
4+
import WebRTCStar from 'libp2p-webrtc-star'
5+
// @ts-expect-error: no types
6+
import WebSocket from 'libp2p-websockets'
7+
import { getBootstrapData } from './bootstrap'
38

49
const wrtc = require('wrtc')
510

6-
interface Message {
7-
nickname?: string;
8-
message: string;
9-
}
10-
1111
export const getStack = async () => {
12+
const bootstrap = await getBootstrapData()
13+
console.log(bootstrap)
14+
1215
const ipfs = await create({
1316
repo: process.env.IPFS_REPO,
1417
init: {
@@ -22,7 +25,7 @@ export const getStack = async () => {
2225
},
2326
config: {
2427
Addresses: {
25-
Swarm: ['/ip4/0.0.0.0/tcp/0', '/dns4/dstack-relay.herokuapp.com/tcp/443/wss/p2p-webrtc-star']
28+
Swarm: bootstrap.listen
2629
},
2730
Discovery: {
2831
MDNS: {
@@ -33,11 +36,20 @@ export const getStack = async () => {
3336
Enabled: true
3437
}
3538
},
36-
Bootstrap: ['/dns4/dstack-relay.herokuapp.com/tcp/443/wss/p2p-webrtc-star/p2p/QmV2uXBKbii29iJKHKVy8sx5m49qdDTBYNybVoa5uLJtrf']
39+
Bootstrap: bootstrap.peers
40+
},
41+
libp2p: {
42+
// @ts-expect-error: incompatible types
43+
modules: {
44+
transport: [WebRTCStar, WebSocket]
45+
},
46+
addresses: {
47+
listen: bootstrap.listen
48+
}
3749
}
3850
}, wrtc)
3951

40-
const stack = await Stack.create('dstack-chat', ipfs)
52+
const stack = await Stack.create('dstack', ipfs)
4153

42-
return { stack: stack as Stack<Message>, ipfs }
54+
return { stack, ipfs }
4355
}

0 commit comments

Comments
 (0)