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

Commit ead9181

Browse files
committed
Use js-ipfs directly instead of ipfs-daemon
1 parent 4768052 commit ead9181

File tree

3 files changed

+85
-14
lines changed

3 files changed

+85
-14
lines changed

examples/ipfm/src/App.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ class App extends Component {
3131
// Initialize our DataStore, ie. start IPFS
3232
dataStore = DataStore.init({
3333
// Directory to which save IPFS data to
34-
IpfsDataDir: '/tmp/ipfm/public222',
34+
IpfsDataDir: '/ipfs/ipfd-2',
3535
// IPFS dev server: webrtc-star-signalling.cloud.ipfs.team
3636
SignalServer: '188.166.203.82:20000',
37+
// Localhost webrtc-star server
38+
// SignalServer: '127.0.0.1:9090',
3739
})
3840

3941
dataStore.on('error', (e) => console.error(e))
@@ -94,10 +96,13 @@ class App extends Component {
9496
}
9597

9698
showPeers (show) {
97-
console.log("Show peers", show)
9899
this.setState({ showPeers: show })
99100
}
100101

102+
connectTo (multiaddr) {
103+
dataStore.connectToPeer(multiaddr)
104+
}
105+
101106
onDragEnter () {
102107
this.setState({ dragActive: true })
103108
}
@@ -127,8 +132,11 @@ class App extends Component {
127132

128133
const instructions = this.props.params && this.props.params.hash === undefined
129134
? <div className='App-instructions'>
130-
Open a feed by adding the feed name to the URL, eg.<br/>
135+
Open a file drop by adding the a name to the URL, eg.<br/>
131136
<a href="/hello-world">http://localhost:3000/hello-world</a>
137+
<br/>
138+
Share the link with others and once connected, <br/>
139+
you'll be able to share files between you.
132140
</div>
133141
: null
134142

@@ -149,7 +157,9 @@ class App extends Component {
149157
: null
150158

151159
const peersElement = this.state.showPeers
152-
? <Peers peers={peers} onClick={this.showPeers.bind(this, false)}/>
160+
? <Peers peers={peers}
161+
onConnectTo={this.connectTo.bind(this)}
162+
onClick={this.showPeers.bind(this, false)}/>
153163
: null
154164

155165
return (
@@ -159,7 +169,7 @@ class App extends Component {
159169
{previewElement}
160170
{peersElement}
161171
<img src={logo} className='App-logo' alt='logo' />
162-
<h1>InterPlanetary File Manager</h1>
172+
<h1>InterPlanetary File Exchange</h1>
163173
<Status className='App-status' text={status}/>
164174
{dropzone}
165175
{feedElement}

examples/ipfm/src/DataStore.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Need to include this to make webpack happy
2-
import { Buffer } from 'buffer' // eslint-disable-line no-unused-vars
2+
import { Buffer } from 'buffer/' // eslint-disable-line no-unused-vars
33
import EventEmitter from 'events'
4-
import IPFS from 'ipfs-daemon/src/ipfs-browser-daemon'
4+
import IPFS from './ipfs'
55
import OrbitDB from 'orbit-db'
66
import { readFileContents } from './read-file'
77

@@ -10,21 +10,24 @@ let instance
1010
class DataStore extends EventEmitter {
1111
constructor (options) {
1212
super()
13-
this.ipfs = new IPFS(options)
14-
this.ipfs.on('error', (e) => console.error(e))
15-
this.ipfs.on('ready', () => {
16-
this.timer = setInterval(() => this._updatePeers(), 1000)
13+
IPFS.create(options, (err, node) => {
14+
if (err) {
15+
console.log(err)
16+
}
17+
this.ipfs = node
1718
this.emit('ready')
1819
})
1920
}
2021

2122
// Open an orbit-db database and hook up to the emitted events
2223
openFeed (name) {
2324
this.orbitdb = new OrbitDB(this.ipfs)
24-
this.feed = this.orbitdb.feed(name, { cachePath: '/ipfm22/ipfm.cache' })
25+
this.feed = this.orbitdb.feed(name, { cachePath: '/ipfd/ipfd2.db' })
2526
this.feed.events.on('ready', () => this.emit('feed'))
2627
this.feed.events.on('history', () => this.emit('update'))
2728
this.feed.events.on('data', () => this.emit('update'))
29+
if (this.timer) clearInterval(this.timer)
30+
this.timer = setInterval(() => this._updatePeers(name), 1000)
2831
}
2932

3033
getFile (hash) {
@@ -76,9 +79,16 @@ class DataStore extends EventEmitter {
7679
.catch((e) => console.error(e))
7780
}
7881

82+
connectToPeer (multiaddr) {
83+
console.log("Connect to:", multiaddr)
84+
this.ipfs.swarm.connect(multiaddr)
85+
.then((res) => console.log("Connected to", multiaddr))
86+
.catch((e) => console.error(e))
87+
}
88+
7989
// Get peers from IPFS and emit 'peers' event after updated
80-
_updatePeers () {
81-
this.ipfs.swarm.peers()
90+
_updatePeers (feed) {
91+
this.ipfs.pubsub.peers(feed)
8292
.then((peers) => this.emit('peers', peers))
8393
.catch((e) => console.error(e))
8494
}

examples/ipfm/src/ipfs.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// import { Buffer } from 'buffer' // eslint-disable-line no-unused-vars
2+
const IPFS = require('ipfs')
3+
4+
function create (options, callback) {
5+
const repoPath = options.IpfsDataDir || '/ipfd/tmp/'
6+
7+
const node = new IPFS(repoPath)
8+
9+
node.init({ emptyRepo: true, bits: 2048 }, (err) => {
10+
if (err && err.message !== 'repo already exists') {
11+
return callback(err)
12+
}
13+
14+
node.config.get((err, config) => {
15+
if (err) {
16+
return callback(err)
17+
}
18+
19+
const host = options.SignalServer.split(':')[0] || '127.0.0.1'
20+
const port = options.SignalServer.split(':')[1] || 9090
21+
const signalServer = `/libp2p-webrtc-star/ip4/${host}/tcp/${port}/ws/ipfs/${config.Identity.PeerID}`
22+
23+
config.Addresses = {
24+
Swarm: [
25+
signalServer
26+
],
27+
API: '',
28+
Gateway: ''
29+
}
30+
31+
config.Discovery.MDNS.Enabled = false
32+
33+
node.config.replace(config, (err) => {
34+
if (err) { return callback(err) }
35+
36+
node.load((err) => {
37+
if (err) { return callback(err) }
38+
node.goOnline((err) => {
39+
if (err) { return callback(err) }
40+
console.log('IPFS node is ready')
41+
callback(null, node)
42+
})
43+
})
44+
})
45+
})
46+
})
47+
}
48+
49+
module.exports = {
50+
create: create
51+
}

0 commit comments

Comments
 (0)