Updates
This commit is contained in:
@@ -1,59 +1,107 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const b4a = require('b4a')
|
||||
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
|
||||
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'qos-topic/v1'
|
||||
const MAX_QOS = 2
|
||||
|
||||
class HyperP2PQosTopic extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.peerHex = b4a.toString(this.keyPair.publicKey, 'hex')
|
||||
this._queues = [ [], [], [] ]
|
||||
this._handlers = new Map()
|
||||
this._stats = { published: 0, delivered: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
put (key, value) {
|
||||
assertNonEmpty(key, 'key')
|
||||
this._store.set(key, value)
|
||||
this._stats.ops++
|
||||
sendGossip(this, { type: 'qos-topic-sync', key, value })
|
||||
this.emit('update', { key, value })
|
||||
return true
|
||||
subscribe (channel, handler, qos = 0) {
|
||||
assertNonEmpty(channel, 'channel')
|
||||
if (typeof handler !== 'function') throw new Error('handler must be a function')
|
||||
this._handlers.set(channel, { handler, qos: Math.min(MAX_QOS, Math.max(0, qos | 0)) })
|
||||
return () => this._handlers.delete(channel)
|
||||
}
|
||||
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'qos-topic-sync', key, value: null })
|
||||
return ok
|
||||
publish (channel, payload, opts = {}) {
|
||||
assertNonEmpty(channel, 'channel')
|
||||
const qos = Math.min(MAX_QOS, Math.max(0, opts.qos | 0))
|
||||
const msg = {
|
||||
type: 'qos-publish',
|
||||
channel,
|
||||
payload,
|
||||
qos,
|
||||
from: this.peerHex,
|
||||
at: Date.now()
|
||||
}
|
||||
this._queues[qos].push(msg)
|
||||
this._stats.published++
|
||||
this._drain()
|
||||
if (this._peerMsgs) {
|
||||
gossipSend(this, msg)
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || d.type !== 'qos-topic-sync') return
|
||||
this._stats.gossipIn++
|
||||
if (d.key !== undefined) {
|
||||
if (d.value === null) this._store.delete(d.key)
|
||||
else this._store.set(d.key, d.value)
|
||||
_drain () {
|
||||
for (let q = MAX_QOS; q >= 0; q--) {
|
||||
while (this._queues[q].length) {
|
||||
const msg = this._queues[q].shift()
|
||||
this._deliver(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getStats () { return { ...this._stats, size: this._store.size, protocol: PROTOCOL } }
|
||||
_deliver (msg) {
|
||||
const sub = this._handlers.get(msg.channel)
|
||||
if (sub && msg.qos >= sub.qos) {
|
||||
this._stats.delivered++
|
||||
sub.handler({ channel: msg.channel, payload: msg.payload, qos: msg.qos, from: msg.from })
|
||||
}
|
||||
this.emit('message', msg)
|
||||
}
|
||||
|
||||
pending (qos) {
|
||||
if (qos == null) return this._queues.reduce((n, q) => n + q.length, 0)
|
||||
return this._queues[qos]?.length || 0
|
||||
}
|
||||
|
||||
_onGossip (data) {
|
||||
if (!data || data.type !== 'qos-publish') return
|
||||
this._stats.gossipIn++
|
||||
this._queues[data.qos].push(data)
|
||||
this._drain()
|
||||
}
|
||||
|
||||
getStats () {
|
||||
return {
|
||||
...this._stats,
|
||||
pending: this.pending(),
|
||||
protocol: PROTOCOL
|
||||
}
|
||||
}
|
||||
|
||||
async ready () {
|
||||
if (this.swarm || !this.topic) return this
|
||||
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
|
||||
await initModuleSwarm(this, {
|
||||
keyPair: this.keyPair,
|
||||
topic: this.topic,
|
||||
protocol: PROTOCOL,
|
||||
onmessage: (d) => this._onGossip(d)
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
async close () {
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
this._handlers.clear()
|
||||
for (const q of this._queues) q.length = 0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,23 +4,38 @@ const { HyperP2PQosTopic, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PQosTopic)
|
||||
t.ok(PROTOCOL)
|
||||
t.is(PROTOCOL, 'qos-topic/v1')
|
||||
})
|
||||
|
||||
test('basic operation', async (t) => {
|
||||
test('higher qos delivered first on drain', async (t) => {
|
||||
const m = new HyperP2PQosTopic()
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
const order = []
|
||||
m.subscribe('c', (msg) => order.push(msg.payload), 0)
|
||||
m._queues[0].push({ type: 'qos-publish', channel: 'c', payload: 'low', qos: 0, from: 'p' })
|
||||
m._queues[2].push({ type: 'qos-publish', channel: 'c', payload: 'high', qos: 2, from: 'p' })
|
||||
m._drain()
|
||||
t.is(order[0], 'high')
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PQosTopic()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
try { m.publish('', 1) } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('gossip delivery', async (t) => {
|
||||
const m = new HyperP2PQosTopic()
|
||||
let got = false
|
||||
m.subscribe('x', () => { got = true })
|
||||
m._onGossip({ type: 'qos-publish', channel: 'x', payload: 1, qos: 1, from: 'p' })
|
||||
t.ok(got)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PQosTopic()
|
||||
t.ok(m.getStats().protocol)
|
||||
m.publish('a', 1)
|
||||
t.is(m.getStats().published, 1)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
@@ -1,60 +1,62 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
|
||||
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
|
||||
|
||||
const PROTOCOL = 'retained-messages/v1'
|
||||
|
||||
class HyperP2PRetainedMessages extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this.maxPerChannel = opts.maxPerChannel || 32
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
this._stats = { set: 0, get: 0, cleared: 0 }
|
||||
}
|
||||
|
||||
put (key, value) {
|
||||
assertNonEmpty(key, 'key')
|
||||
this._store.set(key, value)
|
||||
this._stats.ops++
|
||||
sendGossip(this, { type: 'retained-messages-sync', key, value })
|
||||
this.emit('update', { key, value })
|
||||
return true
|
||||
retain (channel, payload, meta = {}) {
|
||||
assertNonEmpty(channel, 'channel')
|
||||
const list = this._store.get(channel) || []
|
||||
const entry = { payload, meta, at: Date.now() }
|
||||
list.push(entry)
|
||||
while (list.length > this.maxPerChannel) list.shift()
|
||||
this._store.set(channel, list)
|
||||
this._stats.set++
|
||||
this.emit('retain', { channel, entry })
|
||||
return entry
|
||||
}
|
||||
|
||||
get (key) { return this._store.get(key) }
|
||||
latest (channel) {
|
||||
const list = this._store.get(channel)
|
||||
if (!list || !list.length) return null
|
||||
this._stats.get++
|
||||
return list[list.length - 1]
|
||||
}
|
||||
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'retained-messages-sync', key, value: null })
|
||||
list (channel, limit = 10) {
|
||||
const list = this._store.get(channel) || []
|
||||
return list.slice(-limit)
|
||||
}
|
||||
|
||||
clear (channel) {
|
||||
if (channel == null) {
|
||||
this._store.clear()
|
||||
this._stats.cleared++
|
||||
return true
|
||||
}
|
||||
const ok = this._store.delete(channel)
|
||||
if (ok) this._stats.cleared++
|
||||
return ok
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || d.type !== 'retained-messages-sync') return
|
||||
this._stats.gossipIn++
|
||||
if (d.key !== undefined) {
|
||||
if (d.value === null) this._store.delete(d.key)
|
||||
else this._store.set(d.key, d.value)
|
||||
getStats () {
|
||||
return {
|
||||
...this._stats,
|
||||
channels: this._store.size,
|
||||
protocol: PROTOCOL
|
||||
}
|
||||
}
|
||||
|
||||
getStats () { return { ...this._stats, size: this._store.size, protocol: PROTOCOL } }
|
||||
|
||||
async ready () {
|
||||
if (this.swarm || !this.topic) return this
|
||||
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
|
||||
return this
|
||||
}
|
||||
|
||||
async close () {
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
}
|
||||
async ready () { return this }
|
||||
async close () { this._store.clear() }
|
||||
}
|
||||
|
||||
module.exports = { HyperP2PRetainedMessages, PROTOCOL }
|
||||
|
||||
@@ -4,23 +4,41 @@ const { HyperP2PRetainedMessages, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PRetainedMessages)
|
||||
t.ok(PROTOCOL)
|
||||
t.is(PROTOCOL, 'retained-messages/v1')
|
||||
})
|
||||
|
||||
test('basic operation', async (t) => {
|
||||
const m = new HyperP2PRetainedMessages()
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
await m.close()
|
||||
test('retain and latest', async (t) => {
|
||||
const r = new HyperP2PRetainedMessages()
|
||||
r.retain('t', { n: 1 })
|
||||
r.retain('t', { n: 2 })
|
||||
t.is(r.latest('t').payload.n, 2)
|
||||
await r.close()
|
||||
})
|
||||
|
||||
test('maxPerChannel', async (t) => {
|
||||
const r = new HyperP2PRetainedMessages({ maxPerChannel: 2 })
|
||||
r.retain('c', 1)
|
||||
r.retain('c', 2)
|
||||
r.retain('c', 3)
|
||||
t.is(r.list('c').length, 2)
|
||||
t.is(r.latest('c').payload, 3)
|
||||
await r.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PRetainedMessages()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
const r = new HyperP2PRetainedMessages()
|
||||
try {
|
||||
r.retain(null, 1)
|
||||
t.fail('expected throw')
|
||||
} catch (e) {
|
||||
t.ok(e instanceof Error)
|
||||
}
|
||||
await r.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PRetainedMessages()
|
||||
t.ok(m.getStats().protocol)
|
||||
await m.close()
|
||||
const r = new HyperP2PRetainedMessages()
|
||||
r.retain('a', 1)
|
||||
t.ok(r.getStats().protocol)
|
||||
await r.close()
|
||||
})
|
||||
|
||||
@@ -1,59 +1,123 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const { setInterval, clearInterval } = require('bare-timers')
|
||||
const b4a = require('b4a')
|
||||
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
|
||||
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'subscription-lease/v1'
|
||||
const DEFAULT_LEASE_MS = 60000
|
||||
|
||||
class HyperP2PSubscriptionLease extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.ownerHex = b4a.toString(this.keyPair.publicKey, 'hex')
|
||||
this.leaseMs = opts.leaseMs ?? DEFAULT_LEASE_MS
|
||||
this.enableBackgroundTimers = opts.enableBackgroundTimers === true
|
||||
this._leases = new Map()
|
||||
this._timer = null
|
||||
this._stats = { acquired: 0, released: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
put (key, value) {
|
||||
assertNonEmpty(key, 'key')
|
||||
this._store.set(key, value)
|
||||
this._stats.ops++
|
||||
sendGossip(this, { type: 'subscription-lease-sync', key, value })
|
||||
this.emit('update', { key, value })
|
||||
acquire (channel) {
|
||||
assertNonEmpty(channel, 'channel')
|
||||
const now = Date.now()
|
||||
const current = this._leases.get(channel)
|
||||
if (current && current.expiresAt > now && current.holder !== this.ownerHex) {
|
||||
return { ok: false, holder: current.holder }
|
||||
}
|
||||
const lease = { channel, holder: this.ownerHex, acquiredAt: now, expiresAt: now + this.leaseMs }
|
||||
this._leases.set(channel, lease)
|
||||
this._stats.acquired++
|
||||
if (this._peerMsgs) {
|
||||
gossipSend(this, { type: 'sub-lease', lease })
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
this.emit('acquire', lease)
|
||||
return { ok: true, ...lease }
|
||||
}
|
||||
|
||||
renew (channel) {
|
||||
const lease = this._leases.get(channel)
|
||||
if (!lease || lease.holder !== this.ownerHex) return false
|
||||
lease.expiresAt = Date.now() + this.leaseMs
|
||||
if (this._peerMsgs) {
|
||||
gossipSend(this, { type: 'sub-lease', lease })
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'subscription-lease-sync', key, value: null })
|
||||
return ok
|
||||
release (channel) {
|
||||
const lease = this._leases.get(channel)
|
||||
if (!lease || lease.holder !== this.ownerHex) return false
|
||||
this._leases.delete(channel)
|
||||
this._stats.released++
|
||||
if (this._peerMsgs) {
|
||||
gossipSend(this, { type: 'sub-release', channel, holder: this.ownerHex })
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
holder (channel) {
|
||||
const lease = this._leases.get(channel)
|
||||
if (!lease || lease.expiresAt < Date.now()) return null
|
||||
return lease.holder
|
||||
}
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || d.type !== 'subscription-lease-sync') return
|
||||
this._stats.gossipIn++
|
||||
if (d.key !== undefined) {
|
||||
if (d.value === null) this._store.delete(d.key)
|
||||
else this._store.set(d.key, d.value)
|
||||
_expireSweep () {
|
||||
const now = Date.now()
|
||||
for (const [ch, lease] of this._leases) {
|
||||
if (lease.expiresAt < now) {
|
||||
this._leases.delete(ch)
|
||||
this.emit('expired', { channel: ch })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getStats () { return { ...this._stats, size: this._store.size, protocol: PROTOCOL } }
|
||||
getStats () {
|
||||
return { ...this._stats, active: this._leases.size, protocol: PROTOCOL }
|
||||
}
|
||||
|
||||
async ready () {
|
||||
if (this.swarm || !this.topic) return this
|
||||
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
|
||||
await initModuleSwarm(this, {
|
||||
keyPair: this.keyPair,
|
||||
topic: this.topic,
|
||||
protocol: PROTOCOL,
|
||||
onmessage: (data) => {
|
||||
if (data && data.type === 'sub-lease' && data.lease) {
|
||||
const cur = this._leases.get(data.lease.channel)
|
||||
if (!cur || data.lease.expiresAt > cur.expiresAt) {
|
||||
this._leases.set(data.lease.channel, data.lease)
|
||||
}
|
||||
this._stats.gossipIn++
|
||||
} else if (data && data.type === 'sub-release') {
|
||||
const cur = this._leases.get(data.channel)
|
||||
if (cur && cur.holder === data.holder) this._leases.delete(data.channel)
|
||||
this._stats.gossipIn++
|
||||
}
|
||||
}
|
||||
})
|
||||
if (this.enableBackgroundTimers && !this._timer) {
|
||||
this._timer = setInterval(() => this._expireSweep(), 5000)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
async close () {
|
||||
if (this._timer) {
|
||||
clearInterval(this._timer)
|
||||
this._timer = null
|
||||
}
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
this.emit('closed')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,23 +4,44 @@ const { HyperP2PSubscriptionLease, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PSubscriptionLease)
|
||||
t.ok(PROTOCOL)
|
||||
t.is(PROTOCOL, 'subscription-lease/v1')
|
||||
})
|
||||
|
||||
test('basic operation', async (t) => {
|
||||
const m = new HyperP2PSubscriptionLease()
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
await m.close()
|
||||
test('acquire and holder', async (t) => {
|
||||
const l = new HyperP2PSubscriptionLease({ leaseMs: 5000 })
|
||||
const r = l.acquire('chan-a')
|
||||
t.ok(r.ok)
|
||||
t.is(l.holder('chan-a'), r.holder)
|
||||
await l.release('chan-a')
|
||||
await l.close()
|
||||
})
|
||||
|
||||
test('conflict when held', async (t) => {
|
||||
const l = new HyperP2PSubscriptionLease()
|
||||
l._leases.set('c1', {
|
||||
channel: 'c1',
|
||||
holder: 'other-peer',
|
||||
expiresAt: Date.now() + 60000
|
||||
})
|
||||
const r = l.acquire('c1')
|
||||
t.not(r.ok)
|
||||
await l.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PSubscriptionLease()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
const l = new HyperP2PSubscriptionLease()
|
||||
try {
|
||||
l.acquire(null)
|
||||
t.fail('expected throw')
|
||||
} catch (e) {
|
||||
t.ok(e instanceof Error)
|
||||
}
|
||||
await l.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PSubscriptionLease()
|
||||
t.ok(m.getStats().protocol)
|
||||
await m.close()
|
||||
const l = new HyperP2PSubscriptionLease()
|
||||
l.acquire('x')
|
||||
t.ok(l.getStats().protocol)
|
||||
await l.close()
|
||||
})
|
||||
|
||||
@@ -1,15 +1,22 @@
|
||||
# hyper-p2p-topic-channel architecture
|
||||
|
||||
**Tier:** scaffold · **Category:** `messaging-pubsub`
|
||||
**Tier:** production · **Category:** `messaging-pubsub` · **Protocol:** `topic-channel/v1`
|
||||
|
||||
## Role
|
||||
|
||||
Named topic channels.
|
||||
Named topic channels over Hyperswarm + Protomux: subscribe, publish, optional retained messages per channel.
|
||||
|
||||
## Wire messages
|
||||
|
||||
| type | Direction | Fields |
|
||||
|------|-----------|--------|
|
||||
| `subscribe` | gossip | `channel`, `peer` |
|
||||
| `unsubscribe` | gossip | `channel`, `peer` |
|
||||
| `publish` | gossip | `channel`, `payload`, `from`, `at`, `qos`, `retain` |
|
||||
| `retained-sync` | gossip | `channel`, `payload`, `from`, `at` |
|
||||
|
||||
## Composition
|
||||
|
||||
Uses `../../_shared/p2p-bare.js` for Hyperswarm + Protomux when implemented. Does **not** duplicate Holepunch core storage/transport.
|
||||
Composes with `hyper-p2p-gossip-mesh`, `hyper-p2p-topic-lease`, `hyper-p2p-subscription-lease`.
|
||||
|
||||
## Holepunch boundary
|
||||
|
||||
Inspiration: n/a
|
||||
Uses `../../_shared/p2p-bare.js` (`initModuleSwarm`, `gossipSend`).
|
||||
|
||||
@@ -2,7 +2,11 @@ require('bare-process/global')
|
||||
const { HyperP2PTopicChannel } = require('../index.js')
|
||||
|
||||
async function main () {
|
||||
const m = new HyperP2PTopicChannel()
|
||||
console.log('[scaffold]', m.getStats())
|
||||
const ch = new HyperP2PTopicChannel({ retainMessages: true })
|
||||
ch.subscribe('demo', (m) => console.log('[topic-channel]', m))
|
||||
ch.publish('demo', { hello: 'wave9' }, { retain: true })
|
||||
console.log(ch.getStats())
|
||||
await ch.close()
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const b4a = require('b4a')
|
||||
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
|
||||
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'topic-channel/v1'
|
||||
|
||||
@@ -10,50 +11,138 @@ class HyperP2PTopicChannel extends EventEmitter {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.peerHex = b4a.toString(this.keyPair.publicKey, 'hex')
|
||||
this.retainMessages = opts.retainMessages !== false
|
||||
this._subs = new Map()
|
||||
this._retained = new Map()
|
||||
this._stats = { published: 0, received: 0, subscriptions: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
put (key, value) {
|
||||
assertNonEmpty(key, 'key')
|
||||
this._store.set(key, value)
|
||||
this._stats.ops++
|
||||
sendGossip(this, { type: 'topic-channel-sync', key, value })
|
||||
this.emit('update', { key, value })
|
||||
subscribe (channel, handler) {
|
||||
assertNonEmpty(channel, 'channel')
|
||||
if (typeof handler !== 'function') throw new Error('handler must be a function')
|
||||
this._subs.set(channel, handler)
|
||||
this._stats.subscriptions++
|
||||
if (this._peerMsgs) {
|
||||
gossipSend(this, { type: 'subscribe', channel, peer: this.peerHex })
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
if (this.retainMessages && this._retained.has(channel)) {
|
||||
const r = this._retained.get(channel)
|
||||
handler({ channel, payload: r.payload, retained: true, from: r.from })
|
||||
}
|
||||
return () => this.unsubscribe(channel)
|
||||
}
|
||||
|
||||
unsubscribe (channel) {
|
||||
if (!this._subs.has(channel)) return false
|
||||
this._subs.delete(channel)
|
||||
if (this._peerMsgs) {
|
||||
gossipSend(this, { type: 'unsubscribe', channel, peer: this.peerHex })
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'topic-channel-sync', key, value: null })
|
||||
return ok
|
||||
publish (channel, payload, opts = {}) {
|
||||
assertNonEmpty(channel, 'channel')
|
||||
const msg = {
|
||||
type: 'publish',
|
||||
channel,
|
||||
payload,
|
||||
from: this.peerHex,
|
||||
at: Date.now(),
|
||||
qos: opts.qos || 0,
|
||||
retain: !!(opts.retain || this.retainMessages)
|
||||
}
|
||||
if (msg.retain) {
|
||||
this._retained.set(channel, { payload, from: this.peerHex, at: msg.at })
|
||||
}
|
||||
this._deliverLocal(channel, payload, { from: this.peerHex, local: true })
|
||||
if (this._peerMsgs) {
|
||||
gossipSend(this, msg)
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
this._stats.published++
|
||||
return msg
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
getRetained (channel) {
|
||||
return this._retained.get(channel) || null
|
||||
}
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || d.type !== 'topic-channel-sync') return
|
||||
_deliverLocal (channel, payload, meta) {
|
||||
const handler = this._subs.get(channel)
|
||||
if (handler) {
|
||||
this._stats.received++
|
||||
handler({ channel, payload, ...meta })
|
||||
}
|
||||
this.emit('message', { channel, payload, ...meta })
|
||||
}
|
||||
|
||||
_onGossip (data, peerInfo) {
|
||||
if (!data || !data.type) return
|
||||
this._stats.gossipIn++
|
||||
if (d.key !== undefined) {
|
||||
if (d.value === null) this._store.delete(d.key)
|
||||
else this._store.set(d.key, d.value)
|
||||
const from = data.from || (peerInfo && peerInfo.publicKey
|
||||
? b4a.toString(peerInfo.publicKey, 'hex')
|
||||
: null)
|
||||
|
||||
if (data.type === 'publish' && data.channel) {
|
||||
if (data.retain) {
|
||||
this._retained.set(data.channel, {
|
||||
payload: data.payload,
|
||||
from,
|
||||
at: data.at || Date.now()
|
||||
})
|
||||
}
|
||||
this._deliverLocal(data.channel, data.payload, { from, qos: data.qos })
|
||||
}
|
||||
|
||||
if (data.type === 'retained-sync' && data.channel && data.payload !== undefined) {
|
||||
this._retained.set(data.channel, {
|
||||
payload: data.payload,
|
||||
from: data.from,
|
||||
at: data.at || Date.now()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
getStats () { return { ...this._stats, size: this._store.size, protocol: PROTOCOL } }
|
||||
syncRetained (channel) {
|
||||
assertNonEmpty(channel, 'channel')
|
||||
const r = this._retained.get(channel)
|
||||
if (!r || !this._peerMsgs) return false
|
||||
gossipSend(this, { type: 'retained-sync', channel, payload: r.payload, from: r.from, at: r.at })
|
||||
this._stats.gossipOut++
|
||||
return true
|
||||
}
|
||||
|
||||
getStats () {
|
||||
return {
|
||||
...this._stats,
|
||||
channels: this._subs.size,
|
||||
retained: this._retained.size,
|
||||
protocol: PROTOCOL
|
||||
}
|
||||
}
|
||||
|
||||
async ready () {
|
||||
if (this.swarm || !this.topic) return this
|
||||
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
|
||||
await initModuleSwarm(this, {
|
||||
keyPair: this.keyPair,
|
||||
topic: this.topic,
|
||||
protocol: PROTOCOL,
|
||||
onmessage: (data, peerInfo) => this._onGossip(data, peerInfo)
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
async close () {
|
||||
this._subs.clear()
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
this.emit('closed')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,23 +4,45 @@ const { HyperP2PTopicChannel, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PTopicChannel)
|
||||
t.ok(PROTOCOL)
|
||||
t.is(PROTOCOL, 'topic-channel/v1')
|
||||
})
|
||||
|
||||
test('basic operation', async (t) => {
|
||||
const m = new HyperP2PTopicChannel()
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
await m.close()
|
||||
test('subscribe and publish local', async (t) => {
|
||||
const ch = new HyperP2PTopicChannel()
|
||||
let got = null
|
||||
ch.subscribe('news', (m) => { got = m })
|
||||
ch.publish('news', { hello: 1 })
|
||||
t.is(got.payload.hello, 1)
|
||||
t.ok(got.local)
|
||||
await ch.close()
|
||||
})
|
||||
|
||||
test('retained message on subscribe', async (t) => {
|
||||
const ch = new HyperP2PTopicChannel({ retainMessages: true })
|
||||
ch.publish('alerts', { n: 1 }, { retain: true })
|
||||
let got = null
|
||||
ch.subscribe('alerts', (m) => { got = m })
|
||||
t.is(got.payload.n, 1)
|
||||
t.ok(got.retained)
|
||||
await ch.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PTopicChannel()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
const ch = new HyperP2PTopicChannel()
|
||||
try {
|
||||
ch.subscribe(null, () => {})
|
||||
t.fail('expected throw')
|
||||
} catch (e) {
|
||||
t.ok(e instanceof Error)
|
||||
}
|
||||
await ch.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PTopicChannel()
|
||||
t.ok(m.getStats().protocol)
|
||||
await m.close()
|
||||
const ch = new HyperP2PTopicChannel()
|
||||
ch.publish('x', 1)
|
||||
const s = ch.getStats()
|
||||
t.is(s.protocol, 'topic-channel/v1')
|
||||
t.is(s.published, 1)
|
||||
await ch.close()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user