File tree Expand file tree Collapse file tree 3 files changed +33
-16
lines changed
Expand file tree Collapse file tree 3 files changed +33
-16
lines changed Original file line number Diff line number Diff line change 6262 " greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>" ,
6363 " npm-to-cdn-bot (by Forbes Lindesay) <npmcdn-to-unpkg-bot@users.noreply.github.com>"
6464 ]
65- }
65+ }
Original file line number Diff line number Diff line change @@ -106,7 +106,7 @@ Multiaddr.prototype.inspect = function inspect () {
106106Multiaddr . prototype . protos = function protos ( ) {
107107 return map ( this . protoCodes ( ) , function ( code ) {
108108 return extend ( protocols ( code ) )
109- // copy to prevent users from modifying the internal objs.
109+ // copy to prevent users from modifying the internal objs.
110110 } )
111111}
112112
@@ -235,27 +235,28 @@ Multiaddr.prototype.decapsulate = function decapsulate (addr) {
235235/**
236236 * Extract the peerId if the multiaddr contains one
237237 *
238- * @return {String } peerId - The id of the peer
238+ * @return {String|null } peerId - The id of the peer or null if invalid or missing from the ma
239239 * @example
240240 * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string')
241241 * // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string>
242242 *
243- * const peerId
244- *
245- * try {
246- * peerId = mh1.getPeerId()
247- * } catch (err) {
248- * // not a valid base58 address
249- * }
243+ * // should return QmValidBase58string or null if the id is missing or invalid
244+ * const peerId = mh1.getPeerId()
250245 */
251246Multiaddr . prototype . getPeerId = function getPeerId ( ) {
252- let b58str = this . stringTuples ( ) . filter ( ( tuple ) => {
253- if ( tuple [ 0 ] === protocols . names [ 'ipfs' ] . code ) {
254- return true
255- }
256- } ) [ 0 ] [ 1 ]
247+ let b58str = null
248+ try {
249+ b58str = this . stringTuples ( ) . filter ( ( tuple ) => {
250+ if ( tuple [ 0 ] === protocols . names [ 'ipfs' ] . code ) {
251+ return true
252+ }
253+ } ) [ 0 ] [ 1 ]
254+
255+ bs58 . decode ( b58str )
256+ } catch ( e ) {
257+ b58str = null
258+ }
257259
258- bs58 . decode ( b58str )
259260 return b58str
260261}
261262
Original file line number Diff line number Diff line change @@ -535,6 +535,22 @@ describe('helpers', () => {
535535 } )
536536 } )
537537
538+ describe ( '.getPeerId should parse id from multiaddr' , ( ) => {
539+ it ( 'parses extracts the peer Id from a multiaddr' , ( ) => {
540+ expect (
541+ multiaddr ( '/p2p-circuit/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' ) . getPeerId ( )
542+ ) . to . equal ( 'QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' )
543+ } )
544+ } )
545+
546+ describe ( '.getPeerId should return null on missing peer id in multiaddr' , ( ) => {
547+ it ( 'parses extracts the peer Id from a multiaddr' , ( ) => {
548+ expect (
549+ multiaddr ( '/ip4/0.0.0.0/tcp/1234/utp' ) . getPeerId ( )
550+ ) . to . be . null ( )
551+ } )
552+ } )
553+
538554 describe ( 'multiaddr.isMultiaddr' , ( ) => {
539555 it ( 'handles different inputs' , ( ) => {
540556 expect ( multiaddr . isMultiaddr ( multiaddr ( '/' ) ) ) . to . be . eql ( true )
You can’t perform that action at this time.
0 commit comments