refactor secret-stack plugin shape
This commit is contained in:
parent
1c29eb22a2
commit
c8f8e11c0b
|
@ -28,112 +28,111 @@ const HUBS_SUBDOMAIN = 'hubs'
|
||||||
* @typedef {(...args: [Error] | [null, T]) => void } CB
|
* @typedef {(...args: [Error] | [null, T]) => void } CB
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = {
|
/**
|
||||||
name: 'hubClient',
|
* @param {Peer} peer
|
||||||
needs: ['shse', 'net', 'set'],
|
* @param {any} config
|
||||||
manifest: {
|
*/
|
||||||
connect: 'duplex',
|
function initHubClient(peer, config) {
|
||||||
},
|
const hubs = new Map()
|
||||||
permissions: {
|
|
||||||
anonymous: {
|
|
||||||
allow: ['connect'],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
peer.multiserver.transport({
|
||||||
* @param {Peer} peer
|
name: 'tunnel',
|
||||||
* @param {any} config
|
create: makeTunnelPlugin(hubs, peer),
|
||||||
*/
|
})
|
||||||
init(peer, config) {
|
|
||||||
const hubs = new Map()
|
|
||||||
|
|
||||||
peer.multiserver.transport({
|
// Setup discoveredAttendants source pull-stream
|
||||||
name: 'tunnel',
|
const _notifyDiscoveredAttendant = Notify()
|
||||||
create: makeTunnelPlugin(hubs, peer),
|
function discoveredAttendants() {
|
||||||
})
|
return _notifyDiscoveredAttendant.listen()
|
||||||
|
}
|
||||||
// Setup discoveredAttendants source pull-stream
|
// @ts-ignore
|
||||||
const _notifyDiscoveredAttendant = Notify()
|
peer.close.hook(function (fn, args) {
|
||||||
function discoveredAttendants() {
|
_notifyDiscoveredAttendant?.end()
|
||||||
return _notifyDiscoveredAttendant.listen()
|
|
||||||
}
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
peer.close.hook(function (fn, args) {
|
fn.apply(this, args)
|
||||||
_notifyDiscoveredAttendant?.end()
|
})
|
||||||
// @ts-ignore
|
|
||||||
fn.apply(this, args)
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
/**
|
/**
|
||||||
* @param {`/${string}`} multiaddr
|
* @param {`/${string}`} multiaddr
|
||||||
* @param {CB<void>} cb
|
* @param {CB<void>} cb
|
||||||
*/
|
*/
|
||||||
addHub(multiaddr, cb) {
|
addHub(multiaddr, cb) {
|
||||||
peer.set.add(HUBS_SUBDOMAIN, multiaddr, (err, _) => {
|
peer.set.add(HUBS_SUBDOMAIN, multiaddr, (err, _) => {
|
||||||
|
// prettier-ignore
|
||||||
|
if (err) return cb(new Error('Failed to add Hub to my Set feed', {cause: err}))
|
||||||
|
peer.net.connect(multiaddr, (err, rpc) => {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
if (err) return cb(new Error('Failed to add Hub to my Set feed', {cause: err}))
|
if (err) return cb(new Error('Failed to connect to Hub after adding it to my Set feed', {cause: err}))
|
||||||
peer.net.connect(multiaddr, (err, rpc) => {
|
cb(null, void 0)
|
||||||
// prettier-ignore
|
|
||||||
if (err) return cb(new Error('Failed to connect to Hub after adding it to my Set feed', {cause: err}))
|
|
||||||
cb(null, void 0)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
},
|
})
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {number} amount
|
* @param {number} amount
|
||||||
* @param {CB<Array<string>>} cb
|
* @param {CB<Array<string>>} cb
|
||||||
*/
|
*/
|
||||||
getHubs(amount, cb) {
|
getHubs(amount, cb) {
|
||||||
const source = peer.net.peers()
|
const source = peer.net.peers()
|
||||||
source(null, (err, peers) => {
|
source(null, (err, peers) => {
|
||||||
if (err === true || !peers) return cb(null, [])
|
if (err === true || !peers) return cb(null, [])
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
if (err) return cb(new Error('Failed to get hubs', { cause: err }))
|
if (err) return cb(new Error('Failed to get hubs', { cause: err }))
|
||||||
// @ts-ignore
|
|
||||||
const infoMap = /**@type {Map<`/${string}`, Info>}*/ (new Map(peers))
|
|
||||||
const multiaddrs = peer.set.values(HUBS_SUBDOMAIN)
|
|
||||||
const hubs = []
|
|
||||||
for (const multiaddr of multiaddrs) {
|
|
||||||
const stats = infoMap.get(multiaddr)?.stats ?? { failure: 1 }
|
|
||||||
hubs.push({ multiaddr, stats })
|
|
||||||
}
|
|
||||||
hubs.sort((a, b) => (a.stats.failure ?? 1) - (b.stats.failure ?? 1))
|
|
||||||
hubs.splice(amount)
|
|
||||||
const returnable = hubs.map((h) => h.multiaddr)
|
|
||||||
cb(null, returnable)
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} origin
|
|
||||||
* @returns {import('pull-stream').Duplex<unknown, unknown>}
|
|
||||||
*/
|
|
||||||
connect(origin) {
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const hub = this.shse.pubkey
|
const infoMap = /**@type {Map<`/${string}`, Info>}*/ (new Map(peers))
|
||||||
debug('received hubClient.connect(%s) via hub %s', origin, hub)
|
const multiaddrs = peer.set.values(HUBS_SUBDOMAIN)
|
||||||
if (hubs.has(hub) && origin) {
|
const hubs = []
|
||||||
debug('connect() will resolve because handler exists')
|
for (const multiaddr of multiaddrs) {
|
||||||
const handler = hubs.get(hub).handler
|
const stats = infoMap.get(multiaddr)?.stats ?? { failure: 1 }
|
||||||
const [ins, outs] = DuplexPair()
|
hubs.push({ multiaddr, stats })
|
||||||
handler(ins, origin)
|
|
||||||
return outs
|
|
||||||
} else {
|
|
||||||
return ErrorDuplex(`Could not connect to ${origin} via ${hub}`)
|
|
||||||
}
|
}
|
||||||
},
|
hubs.sort((a, b) => (a.stats.failure ?? 1) - (b.stats.failure ?? 1))
|
||||||
|
hubs.splice(amount)
|
||||||
|
const returnable = hubs.map((h) => h.multiaddr)
|
||||||
|
cb(null, returnable)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
// Internal method, needed for api-plugin.ts
|
/**
|
||||||
getHubsMap() {
|
* @param {string} origin
|
||||||
return hubs
|
* @returns {import('pull-stream').Duplex<unknown, unknown>}
|
||||||
},
|
*/
|
||||||
|
connect(origin) {
|
||||||
|
// @ts-ignore
|
||||||
|
const hub = this.shse.pubkey
|
||||||
|
debug('received hubClient.connect(%s) via hub %s', origin, hub)
|
||||||
|
if (hubs.has(hub) && origin) {
|
||||||
|
debug('connect() will resolve because handler exists')
|
||||||
|
const handler = hubs.get(hub).handler
|
||||||
|
const [ins, outs] = DuplexPair()
|
||||||
|
handler(ins, origin)
|
||||||
|
return outs
|
||||||
|
} else {
|
||||||
|
return ErrorDuplex(`Could not connect to ${origin} via ${hub}`)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
discoveredAttendants,
|
// Internal method, needed for api-plugin.ts
|
||||||
|
getHubsMap() {
|
||||||
|
return hubs
|
||||||
|
},
|
||||||
|
|
||||||
// underscore so other modules IN THIS LIBRARY can use it
|
discoveredAttendants,
|
||||||
_notifyDiscoveredAttendant,
|
|
||||||
}
|
// underscore so other modules IN THIS LIBRARY can use it
|
||||||
|
_notifyDiscoveredAttendant,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.name = 'hubClient'
|
||||||
|
exports.init = initHubClient
|
||||||
|
exports.needs = ['shse', 'net', 'set']
|
||||||
|
exports.manifest = {
|
||||||
|
connect: 'duplex',
|
||||||
|
}
|
||||||
|
exports.permissions = {
|
||||||
|
anonymous: {
|
||||||
|
allow: ['connect'],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,9 @@
|
||||||
"exports": {
|
"exports": {
|
||||||
".": {
|
".": {
|
||||||
"require": "./lib/index.js"
|
"require": "./lib/index.js"
|
||||||
|
},
|
||||||
|
"./plugin": {
|
||||||
|
"require": "./lib/plugin-hub-client.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"type": "commonjs",
|
"type": "commonjs",
|
||||||
|
|
Loading…
Reference in New Issue