Skip to content

Commit

Permalink
feat: export constructor
Browse files Browse the repository at this point in the history
BREAKING CHANGE: module export the Multiaddr class instead of a factory function.
  • Loading branch information
hugomrdias committed Dec 15, 2020
1 parent 89966e5 commit 89766b0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 73 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ the global namespace.
```js
$ node

> const multiaddr = require('multiaddr')
> const Multiaddr = require('multiaddr')

> const addr = multiaddr("/ip4/127.0.0.1/udp/1234")
> const addr = new Multiaddr("/ip4/127.0.0.1/udp/1234")
<Multiaddr /ip4/127.0.0.1/udp/1234>

> addr.bytes
Expand Down Expand Up @@ -102,10 +102,10 @@ https://multiformats.github.io/js-multiaddr/
To provide multiaddr resolvers you can do:

```js
const multiaddr = require('multiaddr')
const Multiaddr = require('multiaddr')
const resolvers = require('multiaddr/src/resolvers')

multiaddr.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)
Multiaddr.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)
```

The available resolvers are:
Expand Down
93 changes: 44 additions & 49 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const uint8ArrayEquals = require('uint8arrays/equals')
const resolvers = new Map()
const symbol = Symbol.for('@multiformats/js-multiaddr/multiaddr')

/**
* @typedef {import('./types').Protocol}Protocol
*/

/**
* Creates a [multiaddr](https://github.com/multiformats/multiaddr) from
* a Uint8Array, String or another Multiaddr instance
Expand All @@ -22,7 +26,7 @@ class Multiaddr {
* to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format)
* @example
* ```js
* Multiaddr('/ip4/127.0.0.1/tcp/4001')
* new Multiaddr('/ip4/127.0.0.1/tcp/4001')
* // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
* ```
*/
Expand Down Expand Up @@ -57,7 +61,7 @@ class Multiaddr {
* @returns {string}
* @example
* ```js
* Multiaddr('/ip4/127.0.0.1/tcp/4001').toString()
* new Multiaddr('/ip4/127.0.0.1/tcp/4001').toString()
* // '/ip4/127.0.0.1/tcp/4001'
* ```
*/
Expand All @@ -71,7 +75,7 @@ class Multiaddr {
* @returns {string}
* @example
* ```js
* JSON.stringify(Multiaddr('/ip4/127.0.0.1/tcp/4001'))
* JSON.stringify(new Multiaddr('/ip4/127.0.0.1/tcp/4001'))
* // '/ip4/127.0.0.1/tcp/4001'
* ```
*/
Expand All @@ -85,7 +89,7 @@ class Multiaddr {
* @returns {{family: string, host: string, transport: string, port: number}}
* @example
* ```js
* Multiaddr('/ip4/127.0.0.1/tcp/4001').toOptions()
* new Multiaddr('/ip4/127.0.0.1/tcp/4001').toOptions()
* // { family: 'ipv4', host: '127.0.0.1', transport: 'tcp', port: 4001 }
* ```
*/
Expand All @@ -107,7 +111,7 @@ class Multiaddr {
* @returns {string}
* @example
* ```js
* Multiaddr('/ip4/127.0.0.1/tcp/4001').inspect()
* new Multiaddr('/ip4/127.0.0.1/tcp/4001').inspect()
* // '<Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>'
* ```
*/
Expand All @@ -129,10 +133,10 @@ class Multiaddr {
* and the size of its address space in bits.
* [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv)
*
* @returns {import('./types').Protocol[]} protocols - All the protocols the address is composed of
* @returns {Protocol[]} protocols - All the protocols the address is composed of
* @example
* ```js
* Multiaddr('/ip4/127.0.0.1/tcp/4001').protos()
* new Multiaddr('/ip4/127.0.0.1/tcp/4001').protos()
* // [ { code: 4, size: 32, name: 'ip4' },
* // { code: 6, size: 16, name: 'tcp' } ]
* ```
Expand All @@ -148,7 +152,7 @@ class Multiaddr {
* @returns {Array<number>} protocol codes
* @example
* ```js
* Multiaddr('/ip4/127.0.0.1/tcp/4001').protoCodes()
* new Multiaddr('/ip4/127.0.0.1/tcp/4001').protoCodes()
* // [ 4, 6 ]
* ```
*/
Expand Down Expand Up @@ -177,7 +181,7 @@ class Multiaddr {
* @returns {Array.<string>} protocol names
* @example
* ```js
* Multiaddr('/ip4/127.0.0.1/tcp/4001').protoNames()
* new Multiaddr('/ip4/127.0.0.1/tcp/4001').protoNames()
* // [ 'ip4', 'tcp' ]
* ```
*/
Expand All @@ -191,7 +195,7 @@ class Multiaddr {
* @returns {[number, (Uint8Array | undefined)?][]} tuples
* @example
* ```js
* Multiaddr("/ip4/127.0.0.1/tcp/4001").tuples()
* new Multiaddr("/ip4/127.0.0.1/tcp/4001").tuples()
* // [ [ 4, <Buffer 7f 00 00 01> ], [ 6, <Buffer 0f a1> ] ]
* ```
*/
Expand All @@ -207,7 +211,7 @@ class Multiaddr {
* @returns {[number, string?][]} tuples
* @example
* ```js
* Multiaddr("/ip4/127.0.0.1/tcp/4001").stringTuples()
* new Multiaddr("/ip4/127.0.0.1/tcp/4001").stringTuples()
* // [ [ 4, '127.0.0.1' ], [ 6, '4001' ] ]
* ```
*/
Expand All @@ -223,10 +227,10 @@ class Multiaddr {
* @returns {Multiaddr}
* @example
* ```js
* const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080')
* const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080')
* // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080>
*
* const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/4001')
* const mh2 = new Multiaddr('/ip4/127.0.0.1/tcp/4001')
* // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
*
* const mh3 = mh1.encapsulate(mh2)
Expand All @@ -248,10 +252,10 @@ class Multiaddr {
* @returns {Multiaddr}
* @example
* ```js
* const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080')
* const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080')
* // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080>
*
* const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/4001')
* const mh2 = new Multiaddr('/ip4/127.0.0.1/tcp/4001')
* // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
*
* const mh3 = mh1.encapsulate(mh2)
Expand Down Expand Up @@ -281,13 +285,13 @@ class Multiaddr {
* @returns {Multiaddr}
* @example
* ```js
* const addr = Multiaddr('/ip4/0.0.0.0/tcp/8080/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
* const addr = new Multiaddr('/ip4/0.0.0.0/tcp/8080/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
* // <Multiaddr 0400... - /ip4/0.0.0.0/tcp/8080/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC>
*
* addr.decapsulateCode(421).toString()
* // '/ip4/0.0.0.0/tcp/8080'
*
* Multiaddr('/ip4/127.0.0.1/tcp/8080').decapsulateCode(421).toString()
* new Multiaddr('/ip4/127.0.0.1/tcp/8080').decapsulateCode(421).toString()
* // '/ip4/127.0.0.1/tcp/8080'
* ```
*/
Expand All @@ -307,7 +311,7 @@ class Multiaddr {
* @returns {string | null} peerId - The id of the peer or null if invalid or missing from the ma
* @example
* ```js
* const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string')
* const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string')
* // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string>
*
* // should return QmValidBase58string or null if the id is missing or invalid
Expand Down Expand Up @@ -341,7 +345,7 @@ class Multiaddr {
* @returns {string | null} path - The path of the multiaddr, or null if no path protocol is present
* @example
* ```js
* const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080/unix/tmp/p2p.sock')
* const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080/unix/tmp/p2p.sock')
* // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080/unix/tmp/p2p.sock>
*
* // should return utf8 string or null if the id is missing or invalid
Expand Down Expand Up @@ -375,10 +379,10 @@ class Multiaddr {
* @returns {boolean}
* @example
* ```js
* const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080')
* const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080')
* // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080>
*
* const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/4001')
* const mh2 = new Multiaddr('/ip4/127.0.0.1/tcp/4001')
* // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
*
* mh1.equals(mh1)
Expand All @@ -399,7 +403,7 @@ class Multiaddr {
* @example
* ```js
* Multiaddr.resolvers.set('dnsaddr', resolverFunction)
* const mh1 = Multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb')
* const mh1 = new Multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb')
* const resolvedMultiaddrs = await mh1.resolve()
* // [
* // <Multiaddr 04934b5353060fa1a503221220c10f9319dac35c270a6b74cd644cb3acfc1f6efc8c821f8eb282599fd1814f64 - /ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb>,
Expand Down Expand Up @@ -437,7 +441,7 @@ class Multiaddr {
* @example
*
* ```js
* Multiaddr('/ip4/127.0.0.1/tcp/4001').nodeAddress()
* new Multiaddr('/ip4/127.0.0.1/tcp/4001').nodeAddress()
* // {family: 4, address: '127.0.0.1', port: '4001'}
* ```
*
Expand All @@ -462,7 +466,6 @@ class Multiaddr {
}
}

// TODO find a better example, not sure about it's good enough
/**
* Returns if a Multiaddr is a Thin Waist address or not.
*
Expand All @@ -474,18 +477,22 @@ class Multiaddr {
* @returns {boolean} isThinWaistAddress
* @example
* ```js
* const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/4001')
* const mh1 = new Multiaddr('/ip4/127.0.0.1/tcp/4001')
* // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
* const mh2 = Multiaddr('/ip4/192.168.2.1/tcp/5001')
* const mh2 = new Multiaddr('/ip4/192.168.2.1/tcp/5001')
* // <Multiaddr 04c0a80201061389 - /ip4/192.168.2.1/tcp/5001>
* const mh3 = mh1.encapsulate(mh2)
* // <Multiaddr 047f000001060fa104c0a80201061389 - /ip4/127.0.0.1/tcp/4001/ip4/192.168.2.1/tcp/5001>
* const mh4 = new Multiaddr('/ip4/127.0.0.1/tcp/2000/wss/p2p-webrtc-star/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo2a')
* // <Multiaddr 047f0000010607d0de039302a503221220d52ebb89d85b02a284948203a62ff28389c57c9f42beec4ec20db76a64835843 - /ip4/127.0.0.1/tcp/2000/wss/p2p-webrtc-star/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo2a>
* mh1.isThinWaistAddress()
* // true
* mh2.isThinWaistAddress()
* // true
* mh3.isThinWaistAddress()
* // false
* mh4.isThinWaistAddress()
* // false
* ```
*/
isThinWaistAddress (addr) {
Expand Down Expand Up @@ -559,32 +566,20 @@ class Multiaddr {
static isMultiaddr (value) {
return value instanceof Multiaddr || Boolean(value && value[symbol])
}

/**
* Static factory method
*
* @param {string | Uint8Array | Multiaddr | null} [addr] - If String or Uint8Array, needs to adhere
* to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format)
*/
static multiaddr (addr) {
return new Multiaddr(addr)
}
}

Multiaddr.protocols = protocols

Multiaddr.resolvers = resolvers

/**
* Callable Multiaddr factory function
*
* @param {string | Uint8Array | Multiaddr | null} [addr] - If String or Uint8Array, needs to adhere
* to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format)
* @example
* ```js
* Multiaddr('/ip4/127.0.0.1/tcp/4001')
* // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
* ```
*/
const MultiaddrFactory = (addr) => {
return new Multiaddr(addr)
}

MultiaddrFactory.protocols = protocols
MultiaddrFactory.resolvers = resolvers
MultiaddrFactory.Multiaddr = Multiaddr
MultiaddrFactory.isMultiaddr = Multiaddr.isMultiaddr
MultiaddrFactory.isName = Multiaddr.isName
MultiaddrFactory.fromNodeAddress = Multiaddr.fromNodeAddress

module.exports = MultiaddrFactory
module.exports = Multiaddr
Loading

0 comments on commit 89766b0

Please sign in to comment.