Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: libp2p/js-libp2p-delegated-peer-routing
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: ce9543f939a57a33bda3d3a7d920ef31b3dcb556
Choose a base ref
...
head repository: libp2p/js-libp2p-delegated-peer-routing
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 38d284803094a8847b03d7848c170c4acdd4a3ae
Choose a head ref
  • 19 commits
  • 6 files changed
  • 5 contributors

Commits on Nov 29, 2019

  1. chore: rename timeout option (#14)

    BREAKING CHANGE: maxTimeout option renamed to timeout
    vasco-santos authored and jacobheun committed Nov 29, 2019
    Copy the full SHA
    36d852f View commit details
  2. chore: update contributors

    jacobheun committed Nov 29, 2019

    Verified

    This commit was signed with the committer’s verified signature.
    jacobheun Jacob Heun
    Copy the full SHA
    1510fde View commit details
  3. chore: release version v0.4.0

    License: MIT
    Signed-off-by: Jacob Heun <jacobheun@gmail.com>
    jacobheun committed Nov 29, 2019

    Verified

    This commit was signed with the committer’s verified signature.
    jacobheun Jacob Heun
    Copy the full SHA
    9a46c6b View commit details

Commits on Jan 17, 2020

  1. docs: document required API endpoint (#15)

    Alan Shaw authored and jacobheun committed Jan 17, 2020
    Copy the full SHA
    949c0c0 View commit details

Commits on Feb 4, 2020

  1. chore: update ipfs-http-client dep (#17)

    * chore: update ipfs-http-client dep
    
    TODO: use official release
    
    * perf: smaller bundle
    
    * chore: update ipfs-http-client dep
    Alan Shaw authored Feb 4, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    afbc877 View commit details
  2. chore: update contributors

    jacobheun committed Feb 4, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    jacobheun Jacob Heun
    Copy the full SHA
    ee2128e View commit details
  3. Verified

    This commit was signed with the committer’s verified signature.
    jacobheun Jacob Heun
    Copy the full SHA
    00296ba View commit details

Commits on Feb 18, 2020

  1. chore(deps-dev): bump aegir from 20.6.1 to 21.0.2

    Bumps [aegir](https://github.com/ipfs/aegir) from 20.6.1 to 21.0.2.
    - [Release notes](https://github.com/ipfs/aegir/releases)
    - [Changelog](https://github.com/ipfs/aegir/blob/master/CHANGELOG.md)
    - [Commits](ipfs/aegir@v20.6.1...v21.0.2)
    
    Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
    dependabot-preview[bot] authored and jacobheun committed Feb 18, 2020
    Copy the full SHA
    c777434 View commit details

Commits on Apr 9, 2020

  1. chore: update ipfs-http-client to the latest version (#23)

    * chore: update ipfs-http-client to the latest version
    
    * chore: specify minimum http client version
    achingbrain authored Apr 9, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    67536b4 View commit details
  2. chore: update contributors

    jacobheun committed Apr 9, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    jacobheun Jacob Heun
    Copy the full SHA
    46658e0 View commit details
  3. Verified

    This commit was signed with the committer’s verified signature.
    jacobheun Jacob Heun
    Copy the full SHA
    bdb89b6 View commit details

Commits on Apr 16, 2020

  1. chore: update deps (#30)

    * chore: update deps
    
    * refactor: simplify timeout option
    
    * fix: pass correct timeout option
    
    * chore: update http client
    jacobheun authored Apr 16, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    528adf3 View commit details
  2. chore: update contributors

    jacobheun committed Apr 16, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    jacobheun Jacob Heun
    Copy the full SHA
    e90430c View commit details
  3. Verified

    This commit was signed with the committer’s verified signature.
    jacobheun Jacob Heun
    Copy the full SHA
    3255b6f View commit details

Commits on Apr 22, 2020

  1. chore(deps-dev): bump ipfsd-ctl from 3.1.0 to 4.0.1 (#31)

    Bumps [ipfsd-ctl](https://github.com/ipfs/js-ipfsd-ctl) from 3.1.0 to 4.0.1.
    - [Release notes](https://github.com/ipfs/js-ipfsd-ctl/releases)
    - [Changelog](https://github.com/ipfs/js-ipfsd-ctl/blob/master/CHANGELOG.md)
    - [Commits](ipfs/js-ipfsd-ctl@v3.1.0...v4.0.1)
    
    Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
    
    Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
    dependabot-preview[bot] authored Apr 22, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    4e76474 View commit details

Commits on Apr 23, 2020

  1. chore: remove-peer-info-from-api (#25)

    * chore: remove-peer-info-from-api
    
    BREAKING CHANGE: findPeer returns id and addrs properties instead of peer-info instance
    
    * chore: address review
    vasco-santos authored Apr 23, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f49ddc0 View commit details
  2. chore: make ipfs-http-client a peer dependency (#32)

    BREAKING CHANGE: The ipfs-http-client must now be installed
    as a peer dependency. It is no longer included as a dependency
    of this module.
    
    The reason the http-client should be a peerDependency and
    not a dependency is that its API requires knowledge of the
    http-client (we pass in the api endpoint details).
    jacobheun authored Apr 23, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    a1b1b5e View commit details
  3. chore: update contributors

    jacobheun committed Apr 23, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    jacobheun Jacob Heun
    Copy the full SHA
    1bb207a View commit details
  4. Verified

    This commit was signed with the committer’s verified signature.
    jacobheun Jacob Heun
    Copy the full SHA
    38d2848 View commit details
Showing with 151 additions and 49 deletions.
  1. +15 −4 .aegir.js
  2. +55 −0 CHANGELOG.md
  3. +13 −2 README.md
  4. +14 −10 package.json
  5. +21 −13 src/index.js
  6. +33 −20 test/index.spec.js
19 changes: 15 additions & 4 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
'use strict'

const createServer = require('ipfsd-ctl').createServer

const server = createServer()
const { createServer } = require('ipfsd-ctl')
let server

module.exports = {
hooks: {
browser: {
pre: () => server.start(),
pre: async () => {
server = createServer({
host: '127.0.0.1',
port: 57483
}, {
type: 'go',
ipfsHttpModule: require('ipfs-http-client'),
ipfsBin: require('go-ipfs-dep').path(),
test: true
})

await server.start()
},
post: () => server.stop()
}
}
55 changes: 55 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,58 @@
<a name="0.5.0"></a>
# [0.5.0](https://github.com/libp2p/js-libp2p-delegated-peer-routing/compare/v0.4.3...v0.5.0) (2020-04-23)


### Chores

* make ipfs-http-client a peer dependency ([#32](https://github.com/libp2p/js-libp2p-delegated-peer-routing/issues/32)) ([a1b1b5e](https://github.com/libp2p/js-libp2p-delegated-peer-routing/commit/a1b1b5e))
* remove-peer-info-from-api ([#25](https://github.com/libp2p/js-libp2p-delegated-peer-routing/issues/25)) ([f49ddc0](https://github.com/libp2p/js-libp2p-delegated-peer-routing/commit/f49ddc0))


### BREAKING CHANGES

* The ipfs-http-client must now be installed
as a peer dependency. It is no longer included as a dependency
of this module.

The reason the http-client should be a peerDependency and
not a dependency is that its API requires knowledge of the
http-client (we pass in the api endpoint details).
* findPeer returns id and addrs properties instead of peer-info instance

* chore: address review



<a name="0.4.3"></a>
## [0.4.3](https://github.com/libp2p/js-libp2p-delegated-peer-routing/compare/v0.4.2...v0.4.3) (2020-04-16)



<a name="0.4.2"></a>
## [0.4.2](https://github.com/libp2p/js-libp2p-delegated-peer-routing/compare/v0.4.1...v0.4.2) (2020-04-09)



<a name="0.4.1"></a>
## [0.4.1](https://github.com/libp2p/js-libp2p-delegated-peer-routing/compare/v0.4.0...v0.4.1) (2020-02-04)



<a name="0.4.0"></a>
# [0.4.0](https://github.com/libp2p/js-libp2p-delegated-peer-routing/compare/v0.3.1...v0.4.0) (2019-11-29)


### Chores

* rename timeout option ([#14](https://github.com/libp2p/js-libp2p-delegated-peer-routing/issues/14)) ([36d852f](https://github.com/libp2p/js-libp2p-delegated-peer-routing/commit/36d852f))


### BREAKING CHANGES

* maxTimeout option renamed to timeout



<a name="0.3.1"></a>
## [0.3.1](https://github.com/libp2p/js-libp2p-delegated-peer-routing/compare/v0.3.0...v0.3.1) (2019-07-24)

15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# js-libp2p-delegated-peer-routing

Leverage other peers in the network to perform Peer Routing calls.

Requires access to `/api/v0/dht/findpeer` HTTP API endpoint of the delegate node.

## Lead Maintainer

[Jacob Heun](https://github.com/jacobheun)

## Requirements

`libp2p-delegated-peer-routing` leverages the `ipfs-http-client` library and requires it as a peer dependency, as such, both must be installed in order for this module to work properly.

```sh
npm install ipfs-http-client libp2p-delegated-peer-routing
```

## Example

```js
@@ -14,9 +25,9 @@ const DelegatedPeerRouting = require('libp2p-delegated-peer-routing')
const routing = new DelegatedPeerRouing()

try {
const peerInfo = await routing.findPeer('peerid')
const { id, multiaddrs } = await routing.findPeer('peerid')

console.log('found peer details', peerInfo)
console.log('found peer details', id, multiaddrs)
} catch (err) {
console.error(err)
}
24 changes: 14 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libp2p-delegated-peer-routing",
"version": "0.3.1",
"version": "0.5.0",
"description": "Leverage other peers in the libp2p network to perform Peer Routing calls.",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js",
@@ -18,26 +18,30 @@
"coverage": "aegir coverage"
},
"devDependencies": {
"aegir": "^19.0.4",
"aegir": "^21.7.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"go-ipfs-dep": "~0.4.17",
"ipfsd-ctl": "^0.44.1"
"go-ipfs-dep": "0.4.23-3",
"ipfs-utils": "^2.2.0",
"ipfs-http-client": "^44.0.0",
"ipfsd-ctl": "^4.0.1"
},
"peerDependencies": {
"ipfs-http-client": "^44.0.0"
},
"dependencies": {
"debug": "^4.1.1",
"ipfs-http-client": "^33.1.0",
"p-queue": "^6.1.0",
"peer-id": "~0.12.2"
"p-queue": "^6.3.0",
"peer-id": "^0.13.11"
},
"contributors": [
"Jacob Heun <jacobheun@gmail.com>",
"Alan Shaw <alan.shaw@protocol.ai>",
"Vasco Santos <vasco.santos@moxy.studio>",
"Alex Potsides <alex@achingbrain.net>",
"David Dias <daviddias.p@gmail.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Jacob Heun <jacobheun@gmail.com>",
"Maciej Krüger <mkg20001@gmail.com>",
"Marcin Rataj <lidel@lidel.org>",
"Vasco Santos <vasco.santos@moxy.studio>"
"David Dias <daviddias.p@gmail.com>"
]
}
34 changes: 21 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict'

const PeerId = require('peer-id')
const dht = require('ipfs-http-client/src/dht')
const defaultConfig = require('ipfs-http-client/src/utils/default-config')
const createFindPeer = require('ipfs-http-client/src/dht/find-peer')
const { default: PQueue } = require('p-queue')
const debug = require('debug')

const log = debug('libp2p-delegated-peer-routing')
log.error = debug('libp2p-delegated-peer-routing:error')

const DEFAULT_MAX_TIMEOUT = 30e3 // 30 second default
const DEFAULT_TIMEOUT = 30e3 // 30 second default
const DEFAULT_IPFS_API = {
protocol: 'https',
port: 443,
@@ -19,8 +18,8 @@ const CONCURRENT_HTTP_REQUESTS = 4

class DelegatedPeerRouting {
constructor (api) {
this.api = Object.assign({}, defaultConfig(), DEFAULT_IPFS_API, api)
this.dht = dht(this.api)
this.api = Object.assign({}, DEFAULT_IPFS_API, api)
this.dht = { findPeer: createFindPeer(this.api) }

// limit concurrency to avoid request flood in web browser
// https://github.com/libp2p/js-libp2p-delegated-content-routing/issues/12
@@ -35,21 +34,30 @@ class DelegatedPeerRouting {
*
* @param {PeerID} id
* @param {object} options
* @param {number} options.maxTimeout How long the query can take. Defaults to 30 seconds
* @returns {Promise<PeerInfo>}
* @param {number} options.timeout How long the query can take. Defaults to 30 seconds
* @returns {Promise<{ id: PeerId, multiaddrs: Multiaddr[] }>}
*/
async findPeer (id, options = {}) {
if (PeerId.isPeerId(id)) {
id = id.toB58String()
let idStr = id
if (PeerId.isPeerId(idStr)) {
idStr = id.toB58String()
}

log('findPeer starts: ' + id)

options.maxTimeout = options.maxTimeout || DEFAULT_MAX_TIMEOUT
options.timeout = options.timeout || DEFAULT_TIMEOUT

try {
return await this._httpQueue.add(() => this.dht.findPeer(id, {
timeout: `${options.maxTimeout}ms`// The api requires specification of the time unit (s/ms)
}))
return await this._httpQueue.add(async () => {
const { addrs } = await this.dht.findPeer(idStr, {
timeout: options.timeout
})

return {
id,
multiaddrs: addrs
}
})
} catch (err) {
if (err.message.includes('not found')) {
return undefined
53 changes: 33 additions & 20 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -4,20 +4,29 @@
const chai = require('chai')
const { expect } = chai
chai.use(require('dirty-chai'))
const IPFSFactory = require('ipfsd-ctl')
const { createFactory } = require('ipfsd-ctl')
const PeerID = require('peer-id')
const { isNode } = require('ipfs-utils/src/env')

const DelegatedPeerRouting = require('../src')
const factory = IPFSFactory.create({ type: 'go' })
const factory = createFactory({
type: 'go',
ipfsHttpModule: require('ipfs-http-client'),
ipfsBin: isNode ? require('go-ipfs-dep').path() : undefined,
test: true,
endpoint: 'http://localhost:57483'
})

async function spawnNode (boostrap = []) {
const node = await factory.spawn({
// Lock down the nodes so testing can be deterministic
config: {
Bootstrap: boostrap,
Discovery: {
MDNS: {
Enabled: false
ipfsOptions: {
config: {
Bootstrap: boostrap,
Discovery: {
MDNS: {
Enabled: false
}
}
}
}
@@ -68,7 +77,6 @@ describe('DelegatedPeerRouting', function () {
const router = new DelegatedPeerRouting()

expect(router.api).to.include({
'api-path': '/api/v0/',
protocol: 'https',
port: 443,
host: 'node0.delegate.ipfs.io'
@@ -81,7 +89,6 @@ describe('DelegatedPeerRouting', function () {
})

expect(router.api).to.include({
'api-path': '/api/v0/',
protocol: 'https',
port: 443,
host: 'other.ipfs.io'
@@ -90,7 +97,7 @@ describe('DelegatedPeerRouting', function () {

it('should allow for overriding the api', () => {
const api = {
'api-path': '/api/v1/',
apiPath: '/api/v1',
protocol: 'http',
port: 8000,
host: 'localhost'
@@ -104,15 +111,17 @@ describe('DelegatedPeerRouting', function () {
describe('findPeers', () => {
it('should be able to find peers via the delegate with a peer id string', async () => {
const opts = delegatedNode.apiAddr.toOptions()

const router = new DelegatedPeerRouting({
protocol: 'http',
port: opts.port,
host: opts.host
})

const peer = await router.findPeer(peerIdToFind.id)
expect(peer).to.exist()
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
const { id, multiaddrs } = await router.findPeer(peerIdToFind.id)
expect(id).to.exist()
expect(multiaddrs).to.exist()
expect(id).to.eql(peerIdToFind.id)
})

it('should be able to find peers via the delegate with a peerid', async () => {
@@ -123,22 +132,26 @@ describe('DelegatedPeerRouting', function () {
host: opts.host
})

const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id))
expect(peer).to.exist()
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
const { id, multiaddrs } = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id))
expect(id).to.exist()
expect(multiaddrs).to.exist()

expect(id.toB58String()).to.eql(peerIdToFind.id)
})

it('should be able to specify a maxTimeout', async () => {
it('should be able to specify a timeout', async () => {
const opts = delegatedNode.apiAddr.toOptions()
const router = new DelegatedPeerRouting({
protocol: 'http',
port: opts.port,
host: opts.host
})

const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id), { maxTimeout: 2000 })
expect(peer).to.exist()
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
const { id, multiaddrs } = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id), { timeout: 2000 })
expect(id).to.exist()
expect(multiaddrs).to.exist()

expect(id.toB58String()).to.eql(peerIdToFind.id)
})

it('should not be able to find peers not on the network', async () => {