How to use prepost - 9 common examples

To help you get started, we’ve selected a few prepost examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ipfs-shipyard / ipfs-postmsg-proxy / src / server / pubsub.js View on Github external
peers: expose('ipfs.pubsub.peers', pre(
      opts.pre('pubsub.peers'),
      (...args) => getIpfs().pubsub.peers(...args)
    ), opts),
    ls: expose('ipfs.pubsub.ls', 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 because window.postMessage is asynchronous.
      (...args) => new Promise((resolve) => setTimeout(() => resolve(args))),
      opts.pre('pubsub.ls'),
      (...args) => getIpfs().pubsub.ls(...args)
    ), opts)
  }

  // Clean up any subscriptions on close
  api.subscribe.close = pre(
    (...args) => {
      subs.forEach((sub) => getIpfs().pubsub.unsubscribe(sub.topic, sub.rpc.stubFn))
      return args
    },
    api.subscribe.close
  )

  return api
}
github ipfs-shipyard / ipfs-postmsg-proxy / src / server / files / mv.js View on Github external
export default function (getIpfs, opts) {
  return {
    mv: expose('ipfs.files.mv', pre(
      opts.pre('files.mv'),
      post(
        (...args) => getIpfs().files.mv(...args),
        () => null
      )
    ), opts)
  }
}
github ipfs-shipyard / ipfs-postmsg-proxy / src / server / files / ls.js View on Github external
export default function (getIpfs, opts) {
  return {
    ls: expose('ipfs.files.ls', pre(
      opts.pre('files.ls'),
      (...args) => getIpfs().files.ls(...args)
    ), opts)
  }
}
github ipfs-shipyard / ipfs-postmsg-proxy / src / server / files / cp.js View on Github external
export default function (getIpfs, opts) {
  return {
    cp: expose('ipfs.files.cp', pre(
      opts.pre('files.cp'),
      post(
        (...args) => getIpfs().files.cp(...args),
        () => null
      )
    ), opts)
  }
}
github ipfs-shipyard / ipfs-postmsg-proxy / src / server / files / rm.js View on Github external
export default function (getIpfs, opts) {
  return {
    rm: expose('ipfs.files.rm', pre(
      opts.pre('files.rm'),
      post(
        (...args) => getIpfs().files.rm(...args),
        () => null
      )
    ), opts)
  }
}
github ipfs-shipyard / ipfs-postmsg-proxy / src / server / files / mkdir.js View on Github external
export default function (getIpfs, opts) {
  return {
    mkdir: expose('ipfs.files.mkdir', pre(
      opts.pre('files.mkdir'),
      post(
        (...args) => getIpfs().files.mkdir(...args),
        () => null
      )
    ), opts)
  }
}
github ipfs-shipyard / ipfs-postmsg-proxy / src / client / pubsub.js View on Github external
subscribe: function (topic, options, handler, cb) {
      let sub

      if (typeof options === 'function') {
        cb = handler
        handler = options
        options = {}
      }

      const stub = pre(
        (...args) => {
          const handlerIndex = args.length === 3 ? 2 : 1
          const fnName = `ipfs.pubsub.subscribe.handler.${shortid()}`

          sub = {
            topic,
            handler,
            rpc: {
              fnName,
              exposedFn: expose(fnName, pre(
                (...args) => {
                  if (args[0]) {
                    if (isBufferJson(args[0].data)) {
                      args[0].data = bufferFromJson(args[0].data)
                    }
github ipfs-shipyard / ipfs-postmsg-proxy / src / client / pubsub.js View on Github external
sub.rpc.exposedFn.close()
              subs.splice(subs.indexOf(sub), 1)
              throw err
            })
        }
      )

      if (cb) {
        stub(topic, options, handler)
          .then((res) => process.nextTick(() => cb(null, res)))
          .catch((err) => process.nextTick(() => cb(err)))
      } else {
        return stub(topic, options, handler)
      }
    },
    unsubscribe: pre(
      (...args) => {
        const topic = args[0]
        const sub = subs.find((s) => s.topic === topic && s.handler === args[1])

        if (sub) {
          args[1] = functionToJson(sub.rpc.fnName)
          sub.rpc.exposedFn.close()
          subs.splice(subs.indexOf(sub), 1)
        }

        return args
      },
      caller('ipfs.pubsub.unsubscribe', opts)
    ),
    peers: callbackify.variadic(caller('ipfs.pubsub.peers', opts)),
    ls: callbackify.variadic(caller('ipfs.pubsub.ls', opts)),
github ipfs-shipyard / ipfs-postmsg-proxy / src / server / pubsub.js View on Github external
(...args) => {
          const topic = args[0]
          const handlerIndex = args.length === 3 ? 2 : 1

          if (isFunctionJson(args[handlerIndex])) {
            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[handlerIndex].name, opts)

prepost

Alter arguments and return values before and after a function is called

MIT
Latest version published 7 years ago

Package Health Score

42 / 100
Full package analysis

Popular prepost functions