improve return types
This commit is contained in:
parent
9446faa823
commit
e759e76299
|
@ -8,6 +8,7 @@ const { ErrorDuplex } = require('./utils')
|
||||||
const HUBS_SUBDOMAIN = 'hubs'
|
const HUBS_SUBDOMAIN = 'hubs'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @typedef {`/${string}/${string}/${string}/${string}/${string}/${string}`} HubMultiaddr
|
||||||
* @typedef {ReturnType<import('ppppp-net').init>} PPPPPNet
|
* @typedef {ReturnType<import('ppppp-net').init>} PPPPPNet
|
||||||
* @typedef {ReturnType<import('ppppp-set').init>} PPPPPSet
|
* @typedef {ReturnType<import('ppppp-set').init>} PPPPPSet
|
||||||
* @typedef {import('ppppp-net').Info} Info
|
* @typedef {import('ppppp-net').Info} Info
|
||||||
|
@ -52,12 +53,11 @@ function initHubClient(peer, config) {
|
||||||
fn.apply(this, args)
|
fn.apply(this, args)
|
||||||
})
|
})
|
||||||
|
|
||||||
return {
|
|
||||||
/**
|
/**
|
||||||
* @param {`/${string}`} multiaddr
|
* @param {HubMultiaddr} multiaddr
|
||||||
* @param {CB<void>} cb
|
* @param {CB<void>} cb
|
||||||
*/
|
*/
|
||||||
addHub(multiaddr, cb) {
|
function addHub(multiaddr, cb) {
|
||||||
peer.set.add(HUBS_SUBDOMAIN, multiaddr, (err, _) => {
|
peer.set.add(HUBS_SUBDOMAIN, multiaddr, (err, _) => {
|
||||||
// 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 add Hub to my Set feed', {cause: err}))
|
||||||
|
@ -67,23 +67,21 @@ function initHubClient(peer, config) {
|
||||||
cb(null, void 0)
|
cb(null, void 0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {number} amount
|
* @param {number} amount
|
||||||
* @param {CB<Array<`/${string}`>>} cb
|
* @param {CB<Array<HubMultiaddr>>} cb
|
||||||
*/
|
*/
|
||||||
getHubs(amount, cb) {
|
function 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
|
// @ts-ignore
|
||||||
const infoMap = /**@type {Map<`/${string}`, Info>}*/ (new Map(peers))
|
const infoMap = new Map(peers)
|
||||||
const multiaddrs = /**@type {Array<`/${string}`>}*/ (
|
const multiaddrs = peer.set.values(HUBS_SUBDOMAIN)
|
||||||
peer.set.values(HUBS_SUBDOMAIN)
|
|
||||||
)
|
|
||||||
const hubs = []
|
const hubs = []
|
||||||
for (const multiaddr of multiaddrs) {
|
for (const multiaddr of multiaddrs) {
|
||||||
const stats = infoMap.get(multiaddr)?.stats ?? { failure: 1 }
|
const stats = infoMap.get(multiaddr)?.stats ?? { failure: 1 }
|
||||||
|
@ -94,13 +92,13 @@ function initHubClient(peer, config) {
|
||||||
const returnable = hubs.map((h) => h.multiaddr)
|
const returnable = hubs.map((h) => h.multiaddr)
|
||||||
cb(null, returnable)
|
cb(null, returnable)
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} origin
|
* @param {string} origin
|
||||||
* @returns {import('pull-stream').Duplex<unknown, unknown>}
|
* @returns {import('pull-stream').Duplex<unknown, unknown>}
|
||||||
*/
|
*/
|
||||||
connect(origin) {
|
function connect(origin) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const hub = this.shse.pubkey
|
const hub = this.shse.pubkey
|
||||||
debug('received hubClient.connect(%s) via hub %s', origin, hub)
|
debug('received hubClient.connect(%s) via hub %s', origin, hub)
|
||||||
|
@ -113,15 +111,19 @@ function initHubClient(peer, config) {
|
||||||
} else {
|
} else {
|
||||||
return ErrorDuplex(`Could not connect to ${origin} via ${hub}`)
|
return ErrorDuplex(`Could not connect to ${origin} via ${hub}`)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
// Internal method, needed for api-plugin.ts
|
// Internal method
|
||||||
getHubsMap() {
|
function getHubsMap() {
|
||||||
return hubs
|
return hubs
|
||||||
},
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
addHub,
|
||||||
|
getHubs,
|
||||||
|
connect,
|
||||||
|
getHubsMap,
|
||||||
discoveredAttendants,
|
discoveredAttendants,
|
||||||
|
|
||||||
// underscore so other modules IN THIS LIBRARY can use it
|
// underscore so other modules IN THIS LIBRARY can use it
|
||||||
_notifyDiscoveredAttendant,
|
_notifyDiscoveredAttendant,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue