Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit b2d94e8

Browse files
author
Alan Shaw
committed
feat: add core adapter for swarm peers and docs
1 parent 5b6d1e6 commit b2d94e8

File tree

8 files changed

+155
-21
lines changed

8 files changed

+155
-21
lines changed

API.md

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@
8585
* stats.bwPullStream
8686
* stats.repo
8787
* swarm.addrs
88-
* [swarm.connect](#swarmconnect) TODO: add docs
88+
* [swarm.connect](#swarmconnect)
8989
* swarm.disconnect
9090
* swarm.localAddrs
91-
* [swarm.peers](#swarmpeers) TODO: add docs
91+
* [swarm.peers](#swarmpeers)
9292
* [version](#version) TODO: add docs
9393

9494
Note: All API methods are documented using Promises/async/await but they also accept a callback as their last parameter.
@@ -755,3 +755,102 @@ Unsubscribe all handlers:
755755
```js
756756
await ipfs.pubsub.unsubscribe('my-pubsub-topic')
757757
```
758+
759+
## swarm.connect
760+
761+
Open a connection to a given address.
762+
763+
### `swarm.connect(addr, [options]): Promise<String[]>`
764+
765+
#### Parameters
766+
767+
* `addr` - Multiaddr address(es) of IPFS node(s) to connect to.
768+
* Type: `String`|`String[]`
769+
* `options` (optional)
770+
* Type: `Object`
771+
* Default: `null`
772+
* `options.signal` (optional) - A signal that can be used to abort the request
773+
* Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
774+
* Default: `null`
775+
776+
#### Returns
777+
778+
List of connection status messages for each peer connection attempted.
779+
780+
* Type: `Promise<String[]>`
781+
782+
#### Examples
783+
784+
```js
785+
const res = await ipfs.swarm.connect('/ip4/127.0.0.1/tcp/4101/ipfs/Qmeg3LQNGuiwpinKe69YBbADV2PpqGcGtcgLeaNjoxuUdV')
786+
console.log(res)
787+
/*
788+
[
789+
'connect Qmeg3LQNGuiwpinKe69YBbADV2PpqGcGtcgLeaNjoxuUdV success'
790+
]
791+
*/
792+
```
793+
794+
## swarm.peers
795+
796+
List peers with open connections.
797+
798+
### `swarm.peers([options]): Promise<Object[]>`
799+
800+
#### Parameters
801+
802+
* `options` (optional)
803+
* Type: `Object`
804+
* Default: `null`
805+
* `options.direction` (optional) - Return direction information for each peer (inbound/outbound).
806+
* Type: `Boolean`
807+
* Default: `false`
808+
* `options.signal` (optional) - A signal that can be used to abort the request
809+
* Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
810+
* Default: `null`
811+
* `options.latency` (optional) - Return latency information for each peer.
812+
* Type: `Boolean`
813+
* Default: `false`
814+
* `options.streams` (optional) - Return streams information for each peer.
815+
* Type: `Boolean`
816+
* Default: `false`
817+
* `options.verbose` (optional) - Return streams, latency, and direction info as well as address and ID for each peer.
818+
* Type: `Boolean`
819+
* Default: `false`
820+
821+
#### Returns
822+
823+
Peer information for peers with open connections.
824+
825+
* Type: `Promise<Object[]>`
826+
827+
#### Examples
828+
829+
```js
830+
const peers = await ipfs.swarm.peers({ verbose: true })
831+
console.log(res)
832+
/*
833+
[
834+
{
835+
addr: '/ip6/2a03:b0c0:3:e1::130:e001/tcp/4001',
836+
peer: 'QmYR4jxcGUTQZ7mA5DkfBSxpdJW5Y4CsfgHFfARLAV3goA',
837+
latency: '27.965651ms',
838+
muxer: '',
839+
direction: 2,
840+
streams: [ { protocol: '/ipfs/bitswap/1.1.0' } ]
841+
},
842+
{
843+
addr: '/ipfs/QmdGQoGuK3pao6bRDqGSDvux5SFHa4kC1XNFfHFcvcbydY/p2p-circuit',
844+
peer: 'QmcqrAQZqcQxrVpw2Png7FrSfNArfEFTu9UhPopmKPZpGP',
845+
latency: '772.455375ms',
846+
muxer: '',
847+
direction: 2,
848+
streams: [
849+
{ protocol: '/ipfs/bitswap/1.1.0' },
850+
{ protocol: '/ipfs/kad/1.0.0' }
851+
]
852+
},
853+
// ...
854+
]
855+
*/
856+
```

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ This module is in heavy development, not all API methods are available (or docum
222222
* stats.bwPullStream
223223
* stats.repo
224224
* swarm.addrs
225-
* [swarm.connect](./API.md#swarmconnect) TODO: add docs
225+
* [swarm.connect](./API.md#swarmconnect)
226226
* swarm.disconnect
227227
* swarm.localAddrs
228-
* [swarm.peers](./API.md#swarmpeers) TODO: add docs
228+
* [swarm.peers](./API.md#swarmpeers)
229229
* [version](./API.md#version) TODO: add docs
230230

231231
Note: All API methods are documented using Promises/async/await but they also accept a callback as their last parameter.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646
"interface-ipfs-core": "~0.105.1",
4747
"ipfs-block": "^0.8.1",
4848
"ipfsd-ctl": "^0.43.0",
49+
"multiaddr": "^6.1.0",
4950
"multiaddr-to-uri": "^4.0.1",
5051
"multihashes": "^0.4.14",
52+
"peer-id": "^0.12.2",
5153
"pull-stream": "^3.6.12"
5254
},
5355
"engines": {

src/swarm/peers.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { objectToQuery } = require('../lib/querystring')
44
const configure = require('../lib/configure')
55
const { ok } = require('../lib/fetch')
6+
const toCamel = require('../lib/to-camel')
67

78
module.exports = configure(({ fetch, apiUrl, apiPath, headers }) => {
89
return async options => {
@@ -22,21 +23,10 @@ module.exports = configure(({ fetch, apiUrl, apiPath, headers }) => {
2223
const data = await res.json()
2324

2425
return (data.Peers || []).map(p => {
25-
const peerInfo = {
26-
addr: p.Addr,
27-
peer: p.Peer
28-
}
29-
30-
if (options.verbose || options.streams) {
31-
peerInfo.streams = (p.Streams || []).map(s => ({ protocol: s.Protocol }))
32-
}
33-
34-
if (options.verbose || options.latency) {
35-
peerInfo.latency = p.Latency
36-
}
26+
const peerInfo = toCamel(p)
3727

38-
if (options.verbose) {
39-
peerInfo.muxer = p.Muxer
28+
if (peerInfo.streams) {
29+
peerInfo.streams = peerInfo.streams.map(toCamel)
4030
}
4131

4232
return peerInfo

test/interface.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ describe('interface-ipfs-core tests', () => {
359359

360360
tests.swarm(defaultCommon, {
361361
skip: [
362-
{ name: 'peers', reason: 'LITE: not implemented yet' },
363362
{ name: 'addrs', reason: 'LITE: not implemented yet' },
364363
{ name: 'localAddrs', reason: 'LITE: not implemented yet' },
365364
{ name: 'disconnect', reason: 'LITE: not implemented yet' }

test/utils/core-adapter/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ module.exports = ipfsLite => {
2525
publish: ipfsLite.pubsub.publish,
2626
subscribe: callbackify(require('./pubsub/subscribe')(ipfsLite), { minArgs: 2 }),
2727
unsubscribe: callbackify(require('./pubsub/unsubscribe')(ipfsLite), { minArgs: 2 })
28+
},
29+
swarm: {
30+
connect: ipfsLite.swarm.connect,
31+
peers: callbackify(require('./swarm/peers')(ipfsLite))
2832
}
2933
}
3034

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict'
2+
3+
const Multiaddr = require('multiaddr')
4+
const PeerId = require('peer-id')
5+
6+
module.exports = ipfsLite => async (...args) => {
7+
const list = await ipfsLite.swarm.peers(...args)
8+
return list.map(parsePeer)
9+
}
10+
11+
function parsePeer (peer) {
12+
const res = {}
13+
try {
14+
res.addr = Multiaddr(peer.addr)
15+
res.peer = PeerId.createFromB58String(peer.peer)
16+
res.muxer = peer.muxer
17+
} catch (error) {
18+
res.error = error
19+
res.rawPeerInfo = peer
20+
}
21+
if (peer.latency) {
22+
res.latency = peer.latency
23+
}
24+
if (peer.streams) {
25+
res.streams = peer.streams
26+
}
27+
return res
28+
}

test/utils/interface-common-factory.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,20 @@ function createFactory (options) {
2424
} else {
2525
setup = (callback) => {
2626
callback(null, {
27-
spawnNode (cb) {
28-
ipfsFactory.spawn(options.spawnOptions, (err, _ipfsd) => {
27+
spawnNode (repoPath, config, cb) {
28+
if (typeof repoPath === 'function') {
29+
cb = repoPath
30+
repoPath = null
31+
}
32+
33+
if (typeof config === 'function') {
34+
cb = config
35+
config = null
36+
}
37+
38+
const spawnOptions = { ...options.spawnOptions, repoPath, config }
39+
40+
ipfsFactory.spawn(spawnOptions, (err, _ipfsd) => {
2941
if (err) {
3042
return cb(err)
3143
}

0 commit comments

Comments
 (0)