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 62
62
" greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>" ,
63
63
" npm-to-cdn-bot (by Forbes Lindesay) <npmcdn-to-unpkg-bot@users.noreply.github.com>"
64
64
]
65
- }
65
+ }
Original file line number Diff line number Diff line change @@ -106,7 +106,7 @@ Multiaddr.prototype.inspect = function inspect () {
106
106
Multiaddr . prototype . protos = function protos ( ) {
107
107
return map ( this . protoCodes ( ) , function ( code ) {
108
108
return extend ( protocols ( code ) )
109
- // copy to prevent users from modifying the internal objs.
109
+ // copy to prevent users from modifying the internal objs.
110
110
} )
111
111
}
112
112
@@ -235,27 +235,28 @@ Multiaddr.prototype.decapsulate = function decapsulate (addr) {
235
235
/**
236
236
* Extract the peerId if the multiaddr contains one
237
237
*
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
239
239
* @example
240
240
* const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string')
241
241
* // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string>
242
242
*
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()
250
245
*/
251
246
Multiaddr . 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
+ }
257
259
258
- bs58 . decode ( b58str )
259
260
return b58str
260
261
}
261
262
Original file line number Diff line number Diff line change @@ -535,6 +535,22 @@ describe('helpers', () => {
535
535
} )
536
536
} )
537
537
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
+
538
554
describe ( 'multiaddr.isMultiaddr' , ( ) => {
539
555
it ( 'handles different inputs' , ( ) => {
540
556
expect ( multiaddr . isMultiaddr ( multiaddr ( '/' ) ) ) . to . be . eql ( true )
You can’t perform that action at this time.
0 commit comments