From e72068a8e2f13ff128b8b38814326017cb6adf72 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 28 Jun 2016 10:13:42 +0100 Subject: [PATCH 1/4] init block api --- API/block/README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 API/block/README.md diff --git a/API/block/README.md b/API/block/README.md new file mode 100644 index 00000000..5d8c88f6 --- /dev/null +++ b/API/block/README.md @@ -0,0 +1,2 @@ +block API +========= From e32090e3bb13c72bd50a739074d8bc8095689f23 Mon Sep 17 00:00:00 2001 From: nginnever Date: Sat, 2 Jul 2016 12:12:07 -0700 Subject: [PATCH 2/4] feat(block): spec --- API/block/README.md | 76 +++++++++++++++++++++++++++++++++++++++ src/block.js | 87 +++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 1 + 3 files changed, 164 insertions(+) create mode 100644 src/block.js diff --git a/API/block/README.md b/API/block/README.md index 5d8c88f6..e0252edf 100644 --- a/API/block/README.md +++ b/API/block/README.md @@ -1,2 +1,78 @@ block API ========= + +#### `get` + +> Get a raw IPFS block. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.block.get(multihash, [callback]) + +`multihash` is a [multihash][] which can be passed as + +- Buffer, the raw Buffer of the multihash +- String, the base58 encoded version of the multihash + +`callback` must follow `function (err, block) {}` signature, where `err` is an error if the operation was not successful and `block` is a [Block][]. + + +```js +ipfs.block.get(multihash, function (err, data) { + // data is the raw data contained in a block +}) +``` + +If no `callback` is passed, a promise is returned. + + + + +#### `put` + +> Stores input as an IPFS block. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.block.put(data, [callback]) + +Where `data` can be a + +- Buffer, requiring that the encoding is specified on the options. if no + encoding is specified, Buffer is treated as the Data field +- [Block][] instance + +`callback` has the signature `function (err, block) {}`, where `err` is an error +if the operation was not successful. and `block` is a [Block][]. + +If no `callback` is passed, a promise is returned. + + + + + +#### `stat` + +> Print information of a raw IPFS block. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.block.stat(multihash, [callback]) + +`multihash` is a [multihash][] which can be passed as: + +- Buffer, the raw Buffer of the multihash (or of and encoded version) +- String, the toString version of the multihash (or of an encoded version) + +`callback` must follow the signature `function (err, stats) {}`, where `err` is +an error if the operation was not successful and `stats` is an object with +the format + +```JavaScript +{ + Key: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD', + Size: 10 +} +``` + +If no `callback` is passed, a promise is returned. \ No newline at end of file diff --git a/src/block.js b/src/block.js new file mode 100644 index 00000000..c81684af --- /dev/null +++ b/src/block.js @@ -0,0 +1,87 @@ +/* eslint-env mocha */ +/* globals apiClients */ +'use strict' + +const expect = require('chai').expect + +module.exports = (common) => { + describe.only('.block', () => { + const blorbKey = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ' + const blorb = Buffer('blorb') + + it('returns an error when putting an array of files', () => { + return apiClients.a.block.put([blorb, blorb], (err) => { + console.log(err) + expect(err).to.be.an.instanceof(Error) + }) + }) + + it('block.put', (done) => { + apiClients.a.block.put(blorb, (err, res) => { + expect(err).to.not.exist + expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ') + done() + }) + }) + + it('block.get', (done) => { + apiClients.a.block.get(blorbKey, (err, res) => { + expect(err).to.not.exist + + let buf = '' + res + .on('data', function (data) { buf += data }) + .on('end', function () { + expect(buf).to.be.equal('blorb') + done() + }) + }) + }) + + it('block.stat', (done) => { + apiClients.a.block.stat(blorbKey, (err, res) => { + expect(err).to.not.exist + expect(res).to.have.property('Key') + expect(res).to.have.property('Size') + done() + }) + }) + + describe('promise', () => { + it('returns an error when putting an array of files', () => { + return apiClients.a.block.put([blorb, blorb]) + .catch((err) => { + expect(err).to.be.an.instanceof(Error) + }) + }) + + it('block.put', () => { + return apiClients.a.block.put(blorb) + .then((res) => { + expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ') + }) + }) + + it('block.get', (done) => { + return apiClients.a.block.get(blorbKey) + .then((res) => { + let buf = '' + res + .on('data', function (data) { buf += data }) + .on('end', function () { + expect(buf).to.be.equal('blorb') + done() + }) + }) + }) + + it('block.stat', () => { + return apiClients.a.block.stat(blorbKey) + .then((res) => { + expect(res).to.have.property('Key') + expect(res).to.have.property('Size') + }) + }) + }) + }) +} diff --git a/src/index.js b/src/index.js index f9ad0c53..ee35b234 100644 --- a/src/index.js +++ b/src/index.js @@ -6,3 +6,4 @@ exports.config = require('./config') exports.pin = require('./pin') exports.generic = require('./generic') exports.swarm = require('./swarm') +exports.block = require('./block') From c371550022831023d39c7e70cbb404bcdeafba0c Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 16 Aug 2016 16:57:34 +0100 Subject: [PATCH 3/4] fix(block spec): apply CR --- API/block/README.md | 48 ++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/API/block/README.md b/API/block/README.md index e0252edf..e5064b0f 100644 --- a/API/block/README.md +++ b/API/block/README.md @@ -7,50 +7,43 @@ block API ##### `Go` **WIP** -##### `JavaScript` - ipfs.block.get(multihash, [callback]) +##### `JavaScript` - ipfs.block.get(multihash, [options, callback]) -`multihash` is a [multihash][] which can be passed as +`multihash` is a [multihash][multihash] which can be passed as: - Buffer, the raw Buffer of the multihash - String, the base58 encoded version of the multihash -`callback` must follow `function (err, block) {}` signature, where `err` is an error if the operation was not successful and `block` is a [Block][]. - +`callback` must follow `function (err, block) {}` signature, where `err` is an error if the operation was not successful and `block` is a [Block][block] type object, containing both the data and the hash of the block. ```js -ipfs.block.get(multihash, function (err, data) { - // data is the raw data contained in a block +ipfs.block.get(multihash, function (err, block) { + if (err) { + throw err + } + console.log(block.key, block.data) }) ``` If no `callback` is passed, a promise is returned. - - - #### `put` > Stores input as an IPFS block. ##### `Go` **WIP** -##### `JavaScript` - ipfs.block.put(data, [callback]) +##### `JavaScript` - ipfs.block.put(block, [callback]) -Where `data` can be a +Where `block` can be: -- Buffer, requiring that the encoding is specified on the options. if no - encoding is specified, Buffer is treated as the Data field -- [Block][] instance +- `Buffer` - the raw bytes of the Block +- [`Block`][block] instance -`callback` has the signature `function (err, block) {}`, where `err` is an error -if the operation was not successful. and `block` is a [Block][]. +`callback` has the signature `function (err) {}`, where `err` is an error if the operation was not successful. If no `callback` is passed, a promise is returned. - - - - #### `stat` > Print information of a raw IPFS block. @@ -59,14 +52,12 @@ If no `callback` is passed, a promise is returned. ##### `JavaScript` - ipfs.block.stat(multihash, [callback]) -`multihash` is a [multihash][] which can be passed as: +`multihash` is a [multihash][multihash] which can be passed as: -- Buffer, the raw Buffer of the multihash (or of and encoded version) -- String, the toString version of the multihash (or of an encoded version) +- `Buffer`, the raw Buffer of the multihash (or of and encoded version) +- `String`, the toString version of the multihash (or of an encoded version) -`callback` must follow the signature `function (err, stats) {}`, where `err` is -an error if the operation was not successful and `stats` is an object with -the format +`callback` must follow the signature `function (err, stats) {}`, where `err` is an error if the operation was not successful and `stats` is an object with the format:` ```JavaScript { @@ -75,4 +66,7 @@ the format } ``` -If no `callback` is passed, a promise is returned. \ No newline at end of file +If no `callback` is passed, a promise is returned. + +[block](https://github.com/ipfs/js-ipfs-block) +[multihash](https://github.com/multiformats/multihash) From 488f61bf8bf81fc0de9245cf2c6fe7b6ceed5c06 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 16 Aug 2016 17:16:24 +0100 Subject: [PATCH 4/4] fix(block): tests --- API/block/README.md | 2 +- package.json | 2 +- src/block.js | 121 +++++++++++++++++++++----------------------- 3 files changed, 61 insertions(+), 64 deletions(-) diff --git a/API/block/README.md b/API/block/README.md index e5064b0f..bdb3f1d8 100644 --- a/API/block/README.md +++ b/API/block/README.md @@ -40,7 +40,7 @@ Where `block` can be: - `Buffer` - the raw bytes of the Block - [`Block`][block] instance -`callback` has the signature `function (err) {}`, where `err` is an error if the operation was not successful. +`callback` has the signature `function (err, block) {}`, where `err` is an error if the operation was not successful and `block` is a [Block][block] type object, containing both the data and the hash of the block. If no `callback` is passed, a promise is returned. diff --git a/package.json b/package.json index 1c7c9006..d07b7625 100644 --- a/package.json +++ b/package.json @@ -48,4 +48,4 @@ "greenkeeperio-bot ", "nginnever " ] -} \ No newline at end of file +} diff --git a/src/block.js b/src/block.js index c81684af..39bb682c 100644 --- a/src/block.js +++ b/src/block.js @@ -1,87 +1,84 @@ /* eslint-env mocha */ -/* globals apiClients */ +/* eslint max-nested-callbacks: ["error", 8] */ + 'use strict' const expect = require('chai').expect module.exports = (common) => { - describe.only('.block', () => { - const blorbKey = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ' - const blorb = Buffer('blorb') - - it('returns an error when putting an array of files', () => { - return apiClients.a.block.put([blorb, blorb], (err) => { - console.log(err) - expect(err).to.be.an.instanceof(Error) - }) - }) + describe('.block', () => { + let ipfs - it('block.put', (done) => { - apiClients.a.block.put(blorb, (err, res) => { - expect(err).to.not.exist - expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ') - done() - }) - }) + before(function (done) { + // CI takes longer to instantiate the daemon, + // so we need to increase the timeout for the + // before step + this.timeout(20 * 1000) - it('block.get', (done) => { - apiClients.a.block.get(blorbKey, (err, res) => { + common.setup((err, factory) => { expect(err).to.not.exist - - let buf = '' - res - .on('data', function (data) { buf += data }) - .on('end', function () { - expect(buf).to.be.equal('blorb') - done() - }) + factory.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) }) }) - it('block.stat', (done) => { - apiClients.a.block.stat(blorbKey, (err, res) => { - expect(err).to.not.exist - expect(res).to.have.property('Key') - expect(res).to.have.property('Size') - done() - }) + after((done) => { + common.teardown(done) }) - describe('promise', () => { - it('returns an error when putting an array of files', () => { - return apiClients.a.block.put([blorb, blorb]) - .catch((err) => { - expect(err).to.be.an.instanceof(Error) - }) + describe('callback API', () => { + it('.put', (done) => { + const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ' + const blob = Buffer('blorb') + + ipfs.block.put(blob, (err, res) => { + expect(err).to.not.exist + expect(res).to.have.a.property('Key', expectedHash) + done() + }) }) - it('block.put', () => { - return apiClients.a.block.put(blorb) - .then((res) => { - expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ') - }) + it('.put error with array of blocks', () => { + const blob = Buffer('blorb') + + ipfs.block.put([blob, blob], (err) => { + expect(err).to.be.an.instanceof(Error) + }) }) it('block.get', (done) => { - return apiClients.a.block.get(blorbKey) - .then((res) => { - let buf = '' - res - .on('data', function (data) { buf += data }) - .on('end', function () { - expect(buf).to.be.equal('blorb') - done() - }) - }) + const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ' + + ipfs.block.get(hash, (err, res) => { + expect(err).to.not.exist + + // TODO review this + let buf = '' + res + .on('data', function (data) { buf += data }) + .on('end', function () { + expect(buf).to.be.equal('blorb') + done() + }) + }) }) - it('block.stat', () => { - return apiClients.a.block.stat(blorbKey) - .then((res) => { - expect(res).to.have.property('Key') - expect(res).to.have.property('Size') - }) + it('block.stat', (done) => { + const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ' + + ipfs.block.stat(hash, (err, res) => { + expect(err).to.not.exist + expect(res).to.have.property('Key') + expect(res).to.have.property('Size') + done() + }) }) }) + + describe('promise API', () => { + }) }) }