Kaynağa Gözat

storage-node: Fix linter errors.

Shamil Gadelshin 4 yıl önce
ebeveyn
işleme
59033a758e

+ 36 - 36
storage-node/packages/discovery/discover.js

@@ -171,42 +171,6 @@ async function discoverOverLocalIpfsNode(storageProviderId, runtimeApi) {
   return JSON.parse(content)
 }
 
-/**
- * Cached discovery of storage provider service information. If useCachedValue is
- * set to true, will always return the cached result if found. New discovery will be triggered
- * if record is found to be stale. If a stale record is not desired (CACHE_TTL old) pass a non zero
- * value for maxCacheAge, which will force a new discovery and return the new resolved value.
- * This method in turn calls _discovery which handles concurrent discoveries and selects the appropriate
- * protocol to perform the query.
- * If the storage provider is not registered it will resolve to null
- * @param {number | BN | u64} storageProviderId - provider to discover
- * @param {RuntimeApi} runtimeApi - api instance to query the chain
- * @param {bool} useCachedValue - optionaly use chached queries
- * @param {number} maxCacheAge - maximum age of a cached query that triggers automatic re-discovery
- * @returns { Promise<object | null> } - the published service information
- */
-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) {
-      // get latest value
-      if (Date.now() > cached.updated + maxCacheAge) {
-        return _discover(storageProviderId, runtimeApi)
-      }
-    }
-    // refresh if cache if stale, new value returned on next cached query
-    if (Date.now() > cached.updated + CACHE_TTL) {
-      _discover(storageProviderId, runtimeApi)
-    }
-    // return best known value
-    return cached.value
-  }
-  return _discover(storageProviderId, runtimeApi)
-}
-
 /**
  * Internal method that handles concurrent discoveries and caching of results. Will
  * select the appropriate discovery protocol based on whether we are in a browser environment or not.
@@ -264,6 +228,42 @@ async function _discover(storageProviderId, runtimeApi) {
   }
 }
 
+/**
+ * Cached discovery of storage provider service information. If useCachedValue is
+ * set to true, will always return the cached result if found. New discovery will be triggered
+ * if record is found to be stale. If a stale record is not desired (CACHE_TTL old) pass a non zero
+ * value for maxCacheAge, which will force a new discovery and return the new resolved value.
+ * This method in turn calls _discovery which handles concurrent discoveries and selects the appropriate
+ * protocol to perform the query.
+ * If the storage provider is not registered it will resolve to null
+ * @param {number | BN | u64} storageProviderId - provider to discover
+ * @param {RuntimeApi} runtimeApi - api instance to query the chain
+ * @param {bool} useCachedValue - optionaly use chached queries
+ * @param {number} maxCacheAge - maximum age of a cached query that triggers automatic re-discovery
+ * @returns { Promise<object | null> } - the published service information
+ */
+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) {
+      // get latest value
+      if (Date.now() > cached.updated + maxCacheAge) {
+        return _discover(storageProviderId, runtimeApi)
+      }
+    }
+    // refresh if cache if stale, new value returned on next cached query
+    if (Date.now() > cached.updated + CACHE_TTL) {
+      _discover(storageProviderId, runtimeApi)
+    }
+    // return best known value
+    return cached.value
+  }
+  return _discover(storageProviderId, runtimeApi)
+}
+
 module.exports = {
   discover,
   discoverOverJoystreamDiscoveryService,

+ 68 - 68
storage-node/packages/helios/bin/cli.js

@@ -6,6 +6,74 @@ const { discover } = require('@joystream/service-discovery')
 const axios = require('axios')
 const stripEndingSlash = require('@joystream/storage-utils/stripEndingSlash')
 
+function mapInfoToStatus(providers, currentHeight) {
+  return providers.map(({ providerId, info }) => {
+    if (info) {
+      return {
+        providerId,
+        identity: info.identity.toString(),
+        expiresIn: info.expires_at.sub(currentHeight).toNumber(),
+        expired: currentHeight.gte(info.expires_at),
+      }
+    }
+    return {
+      providerId,
+      identity: null,
+      status: 'down',
+    }
+  })
+}
+
+function makeAssetUrl(contentId, source) {
+  source = stripEndingSlash(source)
+  return `${source}/asset/v0/${encodeAddress(contentId)}`
+}
+
+async function assetRelationshipState(api, contentId, providers) {
+  const dataObject = await api.query.dataDirectory.dataObjectByContentId(contentId)
+
+  const relationshipIds = await api.query.dataObjectStorageRegistry.relationshipsByContentId(contentId)
+
+  // how many relationships associated with active providers and in ready state
+  const activeRelationships = await Promise.all(
+    relationshipIds.map(async (id) => {
+      let relationship = await api.query.dataObjectStorageRegistry.relationships(id)
+      relationship = relationship.unwrap()
+      // only interested in ready relationships
+      if (!relationship.ready) {
+        return undefined
+      }
+      // Does the relationship belong to an active provider ?
+      return providers.find((provider) => relationship.storage_provider.eq(provider))
+    })
+  )
+
+  return [activeRelationships.filter((active) => active).length, dataObject.unwrap().liaison_judgement]
+}
+
+// HTTP HEAD with axios all known content ids on each provider
+async function countContentAvailability(contentIds, source) {
+  const content = {}
+  let found = 0
+  let missing = 0
+  for (let i = 0; i < contentIds.length; i++) {
+    const assetUrl = makeAssetUrl(contentIds[i], source)
+    try {
+      const info = await axios.head(assetUrl)
+      content[encodeAddress(contentIds[i])] = {
+        type: info.headers['content-type'],
+        bytes: info.headers['content-length'],
+      }
+      // TODO: cross check against dataobject size
+      found++
+    } catch (err) {
+      missing++
+    }
+  }
+
+  return { found, missing, content }
+}
+
 async function main() {
   const runtime = await RuntimeApi.create()
   const { api } = runtime
@@ -127,72 +195,4 @@ async function main() {
   })
 }
 
-function mapInfoToStatus(providers, currentHeight) {
-  return providers.map(({ providerId, info }) => {
-    if (info) {
-      return {
-        providerId,
-        identity: info.identity.toString(),
-        expiresIn: info.expires_at.sub(currentHeight).toNumber(),
-        expired: currentHeight.gte(info.expires_at),
-      }
-    }
-    return {
-      providerId,
-      identity: null,
-      status: 'down',
-    }
-  })
-}
-
-// HTTP HEAD with axios all known content ids on each provider
-async function countContentAvailability(contentIds, source) {
-  const content = {}
-  let found = 0
-  let missing = 0
-  for (let i = 0; i < contentIds.length; i++) {
-    const assetUrl = makeAssetUrl(contentIds[i], source)
-    try {
-      const info = await axios.head(assetUrl)
-      content[encodeAddress(contentIds[i])] = {
-        type: info.headers['content-type'],
-        bytes: info.headers['content-length'],
-      }
-      // TODO: cross check against dataobject size
-      found++
-    } catch (err) {
-      missing++
-    }
-  }
-
-  return { found, missing, content }
-}
-
-function makeAssetUrl(contentId, source) {
-  source = stripEndingSlash(source)
-  return `${source}/asset/v0/${encodeAddress(contentId)}`
-}
-
-async function assetRelationshipState(api, contentId, providers) {
-  const dataObject = await api.query.dataDirectory.dataObjectByContentId(contentId)
-
-  const relationshipIds = await api.query.dataObjectStorageRegistry.relationshipsByContentId(contentId)
-
-  // how many relationships associated with active providers and in ready state
-  const activeRelationships = await Promise.all(
-    relationshipIds.map(async (id) => {
-      let relationship = await api.query.dataObjectStorageRegistry.relationships(id)
-      relationship = relationship.unwrap()
-      // only interested in ready relationships
-      if (!relationship.ready) {
-        return undefined
-      }
-      // Does the relationship belong to an active provider ?
-      return providers.find((provider) => relationship.storage_provider.eq(provider))
-    })
-  )
-
-  return [activeRelationships.filter((active) => active).length, dataObject.unwrap().liaison_judgement]
-}
-
 main()

+ 28 - 28
storage-node/packages/runtime-api/workers.js

@@ -22,6 +22,34 @@ const debug = require('debug')('joystream:runtime:roles')
 const BN = require('bn.js')
 const { Worker } = require('@joystream/types/working-group')
 
+/*
+ * Finds assigned worker id corresponding to the application id from the resulting
+ * ApplicationIdToWorkerIdMap map in the OpeningFilled event. Expects map to
+ * contain at least one entry.
+ */
+function getWorkerIdFromApplicationIdToWorkerIdMap(filledMap, applicationId) {
+  if (filledMap.size === 0) {
+    throw new Error('Expected opening to be filled!')
+  }
+
+  let ourApplicationIdKey
+
+  for (const key of filledMap.keys()) {
+    if (key.eq(applicationId)) {
+      ourApplicationIdKey = key
+      break
+    }
+  }
+
+  if (!ourApplicationIdKey) {
+    throw new Error('Expected application id to have been filled!')
+  }
+
+  const workerId = filledMap.get(ourApplicationIdKey)
+
+  return workerId
+}
+
 /*
  * Add worker related functionality to the substrate API.
  */
@@ -266,34 +294,6 @@ class WorkersApi {
   }
 }
 
-/*
- * Finds assigned worker id corresponding to the application id from the resulting
- * ApplicationIdToWorkerIdMap map in the OpeningFilled event. Expects map to
- * contain at least one entry.
- */
-function getWorkerIdFromApplicationIdToWorkerIdMap(filledMap, applicationId) {
-  if (filledMap.size === 0) {
-    throw new Error('Expected opening to be filled!')
-  }
-
-  let ourApplicationIdKey
-
-  for (const key of filledMap.keys()) {
-    if (key.eq(applicationId)) {
-      ourApplicationIdKey = key
-      break
-    }
-  }
-
-  if (!ourApplicationIdKey) {
-    throw new Error('Expected application id to have been filled!')
-  }
-
-  const workerId = filledMap.get(ourApplicationIdKey)
-
-  return workerId
-}
-
 module.exports = {
   WorkersApi,
 }

+ 2 - 0
storage-node/packages/util/externalPromise.js

@@ -7,6 +7,8 @@
 function newExternallyControlledPromise() {
   let resolve, reject
 
+  // Disable lint until the migration to TypeScript.
+  // eslint-disable-next-line promise/param-names
   const promise = new Promise((res, rej) => {
     resolve = res
     reject = rej

+ 1 - 1
storage-node/packages/util/pagination.js

@@ -116,7 +116,7 @@ module.exports = {
 
     // Parse current url
     const url = require('url')
-    const reqUrl = url.parse(req.protocol + '://' + req.get('host') + req.originalUrl)
+    const reqUrl = new URL(req.protocol + '://' + req.get('host') + req.originalUrl)
     const params = new url.URLSearchParams(reqUrl.query)
 
     // Pagination object