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

Commit

Permalink
feat: get Ping to work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Nov 1, 2018
1 parent 2029e05 commit 27d5a57
Show file tree
Hide file tree
Showing 32 changed files with 102 additions and 106 deletions.
6 changes: 3 additions & 3 deletions examples/browser-webpack/src/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ class App extends React.Component {
if (err) { throw err }

const hash = filesAdded[0].hash
self.setState({added_file_hash: hash})
self.setState({ added_file_hash: hash })

node.files.cat(hash, (err, data) => {
if (err) { throw err }
self.setState({added_file_contents: data.toString()})
self.setState({ added_file_contents: data.toString() })
})
})
}
}
render () {
return (
<div style={{textAlign: 'center'}}>
<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>
Expand Down
2 changes: 1 addition & 1 deletion examples/run-in-electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const url = require('url')
let mainWindow

function createWindow () {
mainWindow = new BrowserWindow({width: 800, height: 600})
mainWindow = new BrowserWindow({ width: 800, height: 600 })

// and load the index.html of the app.
mainWindow.loadURL(url.format({
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"execa": "^1.0.0",
"form-data": "^2.3.3",
"hat": "0.0.3",
"interface-ipfs-core": "~0.83.0",
"interface-ipfs-core": "~0.84.2",
"ipfsd-ctl": "~0.39.5",
"ncp": "^2.0.0",
"qs": "^6.5.2",
Expand Down Expand Up @@ -98,7 +98,7 @@
"hoek": "^5.0.4",
"human-to-milliseconds": "^1.0.0",
"interface-datastore": "~0.6.0",
"ipfs-api": "^26.0.3",
"ipfs-api": "^26.1.0",
"ipfs-bitswap": "~0.21.0",
"ipfs-block": "~0.8.0",
"ipfs-block-service": "~0.15.1",
Expand Down Expand Up @@ -150,6 +150,7 @@
"progress": "^2.0.1",
"promisify-es6": "^1.0.3",
"pull-abortable": "^4.1.1",
"pull-catch": "^1.0.0",
"pull-defer": "~0.2.3",
"pull-file": "^1.1.0",
"pull-ndjson": "~0.1.1",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const print = utils.print
const mfs = require('ipfs-mfs/cli')
const debug = require('debug')('ipfs:cli')

const pkg = readPkgUp.sync({cwd: __dirname}).pkg
const pkg = readPkgUp.sync({ cwd: __dirname }).pkg
updateNotifier({
pkg,
updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/file/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {

// Single file? Then print its hash
if (links.length === 0) {
links = [{hash: path}]
links = [{ hash: path }]
}

links.forEach((file) => print(file.hash))
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ module.exports = {
if (argv.progress) {
const bar = createProgressBar(totalBytes)
options.progress = function (byteLength) {
bar.update(byteLength / totalBytes, {progress: byteman(byteLength, 2, 'MB')})
bar.update(byteLength / totalBytes, { progress: byteman(byteLength, 2, 'MB') })
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = {
}

if (argv.headers) {
links = [{hash: 'Hash', size: 'Size', name: 'Name'}].concat(links)
links = [{ hash: 'Hash', size: 'Size', name: 'Name' }].concat(links)
}

const multihashWidth = Math.max.apply(null, links.map((file) => file.hash.length))
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/object/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
},

handler (argv) {
argv.ipfs.object.get(argv.key, {enc: 'base58'}, (err, node) => {
argv.ipfs.object.get(argv.key, { enc: 'base58' }, (err, node) => {
if (err) {
throw err
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/object/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('fs')
const print = require('../../utils').print

function putNode (buf, enc, ipfs) {
ipfs.object.put(buf, {enc: enc}, (err, node) => {
ipfs.object.put(buf, { enc: enc }, (err, node) => {
if (err) {
throw err
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/repo/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
},

handler (argv) {
argv.ipfs.repo.stat({human: argv.human}, (err, stats) => {
argv.ipfs.repo.stat({ human: argv.human }, (err, stats) => {
if (err) {
throw err
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/stats/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
},

handler (argv) {
argv.ipfs.stats.repo({human: argv.human}, (err, stats) => {
argv.ipfs.stats.repo({ human: argv.human }, (err, stats) => {
if (err) {
throw err
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/components/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function bootstrap (self) {
if (err) {
return callback(err)
}
callback(null, {Peers: config.Bootstrap})
callback(null, { Peers: config.Bootstrap })
})
}),
add: promisify((multiaddr, args, callback) => {
Expand Down Expand Up @@ -59,7 +59,7 @@ module.exports = function bootstrap (self) {
rm: promisify((multiaddr, args, callback) => {
if (typeof args === 'function') {
callback = args
args = {all: false}
args = { all: false }
}
if (multiaddr && !isValidMultiaddr(multiaddr)) {
return setImmediate(() => callback(invalidMultiaddrError(multiaddr)))
Expand All @@ -85,7 +85,7 @@ module.exports = function bootstrap (self) {
res.push(multiaddr)
}

callback(null, {Peers: res})
callback(null, { Peers: res })
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ module.exports = function init (self) {
self.log('repo opened')
if (opts.pass) {
self.log('creating keychain')
const keychainOptions = Object.assign({passPhrase: opts.pass}, config.Keychain)
const keychainOptions = Object.assign({ passPhrase: opts.pass }, config.Keychain)
self._keychain = new Keychain(self._repo.keys, keychainOptions)
self._keychain.importPeer('self', { privKey: privateKey }, cb)
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/core/components/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,17 +333,17 @@ module.exports = (self) => {
const { recursive, direct, all } = types
// recursive
if ((type === recursive || type === all) && recursivePins.has(key)) {
return callback(null, {pinned: true, reason: recursive})
return callback(null, { pinned: true, reason: recursive })
}
if ((type === recursive)) {
return callback(null, {pinned: false})
return callback(null, { pinned: false })
}
// direct
if ((type === direct || type === all) && directPins.has(key)) {
return callback(null, {pinned: true, reason: direct})
return callback(null, { pinned: true, reason: direct })
}
if ((type === direct)) {
return callback(null, {pinned: false})
return callback(null, { pinned: false })
}

// indirect (default)
Expand Down
58 changes: 27 additions & 31 deletions src/core/components/ping-pull-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
const PeerId = require('peer-id')
const pull = require('pull-stream')
const Pushable = require('pull-pushable')
const waterfall = require('async/waterfall')

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

const source = Pushable()

waterfall([
(cb) => getPeer(self._libp2pNode, source, peerId, cb),
(peer, cb) => runPing(self._libp2pNode, source, opts.count, peer, cb)
], (err) => {
getPeer(self._libp2pNode, source, peerId, (err, peer) => {
if (err) {
log.error(err)
source.push(getPacket({ success: false, text: err.toString() }))
source.end(err)
return
}

runPing(self._libp2pNode, source, opts.count, peer, (err) => {
if (err) {
log.error(err)
source.push(getPacket({ success: false, text: err.toString() }))
source.end()
}
})
})

return source
Expand All @@ -41,45 +45,40 @@ function getPacket (msg) {
return Object.assign(basePacket, msg)
}

function getPeer (libp2pNode, statusStream, peerId, cb) {
let peer
function getPeer (libp2pNode, statusStream, peerIdStr, cb) {
let peerId

try {
peerId = PeerId.createFromB58String(peerIdStr)
} catch (err) {
return cb(err)
}

let peerInfo

try {
peer = libp2pNode.peerBook.get(peerId)
peerInfo = libp2pNode.peerBook.get(peerId)
} catch (err) {
log('Peer not found in peer book, trying peer routing')
// Share lookup status just as in the go implemmentation
statusStream.push(getPacket({ text: `Looking up peer ${peerId}` }))

// Try to use peerRouting
try {
peerId = PeerId.createFromB58String(peerId)
} catch (err) {
return cb(Object.assign(err, {
message: `failed to parse peer address '${peerId}': input isn't valid multihash`
}))
}

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

cb(null, peer)
cb(null, peerInfo)
}

function runPing (libp2pNode, statusStream, count, peer, cb) {
libp2pNode.ping(peer, (err, p) => {
if (err) {
return cb(err)
}

log('Got peer', peer)
if (err) { return cb(err) }

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

p.on('ping', (time) => {
statusStream.push(getPacket({ time: time }))
statusStream.push(getPacket({ time }))
totalTime += time
packetCount++
if (packetCount >= count) {
Expand All @@ -93,12 +92,9 @@ function runPing (libp2pNode, statusStream, count, peer, cb) {
p.on('error', (err) => {
log.error(err)
p.stop()
statusStream.push(getPacket({ success: false, text: err.toString() }))
statusStream.end(err)
cb(err)
})

p.start()

return cb()
})
}
6 changes: 3 additions & 3 deletions src/core/components/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const promisify = require('promisify-es6')
const pull = require('pull-stream/pull')

module.exports = function ping (self) {
return promisify((peerId, opts, cb) => {
return promisify((peerId, opts, callback) => {
if (typeof opts === 'function') {
cb = opts
callback = opts
opts = {}
}

pull(
self.pingPullStream(peerId, opts),
pull.collect(cb)
pull.collect(callback)
)
})
}
2 changes: 1 addition & 1 deletion src/core/components/pre-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = function preStart (self) {
if (self._keychain) {
// most likely an init or upgrade has happened
} else if (pass) {
const keychainOptions = Object.assign({passPhrase: pass}, config.Keychain)
const keychainOptions = Object.assign({ passPhrase: pass }, config.Keychain)
self._keychain = new Keychain(self._repo.keys, keychainOptions)
self.log('keychain constructed')
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/core/runtime/dns-browser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global self */
'use strict'

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

self.fetch(url, {mode: 'cors'})
self.fetch(url, { mode: 'cors' })
.then((response) => {
return response.json()
})
Expand Down
4 changes: 2 additions & 2 deletions src/core/runtime/libp2p-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const defaultsDeep = require('@nodeutils/defaults-deep')

class Node extends libp2p {
constructor (_options) {
const wrtcstar = new WebRTCStar({id: _options.peerInfo.id})
const wsstar = new WebSocketStar({id: _options.peerInfo.id})
const wrtcstar = new WebRTCStar({ id: _options.peerInfo.id })
const wsstar = new WebSocketStar({ id: _options.peerInfo.id })

const defaults = {
modules: {
Expand Down
2 changes: 1 addition & 1 deletion src/core/runtime/libp2p-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const defaultsDeep = require('@nodeutils/defaults-deep')

class Node extends libp2p {
constructor (_options) {
const wsstar = new WebSocketStar({id: _options.peerInfo.id})
const wsstar = new WebSocketStar({ id: _options.peerInfo.id })

const defaults = {
modules: {
Expand Down
8 changes: 4 additions & 4 deletions src/http/api/resources/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ exports.add = {
return applyError(reply, new Error('Not a valid multiaddr'))
}
} else {
reply({default: def})
reply({ default: def })
}
},
handler (request, reply) {
const ipfs = request.server.app.ipfs
const addr = request.pre.args.addr
const def = request.pre.args.default

ipfs.bootstrap.add(addr && addr.toString(), {default: def}, (err, list) => {
ipfs.bootstrap.add(addr && addr.toString(), { default: def }, (err, list) => {
if (err) {
return applyError(reply, err)
}
Expand All @@ -71,15 +71,15 @@ exports.rm = {
return applyError(reply, new Error('Not a valid multiaddr'))
}
} else {
reply({all: all})
reply({ all: all })
}
},
handler (request, reply) {
const ipfs = request.server.app.ipfs
const addr = request.pre.args.addr
const all = request.pre.args.all

ipfs.bootstrap.rm(addr && addr.toString(), {all: all}, (err, list) => {
ipfs.bootstrap.rm(addr && addr.toString(), { all: all }, (err, list) => {
if (err) {
return applyError(reply, err)
}
Expand Down
Loading

0 comments on commit 27d5a57

Please sign in to comment.