Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

chore: update multiaddr to 9 #17

Merged
merged 2 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
"homepage": "https://github.com/libp2p/js-libp2p-utils#readme",
"devDependencies": {
"@types/debug": "^4.1.5",
"aegir": "^32.1.0",
"aegir": "^33.0.0",
"it-pair": "^1.0.0",
"it-pipe": "^1.1.0",
"libp2p-interfaces": "^0.9.0",
"libp2p-interfaces": "^0.10.0",
"streaming-iterables": "^5.0.3",
"util": "^0.12.3"
},
Expand All @@ -57,7 +57,7 @@
"err-code": "^3.0.1",
"ip-address": "^7.1.0",
"is-loopback-addr": "^1.0.0",
"multiaddr": "^8.1.2",
"multiaddr": "^9.0.1",
"private-ip": "^2.1.1"
},
"contributors": [
Expand Down
2 changes: 1 addition & 1 deletion src/address-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const isPrivate = require('./multiaddr/is-private')

/**
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('multiaddr').Multiaddr} Multiaddr
*/

/**
Expand Down
8 changes: 4 additions & 4 deletions src/ip-port-to-multiaddr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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')

Expand Down Expand Up @@ -36,15 +36,15 @@ function ipPortToMultiaddr (ip, port) {
try {
// Test valid IPv4
new Address4(ip) // eslint-disable-line no-new
return multiaddr(`/ip4/${ip}/tcp/${port}`)
return new Multiaddr(`/ip4/${ip}/tcp/${port}`)
} catch {}

try {
// Test valid IPv6
const ip6 = new Address6(ip)
return ip6.is4()
? multiaddr(`/ip4/${ip6.to4().correctForm()}/tcp/${port}`)
: multiaddr(`/ip6/${ip}/tcp/${port}`)
? new Multiaddr(`/ip4/${ip6.to4().correctForm()}/tcp/${port}`)
: new Multiaddr(`/ip6/${ip}/tcp/${port}`)
} catch (err) {
const errMsg = `invalid ip:port for creating a multiaddr: ${ip}:${port}`
log.error(errMsg)
Expand Down
2 changes: 1 addition & 1 deletion src/multiaddr/is-loopback.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const isLoopbackAddr = require('is-loopback-addr')

/**
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('multiaddr').Multiaddr} Multiaddr
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion src/multiaddr/is-private.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const isIpPrivate = require('private-ip')

/**
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('multiaddr').Multiaddr} Multiaddr
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion src/stream-to-ma-conn.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const debug = require('debug')
const log = debug('libp2p:stream:converter')

/**
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('multiaddr').Multiaddr} Multiaddr
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
*
* @typedef {Object} Timeline
Expand Down
26 changes: 13 additions & 13 deletions test/address-sort.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,52 @@
/* eslint-env mocha */

const { expect } = require('aegir/utils/chai')
const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')

const { publicAddressesFirst } = require('../src/address-sort')

describe('address-sort', () => {
it('should sort public addresses first', () => {
const addresses = [
{
multiaddr: multiaddr('/ip4/127.0.0.1/tcp/4000'),
multiaddr: new Multiaddr('/ip4/127.0.0.1/tcp/4000'),
isCertified: false
},
{
multiaddr: multiaddr('/ip4/30.0.0.1/tcp/4000'),
multiaddr: new Multiaddr('/ip4/30.0.0.1/tcp/4000'),
isCertified: false
},
{
multiaddr: multiaddr('/ip4/31.0.0.1/tcp/4000'),
multiaddr: new Multiaddr('/ip4/31.0.0.1/tcp/4000'),
isCertified: false
}
]

const sortedAddresses = publicAddressesFirst(addresses)
expect(sortedAddresses[0].multiaddr.equals(multiaddr('/ip4/30.0.0.1/tcp/4000'))).to.eql(true)
expect(sortedAddresses[1].multiaddr.equals(multiaddr('/ip4/31.0.0.1/tcp/4000'))).to.eql(true)
expect(sortedAddresses[2].multiaddr.equals(multiaddr('/ip4/127.0.0.1/tcp/4000'))).to.eql(true)
expect(sortedAddresses[0].multiaddr.equals(new Multiaddr('/ip4/30.0.0.1/tcp/4000'))).to.eql(true)
expect(sortedAddresses[1].multiaddr.equals(new Multiaddr('/ip4/31.0.0.1/tcp/4000'))).to.eql(true)
expect(sortedAddresses[2].multiaddr.equals(new Multiaddr('/ip4/127.0.0.1/tcp/4000'))).to.eql(true)
})

it('should sort public certified addresses first', () => {
const addresses = [
{
multiaddr: multiaddr('/ip4/127.0.0.1/tcp/4000'),
multiaddr: new Multiaddr('/ip4/127.0.0.1/tcp/4000'),
isCertified: false
},
{
multiaddr: multiaddr('/ip4/30.0.0.1/tcp/4000'),
multiaddr: new Multiaddr('/ip4/30.0.0.1/tcp/4000'),
isCertified: false
},
{
multiaddr: multiaddr('/ip4/31.0.0.1/tcp/4000'),
multiaddr: new Multiaddr('/ip4/31.0.0.1/tcp/4000'),
isCertified: true
}
]

const sortedAddresses = publicAddressesFirst(addresses)
expect(sortedAddresses[0].multiaddr.equals(multiaddr('/ip4/31.0.0.1/tcp/4000'))).to.eql(true)
expect(sortedAddresses[1].multiaddr.equals(multiaddr('/ip4/30.0.0.1/tcp/4000'))).to.eql(true)
expect(sortedAddresses[2].multiaddr.equals(multiaddr('/ip4/127.0.0.1/tcp/4000'))).to.eql(true)
expect(sortedAddresses[0].multiaddr.equals(new Multiaddr('/ip4/31.0.0.1/tcp/4000'))).to.eql(true)
expect(sortedAddresses[1].multiaddr.equals(new Multiaddr('/ip4/30.0.0.1/tcp/4000'))).to.eql(true)
expect(sortedAddresses[2].multiaddr.equals(new Multiaddr('/ip4/127.0.0.1/tcp/4000'))).to.eql(true)
})
})
48 changes: 24 additions & 24 deletions test/array-equals.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,69 @@
'use strict'

const { expect } = require('aegir/utils/chai')
const multiaddr = require('multiaddr')
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 = [
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/dns4/test.libp2p.io')
new Multiaddr('/ip4/127.0.0.1/tcp/8000'),
new Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
new Multiaddr('/dns4/test.libp2p.io')
]

const b = [
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/dns4/test.libp2p.io')
new Multiaddr('/ip4/127.0.0.1/tcp/8000'),
new Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
new 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 = [
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/dns4/test.libp2p.io')
new Multiaddr('/ip4/127.0.0.1/tcp/8000'),
new Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
new Multiaddr('/dns4/test.libp2p.io')
]

const b = [
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/dns4/test.libp2p.io')
new Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
new Multiaddr('/ip4/127.0.0.1/tcp/8000'),
new Multiaddr('/dns4/test.libp2p.io')
]

expect(arrayEquals(a, b)).to.eql(true)
})

it('returns false if two arrays of multiaddrs are different', () => {
const a = [
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/dns4/test.libp2p.io')
new Multiaddr('/ip4/127.0.0.1/tcp/8000'),
new Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
new Multiaddr('/dns4/test.libp2p.io')
]

const b = [
multiaddr('/ip4/127.0.0.1/tcp/8001'),
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/dns4/test.libp2p.io')
new Multiaddr('/ip4/127.0.0.1/tcp/8001'),
new Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
new 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 = [
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/dns4/test.libp2p.io')
new Multiaddr('/ip4/127.0.0.1/tcp/8000'),
new Multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
new Multiaddr('/dns4/test.libp2p.io')
]

const b = [
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/dns4/test.libp2p.io')
new Multiaddr('/ip4/127.0.0.1/tcp/8000'),
new Multiaddr('/dns4/test.libp2p.io')
]

expect(arrayEquals(a, b)).to.eql(false)
Expand Down
28 changes: 14 additions & 14 deletions test/multiaddr/is-loopback.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,54 @@
'use strict'

const { expect } = require('aegir/utils/chai')
const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')

const isLoopback = require('../../src/multiaddr/is-loopback')

describe('multiaddr isLoopback', () => {
it('identifies loopback ip4 multiaddrs', () => {
[
multiaddr('/ip4/127.0.0.1/tcp/1000'),
multiaddr('/ip4/127.0.1.1/tcp/1000'),
multiaddr('/ip4/127.1.1.1/tcp/1000'),
multiaddr('/ip4/127.255.255.255/tcp/1000')
new Multiaddr('/ip4/127.0.0.1/tcp/1000'),
new Multiaddr('/ip4/127.0.1.1/tcp/1000'),
new Multiaddr('/ip4/127.1.1.1/tcp/1000'),
new Multiaddr('/ip4/127.255.255.255/tcp/1000')
].forEach(ma => {
expect(isLoopback(ma)).to.eql(true)
})
})

it('identifies non loopback ip4 multiaddrs', () => {
[
multiaddr('/ip4/101.0.26.90/tcp/1000'),
multiaddr('/ip4/10.0.0.1/tcp/1000'),
multiaddr('/ip4/192.168.0.1/tcp/1000'),
multiaddr('/ip4/172.16.0.1/tcp/1000')
new Multiaddr('/ip4/101.0.26.90/tcp/1000'),
new Multiaddr('/ip4/10.0.0.1/tcp/1000'),
new Multiaddr('/ip4/192.168.0.1/tcp/1000'),
new Multiaddr('/ip4/172.16.0.1/tcp/1000')
].forEach(ma => {
expect(isLoopback(ma)).to.eql(false)
})
})

it('identifies loopback ip6 multiaddrs', () => {
[
multiaddr('/ip6/::1/tcp/1000')
new Multiaddr('/ip6/::1/tcp/1000')
].forEach(ma => {
expect(isLoopback(ma)).to.eql(true)
})
})

it('identifies non loopback ip6 multiaddrs', () => {
[
multiaddr('/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000'),
multiaddr('/ip6/::/tcp/1000')
new Multiaddr('/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000'),
new Multiaddr('/ip6/::/tcp/1000')
].forEach(ma => {
expect(isLoopback(ma)).to.eql(false)
})
})

it('identifies other multiaddrs as not loopback addresses', () => {
[
multiaddr('/dns4/wss0.bootstrap.libp2p.io/tcp/443'),
multiaddr('/dns6/wss0.bootstrap.libp2p.io/tcp/443')
new Multiaddr('/dns4/wss0.bootstrap.libp2p.io/tcp/443'),
new Multiaddr('/dns6/wss0.bootstrap.libp2p.io/tcp/443')
].forEach(ma => {
expect(isLoopback(ma)).to.eql(false)
})
Expand Down
30 changes: 15 additions & 15 deletions test/multiaddr/is-private.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,55 @@
'use strict'

const { expect } = require('aegir/utils/chai')
const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')

const isPrivate = require('../../src/multiaddr/is-private')

describe('multiaddr isPrivate', () => {
it('identifies private ip4 multiaddrs', () => {
[
multiaddr('/ip4/127.0.0.1/tcp/1000'),
multiaddr('/ip4/10.0.0.1/tcp/1000'),
multiaddr('/ip4/192.168.0.1/tcp/1000'),
multiaddr('/ip4/172.16.0.1/tcp/1000')
new Multiaddr('/ip4/127.0.0.1/tcp/1000'),
new Multiaddr('/ip4/10.0.0.1/tcp/1000'),
new Multiaddr('/ip4/192.168.0.1/tcp/1000'),
new Multiaddr('/ip4/172.16.0.1/tcp/1000')
].forEach(ma => {
expect(isPrivate(ma)).to.eql(true)
})
})

it('identifies public ip4 multiaddrs', () => {
[
multiaddr('/ip4/101.0.26.90/tcp/1000'),
multiaddr('/ip4/40.1.20.9/tcp/1000'),
multiaddr('/ip4/92.168.0.1/tcp/1000'),
multiaddr('/ip4/2.16.0.1/tcp/1000')
new Multiaddr('/ip4/101.0.26.90/tcp/1000'),
new Multiaddr('/ip4/40.1.20.9/tcp/1000'),
new Multiaddr('/ip4/92.168.0.1/tcp/1000'),
new Multiaddr('/ip4/2.16.0.1/tcp/1000')
].forEach(ma => {
expect(isPrivate(ma)).to.eql(false)
})
})

it('identifies private ip6 multiaddrs', () => {
[
multiaddr('/ip6/fd52:8342:fc46:6c91:3ac9:86ff:fe31:7095/tcp/1000'),
multiaddr('/ip6/fd52:8342:fc46:6c91:3ac9:86ff:fe31:1/tcp/1000')
new Multiaddr('/ip6/fd52:8342:fc46:6c91:3ac9:86ff:fe31:7095/tcp/1000'),
new 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', () => {
[
multiaddr('/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000'),
multiaddr('/ip6/2000:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000')
new Multiaddr('/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000'),
new 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', () => {
[
multiaddr('/dns4/wss0.bootstrap.libp2p.io/tcp/443'),
multiaddr('/dns6/wss0.bootstrap.libp2p.io/tcp/443')
new Multiaddr('/dns4/wss0.bootstrap.libp2p.io/tcp/443'),
new Multiaddr('/dns6/wss0.bootstrap.libp2p.io/tcp/443')
].forEach(ma => {
expect(isPrivate(ma)).to.eql(false)
})
Expand Down
Loading