# All files / server/files cat.js

50% Statements 9/18
0% Branches 0/2
0% Functions 0/5
50% Lines 9/18

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

    1x

     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)
      }
    }