Assumptions:
- Multistream1.0 is still used.
- Create a switch with orderer lists of: Crypto and Muxers.
- Use
.handle
exclusively for application protocols./dht/
,/pubsub/
,/ipfs/
, etc.?- All protocols, including crypto and muxers, must be registered with multistream
Pseudo Code
connection = getConnection(peer)
connection = privatize(connection)
connection = addMultistream(connection)
theirSupportedProtocols = connection.ls()
connection = encrypt(connection, theirSupportedProtocols, ourCryptoProtocols)
connection = addStreamMuxers(connection, theirSupportedProtocols, ourStreaMuxerProtocols)
if (applicationProtocol) stream = createStream(connection, applicationProtocol, theirSupportedProtocols)
Get Basic Connection
If configured, must immediately attempt to privatize the new connection
Note: js-libp2p-switch currently does not perform an ls. It will add it in a future update, but may need to be changed to support an ls
at this stage of the dial.
- Immediately register the connection with a
Multistream.Dialer
instance - Perform a
multistreamDialer.ls
on the connection to determine supported protocols of peer
If an [#application-protocol](Application Protocol) is provided for the dial, and that protocol is not present in multistreamDialer.ls
, it may warrant closing the connection immediately. If a dial is performed with no protocol, this may likely be a proactive connect for a multipurpose connection. If the protocol is provided, it may likely be for a specific purpose and the lack of support for that protocol may warrant immediate closure of the connection.
- Select the best available encryption protocol
- Perform a
multistreamDialer.select
for that protocol - On failure, try any other available protocols
- If all protocols fail, end the connection immediately
- Future iterations may wish to perform some type of backoff blacklisting for the peer
- If successful, continue on.
- Given the available protocols from Setup Multistream, select the best stream muxer
- Perform a
multistreamDialer.select
for that muxer - On failure, try any other available stream muxer protocols
- If all stream muxer protocols fail, end the connection immediately
- If all protocols fail, degrade to an unmuxed connection.
- Multiple streams cannot be opened on this connection.
- If successful, continue on.
An application protocol is a custom protocol used to register handlers in a given codebase that will be executed when a connection receives a stream prepended with that protocol. Considered another way, this can be considered Endpoints within an application. For example, if a dial is performed with the protocol /ipfs/kad/1.0.0
,
- If no application protocol is known, take no action
- If an application protocol is known:
- Check the protocol list for the peer to determine if the protocol is supported.
- If not supported, error the request. The connection should not be closed, as other protocol requests may still be supported.
- If supported, perform a
multistreamDialer.select
for that protocol.
- If a new stream is created via
multistreamDialer.select
, return the stream, otherwise error.