# All files / lib/server pubsub.js

93.75% Statements 45/48
62.5% Branches 5/8
93.33% Functions 14/15
93.75% Lines 45/48

    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
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132

    1x

    1x
    19x

    19x

    11014x

    1027x
    3081x

    1027x

    1027x
    1027x
    3081x

    1027x
    1027x

    1027x
    1027x

    1027x

    1027x

    1027x

    1027x

    1027x

    1027x
    2054x

    1027x

    1027x
    1027x
    1027x

    1027x
    1027x

    1027x

    1027x
    2054x

    1027x

    1027x
    1027x

    8x

    9x

    9x
    9x
    9x

    9x

    19x

    1x

    1x

    1x

    1x'use strict';

    Object.defineProperty(exports, "__esModule", {
      value: true
    });

    exports.default = function (getIpfs, opts) {
      var 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
            }
          }
        */
      ];

      var api = {
        publish: (0, _postmsgRpc.expose)('ipfs.pubsub.publish', (0, _prepost.pre)((0, _buffer.preBufferFromJson)(1), opts.pre['pubsub.publish'], function () {
          var _getIpfs$pubsub;

          return (_getIpfs$pubsub = getIpfs().pubsub).publish.apply(_getIpfs$pubsub, arguments);
        }), opts),
        subscribe: (0, _postmsgRpc.expose)('ipfs.pubsub.subscribe', function () {
          for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
            args[_key] = arguments[_key];
          }

          var sub = void 0;

          return (0, _prepost.pre)(function () {
            for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
              args[_key2] = arguments[_key2];
            }

            var topic = args[0];
            var handlerIndex = args.length === 3 ? 2 : 1;

            Eif ((0, _function.isFunctionJson)(args[handlerIndex])) {
              var stubFn = (0, _postmsgRpc.caller)(args[handlerIndex].name, opts);

              sub = {
                topic: topic,
                rpc: {
                  fnName: args[handlerIndex].name,
                  stubFn: stubFn
                }
              };

              subs.push(sub);

              args[handlerIndex] = stubFn;
            }

            return args;
          }, opts.pre['pubsub.subscribe'], function () {
            var _getIpfs$pubsub2;

            return (_getIpfs$pubsub2 = getIpfs().pubsub).subscribe.apply(_getIpfs$pubsub2, arguments).catch(function (err) {
              subs.splice(subs.indexOf(sub), 1);
              throw err;
            });
          }).apply(undefined, args);
        }, opts),
        unsubscribe: (0, _postmsgRpc.expose)('ipfs.pubsub.unsubscribe', (0, _prepost.pre)(function () {
          for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
            args[_key3] = arguments[_key3];
          }

          var topic = args[0];

          Eif ((0, _function.isFunctionJson)(args[1])) {
            var sub = subs.find(function (s) {
              return s.topic === topic && s.rpc.fnName === args[1].name;
            });
            args[1] = sub.rpc.stubFn;
            subs.splice(subs.indexOf(sub), 1);
          }

          return args;
        }, opts.pre['pubsub.unsubscribe'], function () {
          for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
            args[_key4] = arguments[_key4];
          }

          return new Promise(function (resolve) {
            var _getIpfs$pubsub3;

            (_getIpfs$pubsub3 = getIpfs().pubsub).unsubscribe.apply(_getIpfs$pubsub3, args);
            resolve();
          });
        }), opts),
        peers: (0, _postmsgRpc.expose)('ipfs.pubsub.peers', (0, _prepost.pre)(opts.pre['pubsub.peers'], function () {
          var _getIpfs$pubsub4;

          return (_getIpfs$pubsub4 = getIpfs().pubsub).peers.apply(_getIpfs$pubsub4, arguments);
        }), opts),
        ls: (0, _postmsgRpc.expose)('ipfs.pubsub.ls', (0, _prepost.pre)(
        // FIXME: The interface-ipfs-core tests call ls straight after unsubscribe.
        //
        // Unsubscribe in js-ipfs is synchronous but it HAS to be async in the
        // proxy partly because of the way it is coded but mostly because
        // window.postMessage is asynchronous.
        function () {
          for (var _len5 = arguments.length, args = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
            args[_key5] = arguments[_key5];
          }

          return new Promise(function (resolve) {
            return setTimeout(function () {
              return resolve(args);
            });
          });
        }, opts.pre['pubsub.ls'], function () {
          var _getIpfs$pubsub5;

          return (_getIpfs$pubsub5 = getIpfs().pubsub).ls.apply(_getIpfs$pubsub5, arguments);
        }), opts)
      };

      return api;
    };

    var _postmsgRpc = require('postmsg-rpc');

    var _prepost = require('prepost');

    var _function = require('../serialization/function');

    var _buffer = require('../serialization/buffer');