All files / server pubsub.js

20.75% Statements 11/53
0% Branches 0/14
13.33% Functions 2/15
23.4% Lines 11/47

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 1211x 1x 1x 1x     39x                       39x                                                                                                                                                                                     39x 36x 36x   36x         39x    
import { expose, caller } from 'postmsg-rpc'
import { pre } from 'prepost'
import { isFunctionJson } from '../serialization/function'
import { isBuffer, bufferToJson, preBufferFromJson } from '../serialization/buffer'
 
export default function (getIpfs, opts) {
  const subs = [
  /*
    {
      topic,    // name of the topic subscribed to
      rpc: {    // details of the RPC stub created to send updates
        fnName, // the RPC function name the stub calls
        stubFn  // the RPC stub function created by postmsg-rpc
      }
    }
  */
  ]
 
  const api = {
    publish: expose('ipfs.pubsub.publish', pre(
      preBufferFromJson(1),
      opts.pre('pubsub.publish'),
      (...args) => getIpfs().pubsub.publish(...args)
    ), opts),
    subscribe: expose('ipfs.pubsub.subscribe', function (...args) {
      let sub
 
      return pre(
        (...args) => {
          const topic = args[0]
 
          if (isFunctionJson(args[1])) {
            const stubFn = pre(
              (...args) => {
                if (args[0]) {
                  args[0] = Object.assign({}, args[0])
 
                  if (isBuffer(args[0].data)) {
                    args[0].data = bufferToJson(args[0].data)
                  }
 
                  if (isBuffer(args[0].seqno)) {
                    args[0].seqno = bufferToJson(args[0].seqno)
                  }
                }
 
                return args
              },
              caller(args[1].name, opts)
            )
 
            sub = {
              topic,
              rpc: {
                fnName: args[1].name,
                stubFn
              }
            }
 
            subs.push(sub)
 
            args[1] = stubFn
          }
 
          return args
        },
        opts.pre('pubsub.subscribe'),
        (...args) => {
          return getIpfs().pubsub.subscribe(...args)
            .catch((err) => {
              subs.splice(subs.indexOf(sub), 1)
              throw err
            })
        }
      )(...args)
    }, opts),
    unsubscribe: expose('ipfs.pubsub.unsubscribe', pre(
      (...args) => {
        const topic = args[0]
 
        if (isFunctionJson(args[1])) {
          const sub = subs.find((s) => s.topic === topic && s.rpc.fnName === args[1].name)
 
          if (sub) {
            args[1] = sub.rpc.stubFn
            subs.splice(subs.indexOf(sub), 1)
          } else {
            // Well, we don't have a reference to it, so the ipfs node won't
            // either. We shouldn't error either because ipfs won't.
            args[1] = () => {}
          }
        }
 
        return args
      },
      opts.pre('pubsub.unsubscribe'),
      (...args) => getIpfs().pubsub.unsubscribe(...args)
    ), opts),
    peers: expose('ipfs.pubsub.peers', pre(
      opts.pre('pubsub.peers'),
      (...args) => getIpfs().pubsub.peers(...args)
    ), opts),
    ls: expose('ipfs.pubsub.ls', pre(
      opts.pre('pubsub.ls'),
      (...args) => getIpfs().pubsub.ls(...args)
    ), opts)
  }
 
  // Clean up any subscriptions on close
  api.subscribe.close = pre(
    (...args) => {
      return Promise.all(
        subs.map((sub) => getIpfs().pubsub.unsubscribe(sub.topic, sub.rpc.stubFn))
      ).then(() => args)
    },
    api.subscribe.close
  )
 
  return api
}