Skip to content

Commit

Permalink
Merge remote-tracking branch 'utils/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jun 19, 2023
2 parents 7de907d + a226369 commit 3c7f4d1
Show file tree
Hide file tree
Showing 21 changed files with 1,459 additions and 0 deletions.
209 changes: 209 additions & 0 deletions packages/utils/API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# API

* [addressSort.publicAddressesFirst(addresses)](#addresssortpublicaddressesfirstaddresses)
* [Parameters](#parameters)
* [Returns](#returns)
* [Example](#example)
* [arrayEquals(a, b)](#arrayequalsa-b)
* [Parameters](#parameters)
* [Returns](#returns)
* [Example](#example)
* [multiaddr .isLoopback(ma)](#multiaddr-isloopbackma)
* [Parameters](#parameters-1)
* [Returns](#returns-1)
* [Example](#example-1)
* [multiaddr .isPrivate(ma)](#multiaddr-isprivatema)
* [Parameters](#parameters-2)
* [Returns](#returns-2)
* [Example](#example-2)
* [ipPortToMultiaddr(ip, port)](#ipporttomultiaddrip-port)
* [Parameters](#parameters-3)
* [Returns](#returns-3)
* [Example](#example-3)
* [streamToMaConnection(streamProperties, options)](#streamtomaconnectionstreamproperties-options)
* [Parameters](#parameters-4)
* [Returns](#returns-4)
* [Example](#example-4)

## addressSort.publicAddressesFirst(addresses)

Sort given addresses by putting public addresses first. In case of equality, a certified address will come first.

### Parameters

| Name | Type | Description |
|------|------|-------------|
| addresses | `Array<Address>` | Array of AddressBook addresses |

### Returns

| Type | Description |
|------|-------------|
| `Array<Address>` | returns array of sorted addresses |

### Example

```js
const multiaddr = require('multiaddr')
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')

const addresses = [
{
multiaddr: multiaddr('/ip4/127.0.0.1/tcp/4000'),
isCertified: false
},
{
multiaddr: multiaddr('/ip4/30.0.0.1/tcp/4000'),
isCertified: false
}
]

const sortedAddresses = publicAddressesFirst(addresses)
```

## arrayEquals(a, b)

Verify if two arrays of non primitive types with the "equals" function are equal.
Compatible with multiaddr, peer-id and others.

### Parameters

| Name | Type | Description |
|------|------|-------------|
| a | `Array<*>` | First array to verify |
| b | `Array<*>` | Second array to verify |

### Returns

| Type | Description |
|------|-------------|
| `boolean` | returns true if arrays are equal, false otherwise |

### Example

```js
const PeerId = require('peer-id')
const arrayEquals = require('libp2p-utils/src/array-equals')

const peerId1 = await PeerId.create()
const peerId2 = await PeerId.create()

const equals = arrayEquals([peerId1], [peerId2])
```

## multiaddr `.isLoopback(ma)`

Check if a given multiaddr is a loopback address.

### Parameters

| Name | Type | Description |
|------|------|-------------|
| ma | `Multiaddr` | multiaddr to verify |

### Returns

| Type | Description |
|------|-------------|
| `boolean` | returns true if multiaddr is a loopback address, false otherwise |

### Example

```js
const multiaddr = require('multiaddr')
const isLoopback = require('libp2p-utils/src/multiaddr/is-loopback')

const ma = multiaddr('/ip4/127.0.0.1/tcp/1000')
isMultiaddrLoopbackAddrs = isLoopback(ma)
```

## multiaddr `.isPrivate(ma)`

Check if a given multiaddr has a private address.

### Parameters

| Name | Type | Description |
|------|------|-------------|
| ma | `Multiaddr` | multiaddr to verify |

### Returns

| Type | Description |
|------|-------------|
| `boolean` | returns true if multiaddr is a private address, false otherwise |

### Example

```js
const multiaddr = require('multiaddr')
const isPrivate = require('libp2p-utils/src/multiaddr/is-private')

const ma = multiaddr('/ip4/10.0.0.1/tcp/1000')
isMultiaddrPrivateAddrs = isPrivate(ma)
```

## ipPortToMultiaddr(ip, port)

Transform an IP, Port pair into a multiaddr with tcp transport.

### Parameters

| Name | Type | Description |
|------|------|-------------|
| ip | `string` | ip for multiaddr |
| port | `number|string` | port for multiaddr |

### Returns

| Type | Description |
|------|-------------|
| `Multiaddr` | returns created multiaddr |

### Example

```js
const ipPortPairToMultiaddr = require('libp2p-utils/src/multiaddr/ip-port-to-multiaddr')
const ip = '127.0.0.1'
const port = '9090'

const ma = ipPortPairToMultiaddr(ma)
```

## streamToMaConnection(streamProperties, options)

Convert a duplex stream into a [MultiaddrConnection](https://github.com/libp2p/interface-transport#multiaddrconnection).

### Parameters

| Name | Type | Description |
|------|------|-------------|
| streamProperties | `object` | duplex stream properties |
| streamProperties.stream | [`DuplexStream`](https://github.com/libp2p/js-libp2p/blob/master/doc/STREAMING_ITERABLES.md#duplex) | duplex stream |
| streamProperties.remoteAddr | `Multiaddr` | stream remote address |
| streamProperties.localAddr | `Multiaddr` | stream local address |
| [options] | `object` | options |
| [options.signal] | `AbortSignal` | abort signal |

### Returns

| Type | Description |
|------|-------------|
| `Connection` | returns a multiaddr [Connection](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/connection) |

### Example

```js
const streamToMaConnection = require('libp2p-utils/src/stream-to-ma-conn')

const stream = {
sink: async source => {/* ... */},
source: { [Symbol.asyncIterator] () {/* ... */} }
}

const conn = streamToMaConnection({
stream,
remoteAddr: /* ... */
localAddr; /* ... */
})
```
Loading

0 comments on commit 3c7f4d1

Please sign in to comment.