This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
app.js
112 lines (101 loc) · 2.56 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict'
const React = require('react')
const IPFS = require('../../../../src/core') // replace this by line below
// const IPFS = require('ipfs')
const stringToUse = 'hello world from webpacked IPFS'
class App extends React.Component {
constructor (props) {
super(props)
this.state = {
id: null,
version: null,
protocol_version: null,
added_file_hash: null,
added_file_contents: null
}
}
componentDidMount () {
const self = this
let node
create()
function create () {
// Create the IPFS node instance
// for simplicity, we create a new repo everytime the node
// is created, because you can't init already existing repos
const repoPath = String(Math.random())
node = new IPFS({
repo: repoPath,
init: false,
start: false,
EXPERIMENTAL: {
pubsub: false
}
})
node.init({ emptyRepo: true, bits: 2048 }, function (err) {
if (err) {
throw err
}
node.start(function (err) {
if (err) {
throw err
}
console.log('IPFS node is ready')
ops()
})
})
}
function ops () {
node.id((err, res) => {
if (err) {
throw err
}
self.setState({
id: res.id,
version: res.agentVersion,
protocol_version: res.protocolVersion
})
})
node.files.add([new Buffer(stringToUse)], (err, res) => {
if (err) {
throw err
}
const hash = res[0].hash
self.setState({added_file_hash: hash})
node.files.cat(hash, (err, res) => {
if (err) {
throw err
}
let data = ''
res.on('data', (d) => {
data = data + d
})
res.on('end', () => {
self.setState({added_file_contents: data})
})
})
})
}
}
render () {
return (
<div style={{textAlign: 'center'}}>
<h1>Everything is working!</h1>
<p>Your ID is <strong>{this.state.id}</strong></p>
<p>Your IPFS version is <strong>{this.state.version}</strong></p>
<p>Your IPFS protocol version is <strong>{this.state.protocol_version}</strong></p>
<hr />
<div>
Added a file! <br />
{this.state.added_file_hash}
</div>
<br />
<br />
<p>
Contents of this file: <br />
{this.state.added_file_contents}
</p>
</div>
)
}
}
module.exports = App