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

Commit 27d5a57

Browse files
committed
feat: get Ping to work properly
1 parent 2029e05 commit 27d5a57

File tree

32 files changed

+102
-106
lines changed

32 files changed

+102
-106
lines changed

examples/browser-webpack/src/components/app.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ class App extends React.Component {
4949
if (err) { throw err }
5050

5151
const hash = filesAdded[0].hash
52-
self.setState({added_file_hash: hash})
52+
self.setState({ added_file_hash: hash })
5353

5454
node.files.cat(hash, (err, data) => {
5555
if (err) { throw err }
56-
self.setState({added_file_contents: data.toString()})
56+
self.setState({ added_file_contents: data.toString() })
5757
})
5858
})
5959
}
6060
}
6161
render () {
6262
return (
63-
<div style={{textAlign: 'center'}}>
63+
<div style={{ textAlign: 'center' }}>
6464
<h1>Everything is working!</h1>
6565
<p>Your ID is <strong>{this.state.id}</strong></p>
6666
<p>Your IPFS version is <strong>{this.state.version}</strong></p>

examples/run-in-electron/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const url = require('url')
1111
let mainWindow
1212

1313
function createWindow () {
14-
mainWindow = new BrowserWindow({width: 800, height: 600})
14+
mainWindow = new BrowserWindow({ width: 800, height: 600 })
1515

1616
// and load the index.html of the app.
1717
mainWindow.loadURL(url.format({

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"execa": "^1.0.0",
6969
"form-data": "^2.3.3",
7070
"hat": "0.0.3",
71-
"interface-ipfs-core": "~0.83.0",
71+
"interface-ipfs-core": "~0.84.2",
7272
"ipfsd-ctl": "~0.39.5",
7373
"ncp": "^2.0.0",
7474
"qs": "^6.5.2",
@@ -98,7 +98,7 @@
9898
"hoek": "^5.0.4",
9999
"human-to-milliseconds": "^1.0.0",
100100
"interface-datastore": "~0.6.0",
101-
"ipfs-api": "^26.0.3",
101+
"ipfs-api": "^26.1.0",
102102
"ipfs-bitswap": "~0.21.0",
103103
"ipfs-block": "~0.8.0",
104104
"ipfs-block-service": "~0.15.1",
@@ -150,6 +150,7 @@
150150
"progress": "^2.0.1",
151151
"promisify-es6": "^1.0.3",
152152
"pull-abortable": "^4.1.1",
153+
"pull-catch": "^1.0.0",
153154
"pull-defer": "~0.2.3",
154155
"pull-file": "^1.1.0",
155156
"pull-ndjson": "~0.1.1",

src/cli/bin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const print = utils.print
1111
const mfs = require('ipfs-mfs/cli')
1212
const debug = require('debug')('ipfs:cli')
1313

14-
const pkg = readPkgUp.sync({cwd: __dirname}).pkg
14+
const pkg = readPkgUp.sync({ cwd: __dirname }).pkg
1515
updateNotifier({
1616
pkg,
1717
updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week

src/cli/commands/file/ls.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = {
2020

2121
// Single file? Then print its hash
2222
if (links.length === 0) {
23-
links = [{hash: path}]
23+
links = [{ hash: path }]
2424
}
2525

2626
links.forEach((file) => print(file.hash))

src/cli/commands/files/add.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ module.exports = {
227227
if (argv.progress) {
228228
const bar = createProgressBar(totalBytes)
229229
options.progress = function (byteLength) {
230-
bar.update(byteLength / totalBytes, {progress: byteman(byteLength, 2, 'MB')})
230+
bar.update(byteLength / totalBytes, { progress: byteman(byteLength, 2, 'MB') })
231231
}
232232
}
233233

src/cli/commands/ls.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = {
3939
}
4040

4141
if (argv.headers) {
42-
links = [{hash: 'Hash', size: 'Size', name: 'Name'}].concat(links)
42+
links = [{ hash: 'Hash', size: 'Size', name: 'Name' }].concat(links)
4343
}
4444

4545
const multihashWidth = Math.max.apply(null, links.map((file) => file.hash.length))

src/cli/commands/object/get.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
},
1616

1717
handler (argv) {
18-
argv.ipfs.object.get(argv.key, {enc: 'base58'}, (err, node) => {
18+
argv.ipfs.object.get(argv.key, { enc: 'base58' }, (err, node) => {
1919
if (err) {
2020
throw err
2121
}

src/cli/commands/object/put.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const fs = require('fs')
55
const print = require('../../utils').print
66

77
function putNode (buf, enc, ipfs) {
8-
ipfs.object.put(buf, {enc: enc}, (err, node) => {
8+
ipfs.object.put(buf, { enc: enc }, (err, node) => {
99
if (err) {
1010
throw err
1111
}

src/cli/commands/repo/stat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
},
1616

1717
handler (argv) {
18-
argv.ipfs.repo.stat({human: argv.human}, (err, stats) => {
18+
argv.ipfs.repo.stat({ human: argv.human }, (err, stats) => {
1919
if (err) {
2020
throw err
2121
}

src/cli/commands/stats/repo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
},
1616

1717
handler (argv) {
18-
argv.ipfs.stats.repo({human: argv.human}, (err, stats) => {
18+
argv.ipfs.stats.repo({ human: argv.human }, (err, stats) => {
1919
if (err) {
2020
throw err
2121
}

src/core/components/bootstrap.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = function bootstrap (self) {
2323
if (err) {
2424
return callback(err)
2525
}
26-
callback(null, {Peers: config.Bootstrap})
26+
callback(null, { Peers: config.Bootstrap })
2727
})
2828
}),
2929
add: promisify((multiaddr, args, callback) => {
@@ -59,7 +59,7 @@ module.exports = function bootstrap (self) {
5959
rm: promisify((multiaddr, args, callback) => {
6060
if (typeof args === 'function') {
6161
callback = args
62-
args = {all: false}
62+
args = { all: false }
6363
}
6464
if (multiaddr && !isValidMultiaddr(multiaddr)) {
6565
return setImmediate(() => callback(invalidMultiaddrError(multiaddr)))
@@ -85,7 +85,7 @@ module.exports = function bootstrap (self) {
8585
res.push(multiaddr)
8686
}
8787

88-
callback(null, {Peers: res})
88+
callback(null, { Peers: res })
8989
})
9090
})
9191
})

src/core/components/init.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module.exports = function init (self) {
9898
self.log('repo opened')
9999
if (opts.pass) {
100100
self.log('creating keychain')
101-
const keychainOptions = Object.assign({passPhrase: opts.pass}, config.Keychain)
101+
const keychainOptions = Object.assign({ passPhrase: opts.pass }, config.Keychain)
102102
self._keychain = new Keychain(self._repo.keys, keychainOptions)
103103
self._keychain.importPeer('self', { privKey: privateKey }, cb)
104104
} else {

src/core/components/pin.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,17 @@ module.exports = (self) => {
333333
const { recursive, direct, all } = types
334334
// recursive
335335
if ((type === recursive || type === all) && recursivePins.has(key)) {
336-
return callback(null, {pinned: true, reason: recursive})
336+
return callback(null, { pinned: true, reason: recursive })
337337
}
338338
if ((type === recursive)) {
339-
return callback(null, {pinned: false})
339+
return callback(null, { pinned: false })
340340
}
341341
// direct
342342
if ((type === direct || type === all) && directPins.has(key)) {
343-
return callback(null, {pinned: true, reason: direct})
343+
return callback(null, { pinned: true, reason: direct })
344344
}
345345
if ((type === direct)) {
346-
return callback(null, {pinned: false})
346+
return callback(null, { pinned: false })
347347
}
348348

349349
// indirect (default)

src/core/components/ping-pull-stream.js

+27-31
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
55
const PeerId = require('peer-id')
66
const pull = require('pull-stream')
77
const Pushable = require('pull-pushable')
8-
const waterfall = require('async/waterfall')
98

109
const log = debug('jsipfs:pingPullStream')
1110
log.error = debug('jsipfs:pingPullStream:error')
@@ -20,15 +19,20 @@ module.exports = function pingPullStream (self) {
2019

2120
const source = Pushable()
2221

23-
waterfall([
24-
(cb) => getPeer(self._libp2pNode, source, peerId, cb),
25-
(peer, cb) => runPing(self._libp2pNode, source, opts.count, peer, cb)
26-
], (err) => {
22+
getPeer(self._libp2pNode, source, peerId, (err, peer) => {
2723
if (err) {
2824
log.error(err)
29-
source.push(getPacket({ success: false, text: err.toString() }))
3025
source.end(err)
26+
return
3127
}
28+
29+
runPing(self._libp2pNode, source, opts.count, peer, (err) => {
30+
if (err) {
31+
log.error(err)
32+
source.push(getPacket({ success: false, text: err.toString() }))
33+
source.end()
34+
}
35+
})
3236
})
3337

3438
return source
@@ -41,45 +45,40 @@ function getPacket (msg) {
4145
return Object.assign(basePacket, msg)
4246
}
4347

44-
function getPeer (libp2pNode, statusStream, peerId, cb) {
45-
let peer
48+
function getPeer (libp2pNode, statusStream, peerIdStr, cb) {
49+
let peerId
50+
51+
try {
52+
peerId = PeerId.createFromB58String(peerIdStr)
53+
} catch (err) {
54+
return cb(err)
55+
}
56+
57+
let peerInfo
4658

4759
try {
48-
peer = libp2pNode.peerBook.get(peerId)
60+
peerInfo = libp2pNode.peerBook.get(peerId)
4961
} catch (err) {
5062
log('Peer not found in peer book, trying peer routing')
51-
// Share lookup status just as in the go implemmentation
52-
statusStream.push(getPacket({ text: `Looking up peer ${peerId}` }))
53-
54-
// Try to use peerRouting
55-
try {
56-
peerId = PeerId.createFromB58String(peerId)
57-
} catch (err) {
58-
return cb(Object.assign(err, {
59-
message: `failed to parse peer address '${peerId}': input isn't valid multihash`
60-
}))
61-
}
6263

64+
// Share lookup status just as in the go implemmentation
65+
statusStream.push(getPacket({ text: `Looking up peer ${peerIdStr}` }))
6366
return libp2pNode.peerRouting.findPeer(peerId, cb)
6467
}
6568

66-
cb(null, peer)
69+
cb(null, peerInfo)
6770
}
6871

6972
function runPing (libp2pNode, statusStream, count, peer, cb) {
7073
libp2pNode.ping(peer, (err, p) => {
71-
if (err) {
72-
return cb(err)
73-
}
74-
75-
log('Got peer', peer)
74+
if (err) { return cb(err) }
7675

7776
let packetCount = 0
7877
let totalTime = 0
7978
statusStream.push(getPacket({ text: `PING ${peer.id.toB58String()}` }))
8079

8180
p.on('ping', (time) => {
82-
statusStream.push(getPacket({ time: time }))
81+
statusStream.push(getPacket({ time }))
8382
totalTime += time
8483
packetCount++
8584
if (packetCount >= count) {
@@ -93,12 +92,9 @@ function runPing (libp2pNode, statusStream, count, peer, cb) {
9392
p.on('error', (err) => {
9493
log.error(err)
9594
p.stop()
96-
statusStream.push(getPacket({ success: false, text: err.toString() }))
97-
statusStream.end(err)
95+
cb(err)
9896
})
9997

10098
p.start()
101-
102-
return cb()
10399
})
104100
}

src/core/components/ping.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ const promisify = require('promisify-es6')
44
const pull = require('pull-stream/pull')
55

66
module.exports = function ping (self) {
7-
return promisify((peerId, opts, cb) => {
7+
return promisify((peerId, opts, callback) => {
88
if (typeof opts === 'function') {
9-
cb = opts
9+
callback = opts
1010
opts = {}
1111
}
1212

1313
pull(
1414
self.pingPullStream(peerId, opts),
15-
pull.collect(cb)
15+
pull.collect(callback)
1616
)
1717
})
1818
}

src/core/components/pre-start.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module.exports = function preStart (self) {
4848
if (self._keychain) {
4949
// most likely an init or upgrade has happened
5050
} else if (pass) {
51-
const keychainOptions = Object.assign({passPhrase: pass}, config.Keychain)
51+
const keychainOptions = Object.assign({ passPhrase: pass }, config.Keychain)
5252
self._keychain = new Keychain(self._repo.keys, keychainOptions)
5353
self.log('keychain constructed')
5454
} else {

src/core/runtime/dns-browser.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* global self */
12
'use strict'
23

34
module.exports = (domain, opts, callback) => {
@@ -8,7 +9,7 @@ module.exports = (domain, opts, callback) => {
89
url += `&${prop}=${opts[prop]}`
910
}
1011

11-
self.fetch(url, {mode: 'cors'})
12+
self.fetch(url, { mode: 'cors' })
1213
.then((response) => {
1314
return response.json()
1415
})

src/core/runtime/libp2p-browser.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const defaultsDeep = require('@nodeutils/defaults-deep')
1111

1212
class Node extends libp2p {
1313
constructor (_options) {
14-
const wrtcstar = new WebRTCStar({id: _options.peerInfo.id})
15-
const wsstar = new WebSocketStar({id: _options.peerInfo.id})
14+
const wrtcstar = new WebRTCStar({ id: _options.peerInfo.id })
15+
const wsstar = new WebSocketStar({ id: _options.peerInfo.id })
1616

1717
const defaults = {
1818
modules: {

src/core/runtime/libp2p-nodejs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const defaultsDeep = require('@nodeutils/defaults-deep')
1313

1414
class Node extends libp2p {
1515
constructor (_options) {
16-
const wsstar = new WebSocketStar({id: _options.peerInfo.id})
16+
const wsstar = new WebSocketStar({ id: _options.peerInfo.id })
1717

1818
const defaults = {
1919
modules: {

src/http/api/resources/bootstrap.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ exports.add = {
3838
return applyError(reply, new Error('Not a valid multiaddr'))
3939
}
4040
} else {
41-
reply({default: def})
41+
reply({ default: def })
4242
}
4343
},
4444
handler (request, reply) {
4545
const ipfs = request.server.app.ipfs
4646
const addr = request.pre.args.addr
4747
const def = request.pre.args.default
4848

49-
ipfs.bootstrap.add(addr && addr.toString(), {default: def}, (err, list) => {
49+
ipfs.bootstrap.add(addr && addr.toString(), { default: def }, (err, list) => {
5050
if (err) {
5151
return applyError(reply, err)
5252
}
@@ -71,15 +71,15 @@ exports.rm = {
7171
return applyError(reply, new Error('Not a valid multiaddr'))
7272
}
7373
} else {
74-
reply({all: all})
74+
reply({ all: all })
7575
}
7676
},
7777
handler (request, reply) {
7878
const ipfs = request.server.app.ipfs
7979
const addr = request.pre.args.addr
8080
const all = request.pre.args.all
8181

82-
ipfs.bootstrap.rm(addr && addr.toString(), {all: all}, (err, list) => {
82+
ipfs.bootstrap.rm(addr && addr.toString(), { all: all }, (err, list) => {
8383
if (err) {
8484
return applyError(reply, err)
8585
}

0 commit comments

Comments
 (0)