Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 80f0057

Browse files
authored
refactor: return peer IDs as strings not CIDs (#2729)
Also updates all examples to use the new API. Depends on: - [x] ipfs-inactive/interface-js-ipfs-core#581 - [x] ipfs-inactive/js-ipfs-http-client#1226 - [x] libp2p/js-libp2p#545 BREAKING CHANGE: Where `PeerID`s were previously [CID](https://www.npmjs.com/package/cids)s, now they are Strings - `ipfs.bitswap.stat().peers[n]` is now a String (was a CID) - `ipfs.dht.findPeer().id` is now a String (was a CID) - `ipfs.dht.findProvs()[n].id` is now a String (was a CID) - `ipfs.dht.provide()[n].id` is now a String (was a CID) - `ipfs.dht.put()[n].id` is now a String (was a CID) - `ipfs.dht.query()[n].id` is now a String (was a CID) - `ipfs.id().id` is now a String (was a CID) - `ipfs.id().addresses[n]` are now [Multiaddr](https://www.npmjs.com/package/multiaddr)s (were Strings)
1 parent 93ad92d commit 80f0057

File tree

22 files changed

+164
-137
lines changed

22 files changed

+164
-137
lines changed

browser-video-streaming/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<body>
33
<video id="video" controls></video>
44
<script src="../../dist/index.js"></script>
5-
<script src="https://unpkg.com/hlsjs-ipfs-loader@0.1.4/dist/index.js"></script>
5+
<script src="https://unpkg.com/hlsjs-ipfs-loader@0.2.3/dist/index.js"></script>
66
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
77
<script src="streaming.js"></script>
88
</body>

browser-video-streaming/streaming.js

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
/* global Hls Ipfs HlsjsIpfsLoader */
44
/* eslint-env browser */
5-
65
document.addEventListener('DOMContentLoaded', async () => {
76
const testHash = 'QmdpAidwAsBGptFB3b6A9Pyi5coEbgjHrL3K2Qrsutmj9K'
87
const repoPath = 'ipfs-' + Math.random()

circuit-relaying/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"license": "MIT",
1616
"dependencies": {
1717
"ipfs": "file:../../",
18-
"ipfs-pubsub-room": "^1.4.0"
18+
"ipfs-pubsub-room": "^2.0.1"
1919
},
2020
"devDependencies": {
2121
"aegir": "^20.0.0",

circuit-relaying/src/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const mkRoomName = (name) => {
1515

1616
module.exports = (ipfs, peersSet) => {
1717
const createRoom = (name) => {
18-
const room = Room(ipfs, mkRoomName(name))
18+
const room = new Room(ipfs, mkRoomName(name))
1919

2020
room.on('peer joined', (peer) => {
2121
console.log('peer ' + peer + ' joined')

circuit-relaying/test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ async function runTest () {
6666

6767
try {
6868
const id = await ipfsd.api.id()
69-
const address = id.addresses.filter(addr => addr.includes('/ws/ipfs/Qm')).pop()
69+
const address = id.addresses
70+
.map(ma => ma.toString())
71+
.find(addr => addr.includes('/ws/p2p/Qm'))
7072

7173
if (!address) {
7274
throw new Error(`Could not find web socket address in ${id.addresses}`)

custom-ipfs-repo/index.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const IPFS = require('ipfs')
44
const Repo = require('ipfs-repo')
55
const fsLock = require('ipfs-repo/src/lock')
6+
const all = require('it-all')
67

78
// Create our custom options
89
const customRepositoryOptions = {
@@ -79,19 +80,19 @@ async function main () {
7980
console.log('Version:', version)
8081

8182
// Once we have the version, let's add a file to IPFS
82-
const filesAdded = await node.add({
83+
for await (const file of node.add({
8384
path: 'test-data.txt',
8485
content: Buffer.from('We are using a customized repo!')
85-
})
86-
87-
// Log out the added files metadata and cat the file from IPFS
88-
console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
86+
})) {
87+
// Log out the added files metadata and cat the file from IPFS
88+
console.log('\nAdded file:', file.path, file.cid)
8989

90-
const data = await node.cat(filesAdded[0].hash)
90+
const data = Buffer.concat(await all(node.cat(file.cid)))
9191

92-
// Print out the files contents to console
93-
console.log('\nFetched file content:')
94-
process.stdout.write(data)
92+
// Print out the files contents to console
93+
console.log('\nFetched file content:')
94+
process.stdout.write(data)
95+
}
9596

9697
// After everything is done, shut the node down
9798
console.log('\n\nStopping the node')

custom-ipfs-repo/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"dependencies": {
1212
"datastore-fs": "^0.9.1",
1313
"ipfs": "file:../../",
14-
"ipfs-repo": "^0.28.0"
14+
"ipfs-repo": "^0.28.0",
15+
"it-all": "^1.0.1"
1516
},
1617
"devDependencies": {
1718
"execa": "^3.2.0"

custom-libp2p/index.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
'use strict'
22

33
const Libp2p = require('libp2p')
4-
const IPFS = require('ipfs')
4+
const IPFS = require('../../')
55
const TCP = require('libp2p-tcp')
66
const MulticastDNS = require('libp2p-mdns')
7-
const WebSocketStar = require('libp2p-websocket-star')
87
const Bootstrap = require('libp2p-bootstrap')
98
const SPDY = require('libp2p-spdy')
109
const KadDHT = require('libp2p-kad-dht')
11-
const MPLEX = require('pull-mplex')
10+
const MPLEX = require('libp2p-mplex')
1211
const SECIO = require('libp2p-secio')
1312

1413
/**
@@ -32,11 +31,6 @@ const libp2pBundle = (opts) => {
3231
const peerBook = opts.peerBook
3332
const bootstrapList = opts.config.Bootstrap
3433

35-
// Create our WebSocketStar transport and give it our PeerId, straight from the ipfs node
36-
const wsstar = new WebSocketStar({
37-
id: peerInfo.id
38-
})
39-
4034
// Build and return our libp2p node
4135
return new Libp2p({
4236
peerInfo,
@@ -49,8 +43,7 @@ const libp2pBundle = (opts) => {
4943
},
5044
modules: {
5145
transport: [
52-
TCP,
53-
wsstar
46+
TCP
5447
],
5548
streamMuxer: [
5649
MPLEX,
@@ -61,8 +54,7 @@ const libp2pBundle = (opts) => {
6154
],
6255
peerDiscovery: [
6356
MulticastDNS,
64-
Bootstrap,
65-
wsstar.discovery
57+
Bootstrap
6658
],
6759
dht: KadDHT
6860
},

custom-libp2p/package.json

+9-11
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@
99
},
1010
"license": "MIT",
1111
"dependencies": {
12-
"ipfs": "file:../../",
13-
"libp2p": "^0.26.2",
14-
"libp2p-bootstrap": "~0.9.7",
15-
"libp2p-kad-dht": "~0.16.0",
16-
"libp2p-mdns": "~0.12.2",
17-
"libp2p-secio": "~0.11.1",
18-
"libp2p-spdy": "~0.13.3",
19-
"libp2p-tcp": "~0.13.0",
20-
"libp2p-websocket-star": "~0.10.2",
21-
"pull-mplex": "~0.1.0"
12+
"libp2p": "^0.27.0-rc.0",
13+
"libp2p-bootstrap": "^0.10.3",
14+
"libp2p-kad-dht": "^0.18.3",
15+
"libp2p-mdns": "^0.13.1",
16+
"libp2p-mplex": "^0.9.3",
17+
"libp2p-secio": "^0.12.2",
18+
"libp2p-spdy": "^0.13.3",
19+
"libp2p-tcp": "^0.14.3"
2220
},
2321
"devDependencies": {
24-
"execa": "^3.2.0"
22+
"execa": "^4.0.0"
2523
}
2624
}

custom-libp2p/test.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ const execa = require('execa')
55
const Libp2p = require('libp2p')
66
const TCP = require('libp2p-tcp')
77
const SPDY = require('libp2p-spdy')
8-
const MPLEX = require('pull-mplex')
8+
const MPLEX = require('libp2p-mplex')
99
const SECIO = require('libp2p-secio')
1010
const PeerInfo = require('peer-info')
1111
const PeerId = require('peer-id')
1212
const multiaddr = require('multiaddr')
13-
const promisify = require('promisify-es6')
14-
const PeerBook = require('peer-book')
1513

1614
async function test () {
1715
let output = ''
@@ -31,12 +29,11 @@ async function test () {
3129

3230
console.info('Dialling', address)
3331

34-
const peerInfo = new PeerInfo(await promisify(PeerId.create)())
32+
const peerInfo = new PeerInfo(await PeerId.create())
3533
peerInfo.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/0'))
3634

3735
const libp2p = new Libp2p({
3836
peerInfo,
39-
peerBook: new PeerBook(),
4037
modules: {
4138
transport: [
4239
TCP

exchange-files-in-browser/README.md

+26-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This tutorial will help you exchange files between browser nodes and go-ipfs or js-ipfs nodes!
44

5-
**Note:** As `js-ipfs@0.33.x` currently doesn't support DHT peer discovery, the peer from which you are fetching data should be within the reach (local or in public IP) of the browser node.
5+
**Note:** As `js-ipfs@0.41.x` currently doesn't support DHT peer discovery, the peer from which you are fetching data should be within the reach (local or in public IP) of the browser node.
66

77
That being said, we will explain how to circumvent these caveats and once they are fixed, we'll update the tutorial as well.
88

@@ -35,9 +35,10 @@ Here's what we are going to be doing:
3535

3636
1. Install a `go-ipfs` or `js-ipfs` node in your machine
3737
2. Make your daemons listen on WebSockets
38-
3. Start the app
39-
4. Dial to a node using WebSockets (your desktop ones)
40-
5. Transfer files between all of your nodes!
38+
3. Start a `libp2p-webrtc-star` signaling server
39+
4. Start the app
40+
5. Dial to a node using WebSockets (your desktop ones)
41+
6. Transfer files between all of your nodes!
4142

4243
Just follow the instructions below and it will be up and running in no time!
4344

@@ -121,7 +122,25 @@ Daemon is ready
121122

122123
Check the `/ws` in line 5, that means it is listening. Cool.
123124

124-
### 3. Start the app
125+
### 3. Start a `libp2p-webrtc-star` signaling server
126+
127+
This server allows the two browser nodes to talk to each other by doing the initial handshake and network introductions.
128+
129+
First install the `libp2p-webrtc-star` module globally:
130+
131+
```sh
132+
> npm install -g libp2p-webrtc-star
133+
```
134+
135+
This will give you the `webrtc-star` command. Use this to start a signaling server:
136+
137+
```sh
138+
> webrtc-star
139+
```
140+
141+
By default it will listen to all incoming connections on port 13579. Override this with the `--host` and/or `--port` options.
142+
143+
### 4. Start the app
125144

126145
Make sure you're in `js-ipfs/examples/exchange-files-in-browser`.
127146

@@ -147,7 +166,7 @@ Hit CTRL-C to stop the server
147166

148167
Now go to http://127.0.0.1:12345 in a modern browser and you're on!
149168

150-
### 4. Dial to a node using WebSockets (your desktop ones)
169+
### 5. Dial to a node using WebSockets (your desktop ones)
151170

152171
Make sure you have a daemon running. If you don't, run:
153172

@@ -179,7 +198,7 @@ Check that you got connected:
179198
180199
[js-libp2p-crypto#105]: https://github.com/libp2p/js-libp2p-crypto/issues/105
181200

182-
### 5. Transfer files between all of your nodes!
201+
### 6. Transfer files between all of your nodes!
183202

184203
Now you can add files through the CLI with:
185204

0 commit comments

Comments
 (0)