# All files / src/client/files cat.js

95.45% Statements 21/22
100% Branches 2/2
85.71% Functions 6/7
95.45% Lines 21/22

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

    23x

    1x

    23x

    2x

    62x
    60x

    62x

    23x
    2x

    2x
    2x

    2x

    23x

     import { caller } from 'postmsg-rpc'
    import callbackify from 'callbackify'
    import defer from 'pull-defer'
    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 = {
        cat: callbackify.variadic(
          pre(
            preBufferToJson(0),
            preCidToJson(0),
            post(
              caller('ipfs.files.cat', opts),
              bufferFromJson
            )
          )
        ),
        catReadableStream () {
          return toStream.source(api.catPullStream(...arguments))
        },
        catPullStream: (() => {
          const catPullStream = pre(
            preBufferToJson(0),
            preCidToJson(0),
            post(
              caller('ipfs.files.catPullStream', opts),
              (res) => PMS.source(res.name, Object.assign({}, opts, {
                post (res) {
                  if (isBufferJson(res.data)) {
                    res.data = bufferFromJson(res.data)
                  }

                  return res
                }
              }))
            )
          )

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

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

            return deferred
          }
        })()
      }

      return api
    }