1
- const rootElement = document . getElementById ( "ipfs" )
2
- const startButton = document . getElementById ( "start" )
3
- const stopButton = document . getElementById ( "stop" )
4
- const output = document . getElementById ( "state" )
5
- const details = document . getElementById ( "details" )
6
- const peers = document . getElementById ( "peers" )
7
- const errors = document . getElementById ( "errors" )
8
- const directory = document . getElementById ( "directory" )
9
- const dirInput = document . getElementById ( "dir" )
10
- const signalServerInput = document . getElementById ( "signalServerInput" )
11
- const files = document . getElementById ( "files" )
12
- const filesStatus = document . getElementById ( "filesStatus" )
13
- const picture = document . getElementById ( "picture" )
14
- const multihashInput = document . getElementById ( "multihash" )
15
- const catButton = document . getElementById ( "cat" )
16
-
17
- let ipfs , peerInfo , pollPeersTimer
18
-
19
- const ipfsOptions = {
20
- // Directory to which save IPFS data to
21
- IpfsDataDir : dirInput . value ,
22
- // IPFS dev server: webrtc-star-signalling.cloud.ipfs.team
23
- SignalServer : signalServerInput . value ,
24
- // Local webrtc-star server, you can get it from:
25
- // https://github.com/libp2p/js-libp2p-webrtc-star
26
- // SignalServer: '127.0.0.1:9090',
27
- }
1
+ /* global Blob, URL, FileReader */
2
+
3
+ const rootElement = document . getElementById ( 'ipfs' )
4
+ const startButton = document . getElementById ( 'start' )
5
+ const stopButton = document . getElementById ( 'stop' )
6
+ const output = document . getElementById ( 'state' )
7
+ const details = document . getElementById ( 'details' )
8
+ const peers = document . getElementById ( 'peers' )
9
+ const errors = document . getElementById ( 'errors' )
10
+ const directory = document . getElementById ( 'directory' )
11
+ const dirInput = document . getElementById ( 'dir' )
12
+ const signalServerInput = document . getElementById ( 'signalServerInput' )
13
+ const files = document . getElementById ( 'files' )
14
+ const filesStatus = document . getElementById ( 'filesStatus' )
15
+ const picture = document . getElementById ( 'picture' )
16
+ const multihashInput = document . getElementById ( 'multihash' )
17
+ const catButton = document . getElementById ( 'cat' )
18
+
19
+ let ipfs
20
+ let peerInfo
21
+ let pollPeersTimer
28
22
29
23
// Start IPFS instance
30
- const start = ( ) => {
24
+ function start ( ) {
31
25
if ( ! ipfs ) {
32
26
// Update the UI with initial settings
33
27
updateView ( 'starting' , ipfs )
34
28
29
+ /*
30
+ * path - 'dirname' of where the IPFS repo is stored
31
+ * signallAddr - address of the signalling server
32
+ */
33
+ const options = {
34
+ path : dirInput . value ,
35
+ signalAddr : signalServerInput . value
36
+ }
37
+
35
38
// Create an IPFS instance
36
- // window.startIpfs() is exposed in ./start-ipfs.js
37
- window . startIpfs ( ipfsOptions , ( err , node ) => {
39
+ window . createNode ( options , ( err , node ) => {
38
40
if ( err ) {
39
- onError ( err )
40
- return
41
+ return onError ( err )
41
42
}
42
43
43
44
ipfs = node
@@ -59,9 +60,10 @@ const start = () => {
59
60
// Stop IPFS instance
60
61
const stop = ( ) => {
61
62
if ( ipfs ) {
62
- if ( pollPeersTimer )
63
+ if ( pollPeersTimer ) {
63
64
clearInterval ( pollPeersTimer )
64
-
65
+ }
66
+
65
67
ipfs . goOffline ( )
66
68
ipfs = null
67
69
updateView ( 'stopped' , ipfs )
@@ -100,7 +102,7 @@ const catFile = () => {
100
102
// Display an error
101
103
const onError = ( e ) => {
102
104
console . error ( e )
103
- errors . innerHTML = ' <br/><span class=" error" >' + e . stack + '</span>'
105
+ errors . innerHTML = " <br/><span class=' error' >' + e.stack + '</span>"
104
106
errors . className = 'error visible'
105
107
}
106
108
@@ -125,7 +127,7 @@ const onDrop = (event) => {
125
127
// TODO: Promise reduce?
126
128
for ( var i = 0 ; i < files . length ; i ++ ) {
127
129
const file = files [ i ]
128
- console . log ( " Add file" , file . name , file . size )
130
+ console . log ( ' Add file' , file . name , file . size )
129
131
readFileContents ( file )
130
132
. then ( ( buffer ) => {
131
133
// IPFS.files.add()
@@ -136,7 +138,7 @@ const onDrop = (event) => {
136
138
} ] )
137
139
} )
138
140
. then ( ( files ) => {
139
- console . log ( " Files added" , files )
141
+ console . log ( ' Files added' , files )
140
142
multihashInput . value = files [ 0 ] . hash
141
143
filesStatus . innerHTML = files
142
144
. map ( ( e ) => `Added ${ e . path } as ${ e . hash } ` )
@@ -149,28 +151,31 @@ const onDrop = (event) => {
149
151
// Get peers from IPFS and display them
150
152
const updatePeers = ( ) => {
151
153
ipfs . swarm . peers ( ( err , res ) => {
154
+ if ( err ) {
155
+ // TODO ??
156
+ }
152
157
// PeerId.toJSON()
153
158
// https://github.com/libp2p/js-peer-id/blob/3ef704ba32a97a9da26a1f821702cdd3f09c778f/src/index.js#L106
154
159
// Multiaddr.toString()
155
160
// https://multiformats.github.io/js-multiaddr/#multiaddrtostring
156
161
const peersAsHtml = res
157
162
. map ( ( e , idx ) => {
158
- return ( idx + 1 ) + '.'
159
- + e . peer . id . toJSON ( ) . id
160
- + '<br>'
161
- + e . addr . toString ( )
162
- + '<br>'
163
+ return ( idx + 1 ) + '.' +
164
+ e . peer . id . toJSON ( ) . id +
165
+ '<br>' +
166
+ e . addr . toString ( ) +
167
+ '<br>'
163
168
} )
164
169
. join ( '' )
165
170
166
- peers . innerHTML = res . length > 0
171
+ peers . innerHTML = res . length > 0
167
172
? '<h2>Peers</h2>' + peersAsHtml
168
173
: '<h2>Peers</h2><i>Waiting for peers...</i>'
169
174
} )
170
175
}
171
176
172
177
/* UI functions */
173
- function initView ( ) {
178
+ function initView ( ) {
174
179
const initElement = ( e , className ) => {
175
180
e . innerHTML = ''
176
181
e . className = className
@@ -202,19 +207,19 @@ function initView() {
202
207
catButton . addEventListener ( 'click' , catFile )
203
208
}
204
209
205
- function updateView ( state , ipfs ) {
210
+ function updateView ( state , ipfs ) {
206
211
if ( state === 'ready' ) {
207
212
// Set the header to display the current state
208
213
output . innerHTML = '🚀 IPFS started'
209
214
// Display IPFS info
210
- details . innerHTML = '<div>'
211
- + '<h2>IPFS Node</h2>'
212
- + '<b>ID</b><br>'
213
- + peerInfo . id + '<br><br>'
214
- + '<b>Address</b><br>'
215
- + peerInfo . addresses [ 0 ] + '<br><br>'
216
- + '<b>IPFS Data Directory</b><br>'
217
- + dirInput . value
215
+ details . innerHTML = '<div>' +
216
+ '<h2>IPFS Node</h2>' +
217
+ '<b>ID</b><br>' +
218
+ peerInfo . id + '<br><br>' +
219
+ '<b>Address</b><br>' +
220
+ peerInfo . addresses [ 0 ] + '<br><br>' +
221
+ '<b>IPFS Data Directory</b><br>' +
222
+ dirInput . value
218
223
// Set the file status
219
224
filesStatus . innerHTML = '<i>Drop a picture here to add it to IPFS.</i>'
220
225
details . className = 'visible'
@@ -232,4 +237,3 @@ function updateView(state, ipfs) {
232
237
233
238
// Start the app
234
239
initView ( )
235
-
0 commit comments