123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- const axios = require('axios')
- const debug = require('debug')('joystream:discovery:discover')
- const stripEndingSlash = require('@joystream/storage-utils/stripEndingSlash')
- const ipfs = require('ipfs-http-client')('localhost', '5001', { protocol: 'http' })
- const BN = require('bn.js')
- const { newExternallyControlledPromise } = require('@joystream/storage-utils/externalPromise')
- function inBrowser() {
- return typeof window !== 'undefined'
- }
- const activeDiscoveries = {}
- const accountInfoCache = {}
- const CACHE_TTL = 60 * 60 * 1000
- async function getIpnsIdentity(storageProviderId, runtimeApi) {
- storageProviderId = new BN(storageProviderId)
-
- const info = await runtimeApi.discovery.getAccountInfo(storageProviderId)
- if (info === null) {
-
- return null
- }
- return info.identity.toString()
- }
- async function discoverOverIpfsHttpGateway(storageProviderId, runtimeApi, gateway = 'http://localhost:8080') {
- storageProviderId = new BN(storageProviderId)
- const isProvider = await runtimeApi.workers.isStorageProvider(storageProviderId)
- if (!isProvider) {
- throw new Error('Cannot discover non storage providers')
- }
- const identity = await getIpnsIdentity(storageProviderId, runtimeApi)
- if (identity === null) {
-
- throw new Error('no identity to resolve')
- }
- gateway = stripEndingSlash(gateway)
- const url = `${gateway}/ipns/${identity}`
- const response = await axios.get(url)
- return response.data
- }
- async function discoverOverJoystreamDiscoveryService(storageProviderId, runtimeApi, discoverApiEndpoint) {
- storageProviderId = new BN(storageProviderId)
- const isProvider = await runtimeApi.workers.isStorageProvider(storageProviderId)
- if (!isProvider) {
- throw new Error('Cannot discover non storage providers')
- }
- const identity = await getIpnsIdentity(storageProviderId, runtimeApi)
-
- if (identity === null) {
- throw new Error('no identity to resolve')
- }
- if (!discoverApiEndpoint) {
-
- const discoveryBootstrapNodes = await runtimeApi.discovery.getBootstrapEndpoints()
- if (discoveryBootstrapNodes.length) {
- discoverApiEndpoint = stripEndingSlash(discoveryBootstrapNodes[0].toString())
- } else {
- throw new Error('No known discovery bootstrap nodes found on network')
- }
- }
- const url = `${discoverApiEndpoint}/discover/v0/${storageProviderId.toNumber()}`
-
- const response = await axios.get(url)
- return response.data
- }
- async function discoverOverLocalIpfsNode(storageProviderId, runtimeApi) {
- storageProviderId = new BN(storageProviderId)
- const isProvider = await runtimeApi.workers.isStorageProvider(storageProviderId)
- if (!isProvider) {
- throw new Error('Cannot discover non storage providers')
- }
- const identity = await getIpnsIdentity(storageProviderId, runtimeApi)
- if (identity === null) {
-
- throw new Error('no identity to resolve')
- }
- const ipnsAddress = `/ipns/${identity}/`
- debug('resolved ipns to ipfs object')
-
- const ipfsName = await ipfs.name.resolve(ipnsAddress, {
-
- recursive: false,
- nocache: false,
- })
- debug('getting ipfs object', ipfsName)
- const data = await ipfs.get(ipfsName)
-
- const content = data[0].content
- return JSON.parse(content)
- }
- async function _discover(storageProviderId, runtimeApi) {
- storageProviderId = new BN(storageProviderId)
- const id = storageProviderId.toNumber()
- const discoveryResult = activeDiscoveries[id]
- if (discoveryResult) {
- debug('discovery in progress waiting for result for', id)
- return discoveryResult
- }
- debug('starting new discovery for', id)
- const deferredDiscovery = newExternallyControlledPromise()
- activeDiscoveries[id] = deferredDiscovery.promise
- let result
- try {
- if (inBrowser()) {
- result = await discoverOverJoystreamDiscoveryService(storageProviderId, runtimeApi)
- } else {
- result = await discoverOverLocalIpfsNode(storageProviderId, runtimeApi)
- }
- debug(result)
- result = JSON.stringify(result)
- accountInfoCache[id] = {
- value: result,
- updated: Date.now(),
- }
- deferredDiscovery.resolve(result)
- delete activeDiscoveries[id]
- return result
- } catch (err) {
-
-
- debug(err.message)
- delete activeDiscoveries[id]
-
- deferredDiscovery.resolve(null)
-
-
-
-
-
-
- return null
- }
- }
- async function discover(storageProviderId, runtimeApi, useCachedValue = false, maxCacheAge = 0) {
- storageProviderId = new BN(storageProviderId)
- const id = storageProviderId.toNumber()
- const cached = accountInfoCache[id]
- if (cached && useCachedValue) {
- if (maxCacheAge > 0) {
-
- if (Date.now() > cached.updated + maxCacheAge) {
- return _discover(storageProviderId, runtimeApi)
- }
- }
-
- if (Date.now() > cached.updated + CACHE_TTL) {
- _discover(storageProviderId, runtimeApi)
- }
-
- return cached.value
- }
- return _discover(storageProviderId, runtimeApi)
- }
- module.exports = {
- discover,
- discoverOverJoystreamDiscoveryService,
- discoverOverIpfsHttpGateway,
- discoverOverLocalIpfsNode,
- }
|