Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dial protocol should throw if no protocol is provided #914

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exports.codes = {
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
DHT_NOT_STARTED: 'ERR_DHT_NOT_STARTED',
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_FAILED: 'ERR_CONNECTION_FAILED',
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
Expand Down
16 changes: 7 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,26 +462,24 @@ 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`,
* and the `Connection` will be returned
* and the `MuxedStream` will be returned together with the successful negotiated protocol.
*
* @async
* @param {PeerId|Multiaddr|string} peer - The peer to dial
* @param {string[]|string} protocols
* @param {object} [options]
* @param {AbortSignal} [options.signal]
* @returns {Promise<Connection|{ stream: MuxedStream; protocol: string; }>}
* @returns {Promise<{ stream: MuxedStream; protocol: string; }>}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could remove the @returns annotation altogether?

vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
*/
async dialProtocol (peer, protocols, options) {
const connection = await this._dial(peer, options)

// If a protocol was provided, create a new stream
if (protocols && protocols.length) {
return connection.newStream(protocols)
if (!protocols || !protocols.length) {
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
}

return connection
const connection = await this._dial(peer, options)
return connection.newStream(protocols)
}

/**
Expand Down
19 changes: 19 additions & 0 deletions test/dialing/direct.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,25 @@ describe('Dialing (direct, TCP)', () => {
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 () => {
libp2p = new Libp2p({
peerId,
Expand Down