diff --git a/lib/connections.js b/lib/connections.js index b2ab823..59b477f 100644 --- a/lib/connections.js +++ b/lib/connections.js @@ -18,7 +18,6 @@ const IP = require('ip') * | 'disconnecting' * | 'disconnected'; * address: Address; - * parsedAddress: any; * details?: any; * }} ConnectionEvent * @typedef {{ @@ -62,15 +61,16 @@ class Connections { } /** - * @param {Array} parsedAddress + * @param {Address} address * @returns {Info['inferredType']} */ - static inferPeerType(parsedAddress) { - for (const subParsed of parsedAddress) { - const [transport, transform] = subParsed - if (transport.name === 'tunnel') return 'tunnel' - if (transport.name === 'net') { - if (IP.isPrivate(transport.host)) return 'lan' + static inferPeerType(address) { + if (address.startsWith('tunnel:')) return 'tunnel' + if (address.startsWith('net:')) { + const netAddr = address.split('~')[0] + const [, host, port] = netAddr.split(':') + if (host) { + if (IP.isPrivate(host)) return 'lan' else return 'internet' } } @@ -102,50 +102,32 @@ class Connections { const initiator = weAreClient ? 'we' : 'they' debug('Connected to %s, %s initiated it', address, initiator) - const parsedAddress = this.#peer.multiserver.parse(address) this.#rpcs.set(address, rpc) rpc.once('closed', () => { debug('Disconnected from %s', address) this.#rpcs.delete(address) this.#infos.update(address, { state: 'disconnected' }) - this.#notifyEvent({ type: 'disconnected', address, parsedAddress }) + this.#notifyEvent({ type: 'disconnected', address }) this.#infos.emit() }) const state = /**@type {Info['state']}*/ ('connected') - const inferredType = Connections.inferPeerType(parsedAddress) + const inferredType = Connections.inferPeerType(address) this.#infos.update(address, { state, inferredType }) this.#notifyEvent({ type: state, address, - parsedAddress, details: { rpc, weAreClient }, }) this.#infos.emit() } - /** - * TODO: We need to fix secret-stack so that it has a ".ready()" async method, - * after it has initialized multiserver. Then we can remove this hack. - */ - async #waitForSecretStackToInitMultiserver() { - for( let i = 0; i < 10; i++ ) { - try { - this.#peer.multiserver.parse('') - return - } catch (err) { - await new Promise((resolve) => setTimeout(resolve, 16)) - } - } - } - /** * @param {string} address * @returns {Promise} */ async connect(address) { this.#assertNotClosed() - await this.#waitForSecretStackToInitMultiserver() const prevInfo = this.#infos.get(address) switch (prevInfo?.state ?? 'disconnected') { @@ -184,9 +166,8 @@ class Connections { case 'disconnected': { debug('Connecting to %s', address) const state = /**@type {Info['state']}*/ ('connecting') - const parsedAddress = this.#peer.multiserver.parse(address) this.#infos.update(address, { state }) - this.#notifyEvent({ type: state, address, parsedAddress }) + this.#notifyEvent({ type: state, address }) this.#infos.emit() const [err, rpc] = await run(this.#peer.connect)(address) @@ -196,7 +177,6 @@ class Connections { this.#notifyEvent({ type: 'connecting-failed', address, - parsedAddress, details: err, }) this.#infos.emit() @@ -232,7 +212,6 @@ class Connections { */ async disconnect(address) { this.#assertNotClosed() - await this.#waitForSecretStackToInitMultiserver() const prevInfo = this.#infos.get(address) if (!prevInfo || prevInfo?.state === 'disconnected') return false if (prevInfo.state === 'disconnecting') return false @@ -265,9 +244,8 @@ class Connections { debug('Disconnecting from %s', address) const state = /**@type {Info['state']}*/ ('disconnecting') - const parsedAddress = this.#peer.multiserver.parse(address) this.#infos.update(address, { state }) - this.#notifyEvent({ type: state, address, parsedAddress }) + this.#notifyEvent({ type: state, address }) this.#infos.emit() // @ts-ignore await run(rpc.close)(true) diff --git a/test/index.test.js b/test/index.test.js index 6034706..fca8aff 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -69,14 +69,6 @@ test('net', async (t) => { if (i === 1) { assert.equal(ev.type, 'connecting', 'event.type ok') assert.equal(ev.address, TEST_ADDR, 'event.address ok') - assert.equal(ev.parsedAddress.length, 1) - assert.equal(ev.parsedAddress[0].length, 2) - assert.deepEqual(ev.parsedAddress[0][0], { - name: 'net', - host: 'localhost', - port: 9752, - }) - assert.equal(ev.parsedAddress[0][1].name, 'shse') } else if (i === 2) { assert.equal(ev.type, 'connecting-failed', 'event.type ok') assert.equal(ev.address, TEST_ADDR, 'event.address ok')