general refactor
This commit is contained in:
parent
d2dd75de0f
commit
d5b72c3abc
|
@ -19,7 +19,7 @@ module.exports = class HubObserver {
|
|||
* @readonly
|
||||
* @type {any}
|
||||
*/
|
||||
#local
|
||||
#peer
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
|
@ -47,15 +47,15 @@ module.exports = class HubObserver {
|
|||
#attendantsDrain
|
||||
|
||||
/**
|
||||
* @param {any} local
|
||||
* @param {any} peer
|
||||
* @param {string} hubPubkey
|
||||
* @param {string} address
|
||||
* @param {{name: string, admin: string}} hubMetadata
|
||||
* @param {any} rpc
|
||||
* @param {any} onConnect
|
||||
*/
|
||||
constructor(local, hubPubkey, address, hubMetadata, rpc, onConnect) {
|
||||
this.#local = local
|
||||
constructor(peer, hubPubkey, address, hubMetadata, rpc, onConnect) {
|
||||
this.#peer = peer
|
||||
this.#hubPubkey = hubPubkey
|
||||
this.#address = address
|
||||
this.#hubMetadata = hubMetadata
|
||||
|
@ -85,8 +85,8 @@ module.exports = class HubObserver {
|
|||
const { name, admin } = this.#hubMetadata
|
||||
if (name) metadata.name = name
|
||||
if (admin) metadata.admin = admin
|
||||
this.#local.conn.db().update(this.#address, metadata)
|
||||
this.#local.conn.hub().update(this.#address, metadata)
|
||||
this.#peer.conn.db().update(this.#address, metadata)
|
||||
this.#peer.conn.hub().update(this.#address, metadata)
|
||||
}
|
||||
|
||||
debug('Announcing myself to hub %s', this.#hubPubkey)
|
||||
|
@ -126,7 +126,7 @@ module.exports = class HubObserver {
|
|||
|
||||
// Update onlineCount metadata for this hub
|
||||
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
|
||||
const hubName = this.#hubMetadata?.name
|
||||
|
@ -139,8 +139,8 @@ module.exports = class HubObserver {
|
|||
} else if (event.type === 'left') {
|
||||
const address = this.#getAddress(event.pubkey)
|
||||
debug('Will disconnect and unstage %s', address)
|
||||
this.#local.conn.unstage(address)
|
||||
this.#local.conn.disconnect(address)
|
||||
this.#peer.conn.unstage(address)
|
||||
this.#peer.conn.disconnect(address)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,9 +165,9 @@ module.exports = class HubObserver {
|
|||
*/
|
||||
#notifyNewAttendant(attendantPubkey, hubPubkey, hubName) {
|
||||
if (attendantPubkey === hubPubkey) return
|
||||
if (attendantPubkey === this.#local.shse.pubkey) return
|
||||
if (attendantPubkey === this.#peer.shse.pubkey) return
|
||||
const address = this.#getAddress(attendantPubkey)
|
||||
this.#local.hubClient._notifyDiscoveredAttendant({
|
||||
this.#peer.hubClient._notifyDiscoveredAttendant({
|
||||
address,
|
||||
attendantPubkey,
|
||||
hubPubkey,
|
||||
|
@ -212,16 +212,16 @@ module.exports = class HubObserver {
|
|||
this.#attendantsDrain?.abort()
|
||||
for (const pubkey of this.#attendants) {
|
||||
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) {
|
||||
this.#local.conn.unstage(addr)
|
||||
this.#peer.conn.unstage(addr)
|
||||
}
|
||||
}
|
||||
this.rpc.close(true, (/** @type {any} */ 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 {any} local
|
||||
* @param {any} peer
|
||||
*/
|
||||
const makeTunnelPlugin = (hubs, local) => (/** @type {any}} */ msConfig) => {
|
||||
const makeTunnelPlugin = (hubs, peer) => (/** @type {any}} */ msConfig) => {
|
||||
const self = {
|
||||
name: 'tunnel',
|
||||
|
||||
|
@ -28,7 +28,7 @@ const makeTunnelPlugin = (hubs, local) => (/** @type {any}} */ msConfig) => {
|
|||
server(onConnect, startedCB) {
|
||||
// Once a peer connects, detect hubs, and setup observers
|
||||
pull(
|
||||
local.conn.hub().listen(),
|
||||
peer.conn.hub().listen(),
|
||||
pull.filter(({ type }) => type === 'connected'),
|
||||
pull.drain(async ({ address, key, details }) => {
|
||||
if (!key) return
|
||||
|
@ -49,14 +49,14 @@ const makeTunnelPlugin = (hubs, local) => (/** @type {any}} */ msConfig) => {
|
|||
hubs.get(pubkey)?.cancel()
|
||||
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)
|
||||
})
|
||||
)
|
||||
|
||||
// Once a hub disconnects, teardown
|
||||
pull(
|
||||
local.conn.hub().listen(),
|
||||
peer.conn.hub().listen(),
|
||||
pull.filter(({ type }) => type === 'disconnected'),
|
||||
pull.drain(({ key }) => {
|
||||
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 (!hubRPC) {
|
||||
for (const [msaddr] of local.conn.db().entries()) {
|
||||
for (const [msaddr] of peer.conn.db().entries()) {
|
||||
const pubkey = msaddr.split('~shse:')[1]
|
||||
if (pubkey === hub) {
|
||||
// prettier-ignore
|
||||
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) {
|
||||
// prettier-ignore
|
||||
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 (!hubRPC) {
|
||||
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) {
|
||||
// prettier-ignore
|
||||
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) {
|
||||
// prettier-ignore
|
||||
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 = {
|
||||
name: 'hubClient',
|
||||
// needs: ['conn'], // FIXME: uncomment once we re-write conn
|
||||
manifest: {
|
||||
connect: 'duplex',
|
||||
},
|
||||
|
@ -17,19 +18,15 @@ module.exports = {
|
|||
},
|
||||
|
||||
/**
|
||||
* @param {any} local
|
||||
* @param {any} peer
|
||||
* @param {any} config
|
||||
*/
|
||||
init(local, config) {
|
||||
if (!local.conn?.connect) {
|
||||
throw new Error('hub-client is missing the required ssb-conn plugin')
|
||||
}
|
||||
|
||||
init(peer, config) {
|
||||
const hubs = new Map()
|
||||
|
||||
local.multiserver.transport({
|
||||
peer.multiserver.transport({
|
||||
name: 'tunnel',
|
||||
create: makeTunnelPlugin(hubs, local),
|
||||
create: makeTunnelPlugin(hubs, peer),
|
||||
})
|
||||
|
||||
// Setup discoveredAttendants source pull-stream
|
||||
|
@ -38,7 +35,7 @@ module.exports = {
|
|||
return _notifyDiscoveredAttendant.listen()
|
||||
}
|
||||
// @ts-ignore
|
||||
local.close.hook(function (fn, args) {
|
||||
peer.close.hook(function (fn, args) {
|
||||
_notifyDiscoveredAttendant?.end()
|
||||
// @ts-ignore
|
||||
fn.apply(this, args)
|
||||
|
|
|
@ -18,9 +18,9 @@ module.exports = {
|
|||
},
|
||||
|
||||
/**
|
||||
* @param {any} local
|
||||
* @param {any} peer
|
||||
*/
|
||||
init(local) {
|
||||
init(peer) {
|
||||
return {
|
||||
attendants() {
|
||||
return pull.error(new Error('Not implemented on the client'))
|
||||
|
|
|
@ -3,13 +3,14 @@
|
|||
"exclude": ["coverage/", "node_modules/", "test/"],
|
||||
"compilerOptions": {
|
||||
"checkJs": true,
|
||||
"noEmit": true,
|
||||
"declaration": true,
|
||||
"emitDeclarationOnly": true,
|
||||
"exactOptionalPropertyTypes": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"lib": ["es2021", "dom"],
|
||||
"lib": ["es2022", "dom"],
|
||||
"module": "node16",
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"target": "es2021"
|
||||
"target": "es2022"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue