mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-04-24 20:02:20 +00:00
chore: fix transport docs and imports
This commit is contained in:
parent
13844d482c
commit
3230ae4ead
@ -1,14 +1,6 @@
|
||||
interface-transport
|
||||
===================
|
||||
|
||||
[](http://protocol.ai)
|
||||
[](http://libp2p.io/)
|
||||
[](http://webchat.freenode.net/?channels=%23libp2p)
|
||||
[](https://discuss.libp2p.io)
|
||||
[](https://travis-ci.com/libp2p/interface-transport)
|
||||
[](https://david-dm.org/libp2p/interface-transport)
|
||||
[](https://github.com/feross/standard)
|
||||
|
||||
> A test suite and interface you can use to implement a libp2p transport. A libp2p transport is understood as something that offers a dial and listen interface.
|
||||
|
||||
The primary goal of this module is to enable developers to pick and swap their transport module as they see fit for their libp2p installation, without having to go through shims or compatibility issues. This module and test suite were heavily inspired by abstract-blob-store, interface-stream-muxer and others.
|
||||
@ -17,10 +9,6 @@ Publishing a test suite as a module lets multiple modules all ensure compatibili
|
||||
|
||||
The purpose of this interface is not to reinvent any wheels when it comes to dialing and listening to transports. Instead, it tries to provide a uniform API for several transports through a shimmed interface.
|
||||
|
||||
## Lead Maintainer
|
||||
|
||||
[Jacob Heun](https://github.com/jacobheun/)
|
||||
|
||||
# Modules that implement the interface
|
||||
|
||||
- [js-libp2p-tcp](https://github.com/libp2p/js-libp2p-tcp)
|
||||
@ -45,7 +33,7 @@ Include this badge in your readme if you make a module that is compatible with t
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const tests = require('interface-transport')
|
||||
const tests = require('libp2p-interfaces/src/transport/tests')
|
||||
const multiaddr = require('multiaddr')
|
||||
const YourTransport = require('../src')
|
||||
|
||||
@ -147,7 +135,7 @@ This method uses a transport to dial a Peer listening on `multiaddr`.
|
||||
|
||||
`[options]` the options that may be passed to the dial. Must support the `signal` option (see below)
|
||||
|
||||
Dial **MUST** call and return `upgrader.upgradeOutbound(multiaddrConnection)`. The upgrader will return an [interface-connection](https://github.com/libp2p/interface-connection) instance.
|
||||
Dial **MUST** call and return `upgrader.upgradeOutbound(multiaddrConnection)`. The upgrader will return an [interface-connection](../connection) instance.
|
||||
|
||||
The dial may throw an `Error` instance if there was a problem connecting to the `multiaddr`.
|
||||
|
||||
@ -157,7 +145,7 @@ Dials may be cancelled using an `AbortController`:
|
||||
|
||||
```Javascript
|
||||
const AbortController = require('abort-controller')
|
||||
const { AbortError } = require('interface-transport')
|
||||
const { AbortError } = require('libp2p-interfaces/src/transport/errors')
|
||||
const controller = new AbortController()
|
||||
try {
|
||||
const conn = await mytransport.dial(ma, { signal: controller.signal })
|
||||
@ -193,7 +181,7 @@ This method creates a listener on the transport. Implementations **MUST** call `
|
||||
|
||||
`options` is an optional object that contains the properties the listener must have, in order to properly listen on a given transport/socket.
|
||||
|
||||
`handlerFunction` is a function called each time a new connection is received. It must follow the following signature: `function (conn) {}`, where `conn` is a connection that follows the [`interface-connection`](https://github.com/diasdavid/interface-connection).
|
||||
`handlerFunction` is a function called each time a new connection is received. It must follow the following signature: `function (conn) {}`, where `conn` is a connection that follows the [`interface-connection`](../connection).
|
||||
|
||||
The listener object created may emit the following events:
|
||||
|
||||
|
@ -1,80 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const { Connection } = require('interface-connection')
|
||||
const toPull = require('async-iterator-to-pull-stream')
|
||||
const error = require('pull-stream/sources/error')
|
||||
const drain = require('pull-stream/sinks/drain')
|
||||
const noop = () => {}
|
||||
|
||||
function callbackify (fn) {
|
||||
return async function (...args) {
|
||||
let cb = args.pop()
|
||||
if (typeof cb !== 'function') {
|
||||
args.push(cb)
|
||||
cb = noop
|
||||
}
|
||||
let res
|
||||
try {
|
||||
res = await fn(...args)
|
||||
} catch (err) {
|
||||
return cb(err)
|
||||
}
|
||||
cb(null, res)
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy adapter to old transport & connection interface
|
||||
class Adapter {
|
||||
constructor (transport) {
|
||||
this.transport = transport
|
||||
}
|
||||
|
||||
dial (ma, options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
callback = callback || noop
|
||||
|
||||
const conn = new Connection()
|
||||
|
||||
this.transport.dial(ma, options)
|
||||
.then(socket => {
|
||||
conn.setInnerConn(toPull.duplex(socket))
|
||||
conn.getObservedAddrs = callbackify(socket.getObservedAddrs.bind(socket))
|
||||
conn.close = callbackify(socket.close.bind(socket))
|
||||
callback(null, conn)
|
||||
})
|
||||
.catch(err => {
|
||||
conn.setInnerConn({ sink: drain(), source: error(err) })
|
||||
callback(err)
|
||||
})
|
||||
|
||||
return conn
|
||||
}
|
||||
|
||||
createListener (options, handler) {
|
||||
if (typeof options === 'function') {
|
||||
handler = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
const server = this.transport.createListener(options, socket => {
|
||||
const conn = new Connection(toPull.duplex(socket))
|
||||
conn.getObservedAddrs = callbackify(socket.getObservedAddrs.bind(socket))
|
||||
handler(conn)
|
||||
})
|
||||
|
||||
const proxy = {
|
||||
listen: callbackify(server.listen.bind(server)),
|
||||
close: callbackify(server.close.bind(server)),
|
||||
getAddrs: callbackify(server.getAddrs.bind(server)),
|
||||
getObservedAddrs: callbackify(() => server.getObservedAddrs())
|
||||
}
|
||||
|
||||
return new Proxy(server, { get: (_, prop) => proxy[prop] || server[prop] })
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Adapter
|
@ -12,6 +12,3 @@ module.exports = (common) => {
|
||||
filter(common)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports.AbortError = require('../errors').AbortError
|
||||
module.exports.Adapter = require('../adapter')
|
||||
|
Loading…
x
Reference in New Issue
Block a user