This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: avoid sync callback in async functions (#297)
* fix: avoid sync callback in async functions * test: add error check * refactor: clean up async usage * chore: clean up * refactor: remove async waterfall usage on identify * chore: fix linting
- Loading branch information
Showing
3 changed files
with
76 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict' | ||
|
||
const Identify = require('libp2p-identify') | ||
|
||
/** | ||
* For a given multistream, registers to handle the given connection | ||
* @param {MultistreamDialer} multistream | ||
* @param {Connection} connection | ||
* @returns {Promise} | ||
*/ | ||
module.exports.msHandle = (multistream, connection) => { | ||
return new Promise((resolve, reject) => { | ||
multistream.handle(connection, (err) => { | ||
if (err) return reject(err) | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* For a given multistream, selects the given protocol | ||
* @param {MultistreamDialer} multistream | ||
* @param {string} protocol | ||
* @returns {Promise} Resolves the selected Connection | ||
*/ | ||
module.exports.msSelect = (multistream, protocol) => { | ||
return new Promise((resolve, reject) => { | ||
multistream.select(protocol, (err, connection) => { | ||
if (err) return reject(err) | ||
resolve(connection) | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* Runs identify for the given connection and verifies it against the | ||
* PeerInfo provided | ||
* @param {Connection} connection | ||
* @param {PeerInfo} cryptoPeerInfo The PeerInfo determined during crypto exchange | ||
* @returns {Promise} Resolves {peerInfo, observedAddrs} | ||
*/ | ||
module.exports.identifyDialer = (connection, cryptoPeerInfo) => { | ||
return new Promise((resolve, reject) => { | ||
Identify.dialer(connection, cryptoPeerInfo, (err, peerInfo, observedAddrs) => { | ||
if (err) return reject(err) | ||
resolve({ peerInfo, observedAddrs }) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters