diff --git a/lib/connect.js b/lib/connect.js index 4d7f07fa..6711bd95 100644 --- a/lib/connect.js +++ b/lib/connect.js @@ -1,3 +1,5 @@ +/** @import HyperDHT from '../index' */ +/** @import UDXStream from 'udx-native/lib/stream' */ const NoiseSecretStream = require('@hyperswarm/secret-stream') const b4a = require('b4a') const relay = require('blind-relay') @@ -30,6 +32,12 @@ const { SUSPENDED } = require('./errors') +/** + * @param {HyperDHT} dht + * @param {*} publicKey + * @param {*} opts + * @returns + */ module.exports = function connect (dht, publicKey, opts = {}) { dht.tracer.trace('connect', { publicKey, options: opts }) @@ -414,10 +422,24 @@ async function connectThroughNode (c, address, socket) { c.payload = new SecurePayload(hs.holepunchSecret) c.onsocket = function (socket, port, host) { - if (c.rawStream === null) return // Already hole punched - - if (c.rawStream.connected) { - const remoteChanging = c.rawStream.changeRemote(socket, c.connect.payload.udx.id, port, host) + /** @type {UDXStream} */ + const rawStream = c.rawStream + if (rawStream === null) return // Already hole punched + + if (rawStream.connected) { + rawStream.once('remote-changed', () => { + // TODO: ask @mafintosh if this is correct + c.encryptedSocket.relay = { + relaying: false, + relayPaired: false, + relayThrough: null, + remotePublicKey: null + } + // Note - this is really an "un-relay" event but subscribers should know + // to look at the .relay.relaying property + this.encryptedSocket.emit('relay', c.encryptedSocket.relay) + }) + const remoteChanging = rawStream.changeRemote(socket, c.connect.payload.udx.id, port, host) if (remoteChanging) remoteChanging.catch(safetyCatch) } else { diff --git a/lib/raw-stream-set.js b/lib/raw-stream-set.js index 9ad04923..8e8f92c4 100644 --- a/lib/raw-stream-set.js +++ b/lib/raw-stream-set.js @@ -1,5 +1,9 @@ +/** @import HyperDHT from '../index' */ + module.exports = class RawStreamSet { + /** @param {HyperDHT} dht */ constructor (dht) { + /** @type {HyperDHT} */ this._dht = dht this._prefix = 16 - 1 // 16 is the default stream-set side in udx diff --git a/lib/server.js b/lib/server.js index bade990b..75085ef0 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,3 +1,4 @@ +/** @import HyperDHT from '../index' */ const { EventEmitter } = require('events') const safetyCatch = require('safety-catch') const NoiseSecretStream = require('@hyperswarm/secret-stream') @@ -21,7 +22,7 @@ const HANDSHAKE_INITIAL_TIMEOUT = 10000 module.exports = class Server extends EventEmitter { constructor (dht, opts = {}) { super() - + /** @type {HyperDHT} */ this.dht = dht this.target = null @@ -305,6 +306,18 @@ module.exports = class Server extends EventEmitter { hs.rawStream.removeListener('error', autoDestroy) if (hs.rawStream.connected) { + hs.rawStream.once('remote-changed', () => { + // TODO: ask @mafintosh if this is correct + hs.encryptedSocket.relay = { + relaying: false, + relayPaired: false, + relayThrough: null, + remotePublicKey: null + } + // Note - this is really an "un-relay" event but subscribers should know + // to look at the .relay.relaying property + hs.encryptedSocket.emit('relay', hs.encryptedSocket.relay) + }) const remoteChanging = hs.rawStream.changeRemote(socket, remotePayload.udx.id, port, host) if (remoteChanging) remoteChanging.catch(safetyCatch)