mirror of
https://github.com/fluencelabs/js-libp2p-utils
synced 2025-04-25 06:02:19 +00:00
chore: complete docs
This commit is contained in:
parent
d7fa562d91
commit
645b483cb4
95
README.md
95
README.md
@ -40,6 +40,36 @@ const ma = ipAndPortToMultiaddr('127.0.0.1', 9000)
|
|||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
|
### 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/multiaddr/array-equals')
|
||||||
|
|
||||||
|
const peerId1 = await PeerId.create()
|
||||||
|
const peerId2 = await PeerId.create()
|
||||||
|
|
||||||
|
const equals = arrayEquals([peerId1], [peerId2])
|
||||||
|
```
|
||||||
|
|
||||||
### multiaddr `.isLoopback(ma)`
|
### multiaddr `.isLoopback(ma)`
|
||||||
|
|
||||||
Check if a given multiaddr is a loopback address.
|
Check if a given multiaddr is a loopback address.
|
||||||
@ -92,6 +122,71 @@ const ma = multiaddr('/ip4/10.0.0.1/tcp/1000')
|
|||||||
isMultiaddrPrivateAddrs = isPrivate(ma)
|
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; /* ... */
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
## Contribute
|
## Contribute
|
||||||
|
|
||||||
Contributions welcome. Please check out [the issues](https://github.com/libp2p/js-libp2p-utils/issues).
|
Contributions welcome. Please check out [the issues](https://github.com/libp2p/js-libp2p-utils/issues).
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Verify if two arrays of non primitive types with the "equals" function are equal.
|
* Verify if two arrays of non primitive types with the "equals" function are equal.
|
||||||
* Compatible with multiaddr, peer-id and Buffer.
|
* Compatible with multiaddr, peer-id and others.
|
||||||
*
|
*
|
||||||
* @param {Array<*>} a
|
* @param {Array<*>} a
|
||||||
* @param {Array<*>} b
|
* @param {Array<*>} b
|
||||||
|
@ -10,7 +10,13 @@ const errors = {
|
|||||||
ERR_INVALID_IP: 'ERR_INVALID_IP'
|
ERR_INVALID_IP: 'ERR_INVALID_IP'
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = (ip, port) => {
|
/**
|
||||||
|
* Transform an IP, Port pair into a multiaddr
|
||||||
|
*
|
||||||
|
* @param {string} ip
|
||||||
|
* @param {number|string} port
|
||||||
|
*/
|
||||||
|
function ipPortToMultiaddr (ip, port) {
|
||||||
if (typeof ip !== 'string') {
|
if (typeof ip !== 'string') {
|
||||||
throw errCode(new Error(`invalid ip provided: ${ip}`), errors.ERR_INVALID_IP_PARAMETER)
|
throw errCode(new Error(`invalid ip provided: ${ip}`), errors.ERR_INVALID_IP_PARAMETER)
|
||||||
}
|
}
|
||||||
@ -36,4 +42,6 @@ module.exports = (ip, port) => {
|
|||||||
throw errCode(new Error(`invalid ip:port for creating a multiaddr: ${ip}:${port}`), errors.ERR_INVALID_IP)
|
throw errCode(new Error(`invalid ip:port for creating a multiaddr: ${ip}:${port}`), errors.ERR_INVALID_IP)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = ipPortToMultiaddr
|
||||||
|
|
||||||
module.exports.Errors = errors
|
module.exports.Errors = errors
|
||||||
|
@ -3,9 +3,18 @@
|
|||||||
const abortable = require('abortable-iterator')
|
const abortable = require('abortable-iterator')
|
||||||
const log = require('debug')('libp2p:stream:converter')
|
const log = require('debug')('libp2p:stream:converter')
|
||||||
|
|
||||||
// Convert a duplex iterable into a MultiaddrConnection
|
/**
|
||||||
// https://github.com/libp2p/interface-transport#multiaddrconnection
|
* Convert a duplex iterable into a MultiaddrConnection.
|
||||||
module.exports = ({ stream, remoteAddr, localAddr }, options = {}) => {
|
* https://github.com/libp2p/interface-transport#multiaddrconnection
|
||||||
|
*
|
||||||
|
* @param {object} streamProperties
|
||||||
|
* @param {DuplexStream} streamProperties.stream
|
||||||
|
* @param {Multiaddr} streamProperties.remoteAddr
|
||||||
|
* @param {Multiaddr} streamProperties.localAddr
|
||||||
|
* @param {object} [options]
|
||||||
|
* @param {AbortSignal} [options.signal]
|
||||||
|
*/
|
||||||
|
function streamToMaConnection ({ stream, remoteAddr, localAddr }, options = {}) {
|
||||||
const { sink, source } = stream
|
const { sink, source } = stream
|
||||||
const maConn = {
|
const maConn = {
|
||||||
async sink (source) {
|
async sink (source) {
|
||||||
@ -47,3 +56,5 @@ module.exports = ({ stream, remoteAddr, localAddr }, options = {}) => {
|
|||||||
|
|
||||||
return maConn
|
return maConn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = streamToMaConnection
|
||||||
|
Loading…
x
Reference in New Issue
Block a user