How to use the multistream-select.Dialer function in multistream-select

To help you get started, we’ve selected a few multistream-select 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 libp2p / js-libp2p-switch / src / dial.js View on Github external
_attemptMuxerUpgrade (connection, b58Id, callback, abort) {
    const muxers = Object.keys(this.switch.muxers)

    if (muxers.length === 0) {
      return callback(new Error('no muxers available'))
    }

    const msDialer = new multistream.Dialer()
    handleSafe(msDialer, connection, (err) => {
      if (err) {
        return callback(new Error('multistream not supported'))
      }

      // 1. try to handshake in one of the muxers available
      // 2. if succeeds
      //  - add the muxedConn to the list of muxedConns
      //  - add incomming new streams to connHandler
      const nextMuxer = (key) => {
        log('selecting %s', key)
        selectSafe(msDialer, key, (err, _conn) => {
          if (err) {
            if (muxers.length === 0) {
              return callback(new Error('could not upgrade to stream muxing'))
            }
github libp2p / js-libp2p-switch / src / dial.js View on Github external
_performProtocolHandshake (connection, callback) {
    // If there is no protocol set yet, don't perform the handshake
    if (!this.protocol) {
      callback()
    }

    const msDialer = new multistream.Dialer()
    handleSafe(msDialer, connection, (err) => {
      if (err) {
        return callback(err)
      }

      selectSafe(msDialer, this.protocol, (err, _conn) => {
        if (err) {
          log(`could not perform protocol handshake: `, err)
          return callback(err)
        }
        const conn = observeConnection(null, this.protocol, _conn, this.switch.observer)
        callback(null, conn)
      }, callback)
    }, callback)
  }
}
github libp2p / js-libp2p / src / switch / connection / index.js View on Github external
_onUpgrading () {
    const muxers = Object.keys(this.switch.muxers)
    this.log('upgrading connection to %s', this.theirB58Id)

    if (muxers.length === 0) {
      return this._state('stop')
    }

    const msDialer = new multistream.Dialer()
    msDialer.handle(this.conn, (err) => {
      if (err) {
        return this._didUpgrade(err)
      }

      // 1. try to handshake in one of the muxers available
      // 2. if succeeds
      //  - add the muxedConn to the list of muxedConns
      //  - add incomming new streams to connHandler
      const nextMuxer = (key) => {
        this.log('selecting %s', key)
        msDialer.select(key, (err, _conn) => {
          if (err) {
            if (muxers.length === 0) {
              return this._didUpgrade(err)
            }
github libp2p / js-libp2p / src / switch / connection / index.js View on Github external
_protocolHandshake (protocol, connection, callback) {
    const msDialer = new multistream.Dialer()
    msDialer.handle(connection, (err) => {
      if (err) {
        return callback(err, null)
      }

      msDialer.select(protocol, (err, _conn) => {
        if (err) {
          this.log('could not perform protocol handshake:', err)
          return callback(err, null)
        }

        const conn = observeConnection(null, protocol, _conn, this.switch.observer)
        this.log('successfully performed handshake of %s to %s', protocol, this.theirB58Id)
        this.emit('connection', conn)
        callback(null, conn)
      })
github libp2p / js-libp2p / src / upgrader.js View on Github external
async _multiplexOutbound (connection, muxers) {
    const dialer = new Multistream.Dialer(connection)
    const protocols = Array.from(muxers.keys())
    log('outbound selecting muxer %s', protocols)
    try {
      const { stream, protocol } = await dialer.select(protocols)
      log('%s selected as muxer protocol', protocol)
      const Muxer = muxers.get(protocol)
      return { stream, Muxer }
    } catch (err) {
      throw errCode(err, codes.ERR_MUXER_UNAVAILABLE)
    }
  }
github libp2p / js-libp2p-switch / src / connection / manager.js View on Github external
conn.getPeerInfo = async (callback) => {
            const conn = muxedConn.newStream()
            const ms = new multistream.Dialer()
            callback = once(callback)

            let results
            try {
              await msHandle(ms, conn)
              const msConn = await msSelect(ms, identify.multicodec)
              results = await identifyDialer(msConn, cryptoPI)
            } catch (err) {
              return muxedConn.end(() => {
                callback(err, null)
              })
            }

            const { peerInfo } = results

            if (peerInfo) {
github libp2p / js-libp2p / src / switch-next / multiplex.js View on Github external
async function multiplexOutbound (connection, muxers) {
  const dialer = new MSS.Dialer(connection)
  const protocols = Array.from(muxers.keys())
  log('outbound selecting muxer %s', protocols)
  const { stream, protocol } = await dialer.select(protocols)
  log('%s selected as muxer protocol', protocol)
  const Muxer = muxers.get(protocol)

  if (stream) return { stream, Muxer }

  throw new Error('All muxing failed')
}
github libp2p / js-libp2p / src / switch / connection / manager.js View on Github external
conn.getPeerInfo = async (callback) => {
            const conn = muxedConn.newStream()
            const ms = new multistream.Dialer()
            callback = once(callback)

            let results
            try {
              await msHandle(ms, conn)
              const msConn = await msSelect(ms, identify.multicodec)
              results = await identifyDialer(msConn, cryptoPI)
            } catch (err) {
              return muxedConn.end(() => {
                callback(err, null)
              })
            }

            const { peerInfo } = results

            if (peerInfo) {
github libp2p / js-libp2p-switch / src / dial.js View on Github external
_encryptConnection (connection, callback) {
    const msDialer = new multistream.Dialer()
    handleSafe(msDialer, connection, (err) => {
      if (err) {
        return callback(err)
      }

      const myId = this.switch._peerInfo.id
      log('selecting crypto: %s', this.switch.crypto.tag)

      selectSafe(msDialer, this.switch.crypto.tag, (err, _conn) => {
        if (err) {
          return callback(err)
        }

        const conn = observeConnection(null, this.switch.crypto.tag, _conn, this.switch.observer)

        const encryptedConnection = this.switch.crypto.encrypt(myId, conn, this.peerInfo.id, (err) => {

multistream-select

JavaScript implementation of multistream-select

MIT
Latest version published 3 years ago

Package Health Score

48 / 100
Full package analysis