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

100% Statements 18/18
100% Branches 2/2
100% Functions 5/5
100% Lines 18/18

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

    23x

    9x

    2x
    2x
    2x

    2x

    62x
    60x

    62x

    2x

     import { expose } from 'postmsg-rpc'
    import { pre, post } from 'prepost'
    import pull from 'pull-stream'
    import PMS from 'pull-postmsg-stream'
    import shortid from 'shortid'
    import { preCidFromJson } from '../../serialization/cid'
    import { isBuffer, preBufferFromJson, bufferToJson } from '../../serialization/buffer'
    import { functionToJson } from '../../serialization/function'

    export default function (getIpfs, opts) {
      return {
        cat: expose('ipfs.files.cat', pre(
          preBufferFromJson(0),
          preCidFromJson(0),
          opts.pre('files.cat'),
          post(
            (...args) => getIpfs().files.cat(...args),
            bufferToJson
          )
        ), opts),
        catPullStream: expose('ipfs.files.catPullStream', pre(
          preBufferFromJson(0),
          preCidFromJson(0),
          opts.pre('files.catPullStream'),
          post(
            (...args) => getIpfs().files.catPullStream(...args),
            (res) => new Promise(resolve => {
              const readFnName = shortid()

              pull(
                res,
                PMS.sink(readFnName, Object.assign({}, opts, {
                  post (res) {
                    if (isBuffer(res.data)) {
                      res.data = bufferToJson(res.data)
                    }

                    return res
                  }
                }))
              )

              resolve(functionToJson(readFnName))
            })
          )
        ), opts)
      }
    }