diff --git a/index.js b/index.js index b79a110..0178928 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const { EventEmitter } = require('events') const DHT = require('hyperdht') const spq = require('shuffled-priority-queue') const b4a = require('b4a') +const unslab = require('unslab') const PeerInfo = require('./lib/peer-info') const RetryTimer = require('./lib/retry-timer') @@ -423,6 +424,8 @@ module.exports = class Hyperswarm extends EventEmitter { // TODO: When you rejoin, it should reannounce + bump lookup priority join (topic, opts = {}) { if (!topic) throw new Error(ERR_MISSING_TOPIC) + topic = unslab(topic) + const topicString = b4a.toString(topic, 'hex') let discovery = this._discovery.get(topicString) diff --git a/lib/peer-info.js b/lib/peer-info.js index 8ac79e5..1ad1574 100644 --- a/lib/peer-info.js +++ b/lib/peer-info.js @@ -1,5 +1,6 @@ const { EventEmitter } = require('events') const b4a = require('b4a') +const unslab = require('unslab') const MIN_CONNECTION_TIME = 15000 @@ -13,7 +14,7 @@ module.exports = class PeerInfo extends EventEmitter { constructor ({ publicKey, relayAddresses }) { super() - this.publicKey = publicKey + this.publicKey = unslab(publicKey) this.relayAddresses = relayAddresses this.reconnecting = true @@ -28,7 +29,7 @@ module.exports = class PeerInfo extends EventEmitter { // Set by the Swarm this.queued = false this.client = false - this.topics = [] + this.topics = [] // TODO: remove on next major (check with mafintosh for context) this.attempts = 0 this.priority = NORMAL_PRIORITY diff --git a/package.json b/package.json index 6469a5c..b4fbaef 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "bare-events": "^2.2.0", "hyperdht": "^6.11.0", "safety-catch": "^1.0.2", - "shuffled-priority-queue": "^2.1.0" + "shuffled-priority-queue": "^2.1.0", + "unslab": "^1.3.0" }, "devDependencies": { "brittle": "^3.0.2", diff --git a/test/swarm.js b/test/swarm.js index 624a501..f148092 100644 --- a/test/swarm.js +++ b/test/swarm.js @@ -747,6 +747,49 @@ test('peerDiscovery has unslabbed closestNodes', async (t) => { t.is(hasUnslabbeds, false, 'sanity check: all are unslabbed') }) +test('topic and peer get unslabbed in PeerInfo', async (t) => { + const { bootstrap } = await createTestnet(3, t.teardown) + + const swarm1 = new Hyperswarm({ bootstrap }) + const swarm2 = new Hyperswarm({ bootstrap }) + + t.plan(3) + + t.teardown(async () => { + await swarm1.destroy() + await swarm2.destroy() + }) + + swarm2.on('connection', (conn) => { + t.is( + [...swarm2.peers.values()][0].publicKey.buffer.byteLength, + 32, + 'unslabbed publicKey in peerInfo' + ) + t.is([...swarm2.peers.values()][0].topics[0].buffer.byteLength, + 32, + 'unslabbed topic in peerInfo' + ) + + conn.on('error', noop) + conn.end() + }) + swarm1.on('connection', (conn) => { + t.is( + [...swarm1.peers.values()][0].publicKey.buffer.byteLength, + 32, + 'unslabbed publicKey in peerInfo' + ) + + conn.on('error', noop) + conn.end() + }) + + const topic = Buffer.alloc(32).fill('hello world') + await swarm1.join(topic, { server: true, client: false }).flushed() + swarm2.join(topic, { client: true, server: false }) +}) + function noop () {} function eventFlush () {