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

Commit

Permalink
feat: (breaking change) stats spec, spec repo, stream to value on fil…
Browse files Browse the repository at this point in the history
…es read (#679)

* tests: STAT spec

* fix(breaking change): REPO spec

* feat(breaking change): FILES spec: stream to value on files read

* docs: improve readme
  • Loading branch information
hacdias authored and daviddias committed Jan 27, 2018
1 parent ec04f6e commit 118456e
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 113 deletions.
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,27 +170,23 @@ $ 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.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.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)
- [`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-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
- `ipfs.repo.stat()`
- `ipfs.repo.gc()`
#### `Graph`
- [dag (not implemented, yet!)](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/DAG.md)
Expand Down Expand Up @@ -264,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:
- `ipfs.stats.bitswap([callback])`
- `ipfs.stats.bw([options, callback])`
- `ipfs.stats.repo([options, callback])`
- [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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions src/files/flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/files/read.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
'use strict'

const promisify = require('promisify-es6')
const streamToValue = require('../utils/stream-to-value')

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
send({

send.andTransform({
path: 'files/read',
args: args,
qs: opts
}, callback)
}, streamToValue, callback)
})
}
15 changes: 13 additions & 2 deletions src/repo/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
9 changes: 7 additions & 2 deletions src/repo/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
1 change: 0 additions & 1 deletion src/stats/repo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

'use strict'

const promisify = require('promisify-es6')
Expand Down
96 changes: 18 additions & 78 deletions test/files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})
Expand All @@ -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()
})
})
})
Expand Down Expand Up @@ -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()
})
})

Expand Down Expand Up @@ -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)
})
Expand All @@ -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)
})
Expand Down Expand Up @@ -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()
})
})

Expand Down
34 changes: 34 additions & 0 deletions test/interface/repo.spec.js
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 34 additions & 0 deletions test/interface/stats.spec.js
Original file line number Diff line number Diff line change
@@ -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)
Loading

0 comments on commit 118456e

Please sign in to comment.