const crypto = require('hypercore-crypto') const HyperDHT = require('hyperdht') const ReadyResource = require('ready-resource') const b4a = require('b4a') const c = require('compact-encoding') const { NS_ANNOUNCE, PROTOCOL, AnnouncePayload, Announce, Subscribe, Unsubscribe, Bump, getMuxer, unsupported, noop } = require('./lib') class HyperTrackerClient extends ReadyResource { constructor( remotePublicKey, { bootstrap, dht = new HyperDHT({ bootstrap }), suspended = false, shouldReconnect = false } = {} ) { super() this.remotePublicKey = remotePublicKey this.dht = dht this.connection = null this.muxer = null this.channel = null this.suspended = suspended this.connecting = false this._retryTimeout = null this._retryResolve = null this._shouldReconnect = shouldReconnect this._interrupted = false this._pendingAnnounces = new Map() } async suspend() { this.suspended = true this._interrupted = true this._clearRetry() if (!this.connection) return this.connection.destroy() if (this.connection.destroyed) return await new Promise((resolve) => this.connection.on('close', resolve)) } resume() { this.suspended = false if (this._shouldReconnect) this._reconnect() } connect() { this._reconnect() } async _reconnect() { if (this.connecting) return this.channel if (this.channel && this.channel.opened && !this.connection.destroying) return this.channel this.connecting = true this._shouldReconnect = true let strikes = 0 while (!this.closing && !this.suspended) { this._interrupted = false const connection = (this.connection = this.dht.connect(this.remotePublicKey)) this.muxer = getMuxer(this.connection) this.channel = this.muxer.createChannel({ userData: this, protocol: PROTOCOL, messages: [ { encoding: Subscribe, onmessage: unsupported }, { encoding: Unsubscribe, onmessage: unsupported }, { encoding: Announce, onmessage: unsupported }, { encoding: Bump, onmessage: onbump } ] }) if (!this.channel) break this.channel.open() const opened = await this.channel.fullyOpened() if (this.closing || this.suspended) { connection.on('error', noop) connection.destroy() break } if (opened && !connection.destroyed && !connection.destroying) { connection.on('close', this._reconnect.bind(this)) if (this._pendingAnnounces.size) { this.channel.cork() for (const m of this._pendingAnnounces.values()) this.channel.messages[2].send(m) this.channel.uncork() this._pendingAnnounces.clear() } break } if (this._interrupted) continue connection.on('error', noop) connection.destroy() strikes++ await new Promise((resolve) => { this._retryResolve = resolve this._retryTimeout = setTimeout(resolve, strikes < 3 ? 5_000 : 15_000) }) this._retryResolve = null this._retryTimeout = null } this.connecting = false if (this.closing || this.suspended) return this.channel this.emit('connect') return this.channel } _close() { if (this.connection) this.connection.destroy() this._clearRetry() } _clearRetry() { if (!this._retryTimeout) return clearTimeout(this._retryTimeout) this._retryResolve() this._retryResolve = null this._retryTimeout = null } _onbump(bump) { this.emit('announce', bump) } _getChannel() { this._reconnect() if (this.suspended || this.closing) return null return this.channel } cork() { const channel = this._getChannel() if (channel) channel.cork() } uncork() { const channel = this._getChannel() if (channel) channel.uncork() } subscribe(publicKey, { since = 0 } = {}) { const channel = this._getChannel() if (!channel) return channel.messages[0].send({ publicKey, since }) } unsubscribe(publicKey) { const channel = this._getChannel() if (!channel) return channel.messages[1].send({ publicKey }) } announce(keyPair, { bump = Date.now() } = {}) { const channel = this._getChannel() if (!channel) return const ann = { bump } const state = { buffer: null, start: 0, end: 0 } c.fixed32.preencode(state, NS_ANNOUNCE) AnnouncePayload.preencode(state, ann) state.buffer = b4a.allocUnsafe(state.end) c.fixed32.encode(state, NS_ANNOUNCE) AnnouncePayload.encode(state, ann) const signature = crypto.sign(state.buffer, keyPair.secretKey) const m = { publicKey: keyPair.publicKey, announce: ann, signature } if (!this.connecting && !this.connection.destroying) { channel.messages[2].send(m) return } c.encode(Announce, m) m.publicKey = b4a.from(m.publicKey) const id = b4a.toString(m.publicKey, 'hex') const pending = this._pendingAnnounces.get(id) if (!pending || pending.announce.bump < bump) this._pendingAnnounces.set(id, m) } } module.exports = HyperTrackerClient function onbump(bump, channel) { channel.userData._onbump(bump) }