All files / client stats.js

28.21% Statements 11/39
0% Branches 0/20
12.5% Functions 1/8
28.21% Lines 11/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 841x 1x 1x 1x 1x 1x 1x     39x                                                             39x                                                         39x                       39x    
import { caller } from 'postmsg-rpc'
import callbackify from 'callbackify'
import { post } from 'prepost'
import defer from 'pull-defer'
import PMS from 'pull-postmsg-stream'
import toStream from 'pull-stream-to-stream'
import { isBigJson, bigFromJson } from '../serialization/big'
 
export default function (opts) {
  const api = {
    bw: callbackify.variadic(
      post(
        caller('ipfs.stats.bw', opts),
        (stats) => {
          if (stats) {
            if (isBigJson(stats.totalIn)) {
              stats.totalIn = bigFromJson(stats.totalIn)
            }
 
            if (isBigJson(stats.totalOut)) {
              stats.totalOut = bigFromJson(stats.totalOut)
            }
 
            if (isBigJson(stats.rateIn)) {
              stats.rateIn = bigFromJson(stats.rateIn)
            }
 
            if (isBigJson(stats.rateOut)) {
              stats.rateOut = bigFromJson(stats.rateOut)
            }
          }
 
          return stats
        }
      )
    ),
    bwReadableStream () {
      return toStream.source(api.bwPullStream(...arguments))
    },
    bwPullStream: (() => {
      const bwPullStream = post(
        caller('ipfs.stats.bwPullStream', opts),
        (res) => PMS.source(res.name, Object.assign({}, opts, {
          post (res) {
            const stats = res.data
 
            if (stats) {
              if (isBigJson(stats.totalIn)) {
                stats.totalIn = bigFromJson(stats.totalIn)
              }
 
              if (isBigJson(stats.totalOut)) {
                stats.totalOut = bigFromJson(stats.totalOut)
              }
 
              if (isBigJson(stats.rateIn)) {
                stats.rateIn = bigFromJson(stats.rateIn)
              }
 
              if (isBigJson(stats.rateOut)) {
                stats.rateOut = bigFromJson(stats.rateOut)
              }
            }
 
            return res
          }
        }))
      )
 
      return (...args) => {
        const deferred = defer.source()
 
        bwPullStream(...args)
          .then((res) => deferred.resolve(res))
          .catch((err) => deferred.abort(err))
 
        return deferred
      }
    })()
  }
 
  return api
}