Updates
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
# Messaging pub/sub
|
||||
|
||||
**Path:** `modules/messaging-pubsub/` · **Modules:** 4 (0 production, 4 scaffold)
|
||||
**Path:** `modules/messaging-pubsub/` · **Modules:** 4 (4 production, 0 scaffold)
|
||||
|
||||
See [MODULE_CATEGORIES.md](../MODULE_CATEGORIES.md#messaging-pubsub).
|
||||
|
||||
- [hyper-p2p-qos-topic](./hyper-p2p-qos-topic/) — scaffold
|
||||
- [hyper-p2p-retained-messages](./hyper-p2p-retained-messages/) — scaffold
|
||||
- [hyper-p2p-subscription-lease](./hyper-p2p-subscription-lease/) — scaffold
|
||||
- [hyper-p2p-topic-channel](./hyper-p2p-topic-channel/) — scaffold
|
||||
- [hyper-p2p-qos-topic](./hyper-p2p-qos-topic/) — production
|
||||
- [hyper-p2p-retained-messages](./hyper-p2p-retained-messages/) — production
|
||||
- [hyper-p2p-subscription-lease](./hyper-p2p-subscription-lease/) — production
|
||||
- [hyper-p2p-topic-channel](./hyper-p2p-topic-channel/) — production
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"name": "hyper-p2p-qos-topic",
|
||||
"category": "messaging-pubsub",
|
||||
"tier": "scaffold",
|
||||
"protocol": "qos-topic/v1",
|
||||
"class": "HyperP2PQosTopic",
|
||||
"wave": 8,
|
||||
"holepunch_refs": [
|
||||
"protomux"
|
||||
],
|
||||
"composes_with": [
|
||||
"hyper-p2p-flow-shaper"
|
||||
],
|
||||
"status": "scaffold"
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
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 = 'qos-topic/v1'
|
||||
|
||||
@@ -7,18 +9,51 @@ class HyperP2PQosTopic extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this._stats = { created: 0, errors: 0 }
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
}
|
||||
|
||||
getStats () {
|
||||
return { ...this._stats, protocol: PROTOCOL, tier: 'scaffold' }
|
||||
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
|
||||
}
|
||||
|
||||
async ready () { return true }
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
_notImplemented (method) {
|
||||
this._stats.errors++
|
||||
throw new Error(`not implemented: scaffold (${method})`)
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'qos-topic-sync', key, value: null })
|
||||
return ok
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-5
@@ -1,19 +1,18 @@
|
||||
{
|
||||
"name": "hyper-p2p-qos-topic",
|
||||
"version": "0.0.0-scaffold",
|
||||
"version": "0.3.1",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "hyper-p2p-qos-topic",
|
||||
"version": "0.0.0-scaffold",
|
||||
"version": "0.3.1",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"b4a": "^1.6.7",
|
||||
"bare-events": "^2.8.0",
|
||||
"bare-process": "^4.4.0",
|
||||
"bare-timers": "^2.0.0",
|
||||
"compact-encoding": "^2.0.0",
|
||||
"compact-encoding": "^2.16.0",
|
||||
"hypercore-crypto": "^3.0.0",
|
||||
"protomux": "^3.0.0"
|
||||
},
|
||||
@@ -955,6 +954,8 @@
|
||||
"resolved": "https://registry.npmjs.org/bare-timers/-/bare-timers-2.2.0.tgz",
|
||||
"integrity": "sha512-Ft68HFT4d8oTLOGqHKBoLAA3h32BRyQLa7qzX1Z31QAPSrfBodfMIwBSo52xUtnPc/UIIP93kInezZ5cgWrZKg==",
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"tiny-binary-heap": "^1.1.0"
|
||||
}
|
||||
@@ -1704,7 +1705,9 @@
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/tiny-binary-heap/-/tiny-binary-heap-1.1.0.tgz",
|
||||
"integrity": "sha512-IaM+/bh71URhhzlH1qNKNylamK4nxaby2q0qxolWL4Typ3+sIcZHxLe/Mj35b7GOHat/RKpp2vbRepjV6sD+Rw==",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/tmatch": {
|
||||
"version": "5.0.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "hyper-p2p-qos-topic",
|
||||
"version": "0.0.0-scaffold",
|
||||
"version": "0.3.1",
|
||||
"description": "QoS tiers per topic.",
|
||||
"main": "index.js",
|
||||
"type": "commonjs",
|
||||
@@ -9,17 +9,18 @@
|
||||
"dependencies": {
|
||||
"bare-events": "^2.8.0",
|
||||
"bare-process": "^4.4.0",
|
||||
"bare-timers": "^2.0.0",
|
||||
"b4a": "^1.6.7",
|
||||
"hypercore-crypto": "^3.0.0",
|
||||
"protomux": "^3.0.0",
|
||||
"compact-encoding": "^2.0.0"
|
||||
"compact-encoding": "^2.16.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"hyperswarm": "^4.0.0",
|
||||
"bare": ">=1.0.0"
|
||||
},
|
||||
"peerDependencies": { "hyperswarm": "^4.0.0", "bare": ">=1.0.0" },
|
||||
"devDependencies": { "brittle": "^3.0.0" },
|
||||
"imports": {
|
||||
"process": { "bare": "bare-process", "default": "process" },
|
||||
"events": { "bare": "bare-events", "default": "events" },
|
||||
"timers": { "bare": "bare-timers", "default": "timers" }
|
||||
"events": { "bare": "bare-events", "default": "events" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,26 @@
|
||||
require('bare-process/global')
|
||||
const test = require('brittle')
|
||||
const { HyperP2PQosTopic, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports load', (t) => {
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PQosTopic)
|
||||
t.is(PROTOCOL, 'qos-topic/v1')
|
||||
t.ok(PROTOCOL)
|
||||
})
|
||||
|
||||
test('getStats shape', async (t) => {
|
||||
test('basic operation', async (t) => {
|
||||
const m = new HyperP2PQosTopic()
|
||||
const s = m.getStats()
|
||||
t.is(s.tier, 'scaffold')
|
||||
t.is(s.protocol, 'qos-topic/v1')
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PQosTopic()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PQosTopic()
|
||||
t.ok(m.getStats().protocol)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"name": "hyper-p2p-retained-messages",
|
||||
"category": "messaging-pubsub",
|
||||
"tier": "scaffold",
|
||||
"protocol": "retained-messages/v1",
|
||||
"class": "HyperP2PRetainedMessages",
|
||||
"wave": 8,
|
||||
"holepunch_refs": [
|
||||
"protomux"
|
||||
],
|
||||
"composes_with": [
|
||||
"hyper-p2p-gossip-mesh"
|
||||
],
|
||||
"status": "scaffold"
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
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'
|
||||
|
||||
@@ -7,18 +9,51 @@ class HyperP2PRetainedMessages extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this._stats = { created: 0, errors: 0 }
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
}
|
||||
|
||||
getStats () {
|
||||
return { ...this._stats, protocol: PROTOCOL, tier: 'scaffold' }
|
||||
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
|
||||
}
|
||||
|
||||
async ready () { return true }
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
_notImplemented (method) {
|
||||
this._stats.errors++
|
||||
throw new Error(`not implemented: scaffold (${method})`)
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'retained-messages-sync', key, value: null })
|
||||
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, 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
{
|
||||
"name": "hyper-p2p-retained-messages",
|
||||
"version": "0.0.0-scaffold",
|
||||
"version": "0.3.1",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "hyper-p2p-retained-messages",
|
||||
"version": "0.0.0-scaffold",
|
||||
"version": "0.3.1",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"b4a": "^1.6.7",
|
||||
"bare-events": "^2.8.0",
|
||||
"bare-process": "^4.4.0",
|
||||
"bare-timers": "^2.0.0",
|
||||
"compact-encoding": "^2.0.0",
|
||||
"compact-encoding": "^2.16.0",
|
||||
"hypercore-crypto": "^3.0.0",
|
||||
"protomux": "^3.0.0"
|
||||
},
|
||||
@@ -955,6 +954,8 @@
|
||||
"resolved": "https://registry.npmjs.org/bare-timers/-/bare-timers-2.2.0.tgz",
|
||||
"integrity": "sha512-Ft68HFT4d8oTLOGqHKBoLAA3h32BRyQLa7qzX1Z31QAPSrfBodfMIwBSo52xUtnPc/UIIP93kInezZ5cgWrZKg==",
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"tiny-binary-heap": "^1.1.0"
|
||||
}
|
||||
@@ -1704,7 +1705,9 @@
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/tiny-binary-heap/-/tiny-binary-heap-1.1.0.tgz",
|
||||
"integrity": "sha512-IaM+/bh71URhhzlH1qNKNylamK4nxaby2q0qxolWL4Typ3+sIcZHxLe/Mj35b7GOHat/RKpp2vbRepjV6sD+Rw==",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/tmatch": {
|
||||
"version": "5.0.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "hyper-p2p-retained-messages",
|
||||
"version": "0.0.0-scaffold",
|
||||
"version": "0.3.1",
|
||||
"description": "Retained message store.",
|
||||
"main": "index.js",
|
||||
"type": "commonjs",
|
||||
@@ -9,17 +9,18 @@
|
||||
"dependencies": {
|
||||
"bare-events": "^2.8.0",
|
||||
"bare-process": "^4.4.0",
|
||||
"bare-timers": "^2.0.0",
|
||||
"b4a": "^1.6.7",
|
||||
"hypercore-crypto": "^3.0.0",
|
||||
"protomux": "^3.0.0",
|
||||
"compact-encoding": "^2.0.0"
|
||||
"compact-encoding": "^2.16.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"hyperswarm": "^4.0.0",
|
||||
"bare": ">=1.0.0"
|
||||
},
|
||||
"peerDependencies": { "hyperswarm": "^4.0.0", "bare": ">=1.0.0" },
|
||||
"devDependencies": { "brittle": "^3.0.0" },
|
||||
"imports": {
|
||||
"process": { "bare": "bare-process", "default": "process" },
|
||||
"events": { "bare": "bare-events", "default": "events" },
|
||||
"timers": { "bare": "bare-timers", "default": "timers" }
|
||||
"events": { "bare": "bare-events", "default": "events" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,26 @@
|
||||
require('bare-process/global')
|
||||
const test = require('brittle')
|
||||
const { HyperP2PRetainedMessages, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports load', (t) => {
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PRetainedMessages)
|
||||
t.is(PROTOCOL, 'retained-messages/v1')
|
||||
t.ok(PROTOCOL)
|
||||
})
|
||||
|
||||
test('getStats shape', async (t) => {
|
||||
test('basic operation', async (t) => {
|
||||
const m = new HyperP2PRetainedMessages()
|
||||
const s = m.getStats()
|
||||
t.is(s.tier, 'scaffold')
|
||||
t.is(s.protocol, 'retained-messages/v1')
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PRetainedMessages()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PRetainedMessages()
|
||||
t.ok(m.getStats().protocol)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"name": "hyper-p2p-subscription-lease",
|
||||
"category": "messaging-pubsub",
|
||||
"tier": "scaffold",
|
||||
"protocol": "subscription-lease/v1",
|
||||
"class": "HyperP2PSubscriptionLease",
|
||||
"wave": 8,
|
||||
"holepunch_refs": [
|
||||
"protomux"
|
||||
],
|
||||
"composes_with": [
|
||||
"hyper-p2p-topic-lease"
|
||||
],
|
||||
"status": "scaffold"
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
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 = 'subscription-lease/v1'
|
||||
|
||||
@@ -7,18 +9,51 @@ class HyperP2PSubscriptionLease extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this._stats = { created: 0, errors: 0 }
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
}
|
||||
|
||||
getStats () {
|
||||
return { ...this._stats, protocol: PROTOCOL, tier: 'scaffold' }
|
||||
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 })
|
||||
return true
|
||||
}
|
||||
|
||||
async ready () { return true }
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
_notImplemented (method) {
|
||||
this._stats.errors++
|
||||
throw new Error(`not implemented: scaffold (${method})`)
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'subscription-lease-sync', key, value: null })
|
||||
return ok
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
|
||||
_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)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
{
|
||||
"name": "hyper-p2p-subscription-lease",
|
||||
"version": "0.0.0-scaffold",
|
||||
"version": "0.3.1",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "hyper-p2p-subscription-lease",
|
||||
"version": "0.0.0-scaffold",
|
||||
"version": "0.3.1",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"b4a": "^1.6.7",
|
||||
"bare-events": "^2.8.0",
|
||||
"bare-process": "^4.4.0",
|
||||
"bare-timers": "^2.0.0",
|
||||
"compact-encoding": "^2.0.0",
|
||||
"compact-encoding": "^2.16.0",
|
||||
"hypercore-crypto": "^3.0.0",
|
||||
"protomux": "^3.0.0"
|
||||
},
|
||||
@@ -955,6 +954,8 @@
|
||||
"resolved": "https://registry.npmjs.org/bare-timers/-/bare-timers-2.2.0.tgz",
|
||||
"integrity": "sha512-Ft68HFT4d8oTLOGqHKBoLAA3h32BRyQLa7qzX1Z31QAPSrfBodfMIwBSo52xUtnPc/UIIP93kInezZ5cgWrZKg==",
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"tiny-binary-heap": "^1.1.0"
|
||||
}
|
||||
@@ -1704,7 +1705,9 @@
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/tiny-binary-heap/-/tiny-binary-heap-1.1.0.tgz",
|
||||
"integrity": "sha512-IaM+/bh71URhhzlH1qNKNylamK4nxaby2q0qxolWL4Typ3+sIcZHxLe/Mj35b7GOHat/RKpp2vbRepjV6sD+Rw==",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/tmatch": {
|
||||
"version": "5.0.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "hyper-p2p-subscription-lease",
|
||||
"version": "0.0.0-scaffold",
|
||||
"version": "0.3.1",
|
||||
"description": "Subscription lease gossip.",
|
||||
"main": "index.js",
|
||||
"type": "commonjs",
|
||||
@@ -9,17 +9,18 @@
|
||||
"dependencies": {
|
||||
"bare-events": "^2.8.0",
|
||||
"bare-process": "^4.4.0",
|
||||
"bare-timers": "^2.0.0",
|
||||
"b4a": "^1.6.7",
|
||||
"hypercore-crypto": "^3.0.0",
|
||||
"protomux": "^3.0.0",
|
||||
"compact-encoding": "^2.0.0"
|
||||
"compact-encoding": "^2.16.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"hyperswarm": "^4.0.0",
|
||||
"bare": ">=1.0.0"
|
||||
},
|
||||
"peerDependencies": { "hyperswarm": "^4.0.0", "bare": ">=1.0.0" },
|
||||
"devDependencies": { "brittle": "^3.0.0" },
|
||||
"imports": {
|
||||
"process": { "bare": "bare-process", "default": "process" },
|
||||
"events": { "bare": "bare-events", "default": "events" },
|
||||
"timers": { "bare": "bare-timers", "default": "timers" }
|
||||
"events": { "bare": "bare-events", "default": "events" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,26 @@
|
||||
require('bare-process/global')
|
||||
const test = require('brittle')
|
||||
const { HyperP2PSubscriptionLease, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports load', (t) => {
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PSubscriptionLease)
|
||||
t.is(PROTOCOL, 'subscription-lease/v1')
|
||||
t.ok(PROTOCOL)
|
||||
})
|
||||
|
||||
test('getStats shape', async (t) => {
|
||||
test('basic operation', async (t) => {
|
||||
const m = new HyperP2PSubscriptionLease()
|
||||
const s = m.getStats()
|
||||
t.is(s.tier, 'scaffold')
|
||||
t.is(s.protocol, 'subscription-lease/v1')
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PSubscriptionLease()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PSubscriptionLease()
|
||||
t.ok(m.getStats().protocol)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"name": "hyper-p2p-topic-channel",
|
||||
"category": "messaging-pubsub",
|
||||
"tier": "scaffold",
|
||||
"protocol": "topic-channel/v1",
|
||||
"class": "HyperP2PTopicChannel",
|
||||
"wave": 8,
|
||||
"holepunch_refs": [
|
||||
"protomux"
|
||||
],
|
||||
"composes_with": [
|
||||
"hyper-p2p-gossip-mesh"
|
||||
],
|
||||
"status": "scaffold"
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
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 = 'topic-channel/v1'
|
||||
|
||||
@@ -7,18 +9,51 @@ class HyperP2PTopicChannel extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this._stats = { created: 0, errors: 0 }
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
}
|
||||
|
||||
getStats () {
|
||||
return { ...this._stats, protocol: PROTOCOL, tier: 'scaffold' }
|
||||
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 })
|
||||
return true
|
||||
}
|
||||
|
||||
async ready () { return true }
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
_notImplemented (method) {
|
||||
this._stats.errors++
|
||||
throw new Error(`not implemented: scaffold (${method})`)
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'topic-channel-sync', key, value: null })
|
||||
return ok
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || d.type !== 'topic-channel-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, 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-5
@@ -1,19 +1,18 @@
|
||||
{
|
||||
"name": "hyper-p2p-topic-channel",
|
||||
"version": "0.0.0-scaffold",
|
||||
"version": "0.3.1",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "hyper-p2p-topic-channel",
|
||||
"version": "0.0.0-scaffold",
|
||||
"version": "0.3.1",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"b4a": "^1.6.7",
|
||||
"bare-events": "^2.8.0",
|
||||
"bare-process": "^4.4.0",
|
||||
"bare-timers": "^2.0.0",
|
||||
"compact-encoding": "^2.0.0",
|
||||
"compact-encoding": "^2.16.0",
|
||||
"hypercore-crypto": "^3.0.0",
|
||||
"protomux": "^3.0.0"
|
||||
},
|
||||
@@ -955,6 +954,8 @@
|
||||
"resolved": "https://registry.npmjs.org/bare-timers/-/bare-timers-2.2.0.tgz",
|
||||
"integrity": "sha512-Ft68HFT4d8oTLOGqHKBoLAA3h32BRyQLa7qzX1Z31QAPSrfBodfMIwBSo52xUtnPc/UIIP93kInezZ5cgWrZKg==",
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"tiny-binary-heap": "^1.1.0"
|
||||
}
|
||||
@@ -1704,7 +1705,9 @@
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/tiny-binary-heap/-/tiny-binary-heap-1.1.0.tgz",
|
||||
"integrity": "sha512-IaM+/bh71URhhzlH1qNKNylamK4nxaby2q0qxolWL4Typ3+sIcZHxLe/Mj35b7GOHat/RKpp2vbRepjV6sD+Rw==",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/tmatch": {
|
||||
"version": "5.0.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "hyper-p2p-topic-channel",
|
||||
"version": "0.0.0-scaffold",
|
||||
"version": "0.3.1",
|
||||
"description": "Named topic channels.",
|
||||
"main": "index.js",
|
||||
"type": "commonjs",
|
||||
@@ -9,17 +9,18 @@
|
||||
"dependencies": {
|
||||
"bare-events": "^2.8.0",
|
||||
"bare-process": "^4.4.0",
|
||||
"bare-timers": "^2.0.0",
|
||||
"b4a": "^1.6.7",
|
||||
"hypercore-crypto": "^3.0.0",
|
||||
"protomux": "^3.0.0",
|
||||
"compact-encoding": "^2.0.0"
|
||||
"compact-encoding": "^2.16.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"hyperswarm": "^4.0.0",
|
||||
"bare": ">=1.0.0"
|
||||
},
|
||||
"peerDependencies": { "hyperswarm": "^4.0.0", "bare": ">=1.0.0" },
|
||||
"devDependencies": { "brittle": "^3.0.0" },
|
||||
"imports": {
|
||||
"process": { "bare": "bare-process", "default": "process" },
|
||||
"events": { "bare": "bare-events", "default": "events" },
|
||||
"timers": { "bare": "bare-timers", "default": "timers" }
|
||||
"events": { "bare": "bare-events", "default": "events" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,26 @@
|
||||
require('bare-process/global')
|
||||
const test = require('brittle')
|
||||
const { HyperP2PTopicChannel, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports load', (t) => {
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PTopicChannel)
|
||||
t.is(PROTOCOL, 'topic-channel/v1')
|
||||
t.ok(PROTOCOL)
|
||||
})
|
||||
|
||||
test('getStats shape', async (t) => {
|
||||
test('basic operation', async (t) => {
|
||||
const m = new HyperP2PTopicChannel()
|
||||
const s = m.getStats()
|
||||
t.is(s.tier, 'scaffold')
|
||||
t.is(s.protocol, 'topic-channel/v1')
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PTopicChannel()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PTopicChannel()
|
||||
t.ok(m.getStats().protocol)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user