|
@@ -1,31 +1,16 @@
|
|
-import BN from 'bn.js'
|
|
|
|
import fetch from 'cross-fetch'
|
|
import fetch from 'cross-fetch'
|
|
import parseHtml, { HTMLElement } from 'node-html-parser'
|
|
import parseHtml, { HTMLElement } from 'node-html-parser'
|
|
|
|
|
|
import { DataObjectFieldsFragment } from './api/__generated__/sdk'
|
|
import { DataObjectFieldsFragment } from './api/__generated__/sdk'
|
|
import { AppData, MetaTags } from './types'
|
|
import { AppData, MetaTags } from './types'
|
|
|
|
|
|
-const DISTRIBUTOR_ASSET_PATH = 'api/v1/assets'
|
|
|
|
-
|
|
|
|
export const joinUrlFragments = (...fragments: string[]) => {
|
|
export const joinUrlFragments = (...fragments: string[]) => {
|
|
const strippedFragments = fragments.map((f) => f.replace(/^\/|\/$/, ''))
|
|
const strippedFragments = fragments.map((f) => f.replace(/^\/|\/$/, ''))
|
|
return strippedFragments.join('/')
|
|
return strippedFragments.join('/')
|
|
}
|
|
}
|
|
|
|
|
|
export const generateAssetUrl = (asset: DataObjectFieldsFragment) => {
|
|
export const generateAssetUrl = (asset: DataObjectFieldsFragment) => {
|
|
- const workingBuckets = asset.storageBag.distributionBuckets.filter((bucket) => bucket.distributing)
|
|
|
|
- const distributorsEndpoints = workingBuckets.reduce((acc, bucket) => {
|
|
|
|
- const endpoints = bucket.operators
|
|
|
|
- .filter((operator) => !!operator.metadata?.nodeEndpoint)
|
|
|
|
- .map((operator) => operator.metadata?.nodeEndpoint) as string[]
|
|
|
|
- return [...acc, ...endpoints]
|
|
|
|
- }, [] as string[])
|
|
|
|
-
|
|
|
|
- const assetIdBn = new BN(asset.id)
|
|
|
|
- const endpointsCountBn = new BN(distributorsEndpoints.length)
|
|
|
|
- const distributorIndex = assetIdBn.mod(endpointsCountBn).toNumber()
|
|
|
|
- const endpoint = distributorsEndpoints[distributorIndex]
|
|
|
|
- return joinUrlFragments(endpoint, DISTRIBUTOR_ASSET_PATH, asset.id)
|
|
|
|
|
|
+ return asset.resolvedUrls[0]
|
|
}
|
|
}
|
|
|
|
|
|
export const getEnvVariable = (varName: string, required?: boolean) => {
|
|
export const getEnvVariable = (varName: string, required?: boolean) => {
|