rename sstack to local
This commit is contained in:
parent
d60b639b1c
commit
fe72a1a86e
|
@ -19,7 +19,7 @@ module.exports = class HubObserver {
|
|||
* @readonly
|
||||
* @type {any}
|
||||
*/
|
||||
#sstack
|
||||
#local
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
|
@ -47,15 +47,15 @@ module.exports = class HubObserver {
|
|||
#attendantsDrain
|
||||
|
||||
/**
|
||||
* @param {any} sstack
|
||||
* @param {any} local
|
||||
* @param {string} hubPubkey
|
||||
* @param {string} address
|
||||
* @param {{name: string, admin: string}} hubMetadata
|
||||
* @param {any} rpc
|
||||
* @param {any} onConnect
|
||||
*/
|
||||
constructor(sstack, hubPubkey, address, hubMetadata, rpc, onConnect) {
|
||||
this.#sstack = sstack
|
||||
constructor(local, hubPubkey, address, hubMetadata, rpc, onConnect) {
|
||||
this.#local = local
|
||||
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.#sstack.conn.db().update(this.#address, metadata)
|
||||
this.#sstack.conn.hub().update(this.#address, metadata)
|
||||
this.#local.conn.db().update(this.#address, metadata)
|
||||
this.#local.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.#sstack.conn.hub().update(this.#address, { onlineCount })
|
||||
this.#local.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.#sstack.conn.unstage(address)
|
||||
this.#sstack.conn.disconnect(address)
|
||||
this.#local.conn.unstage(address)
|
||||
this.#local.conn.disconnect(address)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -167,7 +167,7 @@ module.exports = class HubObserver {
|
|||
if (attendantPubkey === hubPubkey) return
|
||||
if (attendantPubkey === this.#sstack.id) return
|
||||
const address = this.#getAddress(attendantPubkey)
|
||||
this.#sstack.hubClient._notifyDiscoveredAttendant({
|
||||
this.#local.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.#sstack.conn.unstage(address)
|
||||
this.#local.conn.unstage(address)
|
||||
}
|
||||
for (const [addr, data] of this.#sstack.conn.staging().entries()) {
|
||||
for (const [addr, data] of this.#local.conn.staging().entries()) {
|
||||
if (data.hub === this.#hubPubkey) {
|
||||
this.#sstack.conn.unstage(addr)
|
||||
this.#local.conn.unstage(addr)
|
||||
}
|
||||
}
|
||||
this.rpc.close(true, (/** @type {any} */ err) => {
|
||||
if (err) debug('error when closing connection with room: %o', err)
|
||||
})
|
||||
this.#sstack.conn.disconnect(this.#address, () => {})
|
||||
this.#local.conn.disconnect(this.#address, () => {})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,9 +11,9 @@ const { muxrpcMissing } = require('./utils')
|
|||
|
||||
/**
|
||||
* @param {Hubs} hubs
|
||||
* @param {any} sstack
|
||||
* @param {any} local
|
||||
*/
|
||||
const makeTunnelPlugin = (hubs, sstack) => (/** @type {any}} */ msConfig) => {
|
||||
const makeTunnelPlugin = (hubs, local) => (/** @type {any}} */ msConfig) => {
|
||||
const self = {
|
||||
name: 'tunnel',
|
||||
|
||||
|
@ -28,11 +28,11 @@ const makeTunnelPlugin = (hubs, sstack) => (/** @type {any}} */ msConfig) => {
|
|||
server(onConnect, startedCB) {
|
||||
// Once a peer connects, detect hubs, and setup observers
|
||||
pull(
|
||||
sstack.conn.hub().listen(),
|
||||
local.conn.hub().listen(),
|
||||
pull.filter(({ type }) => type === 'connected'),
|
||||
pull.drain(async ({ address, key, details }) => {
|
||||
if (!key) return
|
||||
// TODO: once secret-stack is updated, we won't need this anymore:
|
||||
// TODO: once ssb-conn is replaced, we won't need this anymore:
|
||||
const pubkey = bs58.encode(Buffer.from(key, 'base64'))
|
||||
if (hubs.has(pubkey)) return
|
||||
if (!details?.rpc) return
|
||||
|
@ -49,18 +49,18 @@ const makeTunnelPlugin = (hubs, sstack) => (/** @type {any}} */ msConfig) => {
|
|||
hubs.get(pubkey)?.cancel()
|
||||
hubs.delete(pubkey)
|
||||
}
|
||||
const obs = new HubObserver(sstack, pubkey, address, res, rpc, onConnect)
|
||||
const obs = new HubObserver(local, pubkey, address, res, rpc, onConnect)
|
||||
hubs.set(pubkey, obs)
|
||||
})
|
||||
)
|
||||
|
||||
// Once a hub disconnects, teardown
|
||||
pull(
|
||||
sstack.conn.hub().listen(),
|
||||
local.conn.hub().listen(),
|
||||
pull.filter(({ type }) => type === 'disconnected'),
|
||||
pull.drain(({ key }) => {
|
||||
if (!key) return
|
||||
// TODO: once secret-stack is updated, we won't need this anymore:
|
||||
// TODO: once ssb-conn is replaced, we won't need this anymore:
|
||||
const pubkey = bs58.encode(Buffer.from(key, 'base64'))
|
||||
if (!hubs.has(pubkey)) return
|
||||
hubs.get(pubkey)?.close()
|
||||
|
@ -97,12 +97,12 @@ const makeTunnelPlugin = (hubs, sstack) => (/** @type {any}} */ msConfig) => {
|
|||
|
||||
// If no hub found, look up hub in connDB and connect to it
|
||||
if (!hubRPC) {
|
||||
for (const [msaddr] of sstack.conn.db().entries()) {
|
||||
for (const [msaddr] of local.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(sstack.conn.connect)(msaddr)
|
||||
const [err, rpc] = await run(local.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))
|
||||
|
@ -116,11 +116,11 @@ const makeTunnelPlugin = (hubs, sstack) => (/** @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 = sstack.conn.db().get(addrStrPlusShse)
|
||||
const peerData = local.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(sstack.conn.connect)(peerData.hubAddress)
|
||||
const [err, rpc] = await run(local.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))
|
||||
|
|
|
@ -18,19 +18,19 @@ module.exports = {
|
|||
},
|
||||
|
||||
/**
|
||||
* @param {any} sstack
|
||||
* @param {any} local
|
||||
* @param {any} config
|
||||
*/
|
||||
init(sstack, config) {
|
||||
if (!sstack.conn?.connect) {
|
||||
init(local, config) {
|
||||
if (!local.conn?.connect) {
|
||||
throw new Error('hub-client is missing the required ssb-conn plugin')
|
||||
}
|
||||
|
||||
const hubs = new Map()
|
||||
|
||||
sstack.multiserver.transport({
|
||||
local.multiserver.transport({
|
||||
name: 'tunnel',
|
||||
create: makeTunnelPlugin(hubs, sstack),
|
||||
create: makeTunnelPlugin(hubs, local),
|
||||
})
|
||||
|
||||
// Setup discoveredAttendants source pull-stream
|
||||
|
@ -39,7 +39,7 @@ module.exports = {
|
|||
return _notifyDiscoveredAttendant.listen()
|
||||
}
|
||||
// @ts-ignore
|
||||
sstack.close.hook(function (fn, args) {
|
||||
local.close.hook(function (fn, args) {
|
||||
_notifyDiscoveredAttendant?.end()
|
||||
// @ts-ignore
|
||||
fn.apply(this, args)
|
||||
|
|
|
@ -18,9 +18,9 @@ module.exports = {
|
|||
},
|
||||
|
||||
/**
|
||||
* @param {any} sstack
|
||||
* @param {any} local
|
||||
*/
|
||||
init(sstack) {
|
||||
init(local) {
|
||||
return {
|
||||
attendants() {
|
||||
return pull.error(new Error('Not implemented on the client'))
|
||||
|
|
Loading…
Reference in New Issue