From 4bce6aa0b9a9a57c80c5c2713290f13a9ee5250f Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 18 Feb 2020 07:45:26 -0500 Subject: [PATCH] chore: use libp2p utils (#559) * chore: use libp2p utils * chore: update libp2p-utils --- package.json | 1 + src/circuit/index.js | 2 +- src/circuit/stream-to-conn.js | 49 ----------------------------------- 3 files changed, 2 insertions(+), 50 deletions(-) delete mode 100644 src/circuit/stream-to-conn.js diff --git a/package.json b/package.json index ec5202ae..3df70117 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "latency-monitor": "~0.2.1", "libp2p-crypto": "^0.17.1", "libp2p-interfaces": "^0.2.3", + "libp2p-utils": "^0.1.2", "mafmt": "^7.0.0", "merge-options": "^2.0.0", "moving-average": "^1.0.0", diff --git a/src/circuit/index.js b/src/circuit/index.js index e55846d6..a5451d06 100644 --- a/src/circuit/index.js +++ b/src/circuit/index.js @@ -10,13 +10,13 @@ const { CircuitRelay: CircuitPB } = require('./protocol') const debug = require('debug') const log = debug('libp2p:circuit') log.error = debug('libp2p:circuit:error') +const toConnection = require('libp2p-utils/src/stream-to-ma-conn') const { relay: multicodec } = require('./multicodec') const createListener = require('./listener') const { handleCanHop, handleHop, hop } = require('./circuit/hop') const { handleStop } = require('./circuit/stop') const StreamHandler = require('./circuit/stream-handler') -const toConnection = require('./stream-to-conn') class Circuit { /** diff --git a/src/circuit/stream-to-conn.js b/src/circuit/stream-to-conn.js deleted file mode 100644 index d9040716..00000000 --- a/src/circuit/stream-to-conn.js +++ /dev/null @@ -1,49 +0,0 @@ -'use strict' - -const abortable = require('abortable-iterator') -const log = require('debug')('libp2p:circuit:stream') - -// Convert a duplex iterable into a MultiaddrConnection -// https://github.com/libp2p/interface-transport#multiaddrconnection -module.exports = ({ stream, remoteAddr, localAddr }, options = {}) => { - const { sink, source } = stream - const maConn = { - async sink (source) { - if (options.signal) { - source = abortable(source, options.signal) - } - - try { - await sink(source) - } catch (err) { - // If aborted we can safely ignore - if (err.type !== 'aborted') { - // If the source errored the socket will already have been destroyed by - // toIterable.duplex(). If the socket errored it will already be - // destroyed. There's nothing to do here except log the error & return. - log(err) - } - } - close() - }, - - source: options.signal ? abortable(source, options.signal) : source, - conn: stream, - localAddr, - remoteAddr, - timeline: { open: Date.now() }, - - close () { - sink([]) - close() - } - } - - function close () { - if (!maConn.timeline.close) { - maConn.timeline.close = Date.now() - } - } - - return maConn -}