All files / server/files read.js

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

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 441x 1x 1x 1x 1x 1x 1x     39x       2x             4x 4x 4x   4x       4x 2x     4x         4x            
import { expose } from 'postmsg-rpc'
import { pre, post } from 'prepost'
import shortid from 'shortid'
import pull from 'pull-stream'
import PMS from 'pull-postmsg-stream'
import { isBuffer, bufferToJson } from '../../serialization/buffer'
import { functionToJson } from '../../serialization/function'
 
export default function (getIpfs, opts) {
  return {
    read: expose('ipfs.files.read', pre(
      opts.pre('files.read'),
      post(
        (...args) => getIpfs().files.read(...args),
        bufferToJson
      )
    ), opts),
    readPullStream: expose('ipfs.files.readPullStream', pre(
      opts.pre('files.readPullStream'),
      post(
        (...args) => getIpfs().files.readPullStream(...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)
  }
}