mirror of https://codeberg.org/pzp/pzp-net.git
remove event.parsedAddress
This commit is contained in:
parent
b0e9039c21
commit
7f5ef18bd7
|
@ -18,7 +18,6 @@ const IP = require('ip')
|
||||||
* | 'disconnecting'
|
* | 'disconnecting'
|
||||||
* | 'disconnected';
|
* | 'disconnected';
|
||||||
* address: Address;
|
* address: Address;
|
||||||
* parsedAddress: any;
|
|
||||||
* details?: any;
|
* details?: any;
|
||||||
* }} ConnectionEvent
|
* }} ConnectionEvent
|
||||||
* @typedef {{
|
* @typedef {{
|
||||||
|
@ -62,15 +61,16 @@ class Connections {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Array<any>} parsedAddress
|
* @param {Address} address
|
||||||
* @returns {Info['inferredType']}
|
* @returns {Info['inferredType']}
|
||||||
*/
|
*/
|
||||||
static inferPeerType(parsedAddress) {
|
static inferPeerType(address) {
|
||||||
for (const subParsed of parsedAddress) {
|
if (address.startsWith('tunnel:')) return 'tunnel'
|
||||||
const [transport, transform] = subParsed
|
if (address.startsWith('net:')) {
|
||||||
if (transport.name === 'tunnel') return 'tunnel'
|
const netAddr = address.split('~')[0]
|
||||||
if (transport.name === 'net') {
|
const [, host, port] = netAddr.split(':')
|
||||||
if (IP.isPrivate(transport.host)) return 'lan'
|
if (host) {
|
||||||
|
if (IP.isPrivate(host)) return 'lan'
|
||||||
else return 'internet'
|
else return 'internet'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,50 +102,32 @@ class Connections {
|
||||||
const initiator = weAreClient ? 'we' : 'they'
|
const initiator = weAreClient ? 'we' : 'they'
|
||||||
debug('Connected to %s, %s initiated it', address, initiator)
|
debug('Connected to %s, %s initiated it', address, initiator)
|
||||||
|
|
||||||
const parsedAddress = this.#peer.multiserver.parse(address)
|
|
||||||
this.#rpcs.set(address, rpc)
|
this.#rpcs.set(address, rpc)
|
||||||
rpc.once('closed', () => {
|
rpc.once('closed', () => {
|
||||||
debug('Disconnected from %s', address)
|
debug('Disconnected from %s', address)
|
||||||
this.#rpcs.delete(address)
|
this.#rpcs.delete(address)
|
||||||
this.#infos.update(address, { state: 'disconnected' })
|
this.#infos.update(address, { state: 'disconnected' })
|
||||||
this.#notifyEvent({ type: 'disconnected', address, parsedAddress })
|
this.#notifyEvent({ type: 'disconnected', address })
|
||||||
this.#infos.emit()
|
this.#infos.emit()
|
||||||
})
|
})
|
||||||
|
|
||||||
const state = /**@type {Info['state']}*/ ('connected')
|
const state = /**@type {Info['state']}*/ ('connected')
|
||||||
const inferredType = Connections.inferPeerType(parsedAddress)
|
const inferredType = Connections.inferPeerType(address)
|
||||||
this.#infos.update(address, { state, inferredType })
|
this.#infos.update(address, { state, inferredType })
|
||||||
this.#notifyEvent({
|
this.#notifyEvent({
|
||||||
type: state,
|
type: state,
|
||||||
address,
|
address,
|
||||||
parsedAddress,
|
|
||||||
details: { rpc, weAreClient },
|
details: { rpc, weAreClient },
|
||||||
})
|
})
|
||||||
this.#infos.emit()
|
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
|
* @param {string} address
|
||||||
* @returns {Promise<RPC>}
|
* @returns {Promise<RPC>}
|
||||||
*/
|
*/
|
||||||
async connect(address) {
|
async connect(address) {
|
||||||
this.#assertNotClosed()
|
this.#assertNotClosed()
|
||||||
await this.#waitForSecretStackToInitMultiserver()
|
|
||||||
|
|
||||||
const prevInfo = this.#infos.get(address)
|
const prevInfo = this.#infos.get(address)
|
||||||
switch (prevInfo?.state ?? 'disconnected') {
|
switch (prevInfo?.state ?? 'disconnected') {
|
||||||
|
@ -184,9 +166,8 @@ class Connections {
|
||||||
case 'disconnected': {
|
case 'disconnected': {
|
||||||
debug('Connecting to %s', address)
|
debug('Connecting to %s', address)
|
||||||
const state = /**@type {Info['state']}*/ ('connecting')
|
const state = /**@type {Info['state']}*/ ('connecting')
|
||||||
const parsedAddress = this.#peer.multiserver.parse(address)
|
|
||||||
this.#infos.update(address, { state })
|
this.#infos.update(address, { state })
|
||||||
this.#notifyEvent({ type: state, address, parsedAddress })
|
this.#notifyEvent({ type: state, address })
|
||||||
this.#infos.emit()
|
this.#infos.emit()
|
||||||
|
|
||||||
const [err, rpc] = await run(this.#peer.connect)(address)
|
const [err, rpc] = await run(this.#peer.connect)(address)
|
||||||
|
@ -196,7 +177,6 @@ class Connections {
|
||||||
this.#notifyEvent({
|
this.#notifyEvent({
|
||||||
type: 'connecting-failed',
|
type: 'connecting-failed',
|
||||||
address,
|
address,
|
||||||
parsedAddress,
|
|
||||||
details: err,
|
details: err,
|
||||||
})
|
})
|
||||||
this.#infos.emit()
|
this.#infos.emit()
|
||||||
|
@ -232,7 +212,6 @@ class Connections {
|
||||||
*/
|
*/
|
||||||
async disconnect(address) {
|
async disconnect(address) {
|
||||||
this.#assertNotClosed()
|
this.#assertNotClosed()
|
||||||
await this.#waitForSecretStackToInitMultiserver()
|
|
||||||
const prevInfo = this.#infos.get(address)
|
const prevInfo = this.#infos.get(address)
|
||||||
if (!prevInfo || prevInfo?.state === 'disconnected') return false
|
if (!prevInfo || prevInfo?.state === 'disconnected') return false
|
||||||
if (prevInfo.state === 'disconnecting') return false
|
if (prevInfo.state === 'disconnecting') return false
|
||||||
|
@ -265,9 +244,8 @@ class Connections {
|
||||||
|
|
||||||
debug('Disconnecting from %s', address)
|
debug('Disconnecting from %s', address)
|
||||||
const state = /**@type {Info['state']}*/ ('disconnecting')
|
const state = /**@type {Info['state']}*/ ('disconnecting')
|
||||||
const parsedAddress = this.#peer.multiserver.parse(address)
|
|
||||||
this.#infos.update(address, { state })
|
this.#infos.update(address, { state })
|
||||||
this.#notifyEvent({ type: state, address, parsedAddress })
|
this.#notifyEvent({ type: state, address })
|
||||||
this.#infos.emit()
|
this.#infos.emit()
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
await run(rpc.close)(true)
|
await run(rpc.close)(true)
|
||||||
|
|
|
@ -69,14 +69,6 @@ test('net', async (t) => {
|
||||||
if (i === 1) {
|
if (i === 1) {
|
||||||
assert.equal(ev.type, 'connecting', 'event.type ok')
|
assert.equal(ev.type, 'connecting', 'event.type ok')
|
||||||
assert.equal(ev.address, TEST_ADDR, 'event.address 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) {
|
} else if (i === 2) {
|
||||||
assert.equal(ev.type, 'connecting-failed', 'event.type ok')
|
assert.equal(ev.type, 'connecting-failed', 'event.type ok')
|
||||||
assert.equal(ev.address, TEST_ADDR, 'event.address ok')
|
assert.equal(ev.address, TEST_ADDR, 'event.address ok')
|
||||||
|
|
Loading…
Reference in New Issue