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

Commit 29c91a5

Browse files
committed
chore: cleanup on the ping component
1 parent 9ced27c commit 29c91a5

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

src/core/components/ping.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4+
const debug = require('debug')
45
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
56
const PeerId = require('peer-id')
67
const PeerInfo = require('peer-info')
78
const pull = require('pull-stream/pull')
8-
const take = require('pull-stream/throughs/take')
99
const Pushable = require('pull-pushable')
1010
const ndjson = require('pull-ndjson')
1111

12+
const log = debug('jsipfs:ping')
13+
log.error = debug('jsipfs:ping:error')
14+
1215
function getPacket (msg) {
1316
// Default msg
1417
const basePacket = {Success: false, Time: 0, Text: ''}
@@ -21,9 +24,7 @@ module.exports = function ping (self) {
2124
return cb(new Error(OFFLINE_ERROR))
2225
}
2326

24-
const source = Pushable(function (err) {
25-
console.log('stream closed!', err)
26-
})
27+
const source = Pushable()
2728

2829
const response = pull(
2930
source,
@@ -42,30 +43,35 @@ module.exports = function ping (self) {
4243

4344
self._libp2pNode.ping(peer, (err, p) => {
4445
if (err) {
45-
console.log('ERROR', err)
46-
return source.abort(err)
46+
log.error(err)
47+
source.push(getPacket({Text: err.toString()}))
48+
return source.end(err)
4749
}
50+
4851
let packetCount = 0
4952
let totalTime = 0
5053
source.push(getPacket({Success: true, Text: `PING ${peerId}`}))
54+
5155
p.on('ping', (time) => {
52-
console.log('ON PING')
5356
source.push(getPacket({ Success: true, Time: time }))
5457
totalTime += time
5558
packetCount++
56-
console.log(packetCount, count)
5759
if (packetCount >= count) {
5860
const average = totalTime/count
5961
p.stop()
60-
source.push(getPacket({ Success: false, Text: `Average latency: ${average}ms`}))
62+
source.push(getPacket({ Success: true, Text: `Average latency: ${average}ms` }))
6163
source.end()
6264
}
6365
})
64-
console.log('Setup handler')
66+
6567
p.on('error', (err) => {
66-
console.log('ERROR BATATA', err)
67-
source.abort(err)
68+
log.error(err)
69+
p.stop()
70+
source.push(getPacket({Text: err.toString()}))
71+
source.end(err)
6872
})
73+
74+
p.start()
6975
})
7076

7177
cb(null, response)

src/http/api/resources/ping.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ exports.get = {
3737
const responseStream = toStream.source(pullStream)
3838
const stream2 = new PassThrough()
3939
pump(responseStream, stream2)
40-
return reply(stream2).type('application/json').header('x-chunked-output', '1')
40+
return reply(stream2).type('application/json').header('X-Chunked-Output', '1')
4141
})
4242
}
4343
}

src/http/api/routes/ping.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ module.exports = (server) => {
99
method: '*',
1010
path: '/api/v0/ping',
1111
config: {
12-
payload: {
13-
parse: false,
14-
output: 'stream'
15-
},
1612
handler: resources.ping.get.handler,
1713
validate: resources.ping.get.validate
1814
}

0 commit comments

Comments
 (0)