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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ipfs.ping cli, http-api and core (#1342)
- Loading branch information
Showing
18 changed files
with
726 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict' | ||
|
||
const pull = require('pull-stream') | ||
const print = require('../utils').print | ||
|
||
module.exports = { | ||
command: 'ping <peerId>', | ||
|
||
description: 'Measure the latency of a connection', | ||
|
||
builder: { | ||
count: { | ||
alias: 'n', | ||
type: 'integer', | ||
default: 10 | ||
} | ||
}, | ||
|
||
handler (argv) { | ||
const peerId = argv.peerId | ||
const count = argv.count || 10 | ||
pull( | ||
argv.ipfs.pingPullStream(peerId, { count }), | ||
pull.drain(({ success, time, text }) => { | ||
// Check if it's a pong | ||
if (success && !text) { | ||
print(`Pong received: time=${time} ms`) | ||
// Status response | ||
} else { | ||
print(text) | ||
} | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict' | ||
|
||
const debug = require('debug') | ||
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') | ||
|
||
module.exports = function pingPullStream (self) { | ||
return (peerId, opts) => { | ||
if (!self.isOnline()) { | ||
return pull.error(new Error(OFFLINE_ERROR)) | ||
} | ||
|
||
opts = Object.assign({ count: 10 }, opts) | ||
|
||
const source = Pushable() | ||
|
||
waterfall([ | ||
(cb) => getPeer(self._libp2pNode, source, peerId, cb), | ||
(peer, cb) => runPing(self._libp2pNode, source, opts.count, peer, cb) | ||
], (err) => { | ||
if (err) { | ||
log.error(err) | ||
source.push(getPacket({ success: false, text: err.toString() })) | ||
source.end(err) | ||
} | ||
}) | ||
|
||
return source | ||
} | ||
} | ||
|
||
function getPacket (msg) { | ||
// Default msg | ||
const basePacket = { success: true, time: 0, text: '' } | ||
return Object.assign(basePacket, msg) | ||
} | ||
|
||
function getPeer (libp2pNode, statusStream, peerId, cb) { | ||
let peer | ||
|
||
try { | ||
peer = 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` | ||
})) | ||
} | ||
|
||
return libp2pNode.peerRouting.findPeer(peerId, cb) | ||
} | ||
|
||
cb(null, peer) | ||
} | ||
|
||
function runPing (libp2pNode, statusStream, count, peer, cb) { | ||
libp2pNode.ping(peer, (err, p) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
log('Got peer', peer) | ||
|
||
let packetCount = 0 | ||
let totalTime = 0 | ||
statusStream.push(getPacket({ text: `PING ${peer.id.toB58String()}` })) | ||
|
||
p.on('ping', (time) => { | ||
statusStream.push(getPacket({ time: time })) | ||
totalTime += time | ||
packetCount++ | ||
if (packetCount >= count) { | ||
const average = totalTime / count | ||
p.stop() | ||
statusStream.push(getPacket({ text: `Average latency: ${average}ms` })) | ||
statusStream.end() | ||
} | ||
}) | ||
|
||
p.on('error', (err) => { | ||
log.error(err) | ||
p.stop() | ||
statusStream.push(getPacket({ success: false, text: err.toString() })) | ||
statusStream.end(err) | ||
}) | ||
|
||
p.start() | ||
|
||
return cb() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
const toStream = require('pull-stream-to-stream') | ||
|
||
module.exports = function pingReadableStream (self) { | ||
return (peerId, opts) => toStream.source(self.pingPullStream(peerId, opts)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const pull = require('pull-stream/pull') | ||
|
||
module.exports = function ping (self) { | ||
return promisify((callback) => { | ||
callback(new Error('Not implemented')) | ||
return promisify((peerId, opts, cb) => { | ||
pull( | ||
self.pingPullStream(peerId, opts), | ||
pull.collect(cb) | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict' | ||
|
||
const Joi = require('joi') | ||
const pull = require('pull-stream') | ||
const toStream = require('pull-stream-to-stream') | ||
const ndjson = require('pull-ndjson') | ||
const PassThrough = require('readable-stream').PassThrough | ||
const pump = require('pump') | ||
|
||
exports.get = { | ||
validate: { | ||
query: Joi.object().keys({ | ||
n: Joi.alternatives() | ||
.when('count', { | ||
is: Joi.any().exist(), | ||
then: Joi.any().forbidden(), | ||
otherwise: Joi.number().integer().greater(0) | ||
}), | ||
count: Joi.number().integer().greater(0), | ||
arg: Joi.string().required() | ||
}).unknown() | ||
}, | ||
handler: (request, reply) => { | ||
const ipfs = request.server.app.ipfs | ||
const peerId = request.query.arg | ||
// Default count to 10 | ||
const count = request.query.n || request.query.count || 10 | ||
|
||
const source = pull( | ||
ipfs.pingPullStream(peerId, { count: count }), | ||
pull.map((chunk) => ({ | ||
Success: chunk.success, | ||
Time: chunk.time, | ||
Text: chunk.text | ||
})), | ||
ndjson.serialize() | ||
) | ||
|
||
// Streams from pull-stream-to-stream don't seem to be compatible | ||
// with the stream2 readable interface | ||
// see: https://github.com/hapijs/hapi/blob/c23070a3de1b328876d5e64e679a147fafb04b38/lib/response.js#L533 | ||
// and: https://github.com/pull-stream/pull-stream-to-stream/blob/e436acee18b71af8e71d1b5d32eee642351517c7/index.js#L28 | ||
const responseStream = toStream.source(source) | ||
const stream2 = new PassThrough() | ||
pump(responseStream, stream2) | ||
return reply(stream2).type('application/json').header('X-Chunked-Output', '1') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict' | ||
|
||
const resources = require('./../resources') | ||
|
||
module.exports = (server) => { | ||
const api = server.select('API') | ||
|
||
api.route({ | ||
method: '*', | ||
path: '/api/v0/ping', | ||
config: { | ||
handler: resources.ping.get.handler, | ||
validate: resources.ping.get.validate | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.