mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
chore: use libp2p utils (#559)
* chore: use libp2p utils * chore: update libp2p-utils
This commit is contained in:
parent
4d11edd62c
commit
4bce6aa0b9
@ -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",
|
||||
|
@ -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 {
|
||||
/**
|
||||
|
@ -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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user