How to use the libp2p-interfaces/src/connection.Connection function in libp2p-interfaces

To help you get started, we’ve selected a few libp2p-interfaces 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 / test / utils / mockConnection.js View on Github external
const mss = new Multistream.Dialer(muxedStream)
    try {
      const { stream, protocol } = await mss.select(protocols)
      return { stream: { ...muxedStream, ...stream }, protocol }
    } catch (err) {
      throw errCode(err, codes.ERR_UNSUPPORTED_PROTOCOL)
    }
  }

  // Pipe all data through the muxer
  pipe(maConn, muxer, maConn)

  maConn.timeline.upgraded = Date.now()

  // Create the connection
  const connection = new Connection({
    localAddr: maConn.localAddr,
    remoteAddr: maConn.remoteAddr,
    localPeer: localPeer,
    remotePeer: remotePeer,
    stat: {
      direction,
      timeline: maConn.timeline,
      multiplexer: Muxer.multicodec,
      encryption: 'N/A'
    },
    newStream,
    getStreams: () => muxer.streams,
    close: err => maConn.close(err)
  })

  return connection
github libp2p / js-libp2p / src / circuit / circuit / stop.js View on Github external
], (err) => {
      if (err) {
        // we don't return the error here,
        // since multistream select don't expect one
        callback()
        return log(err)
      }

      const peerInfo = new PeerInfo(this.utils.peerIdFromId(msg.srcPeer.id))
      msg.srcPeer.addrs.forEach((addr) => peerInfo.multiaddrs.add(addr))
      const newConn = new Connection(sh.rest())
      newConn.setPeerInfo(peerInfo)
      setImmediate(() => this.emit('connection', newConn))
      callback(newConn)
    })
  }
github libp2p / js-libp2p / src / upgrader.js View on Github external
// Pipe all data through the muxer
    pipe(muxedConnection, muxer, muxedConnection)

    maConn.timeline.upgraded = Date.now()
    const timelineProxy = new Proxy(maConn.timeline, {
      set: (...args) => {
        if (args[1] === 'close' && args[2]) {
          this.onConnectionEnd(connection)
        }

        return Reflect.set(...args)
      }
    })

    // Create the connection
    const connection = new Connection({
      localAddr: maConn.localAddr,
      remoteAddr: maConn.remoteAddr,
      localPeer: this.localPeer,
      remotePeer: remotePeer,
      stat: {
        direction,
        timeline: timelineProxy,
        multiplexer: Muxer.multicodec,
        encryption: cryptoProtocol
      },
      newStream,
      getStreams: () => muxer.streams,
      close: err => maConn.close(err)
    })

    this.onConnection(connection)
github libp2p / js-libp2p / src / switch / limit-dialer / queue.js View on Github external
log('work:cancel')
        // clean up already done dials
        pull(empty(), conn)
        // If we can close the connection, do it
        if (typeof conn.close === 'function') {
          return conn.close((_) => callback(null))
        }
        return callback(null)
      }

      // one is enough
      token.cancel = true

      log('work:success')

      const proxyConn = new Connection()
      proxyConn.setInnerConn(conn)
      callback(null, { multiaddr: addr, conn: conn })
    })
  }
github libp2p / js-libp2p / src / switch / observe-connection.js View on Github external
setPeerInfo.call(connection, pi)
        resolve(pi)
      }
    })
  })

  const stream = {
    source: pull(
      connection,
      observer.incoming(transport, protocol, peerInfo)),
    sink: pull(
      observer.outgoing(transport, protocol, peerInfo),
      connection)
  }

  return new Connection(stream, connection)
}