general refactor
This commit is contained in:
parent
d2dd75de0f
commit
d5b72c3abc
|
@ -19,7 +19,7 @@ module.exports = class HubObserver {
|
||||||
* @readonly
|
* @readonly
|
||||||
* @type {any}
|
* @type {any}
|
||||||
*/
|
*/
|
||||||
#local
|
#peer
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {string}
|
* @type {string}
|
||||||
|
@ -47,15 +47,15 @@ module.exports = class HubObserver {
|
||||||
#attendantsDrain
|
#attendantsDrain
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {any} local
|
* @param {any} peer
|
||||||
* @param {string} hubPubkey
|
* @param {string} hubPubkey
|
||||||
* @param {string} address
|
* @param {string} address
|
||||||
* @param {{name: string, admin: string}} hubMetadata
|
* @param {{name: string, admin: string}} hubMetadata
|
||||||
* @param {any} rpc
|
* @param {any} rpc
|
||||||
* @param {any} onConnect
|
* @param {any} onConnect
|
||||||
*/
|
*/
|
||||||
constructor(local, hubPubkey, address, hubMetadata, rpc, onConnect) {
|
constructor(peer, hubPubkey, address, hubMetadata, rpc, onConnect) {
|
||||||
this.#local = local
|
this.#peer = peer
|
||||||
this.#hubPubkey = hubPubkey
|
this.#hubPubkey = hubPubkey
|
||||||
this.#address = address
|
this.#address = address
|
||||||
this.#hubMetadata = hubMetadata
|
this.#hubMetadata = hubMetadata
|
||||||
|
@ -85,8 +85,8 @@ module.exports = class HubObserver {
|
||||||
const { name, admin } = this.#hubMetadata
|
const { name, admin } = this.#hubMetadata
|
||||||
if (name) metadata.name = name
|
if (name) metadata.name = name
|
||||||
if (admin) metadata.admin = admin
|
if (admin) metadata.admin = admin
|
||||||
this.#local.conn.db().update(this.#address, metadata)
|
this.#peer.conn.db().update(this.#address, metadata)
|
||||||
this.#local.conn.hub().update(this.#address, metadata)
|
this.#peer.conn.hub().update(this.#address, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
debug('Announcing myself to hub %s', this.#hubPubkey)
|
debug('Announcing myself to hub %s', this.#hubPubkey)
|
||||||
|
@ -126,7 +126,7 @@ module.exports = class HubObserver {
|
||||||
|
|
||||||
// Update onlineCount metadata for this hub
|
// Update onlineCount metadata for this hub
|
||||||
const onlineCount = this.#attendants.size
|
const onlineCount = this.#attendants.size
|
||||||
this.#local.conn.hub().update(this.#address, { onlineCount })
|
this.#peer.conn.hub().update(this.#address, { onlineCount })
|
||||||
|
|
||||||
// Update ssb-conn-staging
|
// Update ssb-conn-staging
|
||||||
const hubName = this.#hubMetadata?.name
|
const hubName = this.#hubMetadata?.name
|
||||||
|
@ -139,8 +139,8 @@ module.exports = class HubObserver {
|
||||||
} else if (event.type === 'left') {
|
} else if (event.type === 'left') {
|
||||||
const address = this.#getAddress(event.pubkey)
|
const address = this.#getAddress(event.pubkey)
|
||||||
debug('Will disconnect and unstage %s', address)
|
debug('Will disconnect and unstage %s', address)
|
||||||
this.#local.conn.unstage(address)
|
this.#peer.conn.unstage(address)
|
||||||
this.#local.conn.disconnect(address)
|
this.#peer.conn.disconnect(address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,9 +165,9 @@ module.exports = class HubObserver {
|
||||||
*/
|
*/
|
||||||
#notifyNewAttendant(attendantPubkey, hubPubkey, hubName) {
|
#notifyNewAttendant(attendantPubkey, hubPubkey, hubName) {
|
||||||
if (attendantPubkey === hubPubkey) return
|
if (attendantPubkey === hubPubkey) return
|
||||||
if (attendantPubkey === this.#local.shse.pubkey) return
|
if (attendantPubkey === this.#peer.shse.pubkey) return
|
||||||
const address = this.#getAddress(attendantPubkey)
|
const address = this.#getAddress(attendantPubkey)
|
||||||
this.#local.hubClient._notifyDiscoveredAttendant({
|
this.#peer.hubClient._notifyDiscoveredAttendant({
|
||||||
address,
|
address,
|
||||||
attendantPubkey,
|
attendantPubkey,
|
||||||
hubPubkey,
|
hubPubkey,
|
||||||
|
@ -212,16 +212,16 @@ module.exports = class HubObserver {
|
||||||
this.#attendantsDrain?.abort()
|
this.#attendantsDrain?.abort()
|
||||||
for (const pubkey of this.#attendants) {
|
for (const pubkey of this.#attendants) {
|
||||||
const address = this.#getAddress(pubkey)
|
const address = this.#getAddress(pubkey)
|
||||||
this.#local.conn.unstage(address)
|
this.#peer.conn.unstage(address)
|
||||||
}
|
}
|
||||||
for (const [addr, data] of this.#local.conn.staging().entries()) {
|
for (const [addr, data] of this.#peer.conn.staging().entries()) {
|
||||||
if (data.hub === this.#hubPubkey) {
|
if (data.hub === this.#hubPubkey) {
|
||||||
this.#local.conn.unstage(addr)
|
this.#peer.conn.unstage(addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.rpc.close(true, (/** @type {any} */ err) => {
|
this.rpc.close(true, (/** @type {any} */ err) => {
|
||||||
if (err) debug('error when closing connection with room: %o', err)
|
if (err) debug('error when closing connection with room: %o', err)
|
||||||
})
|
})
|
||||||
this.#local.conn.disconnect(this.#address, () => {})
|
this.#peer.conn.disconnect(this.#address, () => {})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,9 @@ const { muxrpcMissing } = require('./utils')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Hubs} hubs
|
* @param {Hubs} hubs
|
||||||
* @param {any} local
|
* @param {any} peer
|
||||||
*/
|
*/
|
||||||
const makeTunnelPlugin = (hubs, local) => (/** @type {any}} */ msConfig) => {
|
const makeTunnelPlugin = (hubs, peer) => (/** @type {any}} */ msConfig) => {
|
||||||
const self = {
|
const self = {
|
||||||
name: 'tunnel',
|
name: 'tunnel',
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ const makeTunnelPlugin = (hubs, local) => (/** @type {any}} */ msConfig) => {
|
||||||
server(onConnect, startedCB) {
|
server(onConnect, startedCB) {
|
||||||
// Once a peer connects, detect hubs, and setup observers
|
// Once a peer connects, detect hubs, and setup observers
|
||||||
pull(
|
pull(
|
||||||
local.conn.hub().listen(),
|
peer.conn.hub().listen(),
|
||||||
pull.filter(({ type }) => type === 'connected'),
|
pull.filter(({ type }) => type === 'connected'),
|
||||||
pull.drain(async ({ address, key, details }) => {
|
pull.drain(async ({ address, key, details }) => {
|
||||||
if (!key) return
|
if (!key) return
|
||||||
|
@ -49,14 +49,14 @@ const makeTunnelPlugin = (hubs, local) => (/** @type {any}} */ msConfig) => {
|
||||||
hubs.get(pubkey)?.cancel()
|
hubs.get(pubkey)?.cancel()
|
||||||
hubs.delete(pubkey)
|
hubs.delete(pubkey)
|
||||||
}
|
}
|
||||||
const obs = new HubObserver(local, pubkey, address, res, rpc, onConnect)
|
const obs = new HubObserver(peer, pubkey, address, res, rpc, onConnect)
|
||||||
hubs.set(pubkey, obs)
|
hubs.set(pubkey, obs)
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
// Once a hub disconnects, teardown
|
// Once a hub disconnects, teardown
|
||||||
pull(
|
pull(
|
||||||
local.conn.hub().listen(),
|
peer.conn.hub().listen(),
|
||||||
pull.filter(({ type }) => type === 'disconnected'),
|
pull.filter(({ type }) => type === 'disconnected'),
|
||||||
pull.drain(({ key }) => {
|
pull.drain(({ key }) => {
|
||||||
if (!key) return
|
if (!key) return
|
||||||
|
@ -103,12 +103,12 @@ const makeTunnelPlugin = (hubs, local) => (/** @type {any}} */ msConfig) => {
|
||||||
|
|
||||||
// If no hub found, look up hub in connDB and connect to it
|
// If no hub found, look up hub in connDB and connect to it
|
||||||
if (!hubRPC) {
|
if (!hubRPC) {
|
||||||
for (const [msaddr] of local.conn.db().entries()) {
|
for (const [msaddr] of peer.conn.db().entries()) {
|
||||||
const pubkey = msaddr.split('~shse:')[1]
|
const pubkey = msaddr.split('~shse:')[1]
|
||||||
if (pubkey === hub) {
|
if (pubkey === hub) {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
debug(`to connect to ${addrStr} we first have to connect to ${hub}`)
|
debug(`to connect to ${addrStr} we first have to connect to ${hub}`)
|
||||||
const [err, rpc] = await run(local.conn.connect)(msaddr)
|
const [err, rpc] = await run(peer.conn.connect)(msaddr)
|
||||||
if (err) {
|
if (err) {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
cb(new Error(`cant connect to ${addrStr} because cant reach the hub ${hub} due to: ` + err.message ?? err))
|
cb(new Error(`cant connect to ${addrStr} because cant reach the hub ${hub} due to: ` + err.message ?? err))
|
||||||
|
@ -122,11 +122,11 @@ const makeTunnelPlugin = (hubs, local) => (/** @type {any}} */ msConfig) => {
|
||||||
// If no hub found, find tunnel addr in connDB and connect to its `hub`
|
// If no hub found, find tunnel addr in connDB and connect to its `hub`
|
||||||
if (!hubRPC) {
|
if (!hubRPC) {
|
||||||
const addrStrPlusShse = `tunnel:${hub}:${target}~shse:${target}`
|
const addrStrPlusShse = `tunnel:${hub}:${target}~shse:${target}`
|
||||||
const peerData = local.conn.db().get(addrStrPlusShse)
|
const peerData = peer.conn.db().get(addrStrPlusShse)
|
||||||
if (peerData?.hub === hub && peerData?.hubAddress) {
|
if (peerData?.hub === hub && peerData?.hubAddress) {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
debug(`to connect to ${addrStr} we first have to connect to ${hub}`)
|
debug(`to connect to ${addrStr} we first have to connect to ${hub}`)
|
||||||
const [err, rpc] = await run(local.conn.connect)(peerData.hubAddress)
|
const [err, rpc] = await run(peer.conn.connect)(peerData.hubAddress)
|
||||||
if (err) {
|
if (err) {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
cb(new Error(`cant connect to ${addrStr} because cant reach the hub ${hub} due to: ` + err.message ?? err))
|
cb(new Error(`cant connect to ${addrStr} because cant reach the hub ${hub} due to: ` + err.message ?? err))
|
||||||
|
|
|
@ -7,6 +7,7 @@ const { ErrorDuplex } = require('./utils')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'hubClient',
|
name: 'hubClient',
|
||||||
|
// needs: ['conn'], // FIXME: uncomment once we re-write conn
|
||||||
manifest: {
|
manifest: {
|
||||||
connect: 'duplex',
|
connect: 'duplex',
|
||||||
},
|
},
|
||||||
|
@ -17,19 +18,15 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {any} local
|
* @param {any} peer
|
||||||
* @param {any} config
|
* @param {any} config
|
||||||
*/
|
*/
|
||||||
init(local, config) {
|
init(peer, config) {
|
||||||
if (!local.conn?.connect) {
|
|
||||||
throw new Error('hub-client is missing the required ssb-conn plugin')
|
|
||||||
}
|
|
||||||
|
|
||||||
const hubs = new Map()
|
const hubs = new Map()
|
||||||
|
|
||||||
local.multiserver.transport({
|
peer.multiserver.transport({
|
||||||
name: 'tunnel',
|
name: 'tunnel',
|
||||||
create: makeTunnelPlugin(hubs, local),
|
create: makeTunnelPlugin(hubs, peer),
|
||||||
})
|
})
|
||||||
|
|
||||||
// Setup discoveredAttendants source pull-stream
|
// Setup discoveredAttendants source pull-stream
|
||||||
|
@ -38,7 +35,7 @@ module.exports = {
|
||||||
return _notifyDiscoveredAttendant.listen()
|
return _notifyDiscoveredAttendant.listen()
|
||||||
}
|
}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
local.close.hook(function (fn, args) {
|
peer.close.hook(function (fn, args) {
|
||||||
_notifyDiscoveredAttendant?.end()
|
_notifyDiscoveredAttendant?.end()
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
fn.apply(this, args)
|
fn.apply(this, args)
|
||||||
|
|
|
@ -18,9 +18,9 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {any} local
|
* @param {any} peer
|
||||||
*/
|
*/
|
||||||
init(local) {
|
init(peer) {
|
||||||
return {
|
return {
|
||||||
attendants() {
|
attendants() {
|
||||||
return pull.error(new Error('Not implemented on the client'))
|
return pull.error(new Error('Not implemented on the client'))
|
||||||
|
|
|
@ -3,13 +3,14 @@
|
||||||
"exclude": ["coverage/", "node_modules/", "test/"],
|
"exclude": ["coverage/", "node_modules/", "test/"],
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"checkJs": true,
|
"checkJs": true,
|
||||||
"noEmit": true,
|
"declaration": true,
|
||||||
|
"emitDeclarationOnly": true,
|
||||||
"exactOptionalPropertyTypes": true,
|
"exactOptionalPropertyTypes": true,
|
||||||
"forceConsistentCasingInFileNames": true,
|
"forceConsistentCasingInFileNames": true,
|
||||||
"lib": ["es2021", "dom"],
|
"lib": ["es2022", "dom"],
|
||||||
"module": "node16",
|
"module": "node16",
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"target": "es2021"
|
"target": "es2022"
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue