From 985e959e33b06f3b2e516c97b1c8b4566a0b1068 Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Fri, 23 Mar 2018 02:47:53 +0000 Subject: [PATCH 01/10] feat: add streaming ping methods and allow for a configurable number of packets --- src/ping-pull-stream.js | 31 +++++++++++++++++++++++++++++++ src/ping-readable-stream.js | 33 +++++++++++++++++++++++++++++++++ src/ping.js | 24 ++++++++++++------------ src/utils/load-commands.js | 2 ++ src/utils/send-request.js | 2 -- test/ping.spec.js | 30 ++++++++++++++++++------------ 6 files changed, 96 insertions(+), 26 deletions(-) create mode 100644 src/ping-pull-stream.js create mode 100644 src/ping-readable-stream.js diff --git a/src/ping-pull-stream.js b/src/ping-pull-stream.js new file mode 100644 index 000000000..d41baaa42 --- /dev/null +++ b/src/ping-pull-stream.js @@ -0,0 +1,31 @@ +'use strict' + +const toPull = require('stream-to-pull-stream') +const deferred = require('pull-defer') +const pull = require('pull-stream') +const log = require('pull-stream/sinks/log') +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.end(err) } + p.resolve(toPull.source(stream)) + }) + + return p + } +} diff --git a/src/ping-readable-stream.js b/src/ping-readable-stream.js new file mode 100644 index 000000000..83ec825e2 --- /dev/null +++ b/src/ping-readable-stream.js @@ -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 + } +} diff --git a/src/ping.js b/src/ping.js index 4dbb77b6c..8aca6c09d 100644 --- a/src/ping.js +++ b/src/ping.js @@ -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: , Time: , Text: } + // [{ Success: , Time: , Text: }] 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) }) } diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index f1b5f6923..e86982e18 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -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'), diff --git a/src/utils/send-request.js b/src/utils/send-request.js index 90f436d47..9213532f9 100644 --- a/src/utils/send-request.js +++ b/src/utils/send-request.js @@ -34,11 +34,9 @@ function onRes (buffer, cb) { const chunkedObjects = Boolean(res.headers['x-chunked-output']) const isJson = res.headers['content-type'] && res.headers['content-type'].indexOf('application/json') === 0 - if (res.statusCode >= 400 || !res.statusCode) { return parseError(res, cb) } - // Return the response stream directly if (stream && !buffer) { return cb(null, res) diff --git a/test/ping.spec.js b/test/ping.spec.js index 747464798..1ed7330a0 100644 --- a/test/ping.spec.js +++ b/test/ping.spec.js @@ -12,7 +12,7 @@ const series = require('async/series') const IPFSApi = require('../src') const f = require('./utils/factory') -describe.skip('.ping', () => { +describe.only('.ping', () => { let ipfs let ipfsd let other @@ -43,7 +43,7 @@ describe.skip('.ping', () => { ipfsd.api.id((err, id) => { expect(err).to.not.exist() const ma = id.addresses[0] - other.api.swarm.connect(ma, cb) + other.swarm.connect(ma, cb) }) } ], done) @@ -63,11 +63,14 @@ describe.skip('.ping', () => { ipfs.ping(id.id, (err, res) => { expect(err).to.not.exist() - expect(res).to.have.a.property('Success') - expect(res).to.have.a.property('Time') - expect(res).to.have.a.property('Text') - expect(res.Text).to.contain('Average latency') - expect(res.Time).to.be.a('number') + expect(res).to.be.an('array') + expect(res).to.have.lengthOf(3) + res.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = res.find(packet => packet.Text.includes('Average latency')) + expect(resultMsg).to.exist() done() }) }) @@ -81,11 +84,14 @@ describe.skip('.ping', () => { return ipfs.ping(id.id) }) .then((res) => { - expect(res).to.have.a.property('Success') - expect(res).to.have.a.property('Time') - expect(res).to.have.a.property('Text') - expect(res.Text).to.contain('Average latency') - expect(res.Time).to.be.a('number') + expect(res).to.be.an('array') + expect(res).to.have.lengthOf(3) + res.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = res.find(packet => packet.Text.includes('Average latency')) + expect(resultMsg).to.exist() }) }) }) From 4fda38d292292c50f6bb2db1b2b52e9c0e2dc002 Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 24 Mar 2018 03:21:41 +0000 Subject: [PATCH 02/10] chore: add tests to ping packet number params and ping stream commands --- test/ping.spec.js | 140 ++++++++++++++++++++++++++++++++++----- test/sub-modules.spec.js | 4 ++ 2 files changed, 129 insertions(+), 15 deletions(-) diff --git a/test/ping.spec.js b/test/ping.spec.js index 1ed7330a0..ab65dc6b4 100644 --- a/test/ping.spec.js +++ b/test/ping.spec.js @@ -3,6 +3,8 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') +const pull = require('pull-stream') +const drain = require('pull-stream/sinks/drain') const expect = chai.expect chai.use(dirtyChai) @@ -12,11 +14,12 @@ const series = require('async/series') const IPFSApi = require('../src') const f = require('./utils/factory') -describe.only('.ping', () => { +describe.only('.ping',function () { let ipfs let ipfsd let other let otherd + let otherId before(function (done) { this.timeout(20 * 1000) // slow CI @@ -45,6 +48,13 @@ describe.only('.ping', () => { const ma = id.addresses[0] other.swarm.connect(ma, cb) }) + }, + (cb) => { + other.id((err, id) => { + expect(err).to.not.exist() + otherId = id.id + cb() + }) } ], done) }) @@ -57,12 +67,63 @@ describe.only('.ping', () => { }) describe('callback API', () => { - it('ping another peer', (done) => { - other.id((err, id) => { - expect(err).to.not.exist() + it('ping another peer with default packet count', (done) => { + ipfs.ping(otherId, (err, res) => { + expect(err).to.not.exist() + expect(res).to.be.an('array') + expect(res).to.have.lengthOf(3) + res.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = res.find(packet => packet.Text.includes('Average latency')) + expect(resultMsg).to.exist() + done() + }) + }) - ipfs.ping(id.id, (err, res) => { - expect(err).to.not.exist() + it('ping another peer with a specifc packet count through parameter count', (done) => { + ipfs.ping(otherId, {count: 3}, (err, res) => { + expect(err).to.not.exist() + expect(res).to.be.an('array') + expect(res).to.have.lengthOf(5) + res.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = res.find(packet => packet.Text.includes('Average latency')) + expect(resultMsg).to.exist() + done() + }) + }) + + it('ping another peer with a specifc packet count through parameter n', (done) => { + ipfs.ping(otherId, {n: 3}, (err, res) => { + expect(err).to.not.exist() + expect(res).to.be.an('array') + expect(res).to.have.lengthOf(5) + res.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = res.find(packet => packet.Text.includes('Average latency')) + expect(resultMsg).to.exist() + done() + }) + }) + + it('sending both n and count should fail', (done) => { + ipfs.ping(otherId, {count: 10, n: 10}, (err, res) => { + expect(err).to.exist() + done() + }) + }) + }) + + describe('promise API', () => { + it('ping another peer with default packet count', () => { + return ipfs.ping(otherId) + .then((res) => { expect(res).to.be.an('array') expect(res).to.have.lengthOf(3) res.forEach(packet => { @@ -71,18 +132,11 @@ describe.only('.ping', () => { }) const resultMsg = res.find(packet => packet.Text.includes('Average latency')) expect(resultMsg).to.exist() - done() }) - }) }) - }) - describe('promise API', () => { - it('ping another peer', () => { - return other.id() - .then((id) => { - return ipfs.ping(id.id) - }) + it('ping another peer with a specifc packet count through parameter count', () => { + return ipfs.ping(otherId, {count: 3}) .then((res) => { expect(res).to.be.an('array') expect(res).to.have.lengthOf(3) @@ -92,7 +146,63 @@ describe.only('.ping', () => { }) const resultMsg = res.find(packet => packet.Text.includes('Average latency')) expect(resultMsg).to.exist() + }) + .catch(console.log) + }) + + it('ping another peer with a specifc packet count through parameter n', () => { + return ipfs.ping(otherId, {n: 3}) + .then((res) => { + expect(res).to.be.an('array') + expect(res).to.have.lengthOf(5) + res.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = res.find(packet => packet.Text.includes('Average latency')) + expect(resultMsg).to.exist() + }) + }) + + it('sending both n and count should fail', () => { + ipfs.ping(otherId) + .catch(err => { + expect(err).to.exist() + done() }) }) }) + + describe('pull stream API', () => { + it('ping another peer', (done) => { + let packetNum = 0 + pull( + ipfs.pingPullStream(otherId), + drain(data => { + packetNum++ + expect(data).to.be.an('object') + expect(data).to.have.keys('Success', 'Time', 'Text') + }, () => { + expect(packetNum).to.equal(3) + done() + }) + ) + }) + }) + + describe('readable stream API', () => { + it('ping another peer', (done) => { + let packetNum = 0 + ipfs.pingReadableStream(otherId) + .on('data', data => { + packetNum++ + expect(data).to.be.an('object') + expect(data).to.have.keys('Success', 'Time', 'Text') + }) + .on('error', err => { + expect(err).not.to.exist() + }) + .on('end', done) + }) + }) }) diff --git a/test/sub-modules.spec.js b/test/sub-modules.spec.js index 3512731cc..8b7dd35a7 100644 --- a/test/sub-modules.spec.js +++ b/test/sub-modules.spec.js @@ -69,8 +69,12 @@ describe('submodules', () => { it('ping', () => { const ping = require('../src/ping')(config) + const pingPullStream = require('../src/ping-pull-stream')(config) + const pingReadableStream = require('../src/ping-readable-stream')(config) expect(ping).to.be.a('function') + expect(pingPullStream ).to.be.a('function') + expect(pingReadableStream ).to.be.a('function') }) it('log', () => { From a2ffd430412a7924551fe64452ac0a1ba988b5db Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 24 Mar 2018 13:40:36 +0000 Subject: [PATCH 03/10] chore: finnish ping tests and lint the code --- package.json | 1 + src/ping-pull-stream.js | 4 +- test/ping.spec.js | 124 +++++++++++++++++++++++++++++++++++----- 3 files changed, 111 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 769d31cc2..bb3210771 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "ipfs": "~0.28.2", "ipfsd-ctl": "~0.30.1", "pre-commit": "^1.2.2", + "pull-catch": "^1.0.0", "socket.io": "^2.0.4", "socket.io-client": "^2.0.4", "stream-equal": "^1.1.1" diff --git a/src/ping-pull-stream.js b/src/ping-pull-stream.js index d41baaa42..440243e43 100644 --- a/src/ping-pull-stream.js +++ b/src/ping-pull-stream.js @@ -2,8 +2,6 @@ const toPull = require('stream-to-pull-stream') const deferred = require('pull-defer') -const pull = require('pull-stream') -const log = require('pull-stream/sinks/log') const moduleConfig = require('./utils/module-config') module.exports = (arg) => { @@ -22,7 +20,7 @@ module.exports = (arg) => { const p = deferred.source() send(request, (err, stream) => { - if (err) { return p.end(err) } + if (err) { return p.abort(err) } p.resolve(toPull.source(stream)) }) diff --git a/test/ping.spec.js b/test/ping.spec.js index ab65dc6b4..a2ee2eb1a 100644 --- a/test/ping.spec.js +++ b/test/ping.spec.js @@ -4,7 +4,7 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') const pull = require('pull-stream') -const drain = require('pull-stream/sinks/drain') +const collect = require('pull-stream/sinks/collect') const expect = chai.expect chai.use(dirtyChai) @@ -14,7 +14,7 @@ const series = require('async/series') const IPFSApi = require('../src') const f = require('./utils/factory') -describe.only('.ping',function () { +describe('.ping',function () { let ipfs let ipfsd let other @@ -147,7 +147,6 @@ describe.only('.ping',function () { const resultMsg = res.find(packet => packet.Text.includes('Average latency')) expect(resultMsg).to.exist() }) - .catch(console.log) }) it('ping another peer with a specifc packet count through parameter n', () => { @@ -164,8 +163,8 @@ describe.only('.ping',function () { }) }) - it('sending both n and count should fail', () => { - ipfs.ping(otherId) + it('sending both n and count should fail', (done) => { + ipfs.ping(otherId, {n: 3, count:3}) .catch(err => { expect(err).to.exist() done() @@ -174,16 +173,66 @@ describe.only('.ping',function () { }) describe('pull stream API', () => { - it('ping another peer', (done) => { - let packetNum = 0 + it('ping another peer with the default packet count', (done) => { pull( ipfs.pingPullStream(otherId), - drain(data => { - packetNum++ - expect(data).to.be.an('object') - expect(data).to.have.keys('Success', 'Time', 'Text') - }, () => { - expect(packetNum).to.equal(3) + collect((err, data) => { + expect(err).to.not.exist() + expect(data).to.be.an('array') + expect(data).to.have.lengthOf(3) + data.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = data.find(packet => packet.Text.includes('Average latency')) + expect(resultMsg).to.exist() + done() + }) + ) + }) + + it('ping another peer with a specifc packet count through parameter count', (done) => { + pull( + ipfs.pingPullStream(otherId, {count: 3}), + collect((err, data) => { + expect(err).to.not.exist() + expect(data).to.be.an('array') + expect(data).to.have.lengthOf(5) + data.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = data.find(packet => packet.Text.includes('Average latency')) + expect(resultMsg).to.exist() + done() + }) + ) + }) + + + it('ping another peer with a specifc packet count through parameter n', (done) => { + pull( + ipfs.pingPullStream(otherId, {n: 3}), + collect((err, data) => { + expect(err).to.not.exist() + expect(data).to.be.an('array') + expect(data).to.have.lengthOf(5) + data.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = data.find(packet => packet.Text.includes('Average latency')) + expect(resultMsg).to.exist() + done() + }) + ) + }) + + it('sending both n and count should fail', (done) => { + pull( + ipfs.pingPullStream(otherId, {n: 3, count: 3}), + collect(err => { + expect(err).to.exist() done() }) ) @@ -191,7 +240,7 @@ describe.only('.ping',function () { }) describe('readable stream API', () => { - it('ping another peer', (done) => { + it('ping another peer with the default packet count', (done) => { let packetNum = 0 ipfs.pingReadableStream(otherId) .on('data', data => { @@ -202,7 +251,52 @@ describe.only('.ping',function () { .on('error', err => { expect(err).not.to.exist() }) - .on('end', done) + .on('end', () => { + expect(packetNum).to.equal(3) + done() + }) + }) + + it('ping another peer with a specifc packet count through parameter count', (done) => { + let packetNum = 0 + ipfs.pingReadableStream(otherId, {count: 3}) + .on('data', data => { + packetNum++ + expect(data).to.be.an('object') + expect(data).to.have.keys('Success', 'Time', 'Text') + }) + .on('error', err => { + expect(err).not.to.exist() + }) + .on('end', () => { + expect(packetNum).to.equal(5) + done() + }) + }) + + it('ping another peer with a specifc packet count through parameter n', (done) => { + let packetNum = 0 + ipfs.pingReadableStream(otherId, {n: 3}) + .on('data', data => { + packetNum++ + expect(data).to.be.an('object') + expect(data).to.have.keys('Success', 'Time', 'Text') + }) + .on('error', err => { + expect(err).not.to.exist() + }) + .on('end', () => { + expect(packetNum).to.equal(5) + done() + }) + }) + + it('sending both n and count should fail', (done) => { + ipfs.pingReadableStream(otherId, {n: 3, count:3}) + .on('error', err => { + expect(err).to.exist() + done() + }) }) }) }) From 2281f446baaf4be7052507eeaf707d412ed74e26 Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 24 Mar 2018 13:45:25 +0000 Subject: [PATCH 04/10] chore: update README with ping info --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 58c48048e..9e9a92a14 100644 --- a/README.md +++ b/README.md @@ -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`. From d7d8ebcf87671fecfda8d4e630659f3415aa1691 Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 24 Mar 2018 13:47:39 +0000 Subject: [PATCH 05/10] chore: remove useless dep --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index bb3210771..769d31cc2 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,6 @@ "ipfs": "~0.28.2", "ipfsd-ctl": "~0.30.1", "pre-commit": "^1.2.2", - "pull-catch": "^1.0.0", "socket.io": "^2.0.4", "socket.io-client": "^2.0.4", "stream-equal": "^1.1.1" From e7e95e32e3011cfdb79b0228ff8db978b6490eeb Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 24 Mar 2018 13:53:37 +0000 Subject: [PATCH 06/10] chore: fix weird tab issue --- src/ping-pull-stream.js | 14 +++++++------- src/ping-readable-stream.js | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ping-pull-stream.js b/src/ping-pull-stream.js index 440243e43..ede87fd34 100644 --- a/src/ping-pull-stream.js +++ b/src/ping-pull-stream.js @@ -16,14 +16,14 @@ module.exports = (arg) => { path: 'ping', args: id, qs: opts - } - const p = deferred.source() + } + const p = deferred.source() - send(request, (err, stream) => { - if (err) { return p.abort(err) } + send(request, (err, stream) => { + if (err) { return p.abort(err) } p.resolve(toPull.source(stream)) - }) + }) - return p - } + return p + } } diff --git a/src/ping-readable-stream.js b/src/ping-readable-stream.js index 83ec825e2..f56a191f9 100644 --- a/src/ping-readable-stream.js +++ b/src/ping-readable-stream.js @@ -17,10 +17,10 @@ module.exports = (arg) => { args: id, qs: opts } - // ndjson streams objects + // ndjson streams objects const pt = new Stream.PassThrough({ - objectMode: true - }) + objectMode: true + }) send(request, (err, stream) => { if (err) { return pt.destroy(err) } From a96127003064c3f1a6b1ff84c133d3e58e4e0a2d Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 24 Mar 2018 13:58:06 +0000 Subject: [PATCH 07/10] chore: fix more weird tab issue --- src/utils/send-request.js | 2 + test/ping.spec.js | 346 +++++++++++++++++++------------------- 2 files changed, 175 insertions(+), 173 deletions(-) diff --git a/src/utils/send-request.js b/src/utils/send-request.js index 9213532f9..90f436d47 100644 --- a/src/utils/send-request.js +++ b/src/utils/send-request.js @@ -34,9 +34,11 @@ function onRes (buffer, cb) { const chunkedObjects = Boolean(res.headers['x-chunked-output']) const isJson = res.headers['content-type'] && res.headers['content-type'].indexOf('application/json') === 0 + if (res.statusCode >= 400 || !res.statusCode) { return parseError(res, cb) } + // Return the response stream directly if (stream && !buffer) { return cb(null, res) diff --git a/test/ping.spec.js b/test/ping.spec.js index a2ee2eb1a..bcfdce72f 100644 --- a/test/ping.spec.js +++ b/test/ping.spec.js @@ -19,7 +19,7 @@ describe('.ping',function () { let ipfsd let other let otherd - let otherId + let otherId before(function (done) { this.timeout(20 * 1000) // slow CI @@ -53,7 +53,7 @@ describe('.ping',function () { other.id((err, id) => { expect(err).to.not.exist() otherId = id.id - cb() + cb() }) } ], done) @@ -68,56 +68,56 @@ describe('.ping',function () { describe('callback API', () => { it('ping another peer with default packet count', (done) => { - ipfs.ping(otherId, (err, res) => { - expect(err).to.not.exist() - expect(res).to.be.an('array') - expect(res).to.have.lengthOf(3) - res.forEach(packet => { - expect(packet).to.have.keys('Success', 'Time', 'Text') - expect(packet.Time).to.be.a('number') - }) - const resultMsg = res.find(packet => packet.Text.includes('Average latency')) - expect(resultMsg).to.exist() - done() - }) + ipfs.ping(otherId, (err, res) => { + expect(err).to.not.exist() + expect(res).to.be.an('array') + expect(res).to.have.lengthOf(3) + res.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = res.find(packet => packet.Text.includes('Average latency')) + expect(resultMsg).to.exist() + done() + }) }) - it('ping another peer with a specifc packet count through parameter count', (done) => { - ipfs.ping(otherId, {count: 3}, (err, res) => { - expect(err).to.not.exist() - expect(res).to.be.an('array') - expect(res).to.have.lengthOf(5) - res.forEach(packet => { - expect(packet).to.have.keys('Success', 'Time', 'Text') - expect(packet.Time).to.be.a('number') - }) - const resultMsg = res.find(packet => packet.Text.includes('Average latency')) - expect(resultMsg).to.exist() - done() - }) - }) + it('ping another peer with a specifc packet count through parameter count', (done) => { + ipfs.ping(otherId, {count: 3}, (err, res) => { + expect(err).to.not.exist() + expect(res).to.be.an('array') + expect(res).to.have.lengthOf(5) + res.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = res.find(packet => packet.Text.includes('Average latency')) + expect(resultMsg).to.exist() + done() + }) + }) - it('ping another peer with a specifc packet count through parameter n', (done) => { - ipfs.ping(otherId, {n: 3}, (err, res) => { - expect(err).to.not.exist() - expect(res).to.be.an('array') - expect(res).to.have.lengthOf(5) - res.forEach(packet => { - expect(packet).to.have.keys('Success', 'Time', 'Text') - expect(packet.Time).to.be.a('number') - }) - const resultMsg = res.find(packet => packet.Text.includes('Average latency')) - expect(resultMsg).to.exist() - done() - }) - }) + it('ping another peer with a specifc packet count through parameter n', (done) => { + ipfs.ping(otherId, {n: 3}, (err, res) => { + expect(err).to.not.exist() + expect(res).to.be.an('array') + expect(res).to.have.lengthOf(5) + res.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = res.find(packet => packet.Text.includes('Average latency')) + expect(resultMsg).to.exist() + done() + }) + }) - it('sending both n and count should fail', (done) => { - ipfs.ping(otherId, {count: 10, n: 10}, (err, res) => { - expect(err).to.exist() - done() - }) - }) + it('sending both n and count should fail', (done) => { + ipfs.ping(otherId, {count: 10, n: 10}, (err, res) => { + expect(err).to.exist() + done() + }) + }) }) describe('promise API', () => { @@ -126,11 +126,11 @@ describe('.ping',function () { .then((res) => { expect(res).to.be.an('array') expect(res).to.have.lengthOf(3) - res.forEach(packet => { - expect(packet).to.have.keys('Success', 'Time', 'Text') - expect(packet.Time).to.be.a('number') - }) - const resultMsg = res.find(packet => packet.Text.includes('Average latency')) + res.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = res.find(packet => packet.Text.includes('Average latency')) expect(resultMsg).to.exist() }) }) @@ -140,11 +140,11 @@ describe('.ping',function () { .then((res) => { expect(res).to.be.an('array') expect(res).to.have.lengthOf(3) - res.forEach(packet => { - expect(packet).to.have.keys('Success', 'Time', 'Text') - expect(packet.Time).to.be.a('number') - }) - const resultMsg = res.find(packet => packet.Text.includes('Average latency')) + res.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = res.find(packet => packet.Text.includes('Average latency')) expect(resultMsg).to.exist() }) }) @@ -154,11 +154,11 @@ describe('.ping',function () { .then((res) => { expect(res).to.be.an('array') expect(res).to.have.lengthOf(5) - res.forEach(packet => { - expect(packet).to.have.keys('Success', 'Time', 'Text') - expect(packet.Time).to.be.a('number') - }) - const resultMsg = res.find(packet => packet.Text.includes('Average latency')) + res.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = res.find(packet => packet.Text.includes('Average latency')) expect(resultMsg).to.exist() }) }) @@ -167,136 +167,136 @@ describe('.ping',function () { ipfs.ping(otherId, {n: 3, count:3}) .catch(err => { expect(err).to.exist() - done() + done() }) }) }) - describe('pull stream API', () => { - it('ping another peer with the default packet count', (done) => { - pull( - ipfs.pingPullStream(otherId), - collect((err, data) => { - expect(err).to.not.exist() + describe('pull stream API', () => { + it('ping another peer with the default packet count', (done) => { + pull( + ipfs.pingPullStream(otherId), + collect((err, data) => { + expect(err).to.not.exist() expect(data).to.be.an('array') expect(data).to.have.lengthOf(3) - data.forEach(packet => { - expect(packet).to.have.keys('Success', 'Time', 'Text') - expect(packet.Time).to.be.a('number') - }) - const resultMsg = data.find(packet => packet.Text.includes('Average latency')) + data.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = data.find(packet => packet.Text.includes('Average latency')) expect(resultMsg).to.exist() - done() - }) - ) - }) + done() + }) + ) + }) - it('ping another peer with a specifc packet count through parameter count', (done) => { - pull( - ipfs.pingPullStream(otherId, {count: 3}), - collect((err, data) => { - expect(err).to.not.exist() + it('ping another peer with a specifc packet count through parameter count', (done) => { + pull( + ipfs.pingPullStream(otherId, {count: 3}), + collect((err, data) => { + expect(err).to.not.exist() expect(data).to.be.an('array') expect(data).to.have.lengthOf(5) - data.forEach(packet => { - expect(packet).to.have.keys('Success', 'Time', 'Text') - expect(packet.Time).to.be.a('number') - }) - const resultMsg = data.find(packet => packet.Text.includes('Average latency')) + data.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = data.find(packet => packet.Text.includes('Average latency')) expect(resultMsg).to.exist() - done() - }) - ) - }) + done() + }) + ) + }) - it('ping another peer with a specifc packet count through parameter n', (done) => { - pull( - ipfs.pingPullStream(otherId, {n: 3}), - collect((err, data) => { - expect(err).to.not.exist() + it('ping another peer with a specifc packet count through parameter n', (done) => { + pull( + ipfs.pingPullStream(otherId, {n: 3}), + collect((err, data) => { + expect(err).to.not.exist() expect(data).to.be.an('array') expect(data).to.have.lengthOf(5) - data.forEach(packet => { - expect(packet).to.have.keys('Success', 'Time', 'Text') - expect(packet.Time).to.be.a('number') - }) - const resultMsg = data.find(packet => packet.Text.includes('Average latency')) + data.forEach(packet => { + expect(packet).to.have.keys('Success', 'Time', 'Text') + expect(packet.Time).to.be.a('number') + }) + const resultMsg = data.find(packet => packet.Text.includes('Average latency')) expect(resultMsg).to.exist() - done() - }) - ) - }) + done() + }) + ) + }) - it('sending both n and count should fail', (done) => { - pull( - ipfs.pingPullStream(otherId, {n: 3, count: 3}), - collect(err => { - expect(err).to.exist() - done() - }) - ) - }) - }) + it('sending both n and count should fail', (done) => { + pull( + ipfs.pingPullStream(otherId, {n: 3, count: 3}), + collect(err => { + expect(err).to.exist() + done() + }) + ) + }) + }) describe('readable stream API', () => { - it('ping another peer with the default packet count', (done) => { - let packetNum = 0 - ipfs.pingReadableStream(otherId) - .on('data', data => { - packetNum++ - expect(data).to.be.an('object') - expect(data).to.have.keys('Success', 'Time', 'Text') - }) - .on('error', err => { - expect(err).not.to.exist() - }) - .on('end', () => { - expect(packetNum).to.equal(3) - done() - }) - }) + it('ping another peer with the default packet count', (done) => { + let packetNum = 0 + ipfs.pingReadableStream(otherId) + .on('data', data => { + packetNum++ + expect(data).to.be.an('object') + expect(data).to.have.keys('Success', 'Time', 'Text') + }) + .on('error', err => { + expect(err).not.to.exist() + }) + .on('end', () => { + expect(packetNum).to.equal(3) + done() + }) + }) - it('ping another peer with a specifc packet count through parameter count', (done) => { - let packetNum = 0 - ipfs.pingReadableStream(otherId, {count: 3}) - .on('data', data => { - packetNum++ - expect(data).to.be.an('object') - expect(data).to.have.keys('Success', 'Time', 'Text') - }) - .on('error', err => { - expect(err).not.to.exist() - }) - .on('end', () => { - expect(packetNum).to.equal(5) - done() - }) - }) + it('ping another peer with a specifc packet count through parameter count', (done) => { + let packetNum = 0 + ipfs.pingReadableStream(otherId, {count: 3}) + .on('data', data => { + packetNum++ + expect(data).to.be.an('object') + expect(data).to.have.keys('Success', 'Time', 'Text') + }) + .on('error', err => { + expect(err).not.to.exist() + }) + .on('end', () => { + expect(packetNum).to.equal(5) + done() + }) + }) - it('ping another peer with a specifc packet count through parameter n', (done) => { - let packetNum = 0 - ipfs.pingReadableStream(otherId, {n: 3}) - .on('data', data => { - packetNum++ - expect(data).to.be.an('object') - expect(data).to.have.keys('Success', 'Time', 'Text') - }) - .on('error', err => { - expect(err).not.to.exist() - }) - .on('end', () => { - expect(packetNum).to.equal(5) - done() - }) - }) + it('ping another peer with a specifc packet count through parameter n', (done) => { + let packetNum = 0 + ipfs.pingReadableStream(otherId, {n: 3}) + .on('data', data => { + packetNum++ + expect(data).to.be.an('object') + expect(data).to.have.keys('Success', 'Time', 'Text') + }) + .on('error', err => { + expect(err).not.to.exist() + }) + .on('end', () => { + expect(packetNum).to.equal(5) + done() + }) + }) - it('sending both n and count should fail', (done) => { - ipfs.pingReadableStream(otherId, {n: 3, count:3}) - .on('error', err => { - expect(err).to.exist() - done() - }) - }) - }) + it('sending both n and count should fail', (done) => { + ipfs.pingReadableStream(otherId, {n: 3, count:3}) + .on('error', err => { + expect(err).to.exist() + done() + }) + }) + }) }) From 9916e38bcfb6cd811ce37601a42027c069bb1c72 Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 24 Mar 2018 14:03:53 +0000 Subject: [PATCH 08/10] fix: add missing pull-stream dev dependency --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 769d31cc2..e1eaea819 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "ipfs": "~0.28.2", "ipfsd-ctl": "~0.30.1", "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" From 87aae6756bd6c544e3a6298fcbb65d8fa85ca09e Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 24 Mar 2018 14:20:11 +0000 Subject: [PATCH 09/10] fix: some remaining linting issues --- src/ping-pull-stream.js | 2 +- src/ping-readable-stream.js | 2 +- test/ping.spec.js | 7 +++---- test/sub-modules.spec.js | 4 ++-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/ping-pull-stream.js b/src/ping-pull-stream.js index ede87fd34..9c18140e2 100644 --- a/src/ping-pull-stream.js +++ b/src/ping-pull-stream.js @@ -20,7 +20,7 @@ module.exports = (arg) => { const p = deferred.source() send(request, (err, stream) => { - if (err) { return p.abort(err) } + if (err) { return p.abort(err) } p.resolve(toPull.source(stream)) }) diff --git a/src/ping-readable-stream.js b/src/ping-readable-stream.js index f56a191f9..6281a44de 100644 --- a/src/ping-readable-stream.js +++ b/src/ping-readable-stream.js @@ -24,7 +24,7 @@ module.exports = (arg) => { send(request, (err, stream) => { if (err) { return pt.destroy(err) } - + pump(stream, pt) }) diff --git a/test/ping.spec.js b/test/ping.spec.js index bcfdce72f..52b347aa8 100644 --- a/test/ping.spec.js +++ b/test/ping.spec.js @@ -14,7 +14,7 @@ const series = require('async/series') const IPFSApi = require('../src') const f = require('./utils/factory') -describe('.ping',function () { +describe('.ping', function () { let ipfs let ipfsd let other @@ -164,7 +164,7 @@ describe('.ping',function () { }) it('sending both n and count should fail', (done) => { - ipfs.ping(otherId, {n: 3, count:3}) + ipfs.ping(otherId, {n: 3, count: 3}) .catch(err => { expect(err).to.exist() done() @@ -209,7 +209,6 @@ describe('.ping',function () { ) }) - it('ping another peer with a specifc packet count through parameter n', (done) => { pull( ipfs.pingPullStream(otherId, {n: 3}), @@ -292,7 +291,7 @@ describe('.ping',function () { }) it('sending both n and count should fail', (done) => { - ipfs.pingReadableStream(otherId, {n: 3, count:3}) + ipfs.pingReadableStream(otherId, {n: 3, count: 3}) .on('error', err => { expect(err).to.exist() done() diff --git a/test/sub-modules.spec.js b/test/sub-modules.spec.js index 8b7dd35a7..1921b0228 100644 --- a/test/sub-modules.spec.js +++ b/test/sub-modules.spec.js @@ -73,8 +73,8 @@ describe('submodules', () => { const pingReadableStream = require('../src/ping-readable-stream')(config) expect(ping).to.be.a('function') - expect(pingPullStream ).to.be.a('function') - expect(pingReadableStream ).to.be.a('function') + expect(pingPullStream).to.be.a('function') + expect(pingReadableStream).to.be.a('function') }) it('log', () => { From fbf1931063e44b50c7d91517160b69af70462414 Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 24 Mar 2018 15:13:16 +0000 Subject: [PATCH 10/10] fix: ping promise test assertion --- test/ping.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ping.spec.js b/test/ping.spec.js index 52b347aa8..09ab34402 100644 --- a/test/ping.spec.js +++ b/test/ping.spec.js @@ -139,7 +139,7 @@ describe('.ping', function () { return ipfs.ping(otherId, {count: 3}) .then((res) => { expect(res).to.be.an('array') - expect(res).to.have.lengthOf(3) + expect(res).to.have.lengthOf(5) res.forEach(packet => { expect(packet).to.have.keys('Success', 'Time', 'Text') expect(packet.Time).to.be.a('number')