From 34c0b30110d68a98e9af7e6c2d021035c31b74cc Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 25 Jan 2018 11:54:03 +0000 Subject: [PATCH 1/4] tests: STAT spec --- README.md | 6 +++--- package.json | 2 +- test/interface/stats.spec.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 test/interface/stats.spec.js diff --git a/README.md b/README.md index 61bb51f3c..98dcea73c 100644 --- a/README.md +++ b/README.md @@ -265,9 +265,9 @@ $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"P - [`ipfs.config.replace(config, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/CONFIG.md#configreplace) - stats: - - `ipfs.stats.bitswap([callback])` - - `ipfs.stats.bw([options, callback])` - - `ipfs.stats.repo([options, callback])` + - [`ipfs.stats.bitswap([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#bitswap) + - [`ipfs.stats.bw([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#bw) + - [`ipfs.stats.repo([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#repo) - log: - `ipfs.log.ls([callback])` diff --git a/package.json b/package.json index 682121c88..6ad31253c 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ "eslint-plugin-react": "^7.5.1", "go-ipfs-dep": "^0.4.13", "gulp": "^3.9.1", - "interface-ipfs-core": "~0.41.0", + "interface-ipfs-core": "~0.43.0", "hapi": "^16.6.2", "ipfsd-ctl": "~0.27.0", "pre-commit": "^1.2.2", diff --git a/test/interface/stats.spec.js b/test/interface/stats.spec.js new file mode 100644 index 000000000..6186c33db --- /dev/null +++ b/test/interface/stats.spec.js @@ -0,0 +1,34 @@ +/* eslint-env mocha */ + +'use strict' + +const test = require('interface-ipfs-core') +const parallel = require('async/parallel') + +const IPFSApi = require('../../src') + +const DaemonFactory = require('ipfsd-ctl') +const df = DaemonFactory.create() + +const nodes = [] +const common = { + setup: function (callback) { + callback(null, { + spawnNode: (cb) => { + df.spawn((err, _ipfsd) => { + if (err) { + return cb(err) + } + + nodes.push(_ipfsd) + cb(null, IPFSApi(_ipfsd.apiAddr)) + }) + } + }) + }, + teardown: function (callback) { + parallel(nodes.map((node) => (cb) => node.stop(cb)), callback) + } +} + +test.stats(common) From d612db34260aa518f9dd5f1a2e226a5d8dcde9e1 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 26 Jan 2018 08:29:30 +0000 Subject: [PATCH 2/4] fix(breaking change): REPO spec --- README.md | 7 ++++--- src/repo/stat.js | 15 +++++++++++++-- src/repo/version.js | 9 +++++++-- src/stats/repo.js | 1 - test/interface/repo.spec.js | 34 ++++++++++++++++++++++++++++++++++ test/repo.spec.js | 10 ++++------ 6 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 test/interface/repo.spec.js diff --git a/README.md b/README.md index 98dcea73c..900b5dc76 100644 --- a/README.md +++ b/README.md @@ -187,9 +187,10 @@ $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"P - [`ipfs.block.put(block, cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md#put) - [`ipfs.block.stat(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md#stat) -- repo - - `ipfs.repo.stat()` - - `ipfs.repo.gc()` +- [repo](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md) + - [`ipfs.repo.gc([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#gc) + - [`ipfs.repo.stat([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#stat) + - [`ipfs.repo.version([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#version) #### `Graph` diff --git a/src/repo/stat.js b/src/repo/stat.js index 8ffbfe526..aca22bf11 100644 --- a/src/repo/stat.js +++ b/src/repo/stat.js @@ -2,15 +2,26 @@ const promisify = require('promisify-es6') +const transform = function (res, callback) { + callback(null, { + numObjects: res.NumObjects, + repoSize: res.RepoSize, + repoPath: res.RepoPath, + version: res.Version, + storageMax: res.StorageMax + }) +} + module.exports = (send) => { return promisify((opts, callback) => { if (typeof (opts) === 'function') { callback = opts opts = {} } - send({ + + send.andTransform({ path: 'repo/stat', qs: opts - }, callback) + }, transform, callback) }) } diff --git a/src/repo/version.js b/src/repo/version.js index 0b49f78c6..84e2a0008 100644 --- a/src/repo/version.js +++ b/src/repo/version.js @@ -2,15 +2,20 @@ const promisify = require('promisify-es6') +const transform = function (res, callback) { + callback(null, res.Version) +} + module.exports = (send) => { return promisify((opts, callback) => { if (typeof (opts) === 'function') { callback = opts opts = {} } - send({ + + send.andTransform({ path: 'repo/version', qs: opts - }, callback) + }, transform, callback) }) } diff --git a/src/stats/repo.js b/src/stats/repo.js index 55a8fdd68..8e2a87a7e 100644 --- a/src/stats/repo.js +++ b/src/stats/repo.js @@ -1,4 +1,3 @@ - 'use strict' const promisify = require('promisify-es6') diff --git a/test/interface/repo.spec.js b/test/interface/repo.spec.js new file mode 100644 index 000000000..acce37e75 --- /dev/null +++ b/test/interface/repo.spec.js @@ -0,0 +1,34 @@ +/* eslint-env mocha */ +/* eslint max-nested-callbacks: ["error", 8] */ +'use strict' + +const test = require('interface-ipfs-core') +const parallel = require('async/parallel') + +const IPFSApi = require('../../src') + +const DaemonFactory = require('ipfsd-ctl') +const df = DaemonFactory.create() + +const nodes = [] +const common = { + setup: function (callback) { + callback(null, { + spawnNode: (cb) => { + df.spawn((err, _ipfsd) => { + if (err) { + return cb(err) + } + + nodes.push(_ipfsd) + cb(null, IPFSApi(_ipfsd.apiAddr)) + }) + } + }) + }, + teardown: function (callback) { + parallel(nodes.map((node) => (cb) => node.stop(cb)), callback) + } +} + +test.repo(common) diff --git a/test/repo.spec.js b/test/repo.spec.js index b0b4fce9f..baf9525d9 100644 --- a/test/repo.spec.js +++ b/test/repo.spec.js @@ -41,8 +41,8 @@ describe('.repo', function () { ipfs.repo.stat((err, res) => { expect(err).to.not.exist() expect(res).to.exist() - expect(res).to.have.a.property('NumObjects') - expect(res).to.have.a.property('RepoSize') + expect(res).to.have.a.property('numObjects') + expect(res).to.have.a.property('repoSize') done() }) }) @@ -51,7 +51,6 @@ describe('.repo', function () { ipfs.repo.version((err, res) => { expect(err).to.not.exist() expect(res).to.exist() - expect(res).to.have.a.property('Version') done() }) }) @@ -66,8 +65,8 @@ describe('.repo', function () { return ipfs.repo.stat() .then((res) => { expect(res).to.exist() - expect(res).to.have.a.property('NumObjects') - expect(res).to.have.a.property('RepoSize') + expect(res).to.have.a.property('numObjects') + expect(res).to.have.a.property('repoSize') }) }) @@ -75,7 +74,6 @@ describe('.repo', function () { return ipfs.repo.version() .then(res => { expect(res).to.exist() - expect(res).to.have.a.property('Version') }) }) }) From c807858e9292580fdbbe60ea5c6bee084e12b55f Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 26 Jan 2018 08:31:51 +0000 Subject: [PATCH 3/4] feat(breaking change): FILES spec: stream to value on files read --- README.md | 18 ++++----- src/files/flush.js | 5 +++ src/files/read.js | 6 ++- test/files.spec.js | 96 +++++++++------------------------------------- 4 files changed, 36 insertions(+), 89 deletions(-) diff --git a/README.md b/README.md index 900b5dc76..35e55dba2 100644 --- a/README.md +++ b/README.md @@ -172,15 +172,15 @@ $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"P - [`ipfs.files.getPullStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#getpullstream) - `ipfs.ls` - MFS (mutable file system) specific: - - `ipfs.files.cp` - - `ipfs.files.ls` - - `ipfs.files.mkdir` - - `ipfs.files.stat` - - `ipfs.files.rm` - - `ipfs.files.read` - - `ipfs.files.write` - - `ipfs.files.mv` - - `ipfs.files.flush(path, [callback])` + - [`ipfs.files.cp([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#cp) + - [`ipfs.files.mkdir(path, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#mkdir) + - [`ipfs.files.stat(path, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#stat) + - [`ipfs.files.rm(path, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#rm) + - [`ipfs.files.read(path, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#read) + - [`ipfs.files.write(path, content, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#write) + - [`ipfs.files.mv([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#mv) + - [`ipfs.files.ls([path, options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#ls) + - [`ipfs.files.flush([path, callback])`(https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#flush) - [block](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md) - [`ipfs.block.get(cid, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md#get) diff --git a/src/files/flush.js b/src/files/flush.js index 9f70f2adb..ea4de8dc6 100644 --- a/src/files/flush.js +++ b/src/files/flush.js @@ -4,6 +4,11 @@ const promisify = require('promisify-es6') module.exports = (send) => { return promisify((args, callback) => { + if (typeof args === 'function') { + callback = args + args = '/' + } + return send({ path: 'files/flush', args: args diff --git a/src/files/read.js b/src/files/read.js index bf339c24b..378d53dfa 100644 --- a/src/files/read.js +++ b/src/files/read.js @@ -1,6 +1,7 @@ 'use strict' const promisify = require('promisify-es6') +const streamToValue = require('../utils/stream-to-value') module.exports = (send) => { return promisify((args, opts, callback) => { @@ -8,10 +9,11 @@ module.exports = (send) => { callback = opts opts = {} } - send({ + + send.andTransform({ path: 'files/read', args: args, qs: opts - }, callback) + }, streamToValue, callback) }) } diff --git a/test/files.spec.js b/test/files.spec.js index 3a9425d56..3c88e26b5 100644 --- a/test/files.spec.js +++ b/test/files.spec.js @@ -244,19 +244,10 @@ describe('.files (the MFS API part)', function () { .write('/test-folder/test-file-2.txt', Buffer.from('hello world'), {create: true}, (err) => { expect(err).to.not.exist() - ipfs.files.read('/test-folder/test-file-2.txt', (err, stream) => { + ipfs.files.read('/test-folder/test-file-2.txt', (err, buf) => { expect(err).to.not.exist() - - let buf = '' - stream - .on('error', (err) => expect(err).to.not.exist()) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.be.equal('hello world') - done() - }) + expect(buf.toString()).to.be.equal('hello world') + done() }) }) }) @@ -266,21 +257,10 @@ describe('.files (the MFS API part)', function () { .write('/test-folder/test-file-2.txt', Buffer.from('hello world'), (err) => { expect(err).to.not.exist() - ipfs.files.read('/test-folder/test-file-2.txt', (err, stream) => { + ipfs.files.read('/test-folder/test-file-2.txt', (err, buf) => { expect(err).to.not.exist() - - let buf = '' - stream - .on('error', (err) => { - expect(err).to.not.exist() - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.be.equal('hello world') - done() - }) + expect(buf.toString()).to.be.equal('hello world') + done() }) }) }) @@ -315,20 +295,10 @@ describe('.files (the MFS API part)', function () { return done() } - ipfs.files.read('/test-folder/test-file', (err, stream) => { + ipfs.files.read('/test-folder/test-file', (err, buf) => { expect(err).to.not.exist() - let buf = '' - stream - .on('error', (err) => { - expect(err).to.not.exist() - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(Buffer.from(buf)).to.deep.equal(testfile) - done() - }) + expect(Buffer.from(buf)).to.deep.equal(testfile) + done() }) }) @@ -417,19 +387,9 @@ describe('.files (the MFS API part)', function () { .then(() => { return ipfs.files.read('/test-folder/test-file-2.txt') }) - .then((stream) => { - let buf = '' - stream - .on('error', (err) => { - expect(err).to.not.exist() - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.be.equal('hello world') - done() - }) + .then((buf) => { + expect(buf.toString()).to.be.equal('hello world') + done() }) .catch(done) }) @@ -440,19 +400,9 @@ describe('.files (the MFS API part)', function () { .then(() => { return ipfs.files.read('/test-folder/test-file-2.txt') }) - .then((stream) => { - let buf = '' - stream - .on('error', (err) => { - expect(err).to.not.exist() - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.be.equal('hello world') - done() - }) + .then((buf) => { + expect(buf.toString()).to.be.equal('hello world') + done() }) .catch(done) }) @@ -482,19 +432,9 @@ describe('.files (the MFS API part)', function () { if (!isNode) { return done() } ipfs.files.read('/test-folder/test-file') - .then((stream) => { - let buf = '' - stream - .on('error', (err) => { - expect(err).to.not.exist() - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(Buffer.from(buf)).to.eql(testfile) - done() - }) + .then((buf) => { + expect(Buffer.from(buf)).to.eql(testfile) + done() }) }) From e3c20c16ba9e91aec863765252749bf1d2053fa3 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 26 Jan 2018 08:38:55 +0000 Subject: [PATCH 4/4] docs: improve readme --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 35e55dba2..2a052175e 100644 --- a/README.md +++ b/README.md @@ -170,8 +170,8 @@ $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"P - [`ipfs.files.get(ipfsPath, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#get). Alias to `ipfs.get`. - [`ipfs.files.getReadableStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#getreadablestream) - [`ipfs.files.getPullStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#getpullstream) - - `ipfs.ls` - - MFS (mutable file system) specific: + - [`ipfs.ls(ipfsPath, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#ls) + - [MFS (mutable file system) specific](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#mutable-file-system) - [`ipfs.files.cp([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#cp) - [`ipfs.files.mkdir(path, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#mkdir) - [`ipfs.files.stat(path, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#stat) @@ -179,19 +179,14 @@ $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"P - [`ipfs.files.read(path, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#read) - [`ipfs.files.write(path, content, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#write) - [`ipfs.files.mv([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#mv) - - [`ipfs.files.ls([path, options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#ls) - - [`ipfs.files.flush([path, callback])`(https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#flush) + - [`ipfs.files.ls([path, options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#ls-1) + - [`ipfs.files.flush([path, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#flush) - [block](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md) - [`ipfs.block.get(cid, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md#get) - [`ipfs.block.put(block, cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md#put) - [`ipfs.block.stat(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md#stat) -- [repo](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md) - - [`ipfs.repo.gc([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#gc) - - [`ipfs.repo.stat([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#stat) - - [`ipfs.repo.version([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#version) - #### `Graph` - [dag (not implemented, yet!)](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/DAG.md) @@ -265,17 +260,22 @@ $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"P - [`ipfs.config.set(key, value, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/CONFIG.md#configset) - [`ipfs.config.replace(config, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/CONFIG.md#configreplace) -- stats: +- [stats](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md) - [`ipfs.stats.bitswap([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#bitswap) - [`ipfs.stats.bw([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#bw) - [`ipfs.stats.repo([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#repo) -- log: +- log - `ipfs.log.ls([callback])` - `ipfs.log.tail([callback])` - `ipfs.log.level(subsystem, level, [options, callback])` + +- [repo](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md) + - [`ipfs.repo.gc([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#gc) + - [`ipfs.repo.stat([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#stat) + - [`ipfs.repo.version([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#version) -- key: +- [key](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/KEY.md) - [`ipfs.key.gen(name, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/KEY.md#javascript---ipfskeygenname-options-callback) - [`ipfs.key.list([options, callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/KEY.md#javascript---ipfskeylistcallback) - [`ipfs.key.rm(name, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/KEY.md#javascript---ipfskeyrmname-callback)