mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
fix: dial protocol should throw if no protocol is provided (#914)
BREAKING CHANGE: dialProtocol does not return connection when no protocols are provided
This commit is contained in:
parent
a93cca9178
commit
21c9aeecb1
@ -11,6 +11,7 @@ exports.codes = {
|
|||||||
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
|
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
|
||||||
DHT_NOT_STARTED: 'ERR_DHT_NOT_STARTED',
|
DHT_NOT_STARTED: 'ERR_DHT_NOT_STARTED',
|
||||||
CONN_ENCRYPTION_REQUIRED: 'ERR_CONN_ENCRYPTION_REQUIRED',
|
CONN_ENCRYPTION_REQUIRED: 'ERR_CONN_ENCRYPTION_REQUIRED',
|
||||||
|
ERR_INVALID_PROTOCOLS_FOR_STREAM: 'ERR_INVALID_PROTOCOLS_FOR_STREAM',
|
||||||
ERR_CONNECTION_ENDED: 'ERR_CONNECTION_ENDED',
|
ERR_CONNECTION_ENDED: 'ERR_CONNECTION_ENDED',
|
||||||
ERR_CONNECTION_FAILED: 'ERR_CONNECTION_FAILED',
|
ERR_CONNECTION_FAILED: 'ERR_CONNECTION_FAILED',
|
||||||
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
|
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
|
||||||
|
15
src/index.js
15
src/index.js
@ -462,26 +462,23 @@ class Libp2p extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dials to the provided peer and handshakes with the given protocol.
|
* Dials to the provided peer and tries to handshake with the given protocols in order.
|
||||||
* If successful, the known metadata of the peer will be added to the nodes `peerStore`,
|
* If successful, the known metadata of the peer will be added to the nodes `peerStore`,
|
||||||
* and the `Connection` will be returned
|
* and the `MuxedStream` will be returned together with the successful negotiated protocol.
|
||||||
*
|
*
|
||||||
* @async
|
* @async
|
||||||
* @param {PeerId|Multiaddr|string} peer - The peer to dial
|
* @param {PeerId|Multiaddr|string} peer - The peer to dial
|
||||||
* @param {string[]|string} protocols
|
* @param {string[]|string} protocols
|
||||||
* @param {object} [options]
|
* @param {object} [options]
|
||||||
* @param {AbortSignal} [options.signal]
|
* @param {AbortSignal} [options.signal]
|
||||||
* @returns {Promise<Connection|{ stream: MuxedStream; protocol: string; }>}
|
|
||||||
*/
|
*/
|
||||||
async dialProtocol (peer, protocols, options) {
|
async dialProtocol (peer, protocols, options) {
|
||||||
const connection = await this._dial(peer, options)
|
if (!protocols || !protocols.length) {
|
||||||
|
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
||||||
// If a protocol was provided, create a new stream
|
|
||||||
if (protocols && protocols.length) {
|
|
||||||
return connection.newStream(protocols)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return connection
|
const connection = await this._dial(peer, options)
|
||||||
|
return connection.newStream(protocols)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -352,6 +352,25 @@ describe('Dialing (direct, TCP)', () => {
|
|||||||
await pWaitFor(() => remoteConn.streams.length === 0)
|
await pWaitFor(() => remoteConn.streams.length === 0)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should throw when using dialProtocol with no protocols', async () => {
|
||||||
|
libp2p = new Libp2p({
|
||||||
|
peerId,
|
||||||
|
modules: {
|
||||||
|
transport: [Transport],
|
||||||
|
streamMuxer: [Muxer],
|
||||||
|
connEncryption: [Crypto]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
await expect(libp2p.dialProtocol(remotePeerId))
|
||||||
|
.to.eventually.be.rejectedWith(Error)
|
||||||
|
.and.to.have.property('code', ErrorCodes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
||||||
|
|
||||||
|
await expect(libp2p.dialProtocol(remotePeerId, []))
|
||||||
|
.to.eventually.be.rejectedWith(Error)
|
||||||
|
.and.to.have.property('code', ErrorCodes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
||||||
|
})
|
||||||
|
|
||||||
it('should be able to use hangup to close connections', async () => {
|
it('should be able to use hangup to close connections', async () => {
|
||||||
libp2p = new Libp2p({
|
libp2p = new Libp2p({
|
||||||
peerId,
|
peerId,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user