Skip to content

Commit 21c9aee

Browse files
authored
fix: dial protocol should throw if no protocol is provided (#914)
BREAKING CHANGE: dialProtocol does not return connection when no protocols are provided
1 parent a93cca9 commit 21c9aee

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ exports.codes = {
1111
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
1212
DHT_NOT_STARTED: 'ERR_DHT_NOT_STARTED',
1313
CONN_ENCRYPTION_REQUIRED: 'ERR_CONN_ENCRYPTION_REQUIRED',
14+
ERR_INVALID_PROTOCOLS_FOR_STREAM: 'ERR_INVALID_PROTOCOLS_FOR_STREAM',
1415
ERR_CONNECTION_ENDED: 'ERR_CONNECTION_ENDED',
1516
ERR_CONNECTION_FAILED: 'ERR_CONNECTION_FAILED',
1617
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',

src/index.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -462,26 +462,23 @@ class Libp2p extends EventEmitter {
462462
}
463463

464464
/**
465-
* Dials to the provided peer and handshakes with the given protocol.
465+
* Dials to the provided peer and tries to handshake with the given protocols in order.
466466
* If successful, the known metadata of the peer will be added to the nodes `peerStore`,
467-
* and the `Connection` will be returned
467+
* and the `MuxedStream` will be returned together with the successful negotiated protocol.
468468
*
469469
* @async
470470
* @param {PeerId|Multiaddr|string} peer - The peer to dial
471471
* @param {string[]|string} protocols
472472
* @param {object} [options]
473473
* @param {AbortSignal} [options.signal]
474-
* @returns {Promise<Connection|{ stream: MuxedStream; protocol: string; }>}
475474
*/
476475
async dialProtocol (peer, protocols, options) {
477-
const connection = await this._dial(peer, options)
478-
479-
// If a protocol was provided, create a new stream
480-
if (protocols && protocols.length) {
481-
return connection.newStream(protocols)
476+
if (!protocols || !protocols.length) {
477+
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
482478
}
483479

484-
return connection
480+
const connection = await this._dial(peer, options)
481+
return connection.newStream(protocols)
485482
}
486483

487484
/**

test/dialing/direct.node.js

+19
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,25 @@ describe('Dialing (direct, TCP)', () => {
352352
await pWaitFor(() => remoteConn.streams.length === 0)
353353
})
354354

355+
it('should throw when using dialProtocol with no protocols', async () => {
356+
libp2p = new Libp2p({
357+
peerId,
358+
modules: {
359+
transport: [Transport],
360+
streamMuxer: [Muxer],
361+
connEncryption: [Crypto]
362+
}
363+
})
364+
365+
await expect(libp2p.dialProtocol(remotePeerId))
366+
.to.eventually.be.rejectedWith(Error)
367+
.and.to.have.property('code', ErrorCodes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
368+
369+
await expect(libp2p.dialProtocol(remotePeerId, []))
370+
.to.eventually.be.rejectedWith(Error)
371+
.and.to.have.property('code', ErrorCodes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
372+
})
373+
355374
it('should be able to use hangup to close connections', async () => {
356375
libp2p = new Libp2p({
357376
peerId,

0 commit comments

Comments
 (0)