Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
feat: streamable ping and optional packet number (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
JGAntunes authored and daviddias committed Mar 25, 2018
1 parent 2dc6969 commit 3f3ce8a
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 34 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"P
- [miscellaneous operations](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/MISCELLANEOUS.md)
- [`ipfs.id([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/MISCELLANEOUS.md#id)
- [`ipfs.version([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/MISCELLANEOUS.md#version)
- [`ipfs.ping()`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/MISCELLANEOUS.md#ping)
- [`ipfs.ping(id, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/MISCELLANEOUS.md#ping)
- `ipfs.pingPullStream(id, [options])`
- `ipfs.pingReadableStream(id, [options])`
- [`ipfs.dns(domain, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/MISCELLANEOUS.md#dns)
- [`ipfs.stop([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/MISCELLANEOUS.md#stop). Alias to `ipfs.shutdown`.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"ipfs": "~0.28.2",
"ipfsd-ctl": "~0.30.4",
"pre-commit": "^1.2.2",
"pull-stream": "^3.6.2",
"socket.io": "^2.0.4",
"socket.io-client": "^2.0.4",
"stream-equal": "^1.1.1"
Expand Down
29 changes: 29 additions & 0 deletions src/ping-pull-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const toPull = require('stream-to-pull-stream')
const deferred = require('pull-defer')
const moduleConfig = require('./utils/module-config')

module.exports = (arg) => {
const send = moduleConfig(arg)

return (id, opts = {}) => {
// Default number of packtes to 1
if (!opts.n && !opts.count) {
opts.n = 1
}
const request = {
path: 'ping',
args: id,
qs: opts
}
const p = deferred.source()

send(request, (err, stream) => {
if (err) { return p.abort(err) }
p.resolve(toPull.source(stream))
})

return p
}
}
33 changes: 33 additions & 0 deletions src/ping-readable-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const Stream = require('readable-stream')
const pump = require('pump')
const moduleConfig = require('./utils/module-config')

module.exports = (arg) => {
const send = moduleConfig(arg)

return (id, opts = {}) => {
// Default number of packtes to 1
if (!opts.n && !opts.count) {
opts.n = 1
}
const request = {
path: 'ping',
args: id,
qs: opts
}
// ndjson streams objects
const pt = new Stream.PassThrough({
objectMode: true
})

send(request, (err, stream) => {
if (err) { return pt.destroy(err) }

pump(stream, pt)
})

return pt
}
}
24 changes: 12 additions & 12 deletions src/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ const streamToValue = require('./utils/stream-to-value')
module.exports = (arg) => {
const send = moduleConfig(arg)

return promisify((id, callback) => {
return promisify((id, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = {}
}
// Default number of packtes to 1
if (!opts.n && !opts.count) {
opts.n = 1
}
const request = {
path: 'ping',
args: id,
qs: { n: 1 }
qs: opts
}

// Transform the response stream to a value:
// { Success: <boolean>, Time: <number>, Text: <string> }
// [{ Success: <boolean>, Time: <number>, Text: <string> }]
const transform = (res, callback) => {
streamToValue(res, (err, res) => {
if (err) {
return callback(err)
}

// go-ipfs http api currently returns 3 lines for a ping.
// they're a little messed, so take the correct values from each lines.
const pingResult = {
Success: res[1].Success,
Time: res[1].Time,
Text: res[2].Text
}

callback(null, pingResult)
callback(null, res)
})
}

Expand Down
2 changes: 2 additions & 0 deletions src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ function requireCommands () {
object: require('../object'),
pin: require('../pin'),
ping: require('../ping'),
pingReadableStream: require('../ping-readable-stream'),
pingPullStream: require('../ping-pull-stream'),
refs: require('../refs'),
repo: require('../repo'),
stop: require('../stop'),
Expand Down
Loading

0 comments on commit 3f3ce8a

Please sign in to comment.