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

Commit 33ec323

Browse files
committed
codestyle: fix all the linting problems
1 parent 6ac40ea commit 33ec323

File tree

4 files changed

+94
-88
lines changed

4 files changed

+94
-88
lines changed
Binary file not shown.

examples/transfer-files/complete/public/index.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h2>Files</h2>
3434
<label for="multihash">Mutihash</label>
3535
<input id="multihash" type="text" placeholder="Multihash"/>
3636
<button id="cat" type="button">Cat</button>
37-
<img id="picture" />
37+
<img id="picture" alt=""/>
3838
</div>
3939
<pre id="errors"></pre>
4040
</div>
@@ -43,13 +43,11 @@ <h2>Files</h2>
4343
<!-- The IPFS node module -->
4444
<script src="js/ipfs.js"></script>
4545
<!--
46-
The latest js-ipfs published version is also available through the unpkg CDN
46+
The latest js-ipfs published version is also available
47+
through the unpkg CDN, you can use it directly
4748
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
4849
-->
4950

50-
51-
52-
5351
<!-- Helper script to spawn our IPFS node instance -->
5452
<script src="js/create-node.js"></script>
5553

examples/transfer-files/complete/public/js/app.js

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
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
2822

2923
// Start IPFS instance
30-
const start = () => {
24+
function start () {
3125
if (!ipfs) {
3226
// Update the UI with initial settings
3327
updateView('starting', ipfs)
3428

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+
3538
// 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) => {
3840
if (err) {
39-
onError(err)
40-
return
41+
return onError(err)
4142
}
4243

4344
ipfs = node
@@ -59,9 +60,10 @@ const start = () => {
5960
// Stop IPFS instance
6061
const stop = () => {
6162
if (ipfs) {
62-
if (pollPeersTimer)
63+
if (pollPeersTimer) {
6364
clearInterval(pollPeersTimer)
64-
65+
}
66+
6567
ipfs.goOffline()
6668
ipfs = null
6769
updateView('stopped', ipfs)
@@ -100,7 +102,7 @@ const catFile = () => {
100102
// Display an error
101103
const onError = (e) => {
102104
console.error(e)
103-
errors.innerHTML = '<br/><span class="error">' + e.stack + '</span>'
105+
errors.innerHTML = "<br/><span class='error'>' + e.stack + '</span>"
104106
errors.className = 'error visible'
105107
}
106108

@@ -125,7 +127,7 @@ const onDrop = (event) => {
125127
// TODO: Promise reduce?
126128
for (var i = 0; i < files.length; i++) {
127129
const file = files[i]
128-
console.log("Add file", file.name, file.size)
130+
console.log('Add file', file.name, file.size)
129131
readFileContents(file)
130132
.then((buffer) => {
131133
// IPFS.files.add()
@@ -136,7 +138,7 @@ const onDrop = (event) => {
136138
}])
137139
})
138140
.then((files) => {
139-
console.log("Files added", files)
141+
console.log('Files added', files)
140142
multihashInput.value = files[0].hash
141143
filesStatus.innerHTML = files
142144
.map((e) => `Added ${e.path} as ${e.hash}`)
@@ -149,28 +151,31 @@ const onDrop = (event) => {
149151
// Get peers from IPFS and display them
150152
const updatePeers = () => {
151153
ipfs.swarm.peers((err, res) => {
154+
if (err) {
155+
// TODO ??
156+
}
152157
// PeerId.toJSON()
153158
// https://github.com/libp2p/js-peer-id/blob/3ef704ba32a97a9da26a1f821702cdd3f09c778f/src/index.js#L106
154159
// Multiaddr.toString()
155160
// https://multiformats.github.io/js-multiaddr/#multiaddrtostring
156161
const peersAsHtml = res
157162
.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>'
163168
})
164169
.join('')
165170

166-
peers.innerHTML = res.length > 0
171+
peers.innerHTML = res.length > 0
167172
? '<h2>Peers</h2>' + peersAsHtml
168173
: '<h2>Peers</h2><i>Waiting for peers...</i>'
169174
})
170175
}
171176

172177
/* UI functions */
173-
function initView() {
178+
function initView () {
174179
const initElement = (e, className) => {
175180
e.innerHTML = ''
176181
e.className = className
@@ -202,19 +207,19 @@ function initView() {
202207
catButton.addEventListener('click', catFile)
203208
}
204209

205-
function updateView(state, ipfs) {
210+
function updateView (state, ipfs) {
206211
if (state === 'ready') {
207212
// Set the header to display the current state
208213
output.innerHTML = '🚀 IPFS started'
209214
// 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
218223
// Set the file status
219224
filesStatus.innerHTML = '<i>Drop a picture here to add it to IPFS.</i>'
220225
details.className = 'visible'
@@ -232,4 +237,3 @@ function updateView(state, ipfs) {
232237

233238
// Start the app
234239
initView()
235-
Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
'use strict'
2-
/* eslint max-nested-callbacks: ["error", 8] */
3-
4-
// Start an IPFS instance
5-
window.startIpfs = (options, callback) => {
6-
const repoPath = options.IpfsDataDir || '/tmp/ipfs'
72

3+
/*
4+
* Create an IPFS node helper
5+
*/
6+
window.createNode = (options, callback) => {
7+
const repoPath = options.path || '/tmp/ipfs' + Math.random()
88
const node = new window.Ipfs(repoPath)
99

10-
node.init({ emptyRepo: true, bits: 2048 }, (err) => {
11-
if (err && err.message !== 'repo already exists') {
10+
node.init({ emptyRepo: true, bits: 2048 }, updateConfig)
11+
12+
function updateConfig (err) {
13+
if (err) {
1214
return callback(err)
1315
}
1416

@@ -17,34 +19,36 @@ window.startIpfs = (options, callback) => {
1719
return callback(err)
1820
}
1921

20-
const host = options.SignalServer.split(':')[0] || '127.0.0.1'
21-
const port = options.SignalServer.split(':')[1] || 9090
22-
const signalServer = `/libp2p-webrtc-star/ip4/${host}/tcp/${port}/ws/ipfs/${config.Identity.PeerID}`
22+
// TODO change to use wstar in DNS instead
23+
const host = options.signalAddr.split(':')[0] || '127.0.0.1'
24+
const port = options.signalAddr.split(':')[1] || 9090
2325

24-
config.Addresses = {
25-
Swarm: [
26-
signalServer
27-
],
28-
API: '',
29-
Gateway: ''
30-
}
26+
const wstarMultiaddr = `/libp2p-webrtc-star/ip4/${host}/tcp/${port}/ws/ipfs/${config.Identity.PeerID}`
3127

32-
config.Discovery.MDNS.Enabled = false
28+
config.Addresses.Swarm = [ wstarMultiaddr ]
29+
30+
node.config.replace(config, bootNode)
31+
})
32+
}
3333

34-
node.config.replace(config, (err) => {
35-
if (err) { return callback(err) }
34+
function bootNode (err) {
35+
if (err) {
36+
return callback(err)
37+
}
3638

37-
node.load((err) => {
38-
if (err) { return callback(err) }
39+
node.load((err) => {
40+
if (err) {
41+
return callback(err)
42+
}
3943

40-
node.goOnline((err) => {
41-
if (err) { return callback(err) }
44+
node.goOnline((err) => {
45+
if (err) {
46+
return callback(err)
47+
}
4248

43-
console.log('IPFS node is ready')
44-
callback(null, node)
45-
})
46-
})
49+
// console.log('IPFS node is ready')
50+
callback(null, node)
4751
})
4852
})
49-
})
53+
}
5054
}

0 commit comments

Comments
 (0)