mirror of
https://github.com/vacp2p/waku-web-chat.git
synced 2026-01-08 20:57:58 -05:00
Start to port nodejs changes to browser
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
const protons = require('protons')
|
||||
const EventEmitter = require('events')
|
||||
|
||||
const { Request, Stats } = protons(`
|
||||
const { Request, Stats, WakuMessage } = protons(`
|
||||
message WakuMessage {
|
||||
optional bytes payload = 1;
|
||||
optional string contentTopic = 2;
|
||||
optional string version = 3;
|
||||
}
|
||||
|
||||
message Request {
|
||||
enum Type {
|
||||
SEND_MESSAGE = 0;
|
||||
@@ -66,6 +72,24 @@ class Chat extends EventEmitter {
|
||||
|
||||
// Join if libp2p is already on
|
||||
if (this.libp2p.isStarted()) this.join()
|
||||
|
||||
// Experimental feature flag for WIP WakuMessage usage.
|
||||
//
|
||||
// If this flag is enabled:
|
||||
// TODO
|
||||
// - This implementation is according to spec
|
||||
// - Messages are published and subscribed on as WakuMessage
|
||||
// - No other Requests works, such as Stats etc
|
||||
// - No interop with nodejs yet
|
||||
//
|
||||
// If it isn't enabled:
|
||||
// - Largely inverse of above, notably not according to spec
|
||||
// - No real interop with nim-waku
|
||||
// - On flip side, nice UI with browser and Stats/Nick etc
|
||||
this.useWakuMessage = true
|
||||
|
||||
console.info("Using WakuMessage?", this.useWakuMessage)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,24 +101,34 @@ class Chat extends EventEmitter {
|
||||
this.libp2p.pubsub.subscribe(this.topic, (message) => {
|
||||
try {
|
||||
console.info("Received message on topic, trying to decode...")
|
||||
const request = Request.decode(message.data)
|
||||
switch (request.type) {
|
||||
case Request.Type.UPDATE_PEER:
|
||||
this.emit('peer:update', {
|
||||
id: message.from,
|
||||
name: request.updatePeer.userHandle.toString()
|
||||
})
|
||||
break
|
||||
case Request.Type.STATS:
|
||||
this.stats.set(message.from, request.stats)
|
||||
console.log('Incoming Stats:', message.from, request.stats)
|
||||
this.emit('stats', this.stats)
|
||||
break
|
||||
default:
|
||||
this.emit('message', {
|
||||
from: message.from,
|
||||
...request.sendMessage
|
||||
})
|
||||
if (this.useWakuMessage) {
|
||||
console.info("Reading message as a WakuMessage")
|
||||
const msg = WakuMessage.decode(message.data)
|
||||
// XXX: Might not always work...
|
||||
const text = String.fromCharCode(...msg.payload)
|
||||
console.info("WakuMessage: ", msg.contentTopic, text)
|
||||
}
|
||||
else {
|
||||
//TODO Figure out how to re-enable / remove wrt chat2 example
|
||||
const request = Request.decode(message.data)
|
||||
switch (request.type) {
|
||||
case Request.Type.UPDATE_PEER:
|
||||
this.emit('peer:update', {
|
||||
id: message.from,
|
||||
name: request.updatePeer.userHandle.toString()
|
||||
})
|
||||
break
|
||||
case Request.Type.STATS:
|
||||
this.stats.set(message.from, request.stats)
|
||||
console.log('Incoming Stats:', message.from, request.stats)
|
||||
this.emit('stats', this.stats)
|
||||
break
|
||||
default:
|
||||
this.emit('message', {
|
||||
from: message.from,
|
||||
...request.sendMessage
|
||||
})
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
@@ -170,20 +204,30 @@ class Chat extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Publishes the given `message` to pubsub peers
|
||||
* @param {Buffer|string} message The chat message to send
|
||||
*/
|
||||
async send (message) {
|
||||
const msg = Request.encode({
|
||||
type: Request.Type.SEND_MESSAGE,
|
||||
sendMessage: {
|
||||
id: (~~(Math.random() * 1e9)).toString(36) + Date.now(),
|
||||
data: Buffer.from(message),
|
||||
created: Date.now()
|
||||
}
|
||||
})
|
||||
|
||||
var msg
|
||||
// NOTE Conditionally wrap in WakuMessage or not
|
||||
if (this.useWakuMessage) {
|
||||
msg = WakuMessage.encode({
|
||||
contentTopic: 'dingpu',
|
||||
payload: Buffer.from(message)
|
||||
})
|
||||
}
|
||||
else {
|
||||
msg = Request.encode({
|
||||
type: Request.Type.SEND_MESSAGE,
|
||||
sendMessage: {
|
||||
id: (~~(Math.random() * 1e9)).toString(36) + Date.now(),
|
||||
data: Buffer.from(message),
|
||||
created: Date.now()
|
||||
}
|
||||
})
|
||||
}
|
||||
await this.libp2p.pubsub.publish(this.topic, msg)
|
||||
}
|
||||
}
|
||||
|
||||
60
browser/package-lock.json
generated
60
browser/package-lock.json
generated
@@ -5609,6 +5609,11 @@
|
||||
"ip-regex": "^4.0.0"
|
||||
}
|
||||
},
|
||||
"is-loopback-addr": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/is-loopback-addr/-/is-loopback-addr-1.0.1.tgz",
|
||||
"integrity": "sha512-DhWU/kqY7X2F6KrrVTu7mHlbd2Pbo4D1YkAzasBMjQs6lJAoefxaA6m6CpSX0K6pjt9D0b9PNFI5zduy/vzOYw=="
|
||||
},
|
||||
"is-negative-zero": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/is-negative-zero/-/is-negative-zero-2.0.0.tgz",
|
||||
@@ -6458,31 +6463,31 @@
|
||||
}
|
||||
},
|
||||
"libp2p-gossipsub": {
|
||||
"version": "0.6.1",
|
||||
"resolved": "https://registry.npmjs.org/libp2p-gossipsub/-/libp2p-gossipsub-0.6.1.tgz",
|
||||
"integrity": "sha512-gwmRlS//Zz1nYuq4BfOsV3yg27i++uXihnteF5RztqRz6FqrRd0JsID32HtzD+LQ93PGTB457sxuOOpDvXLapQ==",
|
||||
"version": "0.6.3",
|
||||
"resolved": "https://registry.npmjs.org/libp2p-gossipsub/-/libp2p-gossipsub-0.6.3.tgz",
|
||||
"integrity": "sha512-eaRGtMYns29zPYDjbyvtdIJRGfmnsdwmx5QxqPUcHbmUnwDX1FfeKxT1hY8EuDhhGSsinoZO9w3d6TXUrr1jbQ==",
|
||||
"requires": {
|
||||
"@types/debug": "^4.1.5",
|
||||
"debug": "^4.1.1",
|
||||
"denque": "^1.4.1",
|
||||
"err-code": "^2.0.0",
|
||||
"it-pipe": "^1.0.1",
|
||||
"libp2p-interfaces": "^0.5.1",
|
||||
"libp2p-interfaces": "^0.5.2",
|
||||
"peer-id": "^0.14.0",
|
||||
"protons": "^2.0.0",
|
||||
"time-cache": "^0.3.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"cids": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/cids/-/cids-1.0.0.tgz",
|
||||
"integrity": "sha512-HEBCIElSiXlkgZq3dgHJc3eDcnFteFp96N8/1/oqX5lkxBtB66sZ12jqEP3g7Ut++jEk6kIUGifQ1Qrya1jcNQ==",
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/cids/-/cids-1.0.2.tgz",
|
||||
"integrity": "sha512-ohCcYyEHh0Z5Hl+O1IML4kt6Kx5GPho1ybxtqK4zyk6DeV5CvOLoT/mqDh0cgKcAvsls3vcVa9HjZc7RQr3geA==",
|
||||
"requires": {
|
||||
"class-is": "^1.1.0",
|
||||
"multibase": "^3.0.0",
|
||||
"multicodec": "^2.0.0",
|
||||
"multibase": "^3.0.1",
|
||||
"multicodec": "^2.0.1",
|
||||
"multihashes": "^3.0.1",
|
||||
"uint8arrays": "^1.0.0"
|
||||
"uint8arrays": "^1.1.0"
|
||||
}
|
||||
},
|
||||
"libp2p-crypto": {
|
||||
@@ -6506,9 +6511,9 @@
|
||||
}
|
||||
},
|
||||
"libp2p-interfaces": {
|
||||
"version": "0.5.1",
|
||||
"resolved": "https://registry.npmjs.org/libp2p-interfaces/-/libp2p-interfaces-0.5.1.tgz",
|
||||
"integrity": "sha512-mqu8kN5KppDjRIzdOZqg7yEMwLJOxFGDpdXhvTq4obephTIusW4lLSunst7C5VVSN6UE0SSVliN0tHvyW8tpag==",
|
||||
"version": "0.5.2",
|
||||
"resolved": "https://registry.npmjs.org/libp2p-interfaces/-/libp2p-interfaces-0.5.2.tgz",
|
||||
"integrity": "sha512-jnf7D2tJ0eemfQp0j+u4s9fRlILduqXuanCpXt0QSxwqj8LVXUvglQddqoHjH6LGzxBvWXdOAk/ZXEUCcH4ZTw==",
|
||||
"requires": {
|
||||
"abort-controller": "^3.0.0",
|
||||
"abortable-iterator": "^3.0.0",
|
||||
@@ -6555,15 +6560,17 @@
|
||||
}
|
||||
},
|
||||
"libp2p-utils": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/libp2p-utils/-/libp2p-utils-0.2.0.tgz",
|
||||
"integrity": "sha512-tZmqu27SULiIvfV+RZg5WOomxXIqM/SEd9FwKuirYTHHU1eet2bLzVQBhigatrdyQxebqi8GVnwbKmqdRElgCA==",
|
||||
"version": "0.2.1",
|
||||
"resolved": "https://registry.npmjs.org/libp2p-utils/-/libp2p-utils-0.2.1.tgz",
|
||||
"integrity": "sha512-oaPUhYZrg3iW8+V7/PJsMHbLsFiOaNKM+D3WzNkne8mP7CCM4+0B4TIid5nEvrUT8Z432Nb64nFaqie/Wif5GA==",
|
||||
"requires": {
|
||||
"abortable-iterator": "^3.0.0",
|
||||
"debug": "^4.1.1",
|
||||
"debug": "^4.2.0",
|
||||
"err-code": "^2.0.3",
|
||||
"ip-address": "^6.1.0",
|
||||
"multiaddr": "^8.0.0"
|
||||
"is-loopback-addr": "^1.0.0",
|
||||
"multiaddr": "^8.0.0",
|
||||
"private-ip": "^1.0.5"
|
||||
}
|
||||
},
|
||||
"mafmt": {
|
||||
@@ -6640,9 +6647,9 @@
|
||||
}
|
||||
},
|
||||
"peer-id": {
|
||||
"version": "0.14.1",
|
||||
"resolved": "https://registry.npmjs.org/peer-id/-/peer-id-0.14.1.tgz",
|
||||
"integrity": "sha512-QSEyJy9fEOtgB/NVrlJvlxO1Q8ZKpTLJ/HBVTj7bGJFGnm4febqSB/KlEL4WYm/fgvriHM+Wkfea3yD1Uacllw==",
|
||||
"version": "0.14.2",
|
||||
"resolved": "https://registry.npmjs.org/peer-id/-/peer-id-0.14.2.tgz",
|
||||
"integrity": "sha512-8iZWaUT7jq8rVyyFZUHYUwFCvhoI5B1Q2MAJjUF9MTf4TsNRQPnod4Mycf2jrK/uXFBN5/9K1NhPoieFyz/PRw==",
|
||||
"requires": {
|
||||
"cids": "^1.0.0",
|
||||
"class-is": "^1.1.0",
|
||||
@@ -6665,9 +6672,9 @@
|
||||
}
|
||||
},
|
||||
"streaming-iterables": {
|
||||
"version": "5.0.2",
|
||||
"resolved": "https://registry.npmjs.org/streaming-iterables/-/streaming-iterables-5.0.2.tgz",
|
||||
"integrity": "sha512-9z5iBWe9WXzdT0X1JT9fVC0mCcVxWt5yzZMBUIgjZnt2k23+UQF8Ac6kiI8DnlYZJn5iysvxKl3uGzlijMQ+/g=="
|
||||
"version": "5.0.3",
|
||||
"resolved": "https://registry.npmjs.org/streaming-iterables/-/streaming-iterables-5.0.3.tgz",
|
||||
"integrity": "sha512-1AgrKjHTvaaK+iA+N3BuTXQWVb7Adyb6+v8yIW3SCTwlBVYEbm76mF8Mf0/IVo+DOk7hoeELOURBKTCMhe/qow=="
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -9074,6 +9081,11 @@
|
||||
"integrity": "sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=",
|
||||
"dev": true
|
||||
},
|
||||
"private-ip": {
|
||||
"version": "1.0.5",
|
||||
"resolved": "https://registry.npmjs.org/private-ip/-/private-ip-1.0.5.tgz",
|
||||
"integrity": "sha1-ItAYP7oJ0OwaKk4PRv63cVY9FEk="
|
||||
},
|
||||
"process": {
|
||||
"version": "0.11.10",
|
||||
"resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz",
|
||||
|
||||
@@ -75,10 +75,8 @@ class Chat {
|
||||
// Experimental feature flag for WIP WakuMessage usage.
|
||||
//
|
||||
// If this flag is enabled:
|
||||
// - This impl is according to spec
|
||||
// - This implementation is according to spec
|
||||
// - Messages are published and subscribed on as WakuMessage
|
||||
// - Messages published here show up on nim-waku in clear text
|
||||
// - Messages published on nim-waku for some reason don't show up here yet
|
||||
// - No other Requests works, such as Stats etc
|
||||
// - No interop with browser yet
|
||||
//
|
||||
@@ -86,7 +84,7 @@ class Chat {
|
||||
// - Largely inverse of above, notably not according to spec
|
||||
// - No real interop with nim-waku
|
||||
// - On flip side, nice UI with browser and Stats/Nick etc
|
||||
this.useWakuMessage = false
|
||||
this.useWakuMessage = true
|
||||
|
||||
console.info("Using WakuMessage?", this.useWakuMessage)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user