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