# All files / client/files get.js

39.39% Statements 13/33
0% Branches 0/8
9.09% Functions 1/11
39.39% Lines 13/33

    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
    831x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x

    1x

    1x

     import { caller } from 'postmsg-rpc'
    import callbackify from 'callbackify'
    import defer from 'pull-defer'
    import pull from 'pull-stream'
    import toStream from 'pull-stream-to-stream'
    import PMS from 'pull-postmsg-stream'
    import { pre, post } from 'prepost'
    import { preCidToJson } from '../../serialization/cid'
    import { isBufferJson, preBufferToJson, bufferFromJson } from '../../serialization/buffer'

    export default function (opts) {
      const api = {
        get: callbackify.variadic(
          pre(
            preBufferToJson(0),
            preCidToJson(0),
            post(
              caller('ipfs.files.get', opts),
              (files) => files.map((file) => {
                if (file.content) {
                  file.content = bufferFromJson(file.content)
                }

                return file
              })
            )
          )
        ),
        getReadableStream () {
          return toStream.source(
            pull(
              api.getPullStream(...arguments),
              pull.map((file) => {
                if (file.content) {
                  file.content = toStream.source(file.content)
                }
                return file
              })
            )
          )
        },
        getPullStream: (() => {
          const getPullStream = pre(
            preBufferToJson(0),
            preCidToJson(0),
            post(
              caller('ipfs.files.getPullStream', opts),
              (res) => pull(
                PMS.source(res.name, opts),
                pull.map(file => {
                  if (file.content) {
                    file.content = PMS.source(file.content.name, Object.assign({}, opts, {
                      post (res) {
                        if (isBufferJson(res.data)) {
                          res.data = bufferFromJson(res.data)
                        }

                        return res
                      }
                    }))
                  }

                  return file
                })
              )
            )
          )

          return (...args) => {
            const deferred = defer.source()

            getPullStream(...args)
              .then((res) => deferred.resolve(res))
              .catch((err) => deferred.abort(err))

            return deferred
          }
        })()
      }

      return api
    }