mirror of
https://github.com/fluencelabs/js-libp2p-utils
synced 2025-04-25 18:12:18 +00:00
Compare commits
No commits in common. "master" and "v0.1.0" have entirely different histories.
74
.github/workflows/main.yml
vendored
74
.github/workflows/main.yml
vendored
@ -1,74 +0,0 @@
|
|||||||
name: ci
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
check:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- run: npm install
|
|
||||||
- run: npx aegir lint
|
|
||||||
- run: npx aegir ts -p check
|
|
||||||
- run: npx aegir build
|
|
||||||
- run: npx aegir dep-check
|
|
||||||
- uses: ipfs/aegir/actions/bundle-size@master
|
|
||||||
with:
|
|
||||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
test-node:
|
|
||||||
needs: check
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os: [windows-latest, ubuntu-latest, macos-latest]
|
|
||||||
node: [14, 15]
|
|
||||||
fail-fast: true
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- uses: actions/setup-node@v1
|
|
||||||
with:
|
|
||||||
node-version: ${{ matrix.node }}
|
|
||||||
- run: npm install
|
|
||||||
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
|
|
||||||
- uses: codecov/codecov-action@v1
|
|
||||||
test-chrome:
|
|
||||||
needs: check
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- run: npm install
|
|
||||||
- run: npx aegir test -t browser -t webworker --bail
|
|
||||||
test-firefox:
|
|
||||||
needs: check
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- run: npm install
|
|
||||||
- run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless
|
|
||||||
test-webkit:
|
|
||||||
needs: check
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- uses: microsoft/playwright-github-action@v1
|
|
||||||
- run: npm install
|
|
||||||
- run: npx aegir test -t browser -t webworker --bail -- --browser webkit
|
|
||||||
test-electron-main:
|
|
||||||
needs: check
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- run: npm install
|
|
||||||
- run: npx xvfb-maybe aegir test -t electron-main --bail
|
|
||||||
test-electron-renderer:
|
|
||||||
needs: check
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- run: npm install
|
|
||||||
- run: npx xvfb-maybe aegir test -t electron-renderer --bail
|
|
52
.travis.yml
Normal file
52
.travis.yml
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
language: node_js
|
||||||
|
cache: npm
|
||||||
|
stages:
|
||||||
|
- check
|
||||||
|
- test
|
||||||
|
- cov
|
||||||
|
|
||||||
|
node_js:
|
||||||
|
- '10'
|
||||||
|
- '12'
|
||||||
|
|
||||||
|
os:
|
||||||
|
- linux
|
||||||
|
- osx
|
||||||
|
|
||||||
|
script: npx nyc -s npm run test:node -- --bail
|
||||||
|
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
include:
|
||||||
|
- os: windows
|
||||||
|
filter_secrets: false
|
||||||
|
cache: false
|
||||||
|
|
||||||
|
- stage: check
|
||||||
|
script:
|
||||||
|
- npx aegir commitlint --travis
|
||||||
|
- npx aegir dep-check
|
||||||
|
- npm run lint
|
||||||
|
|
||||||
|
- stage: test
|
||||||
|
name: chrome
|
||||||
|
addons:
|
||||||
|
chrome: stable
|
||||||
|
script: npx aegir test -t browser -t webworker
|
||||||
|
|
||||||
|
- stage: test
|
||||||
|
name: firefox
|
||||||
|
addons:
|
||||||
|
firefox: latest
|
||||||
|
script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless
|
||||||
|
|
||||||
|
- stage: test
|
||||||
|
name: electron
|
||||||
|
services:
|
||||||
|
- xvfb
|
||||||
|
script:
|
||||||
|
- npm run test:electron
|
||||||
|
- npm run test:electron-renderer
|
||||||
|
|
||||||
|
notifications:
|
||||||
|
email: false
|
209
API.md
209
API.md
@ -1,209 +0,0 @@
|
|||||||
# 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; /* ... */
|
|
||||||
})
|
|
||||||
```
|
|
90
CHANGELOG.md
90
CHANGELOG.md
@ -1,93 +1,3 @@
|
|||||||
# [0.3.0](https://github.com/libp2p/js-libp2p-utils/compare/v0.2.3...v0.3.0) (2021-04-08)
|
|
||||||
|
|
||||||
|
|
||||||
### Features
|
|
||||||
|
|
||||||
* add types ([#16](https://github.com/libp2p/js-libp2p-utils/issues/16)) ([e0552b5](https://github.com/libp2p/js-libp2p-utils/commit/e0552b5b6b1d912a8f6f1e39b1a4b70fca91f547))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.2.3"></a>
|
|
||||||
## [0.2.3](https://github.com/libp2p/js-libp2p-utils/compare/v0.2.2...v0.2.3) (2020-11-30)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.2.2"></a>
|
|
||||||
## [0.2.2](https://github.com/libp2p/js-libp2p-utils/compare/v0.2.1...v0.2.2) (2020-11-16)
|
|
||||||
|
|
||||||
|
|
||||||
### Features
|
|
||||||
|
|
||||||
* address sorter ([#13](https://github.com/libp2p/js-libp2p-utils/issues/13)) ([cb5e716](https://github.com/libp2p/js-libp2p-utils/commit/cb5e716))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.2.1"></a>
|
|
||||||
## [0.2.1](https://github.com/libp2p/js-libp2p-utils/compare/v0.1.3...v0.2.1) (2020-10-08)
|
|
||||||
|
|
||||||
|
|
||||||
### Chores
|
|
||||||
|
|
||||||
* update deps ([#9](https://github.com/libp2p/js-libp2p-utils/issues/9)) ([a2ea68f](https://github.com/libp2p/js-libp2p-utils/commit/a2ea68f))
|
|
||||||
|
|
||||||
|
|
||||||
### Features
|
|
||||||
|
|
||||||
* is multiaddr private and loopback ([#10](https://github.com/libp2p/js-libp2p-utils/issues/10)) ([d7fa562](https://github.com/libp2p/js-libp2p-utils/commit/d7fa562))
|
|
||||||
|
|
||||||
|
|
||||||
### BREAKING CHANGES
|
|
||||||
|
|
||||||
* - The multiaddr dep of this module uses Uint8Arrays and may not be
|
|
||||||
compatible with previous versions
|
|
||||||
|
|
||||||
* chore: remove gh url
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.2.0"></a>
|
|
||||||
# [0.2.0](https://github.com/libp2p/js-libp2p-utils/compare/v0.1.3...v0.2.0) (2020-08-07)
|
|
||||||
|
|
||||||
|
|
||||||
### Chores
|
|
||||||
|
|
||||||
* update deps ([#9](https://github.com/libp2p/js-libp2p-utils/issues/9)) ([a2ea68f](https://github.com/libp2p/js-libp2p-utils/commit/a2ea68f))
|
|
||||||
|
|
||||||
|
|
||||||
### BREAKING CHANGES
|
|
||||||
|
|
||||||
* - The multiaddr dep of this module uses Uint8Arrays and may not be
|
|
||||||
compatible with previous versions
|
|
||||||
|
|
||||||
* chore: remove gh url
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.1.3"></a>
|
|
||||||
## [0.1.3](https://github.com/libp2p/js-libp2p-utils/compare/v0.1.2...v0.1.3) (2020-07-15)
|
|
||||||
|
|
||||||
|
|
||||||
### Features
|
|
||||||
|
|
||||||
* arrayEquals for non primitive types with equals function ([80668ff](https://github.com/libp2p/js-libp2p-utils/commit/80668ff))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.1.2"></a>
|
|
||||||
## [0.1.2](https://github.com/libp2p/js-libp2p-utils/compare/v0.1.1...v0.1.2) (2020-02-15)
|
|
||||||
|
|
||||||
|
|
||||||
### Features
|
|
||||||
|
|
||||||
* stream to multiaddr connection converter ([#2](https://github.com/libp2p/js-libp2p-utils/issues/2)) ([6220631](https://github.com/libp2p/js-libp2p-utils/commit/6220631))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.1.1"></a>
|
|
||||||
## [0.1.1](https://github.com/libp2p/js-libp2p-utils/compare/v0.1.0...v0.1.1) (2020-02-13)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.1.0"></a>
|
<a name="0.1.0"></a>
|
||||||
# 0.1.0 (2019-09-23)
|
# 0.1.0 (2019-09-23)
|
||||||
|
|
||||||
|
@ -38,8 +38,6 @@ const ipAndPortToMultiaddr = require('libp2p-utils/src/ip-port-to-multiaddr')
|
|||||||
const ma = ipAndPortToMultiaddr('127.0.0.1', 9000)
|
const ma = ipAndPortToMultiaddr('127.0.0.1', 9000)
|
||||||
```
|
```
|
||||||
|
|
||||||
You can check the [API docs](./API.md).
|
|
||||||
|
|
||||||
## 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).
|
||||||
|
39
package.json
39
package.json
@ -1,24 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-utils",
|
"name": "libp2p-utils",
|
||||||
"version": "0.3.0",
|
"version": "0.1.0",
|
||||||
"description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem",
|
"description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem",
|
||||||
"leadMaintainer": "Vasco Santos <santos.vasco10@gmail.com>",
|
"leadMaintainer": "Vasco Santos <santos.vasco10@gmail.com>",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"types": "dist/src/index.d.ts",
|
|
||||||
"typesVersions": {
|
|
||||||
"*": {
|
|
||||||
"src/*": [
|
|
||||||
"dist/src/*",
|
|
||||||
"dist/src/*/index"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"files": [
|
|
||||||
"src",
|
|
||||||
"dist"
|
|
||||||
],
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepare": "aegir build --no-bundle",
|
|
||||||
"test": "aegir test",
|
"test": "aegir test",
|
||||||
"test:browser": "aegir test -t browser",
|
"test:browser": "aegir test -t browser",
|
||||||
"test:node": "aegir test -t node",
|
"test:node": "aegir test -t node",
|
||||||
@ -43,26 +29,15 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/libp2p/js-libp2p-utils#readme",
|
"homepage": "https://github.com/libp2p/js-libp2p-utils#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/debug": "^4.1.5",
|
"aegir": "^20.3.1",
|
||||||
"aegir": "^32.1.0",
|
"chai": "^4.2.0",
|
||||||
"it-pair": "^1.0.0",
|
"dirty-chai": "^2.0.1"
|
||||||
"it-pipe": "^1.1.0",
|
|
||||||
"libp2p-interfaces": "fluencelabs/js-libp2p-interfaces",
|
|
||||||
"streaming-iterables": "^5.0.3",
|
|
||||||
"util": "^0.12.3"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"abortable-iterator": "^3.0.0",
|
"ip-address": "^6.1.0",
|
||||||
"debug": "^4.3.0",
|
"multiaddr": "^7.1.0"
|
||||||
"err-code": "^3.0.1",
|
|
||||||
"ip-address": "^7.1.0",
|
|
||||||
"is-loopback-addr": "^1.0.0",
|
|
||||||
"multiaddr": "^9.0.1",
|
|
||||||
"private-ip": "^2.1.1"
|
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"Vasco Santos <vasco.santos@moxy.studio>",
|
"Vasco Santos <vasco.santos@moxy.studio>"
|
||||||
"Jacob Heun <jacobheun@gmail.com>",
|
|
||||||
"Alex Potsides <alex@achingbrain.net>"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -1,54 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
const isPrivate = require('./multiaddr/is-private')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {import('multiaddr').Multiaddr} Multiaddr
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {Object} Address
|
|
||||||
* @property {Multiaddr} multiaddr peer multiaddr.
|
|
||||||
* @property {boolean} isCertified obtained from a signed peer record.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compare function for array.sort().
|
|
||||||
* This sort aims to move the private adresses to the end of the array.
|
|
||||||
* In case of equality, a certified address will come first.
|
|
||||||
*
|
|
||||||
* @param {Address} a
|
|
||||||
* @param {Address} b
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
function addressesPublicFirstCompareFunction (a, b) {
|
|
||||||
const isAPrivate = isPrivate(a.multiaddr)
|
|
||||||
const isBPrivate = isPrivate(b.multiaddr)
|
|
||||||
|
|
||||||
if (isAPrivate && !isBPrivate) {
|
|
||||||
return 1
|
|
||||||
} else if (!isAPrivate && isBPrivate) {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
// Check certified?
|
|
||||||
if (a.isCertified && !b.isCertified) {
|
|
||||||
return -1
|
|
||||||
} else if (!a.isCertified && b.isCertified) {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sort given addresses by putting public addresses first.
|
|
||||||
* In case of equality, a certified address will come first.
|
|
||||||
*
|
|
||||||
* @param {Array<Address>} addresses
|
|
||||||
* @returns {Array<Address>}
|
|
||||||
*/
|
|
||||||
function publicAddressesFirst (addresses) {
|
|
||||||
return [...addresses].sort(addressesPublicFirstCompareFunction)
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports.publicAddressesFirst = publicAddressesFirst
|
|
@ -1,15 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Verify if two arrays of non primitive types with the "equals" function are equal.
|
|
||||||
* Compatible with multiaddr, peer-id and others.
|
|
||||||
*
|
|
||||||
* @param {Array<*>} a
|
|
||||||
* @param {Array<*>} b
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
function arrayEquals (a, b) {
|
|
||||||
return a.length === b.length && b.sort() && a.sort().every((item, index) => b[index].equals(item))
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = arrayEquals
|
|
@ -1,57 +1,30 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const debug = require('debug')
|
|
||||||
const log = Object.assign(debug('libp2p:ip-port-to-multiaddr'), {
|
|
||||||
error: debug('libp2p:ip-port-to-multiaddr:err')
|
|
||||||
})
|
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const errCode = require('err-code')
|
|
||||||
const { Address4, Address6 } = require('ip-address')
|
const { Address4, Address6 } = require('ip-address')
|
||||||
|
|
||||||
const errors = {
|
module.exports = (ip, port) => {
|
||||||
ERR_INVALID_IP_PARAMETER: 'ERR_INVALID_IP_PARAMETER',
|
|
||||||
ERR_INVALID_PORT_PARAMETER: 'ERR_INVALID_PORT_PARAMETER',
|
|
||||||
ERR_INVALID_IP: 'ERR_INVALID_IP'
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 new Error('invalid ip')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof port === 'string') {
|
port = parseInt(port)
|
||||||
port = parseInt(port)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isNaN(port)) {
|
if (isNaN(port)) {
|
||||||
throw errCode(new Error(`invalid port provided: ${port}`), errors.ERR_INVALID_PORT_PARAMETER)
|
throw new Error('invalid port')
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (new Address4(ip).isValid()) {
|
||||||
// Test valid IPv4
|
return multiaddr(`/ip4/${ip}/tcp/${port}`)
|
||||||
new Address4(ip) // eslint-disable-line no-new
|
}
|
||||||
return new multiaddr.Multiaddr(`/ip4/${ip}/tcp/${port}`)
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
try {
|
const ip6 = new Address6(ip)
|
||||||
// Test valid IPv6
|
|
||||||
const ip6 = new Address6(ip)
|
if (ip6.isValid()) {
|
||||||
return ip6.is4()
|
return ip6.is4()
|
||||||
? new multiaddr.Multiaddr(`/ip4/${ip6.to4().correctForm()}/tcp/${port}`)
|
? multiaddr(`/ip4/${ip6.to4().correctForm()}/tcp/${port}`)
|
||||||
: new multiaddr.Multiaddr(`/ip6/${ip}/tcp/${port}`)
|
: multiaddr(`/ip6/${ip}/tcp/${port}`)
|
||||||
} catch (err) {
|
|
||||||
const errMsg = `invalid ip:port for creating a multiaddr: ${ip}:${port}`
|
|
||||||
log.error(errMsg)
|
|
||||||
throw errCode(new Error(errMsg), errors.ERR_INVALID_IP)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new Error('invalid ip')
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = ipPortToMultiaddr
|
|
||||||
|
|
||||||
module.exports.Errors = errors
|
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
// @ts-ignore is-loopback-addr does not publish types
|
|
||||||
const isLoopbackAddr = require('is-loopback-addr')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {import('multiaddr').Multiaddr} Multiaddr
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a given multiaddr is a loopback address.
|
|
||||||
*
|
|
||||||
* @param {Multiaddr} ma
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
function isLoopback (ma) {
|
|
||||||
const { address } = ma.nodeAddress()
|
|
||||||
|
|
||||||
return isLoopbackAddr(address)
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = isLoopback
|
|
@ -1,22 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
// @ts-ignore private-ip does not publish types
|
|
||||||
const isIpPrivate = require('private-ip')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {import('multiaddr').Multiaddr} Multiaddr
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a given multiaddr has a private address.
|
|
||||||
*
|
|
||||||
* @param {Multiaddr} ma
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
function isPrivate (ma) {
|
|
||||||
const { address } = ma.nodeAddress()
|
|
||||||
|
|
||||||
return isIpPrivate(address)
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = isPrivate
|
|
@ -1,76 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
const { source: abortable } = require('abortable-iterator')
|
|
||||||
const debug = require('debug')
|
|
||||||
const log = debug('libp2p:stream:converter')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {import('multiaddr').Multiaddr} Multiaddr
|
|
||||||
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
|
|
||||||
*
|
|
||||||
* @typedef {Object} Timeline
|
|
||||||
* @property {number} open - connection opening timestamp.
|
|
||||||
* @property {number} [upgraded] - connection upgraded timestamp.
|
|
||||||
* @property {number} [close]
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a duplex iterable into a MultiaddrConnection.
|
|
||||||
* https://github.com/libp2p/interface-transport#multiaddrconnection
|
|
||||||
*
|
|
||||||
* @param {object} streamProperties
|
|
||||||
* @param {MuxedStream} streamProperties.stream
|
|
||||||
* @param {Multiaddr} streamProperties.remoteAddr
|
|
||||||
* @param {Multiaddr} streamProperties.localAddr
|
|
||||||
* @param {object} [options]
|
|
||||||
* @param {AbortSignal} [options.signal]
|
|
||||||
* @returns {import('libp2p-interfaces/src/transport/types').MultiaddrConnection}
|
|
||||||
*/
|
|
||||||
function streamToMaConnection ({ stream, remoteAddr, localAddr }, options = {}) {
|
|
||||||
const { sink, source } = stream
|
|
||||||
const maConn = {
|
|
||||||
/**
|
|
||||||
* @param {Uint8Array} source
|
|
||||||
*/
|
|
||||||
async sink (source) {
|
|
||||||
if (options.signal) {
|
|
||||||
// @ts-ignore ts infers source template will be a number
|
|
||||||
source = abortable(source, options.signal)
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await sink(source)
|
|
||||||
} catch (err) {
|
|
||||||
// If aborted we can safely ignore
|
|
||||||
if (err.type !== 'aborted') {
|
|
||||||
// If the source errored the socket will already have been destroyed by
|
|
||||||
// toIterable.duplex(). If the socket errored it will already be
|
|
||||||
// destroyed. There's nothing to do here except log the error & return.
|
|
||||||
log(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
close()
|
|
||||||
},
|
|
||||||
source: options.signal ? abortable(source, options.signal) : source,
|
|
||||||
conn: stream,
|
|
||||||
localAddr,
|
|
||||||
remoteAddr,
|
|
||||||
/** @type {Timeline} */
|
|
||||||
timeline: { open: Date.now(), close: undefined },
|
|
||||||
close () {
|
|
||||||
sink(new Uint8Array(0))
|
|
||||||
return close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function close () {
|
|
||||||
if (!maConn.timeline.close) {
|
|
||||||
maConn.timeline.close = Date.now()
|
|
||||||
}
|
|
||||||
return Promise.resolve()
|
|
||||||
}
|
|
||||||
|
|
||||||
return maConn
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = streamToMaConnection
|
|
@ -1,53 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
/* eslint-env mocha */
|
|
||||||
|
|
||||||
const { expect } = require('aegir/utils/chai')
|
|
||||||
const multiaddr = require('multiaddr')
|
|
||||||
|
|
||||||
const { publicAddressesFirst } = require('../src/address-sort')
|
|
||||||
|
|
||||||
describe('address-sort', () => {
|
|
||||||
it('should sort public addresses first', () => {
|
|
||||||
const addresses = [
|
|
||||||
{
|
|
||||||
multiaddr: new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/4000'),
|
|
||||||
isCertified: false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
multiaddr: new multiaddr.Multiaddr('/ip4/30.0.0.1/tcp/4000'),
|
|
||||||
isCertified: false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
multiaddr: new multiaddr.Multiaddr('/ip4/31.0.0.1/tcp/4000'),
|
|
||||||
isCertified: false
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
const sortedAddresses = publicAddressesFirst(addresses)
|
|
||||||
expect(sortedAddresses[0].multiaddr.equals(new multiaddr.Multiaddr('/ip4/30.0.0.1/tcp/4000'))).to.eql(true)
|
|
||||||
expect(sortedAddresses[1].multiaddr.equals(new multiaddr.Multiaddr('/ip4/31.0.0.1/tcp/4000'))).to.eql(true)
|
|
||||||
expect(sortedAddresses[2].multiaddr.equals(new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/4000'))).to.eql(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should sort public certified addresses first', () => {
|
|
||||||
const addresses = [
|
|
||||||
{
|
|
||||||
multiaddr: new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/4000'),
|
|
||||||
isCertified: false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
multiaddr: new multiaddr.Multiaddr('/ip4/30.0.0.1/tcp/4000'),
|
|
||||||
isCertified: false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
multiaddr: new multiaddr.Multiaddr('/ip4/31.0.0.1/tcp/4000'),
|
|
||||||
isCertified: true
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
const sortedAddresses = publicAddressesFirst(addresses)
|
|
||||||
expect(sortedAddresses[0].multiaddr.equals(new multiaddr.Multiaddr('/ip4/31.0.0.1/tcp/4000'))).to.eql(true)
|
|
||||||
expect(sortedAddresses[1].multiaddr.equals(new multiaddr.Multiaddr('/ip4/30.0.0.1/tcp/4000'))).to.eql(true)
|
|
||||||
expect(sortedAddresses[2].multiaddr.equals(new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/4000'))).to.eql(true)
|
|
||||||
})
|
|
||||||
})
|
|
@ -1,72 +0,0 @@
|
|||||||
/* eslint-env mocha */
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
const { expect } = require('aegir/utils/chai')
|
|
||||||
const multiaddr = require('multiaddr')
|
|
||||||
|
|
||||||
const arrayEquals = require('../src/array-equals')
|
|
||||||
|
|
||||||
describe('non primitive array equals', () => {
|
|
||||||
it('returns true if two arrays of multiaddrs are equal', () => {
|
|
||||||
const a = [
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
|
|
||||||
new multiaddr.Multiaddr('/dns4/test.libp2p.io')
|
|
||||||
]
|
|
||||||
|
|
||||||
const b = [
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
|
|
||||||
new multiaddr.Multiaddr('/dns4/test.libp2p.io')
|
|
||||||
]
|
|
||||||
|
|
||||||
expect(arrayEquals(a, b)).to.eql(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns true if two arrays of multiaddrs have the same content but different orders', () => {
|
|
||||||
const a = [
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
|
|
||||||
new multiaddr.Multiaddr('/dns4/test.libp2p.io')
|
|
||||||
]
|
|
||||||
|
|
||||||
const b = [
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000'),
|
|
||||||
new multiaddr.Multiaddr('/dns4/test.libp2p.io')
|
|
||||||
]
|
|
||||||
|
|
||||||
expect(arrayEquals(a, b)).to.eql(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns false if two arrays of multiaddrs are different', () => {
|
|
||||||
const a = [
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
|
|
||||||
new multiaddr.Multiaddr('/dns4/test.libp2p.io')
|
|
||||||
]
|
|
||||||
|
|
||||||
const b = [
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8001'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
|
|
||||||
new multiaddr.Multiaddr('/dns4/test.libp2p.io')
|
|
||||||
]
|
|
||||||
|
|
||||||
expect(arrayEquals(a, b)).to.eql(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns false if two arrays of multiaddrs are partially equal, but different lengths', () => {
|
|
||||||
const a = [
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
|
|
||||||
new multiaddr.Multiaddr('/dns4/test.libp2p.io')
|
|
||||||
]
|
|
||||||
|
|
||||||
const b = [
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000'),
|
|
||||||
new multiaddr.Multiaddr('/dns4/test.libp2p.io')
|
|
||||||
]
|
|
||||||
|
|
||||||
expect(arrayEquals(a, b)).to.eql(false)
|
|
||||||
})
|
|
||||||
})
|
|
@ -1,9 +1,11 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const { expect } = require('aegir/utils/chai')
|
const chai = require('chai')
|
||||||
|
const dirtyChai = require('dirty-chai')
|
||||||
|
const expect = chai.expect
|
||||||
|
chai.use(dirtyChai)
|
||||||
const toMultiaddr = require('../src/ip-port-to-multiaddr')
|
const toMultiaddr = require('../src/ip-port-to-multiaddr')
|
||||||
const { Errors } = require('../src/ip-port-to-multiaddr')
|
|
||||||
|
|
||||||
describe('IP and port to Multiaddr', () => {
|
describe('IP and port to Multiaddr', () => {
|
||||||
it('creates multiaddr from valid IPv4 IP and port', () => {
|
it('creates multiaddr from valid IPv4 IP and port', () => {
|
||||||
@ -31,18 +33,18 @@ describe('IP and port to Multiaddr', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('throws for missing IP address', () => {
|
it('throws for missing IP address', () => {
|
||||||
expect(() => toMultiaddr()).to.throw('invalid ip provided').with.property('code', Errors.ERR_INVALID_IP_PARAMETER)
|
expect(() => toMultiaddr()).to.throw('invalid ip')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws for invalid IP address', () => {
|
it('throws for invalid IP address', () => {
|
||||||
const ip = 'aewmrn4awoew'
|
const ip = 'aewmrn4awoew'
|
||||||
const port = '234'
|
const port = '234'
|
||||||
expect(() => toMultiaddr(ip, port)).to.throw('invalid ip:port for creating a multiaddr').with.property('code', Errors.ERR_INVALID_IP)
|
expect(() => toMultiaddr(ip, port)).to.throw('invalid ip')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws for invalid port', () => {
|
it('throws for invalid port', () => {
|
||||||
const ip = '127.0.0.1'
|
const ip = '127.0.0.1'
|
||||||
const port = 'garbage'
|
const port = 'garbage'
|
||||||
expect(() => toMultiaddr(ip, port)).to.throw('invalid port provided').with.property('code', Errors.ERR_INVALID_PORT_PARAMETER)
|
expect(() => toMultiaddr(ip, port)).to.throw('invalid port')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,57 +0,0 @@
|
|||||||
/* eslint-env mocha */
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
const { expect } = require('aegir/utils/chai')
|
|
||||||
const multiaddr = require('multiaddr')
|
|
||||||
|
|
||||||
const isLoopback = require('../../src/multiaddr/is-loopback')
|
|
||||||
|
|
||||||
describe('multiaddr isLoopback', () => {
|
|
||||||
it('identifies loopback ip4 multiaddrs', () => {
|
|
||||||
[
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.1.1/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.1.1.1/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.255.255.255/tcp/1000')
|
|
||||||
].forEach(ma => {
|
|
||||||
expect(isLoopback(ma)).to.eql(true)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('identifies non loopback ip4 multiaddrs', () => {
|
|
||||||
[
|
|
||||||
new multiaddr.Multiaddr('/ip4/101.0.26.90/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/10.0.0.1/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/192.168.0.1/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/172.16.0.1/tcp/1000')
|
|
||||||
].forEach(ma => {
|
|
||||||
expect(isLoopback(ma)).to.eql(false)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('identifies loopback ip6 multiaddrs', () => {
|
|
||||||
[
|
|
||||||
new multiaddr.Multiaddr('/ip6/::1/tcp/1000')
|
|
||||||
].forEach(ma => {
|
|
||||||
expect(isLoopback(ma)).to.eql(true)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('identifies non loopback ip6 multiaddrs', () => {
|
|
||||||
[
|
|
||||||
new multiaddr.Multiaddr('/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip6/::/tcp/1000')
|
|
||||||
].forEach(ma => {
|
|
||||||
expect(isLoopback(ma)).to.eql(false)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('identifies other multiaddrs as not loopback addresses', () => {
|
|
||||||
[
|
|
||||||
new multiaddr.Multiaddr('/dns4/wss0.bootstrap.libp2p.io/tcp/443'),
|
|
||||||
new multiaddr.Multiaddr('/dns6/wss0.bootstrap.libp2p.io/tcp/443')
|
|
||||||
].forEach(ma => {
|
|
||||||
expect(isLoopback(ma)).to.eql(false)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
@ -1,58 +0,0 @@
|
|||||||
/* eslint-env mocha */
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
const { expect } = require('aegir/utils/chai')
|
|
||||||
const multiaddr = require('multiaddr')
|
|
||||||
|
|
||||||
const isPrivate = require('../../src/multiaddr/is-private')
|
|
||||||
|
|
||||||
describe('multiaddr isPrivate', () => {
|
|
||||||
it('identifies private ip4 multiaddrs', () => {
|
|
||||||
[
|
|
||||||
new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/10.0.0.1/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/192.168.0.1/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/172.16.0.1/tcp/1000')
|
|
||||||
].forEach(ma => {
|
|
||||||
expect(isPrivate(ma)).to.eql(true)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('identifies public ip4 multiaddrs', () => {
|
|
||||||
[
|
|
||||||
new multiaddr.Multiaddr('/ip4/101.0.26.90/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/40.1.20.9/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/92.168.0.1/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip4/2.16.0.1/tcp/1000')
|
|
||||||
].forEach(ma => {
|
|
||||||
expect(isPrivate(ma)).to.eql(false)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('identifies private ip6 multiaddrs', () => {
|
|
||||||
[
|
|
||||||
new multiaddr.Multiaddr('/ip6/fd52:8342:fc46:6c91:3ac9:86ff:fe31:7095/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip6/fd52:8342:fc46:6c91:3ac9:86ff:fe31:1/tcp/1000')
|
|
||||||
].forEach(ma => {
|
|
||||||
expect(isPrivate(ma)).to.eql(true)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('identifies public ip6 multiaddrs', () => {
|
|
||||||
[
|
|
||||||
new multiaddr.Multiaddr('/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000'),
|
|
||||||
new multiaddr.Multiaddr('/ip6/2000:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000')
|
|
||||||
].forEach(ma => {
|
|
||||||
expect(isPrivate(ma)).to.eql(false)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('identifies other multiaddrs as not private addresses', () => {
|
|
||||||
[
|
|
||||||
new multiaddr.Multiaddr('/dns4/wss0.bootstrap.libp2p.io/tcp/443'),
|
|
||||||
new multiaddr.Multiaddr('/dns6/wss0.bootstrap.libp2p.io/tcp/443')
|
|
||||||
].forEach(ma => {
|
|
||||||
expect(isPrivate(ma)).to.eql(false)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
@ -1,52 +0,0 @@
|
|||||||
/* eslint-env mocha */
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
const { expect } = require('aegir/utils/chai')
|
|
||||||
const pair = require('it-pair')
|
|
||||||
const pipe = require('it-pipe')
|
|
||||||
const { collect } = require('streaming-iterables')
|
|
||||||
const multiaddr = require('multiaddr')
|
|
||||||
|
|
||||||
const streamToMaConn = require('../src/stream-to-ma-conn')
|
|
||||||
|
|
||||||
describe('Convert stream into a multiaddr connection', () => {
|
|
||||||
it('converts a stream and adds the provided metadata', () => {
|
|
||||||
const stream = pair()
|
|
||||||
const localAddr = new multiaddr.Multiaddr('/ip4/101.45.75.219/tcp/6000')
|
|
||||||
const remoteAddr = new multiaddr.Multiaddr('/ip4/100.46.74.201/tcp/6002')
|
|
||||||
|
|
||||||
const maConn = streamToMaConn({
|
|
||||||
stream,
|
|
||||||
localAddr,
|
|
||||||
remoteAddr
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(maConn).to.exist()
|
|
||||||
expect(maConn.sink).to.exist()
|
|
||||||
expect(maConn.source).to.exist()
|
|
||||||
expect(maConn.localAddr).to.eql(localAddr)
|
|
||||||
expect(maConn.remoteAddr).to.eql(remoteAddr)
|
|
||||||
expect(maConn.timeline).to.exist()
|
|
||||||
expect(maConn.timeline.open).to.exist()
|
|
||||||
expect(maConn.timeline.close).to.not.exist()
|
|
||||||
|
|
||||||
maConn.close()
|
|
||||||
expect(maConn.timeline.close).to.exist()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('can stream data over the multiaddr connection', async () => {
|
|
||||||
const stream = pair()
|
|
||||||
const maConn = streamToMaConn({ stream })
|
|
||||||
|
|
||||||
const data = 'hey'
|
|
||||||
const streamData = await pipe(
|
|
||||||
[data],
|
|
||||||
maConn,
|
|
||||||
collect
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(streamData).to.eql([data])
|
|
||||||
// underlying stream end closes the connection
|
|
||||||
expect(maConn.timeline.close).to.exist()
|
|
||||||
})
|
|
||||||
})
|
|
@ -1,9 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "aegir/src/config/tsconfig.aegir.json",
|
|
||||||
"compilerOptions": {
|
|
||||||
"outDir": "dist"
|
|
||||||
},
|
|
||||||
"include": [
|
|
||||||
"src"
|
|
||||||
]
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user