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

100% Statements 27/27
83.33% Branches 5/6
100% Functions 8/8
100% Lines 27/27

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

    23x

    7x
    6x
    15x
    11x

    15x

    2x
    2x
    2x

    2x

    2x
    2x

    2x

    4x
    2x

    4x

    2x

    2x

    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 {
        get: expose('ipfs.files.get', pre(
          preBufferFromJson(0),
          preCidFromJson(0),
          opts.pre('files.get'),
          post(
            (...args) => getIpfs().files.get(...args),
            (files) => files.map((file) => {
              if (file.content) {
                file.content = bufferToJson(file.content)
              }

              return file
            })
          )
        ), opts),
        getPullStream: expose('ipfs.files.getPullStream', pre(
          preBufferFromJson(0),
          preCidFromJson(0),
          opts.pre('files.getPullStream'),
          post(
            (...args) => getIpfs().files.getPullStream(...args),
            (res) => new Promise(resolve => {
              const readFnName = shortid()

              pull(
                res,
                pull.map(file => {
                  Eif (file.content) {
                    const readFnName = shortid()

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

                          return res
                        }
                      }))
                    )

                    file.content = functionToJson(readFnName)
                  }

                  return file
                }),
                PMS.sink(readFnName, opts)
              )

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