From 3c010707a6e552b9d3c46e1339990f5cc7305ee4 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 8 Apr 2016 15:59:29 +0200 Subject: [PATCH 1/4] Add extension prop and fix tests --- .gitignore | 2 +- .npmignore | 2 +- package.json | 3 + src/block-service.js | 46 ++++-- src/block.js | 14 +- test/block-service-test.js | 144 ++++++++++-------- test/{block-test.js => block.spec.js} | 20 +-- test/browser.js | 50 +++++- test/node.js | 37 ++--- ...c55d25f3042c22501e41d1246e7a1e9d3d8ec.data | 0 ...dcf095c1d219356cdf538ffce705a52d5738d.data | Bin ...ea3b310512d7767a9abfbd7a928a85e977173.data | 0 ...0b0d3be3528fbbcc814feb7f2fbf71ca06162.data | Bin ...60e64383755a8c5163ba3c053c3b65777ed16.data | 0 ...0aa787e820fe98dbf68131bf70e796693d373.data | Bin ...ccadfda3d8bc7d2f8cdf302aa51dae8dae9da.data | Bin ...223c2d96a9f70b5fd8b1d513f8c5b69dcaed4.data | 0 ...932be52c56d99c5966b65e0111239f098bbef.data | 0 ...6f9fccbf5fa0106df39556b3b411ea4121a6f.data | Bin ...481e88d2c710099738ccbd6964075730194ba.data | Bin ...3346a644605d7614dca53cd3a59f7385a8abb.data | 0 ...7f28fa9367ee167c312e6d65a2e02e81ab815.data | Bin ...68af3d10e1a497971629c07606bfdb812303d.data | Bin ...02a72b057d1c7f2e682d0776a5363e2cca974.data | 0 ...dced761633aca6704ba01a9be55934d95e5f1.data | Bin ...16cfa9a7ae5533f8e997cdab2ebadd7506340.data | 0 ...e5cf3497a7845ee8f94456ccf4d1d2f6602b5.data | 0 ...c0f1e4947cff8e422610996e609dbcb976598.data | 0 ...62f687a00c638bcb580feae06452e0c1f20b4.data | 0 ...56baef91c7dc345e73d049ab570abe10dfbb9.data | Bin ...3edca90b68053c00b3004b7f0accbe1e8eedf.data | Bin ...2fdadf9d45666c3be122a2efb5db93c1d5fa6.data | 0 ...be72a83e80f9c7bca1abcaa42298a57a33ff5.data | Bin ...0e8710bd97eafcce5380a127dcc5ea32a887f.data | Bin ...fb92427ae41e4649b934ca495991b7852b855.data | 0 ...9e72dc99d23a391d3db5e1e42d00527241671.data | 0 ...eb49e3772f8949dacec55d099e3e17ac851c3.data | 0 ...41317afc9f967abe2aa05e7c641f7bbe98a37.data | Bin ...807def07b0db2f018808620eb30b980e94011.data | 0 ...38b95dd29d31eac6433af0fb6fcd83dd80778.data | 0 test/{example-repo => test-repo}/config | 0 .../datastore/000002.ldb | Bin .../datastore/000005.ldb | Bin .../datastore/CURRENT | 0 .../datastore/LOCK | 0 .../{example-repo => test-repo}/datastore/LOG | 0 .../datastore/LOG.old | 0 .../datastore/MANIFEST-000007 | Bin test/{example-repo => test-repo}/version | 0 49 files changed, 197 insertions(+), 121 deletions(-) rename test/{block-test.js => block.spec.js} (64%) rename test/{example-repo => test-repo}/blocks/1220120f/1220120f6af601d46e10b2d2e11ed71c55d25f3042c22501e41d1246e7a1e9d3d8ec.data (100%) rename test/{example-repo => test-repo}/blocks/122031d6/122031d6da265092f1b03fec969243fdcf095c1d219356cdf538ffce705a52d5738d.data (100%) rename test/{example-repo => test-repo}/blocks/122031e7/122031e7a41c15d03feb8cd793c3348ea3b310512d7767a9abfbd7a928a85e977173.data (100%) rename test/{example-repo => test-repo}/blocks/12203628/12203628a4a19525dd84bbbffe132ec0b0d3be3528fbbcc814feb7f2fbf71ca06162.data (100%) rename test/{example-repo => test-repo}/blocks/12204a5a/12204a5a95586f52e25811cf214677160e64383755a8c5163ba3c053c3b65777ed16.data (100%) rename test/{example-repo => test-repo}/blocks/1220503a/1220503aec3bbd533b39fd56f5148750aa787e820fe98dbf68131bf70e796693d373.data (100%) rename test/{example-repo => test-repo}/blocks/12205200/12205200cc6b6f79e1588480d9f9016ccadfda3d8bc7d2f8cdf302aa51dae8dae9da.data (100%) rename test/{example-repo => test-repo}/blocks/122052c6/122052c63c7775396b3f82c639977a7223c2d96a9f70b5fd8b1d513f8c5b69dcaed4.data (100%) rename test/{example-repo => test-repo}/blocks/12205994/122059948439065f29619ef41280cbb932be52c56d99c5966b65e0111239f098bbef.data (100%) rename test/{example-repo => test-repo}/blocks/12205dff/12205dffb3c88f3de7036dc1840f0086f9fccbf5fa0106df39556b3b411ea4121a6f.data (100%) rename test/{example-repo => test-repo}/blocks/12206006/1220600627d80642365010791de1d62481e88d2c710099738ccbd6964075730194ba.data (100%) rename test/{example-repo => test-repo}/blocks/122062ce/122062ce1f2c91a13a97b596e873b5a3346a644605d7614dca53cd3a59f7385a8abb.data (100%) rename test/{example-repo => test-repo}/blocks/12206781/122067817186b8ff365c758f387e3ae7f28fa9367ee167c312e6d65a2e02e81ab815.data (100%) rename test/{example-repo => test-repo}/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data (100%) rename test/{example-repo => test-repo}/blocks/1220709b/1220709b2dcc5f6a90ad64d6fe3a5d202a72b057d1c7f2e682d0776a5363e2cca974.data (100%) rename test/{example-repo => test-repo}/blocks/12207d5c/12207d5cd815f41ac0566dd956c8541dced761633aca6704ba01a9be55934d95e5f1.data (100%) rename test/{example-repo => test-repo}/blocks/12207fb8/12207fb898b5d7be46d85feb75d894e16cfa9a7ae5533f8e997cdab2ebadd7506340.data (100%) rename test/{example-repo => test-repo}/blocks/12208b87/12208b872ca4ee517608331696dd6b3e5cf3497a7845ee8f94456ccf4d1d2f6602b5.data (100%) rename test/{example-repo => test-repo}/blocks/122090c0/122090c07a7795c1193510a696d1fdfc0f1e4947cff8e422610996e609dbcb976598.data (100%) rename test/{example-repo => test-repo}/blocks/1220929a/1220929a303c39da8a0b67c09697462f687a00c638bcb580feae06452e0c1f20b4.data (100%) rename test/{example-repo => test-repo}/blocks/1220933b/1220933b41d37fd4508cdff45930dff56baef91c7dc345e73d049ab570abe10dfbb9.data (100%) rename test/{example-repo => test-repo}/blocks/12209d6c/12209d6c2be50f706953479ab9df2ce3edca90b68053c00b3004b7f0accbe1e8eedf.data (100%) rename test/{example-repo => test-repo}/blocks/1220a52c/1220a52c3602030cb912edfe4de97002fdadf9d45666c3be122a2efb5db93c1d5fa6.data (100%) rename test/{example-repo => test-repo}/blocks/1220c0fc/1220c0fc6b49543d7bf04e83d2a5a7cbe72a83e80f9c7bca1abcaa42298a57a33ff5.data (100%) rename test/{example-repo => test-repo}/blocks/1220d15b/1220d15b2cf7ecc6eccbf7e68c2e4290e8710bd97eafcce5380a127dcc5ea32a887f.data (100%) rename test/{example-repo => test-repo}/blocks/1220e3b0/1220e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.data (100%) rename test/{example-repo => test-repo}/blocks/1220e586/1220e586199640e1a4c63fa38c5434b9e72dc99d23a391d3db5e1e42d00527241671.data (100%) rename test/{example-repo => test-repo}/blocks/1220e5f8/1220e5f87b2b69cb032267bbc72a598eb49e3772f8949dacec55d099e3e17ac851c3.data (100%) rename test/{example-repo => test-repo}/blocks/1220e605/1220e605408ac3f78113ac9a7fd486441317afc9f967abe2aa05e7c641f7bbe98a37.data (100%) rename test/{example-repo => test-repo}/blocks/1220e6a0/1220e6a045864ff8569e43e4866c0af807def07b0db2f018808620eb30b980e94011.data (100%) rename test/{example-repo => test-repo}/blocks/1220ec5b/1220ec5b533a3218991f4377b8b8c2538b95dd29d31eac6433af0fb6fcd83dd80778.data (100%) rename test/{example-repo => test-repo}/config (100%) rename test/{example-repo => test-repo}/datastore/000002.ldb (100%) rename test/{example-repo => test-repo}/datastore/000005.ldb (100%) rename test/{example-repo => test-repo}/datastore/CURRENT (100%) rename test/{example-repo => test-repo}/datastore/LOCK (100%) rename test/{example-repo => test-repo}/datastore/LOG (100%) rename test/{example-repo => test-repo}/datastore/LOG.old (100%) rename test/{example-repo => test-repo}/datastore/MANIFEST-000007 (100%) rename test/{example-repo => test-repo}/version (100%) diff --git a/.gitignore b/.gitignore index 513e8cb..af20d81 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -tests/repo-just-for-test* +test/repo-just-for-test* # Logs logs diff --git a/.npmignore b/.npmignore index 029979f..16c0a9b 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,4 @@ -tests/repo-just-for-test* +test # Logs logs diff --git a/package.json b/package.json index 87d02d4..1248abf 100644 --- a/package.json +++ b/package.json @@ -34,10 +34,13 @@ "homepage": "https://github.com/ipfs/js-ipfs-blocks#readme", "devDependencies": { "bs58": "^3.0.0", + "buffer-loader": "0.0.1", "chai": "^3.5.0", "dignified.js": "^1.0.0", "fs-blob-store": "^5.2.1", + "idb-plus-blob-store": "^1.0.0", "ipfs-repo": "^0.3.0", + "lodash": "^4.8.2", "ncp": "^2.0.0", "pre-commit": "^1.1.2", "rimraf": "^2.5.1" diff --git a/src/block-service.js b/src/block-service.js index 18e203e..d7bffb0 100644 --- a/src/block-service.js +++ b/src/block-service.js @@ -9,9 +9,9 @@ const async = require('async') // It uses an internal `datastore.Datastore` instance to store values. function BlockService (ipfsRepo, exchange) { this.addBlock = (block, callback) => { - const ws = ipfsRepo.datastore.createWriteStream(block.key) + const ws = ipfsRepo.datastore.createWriteStream(block.key, block.extension) ws.write(block.data) - ws.on('finish', callback) + ws.once('finish', callback) ws.end() } @@ -22,32 +22,40 @@ function BlockService (ipfsRepo, exchange) { async.each(blocks, (block, next) => { this.addBlock(block, next) - }, (err) => { - callback(err) - }) + }, callback) } - this.getBlock = (multihash, callback) => { + this.getBlock = (multihash, extension, callback) => { if (!multihash) { return callback(new Error('Invalid multihash')) } - ipfsRepo.datastore.createReadStream(multihash) + if (typeof extension === 'function') { + callback = extension + extension = undefined + } + + ipfsRepo.datastore.createReadStream(multihash, extension) .pipe(bl((err, data) => { if (err) { return callback(err) } - callback(null, new Block(data)) + callback(null, new Block(data, extension)) })) } - this.getBlocks = (multihashes, callback) => { + this.getBlocks = (multihashes, extension, callback) => { if (!Array.isArray(multihashes)) { return callback(new Error('Invalid batch of multihashes')) } + if (typeof extension === 'function') { + callback = extension + extension = undefined + } + const blocks = [] async.each(multihashes, (multihash, next) => { - this.getBlock(multihash, (err, block) => { + this.getBlock(multihash, extension, (err, block) => { if (err) { return next(err) } blocks.push(block) }) @@ -56,21 +64,31 @@ function BlockService (ipfsRepo, exchange) { }) } - this.deleteBlock = (multihash, callback) => { + this.deleteBlock = (multihash, extension, callback) => { if (!multihash) { return callback(new Error('Invalid multihash')) } - ipfsRepo.datastore.remove(multihash, callback) + if (typeof extension === 'function') { + callback = extension + extension = undefined + } + + ipfsRepo.datastore.remove(multihash, extension, callback) } - this.deleteBlocks = (multihashes, callback) => { + this.deleteBlocks = (multihashes, extension, callback) => { if (!Array.isArray(multihashes)) { return callback('Invalid batch of multihashes') } + if (typeof extension === 'function') { + callback = extension + extension = undefined + } + async.each(multihashes, (multihash, next) => { - this.deleteBlock(multihash, next) + this.deleteBlock(multihash, extension, next) }, (err) => { callback(err) }) diff --git a/src/block.js b/src/block.js index 6e90ec6..56e2b34 100644 --- a/src/block.js +++ b/src/block.js @@ -2,15 +2,23 @@ const util = require('./util') // Immutable block of data -function Block (data) { - if (!data) { throw new Error('Block must be constructed with data') } +function Block (data, extension) { + if (!data) { + throw new Error('Block must be constructed with data') + } if (!(this instanceof Block)) { return new Block(data) } - this.data = new Buffer(data) + if (data instanceof Buffer) { + this.data = data + } else { + this.data = new Buffer(data) + } + this.key = util.hash(this.data) + this.extension = extension || 'data' } module.exports = Block diff --git a/test/block-service-test.js b/test/block-service-test.js index bc593f6..ad1cc22 100644 --- a/test/block-service-test.js +++ b/test/block-service-test.js @@ -5,90 +5,106 @@ const expect = require('chai').expect const Block = require('../src').Block const BlockService = require('../src').BlockService -const IPFSRepo = require('ipfs-repo') +module.exports = (repo) => { + describe('block-service', () => { + let bs -describe('block-service', () => { - let bs - - it('create a block-service', (done) => { - const repo = new IPFSRepo(process.env.IPFS_PATH) - bs = new BlockService(repo) - expect(bs).to.exist - done() - }) + it('create a block-service', (done) => { + bs = new BlockService(repo) + expect(bs).to.exist + done() + }) - it('store a block', (done) => { - const b = new Block('A random data block') - bs.addBlock(b, (err) => { - expect(err).to.not.exist - bs.getBlock(b.key, (err, block) => { + it('store a block', (done) => { + const b = new Block('A random data block') + bs.addBlock(b, (err) => { expect(err).to.not.exist - expect(b.data.equals(block.data)).to.equal(true) - expect(b.key.equals(block.key)).to.equal(true) - done() + bs.getBlock(b.key, (err, block) => { + expect(err).to.not.exist + expect(b.data.equals(block.data)).to.equal(true) + expect(b.key.equals(block.key)).to.equal(true) + done() + }) }) }) - }) - it('get a non existent block', (done) => { - const b = new Block('Not stored') - bs.getBlock(b.key, (err, block) => { - expect(err).to.exist - done() + it('store a block, with custom extension', (done) => { + const b = new Block('A random data block', 'ext') + bs.addBlock(b, (err) => { + expect(err).to.not.exist + bs.getBlock(b.key, 'ext', (err, block) => { + expect(err).to.not.exist + expect(b.data.equals(block.data)).to.equal(true) + expect(b.key.equals(block.key)).to.equal(true) + done() + }) + }) }) - }) - it('store many blocks', (done) => { - const b1 = new Block('1') - const b2 = new Block('2') - const b3 = new Block('3') + it('get a non existent block', (done) => { + const b = new Block('Not stored') + bs.getBlock(b.key, (err, block) => { + expect(err).to.exist + done() + }) + }) - const blocks = [] - blocks.push(b1) - blocks.push(b2) - blocks.push(b3) + it('store many blocks', (done) => { + const b1 = new Block('1') + const b2 = new Block('2') + const b3 = new Block('3') - bs.addBlocks(blocks, (err) => { - expect(err).to.not.exist - done() + bs.addBlocks([b1, b2, b3], (err) => { + expect(err).to.not.exist + done() + }) }) - }) - it('delete a block', (done) => { - const b = new Block('Will not live that much') - bs.addBlock(b, (err) => { - expect(err).to.not.exist - bs.deleteBlock(b.key, (err) => { + it('delete a block', (done) => { + const b = new Block('Will not live that much') + bs.addBlock(b, (err) => { expect(err).to.not.exist - bs.getBlock(b.key, (err, block) => { - expect(err).to.exist - done() + bs.deleteBlock(b.key, (err) => { + expect(err).to.not.exist + bs.getBlock(b.key, (err, block) => { + expect(err).to.exist + done() + }) }) }) }) - }) - it('delete a non existent block', (done) => { - const b = new Block('I do not exist') - bs.deleteBlock(b.key, (err) => { - expect(err).to.not.exist - done() + it('delete a block, with custom extension', (done) => { + const b = new Block('Will not live that much', 'ext') + bs.addBlock(b, (err) => { + expect(err).to.not.exist + bs.deleteBlock(b.key, 'ext', (err) => { + expect(err).to.not.exist + bs.getBlock(b.key, 'ext', (err, block) => { + expect(err).to.exist + done() + }) + }) + }) }) - }) - it('delete many blocks', (done) => { - const b1 = new Block('1') - const b2 = new Block('2') - const b3 = new Block('3') + it('delete a non existent block', (done) => { + const b = new Block('I do not exist') + bs.deleteBlock(b.key, (err) => { + expect(err).to.not.exist + done() + }) + }) - const blocks = [] - blocks.push(b1.key) - blocks.push(b2.key) - blocks.push(b3.key) + it('delete many blocks', (done) => { + const b1 = new Block('1') + const b2 = new Block('2') + const b3 = new Block('3') - bs.deleteBlocks(blocks, (err) => { - expect(err).to.not.exist - done() + bs.deleteBlocks([b1, b2, b3], (err) => { + expect(err).to.not.exist + done() + }) }) }) -}) +} diff --git a/test/block-test.js b/test/block.spec.js similarity index 64% rename from test/block-test.js rename to test/block.spec.js index af32b02..ead7ab3 100644 --- a/test/block-test.js +++ b/test/block.spec.js @@ -5,31 +5,24 @@ const expect = require('chai').expect const Block = require('../src').Block describe('block', () => { - it('block: \t\t create a new block', (done) => { + it('create', () => { const b = new Block('random-data') expect(b.key).to.exist expect(b.data).to.exist - done() + expect(b.extension).to.be.eql('data') }) - it('fail to create an empty block', (done) => { - let b - try { - b = new Block() - } catch (err) { - expect(b).to.not.exist - done() - } + it('fail to create an empty block', () => { + expect(() => new Block).to.throw() }) - it('2 different blocks have different hashes', (done) => { + it('2 different blocks have different hashes', () => { const b1 = new Block('random-data') const b2 = new Block('more-random-data') expect(b1).to.not.deep.equal(b2) - done() }) - it.skip('block stays immutable', (done) => { + it.skip('block stays immutable', () => { // it from the original implementation // It doesn't stricly verify the immutability of the Block object const block = new Block("Can't change this!") @@ -37,6 +30,5 @@ describe('block', () => { key = new Buffer('new key') expect(key.equals(block.key)).to.equal(false) - done() }) }) diff --git a/test/browser.js b/test/browser.js index 58a75b4..d3b22d9 100644 --- a/test/browser.js +++ b/test/browser.js @@ -1,6 +1,52 @@ /* eslint-env mocha */ 'use strict' -describe.skip('blocks', () => { - it('works in the browser') +const async = require('async') +const store = require('idb-plus-blob-store') +const _ = require('lodash') +const IPFSRepo = require('ipfs-repo') + +const repoContext = require.context('buffer!./test-repo', true) +const tests = require('./block-service-test') + +const idb = window.indexedDB || + window.mozIndexedDB || + window.webkitIndexedDB || + window.msIndexedDB + +idb.deleteDatabase('ipfs') +idb.deleteDatabase('ipfs/blocks') + +describe('IPFS Repo Tests on the Browser', function () { + this.timeout(1000) + before((done) => { + const repoData = [] + repoContext.keys().forEach((key) => { + repoData.push({ + key: key.replace('./', ''), + value: repoContext(key) + }) + }) + + const mainBlob = store('ipfs') + const blocksBlob = store('ipfs/blocks') + + async.eachSeries(repoData, (file, cb) => { + if (_.startsWith(file.key, 'datastore/')) { + return cb() + } + + const blocks = _.startsWith(file.key, 'blocks/') + const blob = blocks ? blocksBlob : mainBlob + + const key = blocks ? file.key.replace(/^blocks\//, '') : file.key + + blob.createWriteStream({ + key: key + }).end(file.value, cb) + }, done) + }) + + const repo = new IPFSRepo('ipfs', {stores: store}) + tests(repo) }) diff --git a/test/node.js b/test/node.js index 8da5ae8..3db9e1a 100644 --- a/test/node.js +++ b/test/node.js @@ -1,41 +1,34 @@ /* eslint-env mocha */ 'use strict' -const fs = require('fs') const ncp = require('ncp').ncp const rimraf = require('rimraf') const expect = require('chai').expect +const path = require('path') +const IPFSRepo = require('ipfs-repo') -describe('blocks', () => { - const repoExample = process.cwd() + '/test/example-repo' - const repoTests = process.cwd() + '/test/repo-just-for-test' + Date.now() +const tests = require('./block-service-test') + +describe('IPFS Block Tests on Node.js', () => { + const testRepoPath = path.join(__dirname, 'test-repo') + const date = Date.now().toString() + const repoPath = testRepoPath + '-for-' + date before((done) => { - ncp(repoExample, repoTests, (err) => { - process.env.IPFS_PATH = repoTests - expect(err).to.equal(null) + ncp(testRepoPath, repoPath, (err) => { + expect(err).to.not.exist done() }) }) after((done) => { - rimraf(repoTests, (err) => { - expect(err).to.equal(null) + rimraf(repoPath, (err) => { + expect(err).to.not.exist done() }) }) - const tests = fs.readdirSync(__dirname) - tests.filter((file) => { - if (file === 'index.js' || - file === 'example-repo' || - file.indexOf('repo-just-for-test') > -1 || - file === 'browser.js') { - return false - } - - return true - }).forEach((file) => { - require('./' + file) - }) + const fs = require('fs-blob-store') + const repo = new IPFSRepo(repoPath, {stores: fs}) + tests(repo) }) diff --git a/test/example-repo/blocks/1220120f/1220120f6af601d46e10b2d2e11ed71c55d25f3042c22501e41d1246e7a1e9d3d8ec.data b/test/test-repo/blocks/1220120f/1220120f6af601d46e10b2d2e11ed71c55d25f3042c22501e41d1246e7a1e9d3d8ec.data similarity index 100% rename from test/example-repo/blocks/1220120f/1220120f6af601d46e10b2d2e11ed71c55d25f3042c22501e41d1246e7a1e9d3d8ec.data rename to test/test-repo/blocks/1220120f/1220120f6af601d46e10b2d2e11ed71c55d25f3042c22501e41d1246e7a1e9d3d8ec.data diff --git a/test/example-repo/blocks/122031d6/122031d6da265092f1b03fec969243fdcf095c1d219356cdf538ffce705a52d5738d.data b/test/test-repo/blocks/122031d6/122031d6da265092f1b03fec969243fdcf095c1d219356cdf538ffce705a52d5738d.data similarity index 100% rename from test/example-repo/blocks/122031d6/122031d6da265092f1b03fec969243fdcf095c1d219356cdf538ffce705a52d5738d.data rename to test/test-repo/blocks/122031d6/122031d6da265092f1b03fec969243fdcf095c1d219356cdf538ffce705a52d5738d.data diff --git a/test/example-repo/blocks/122031e7/122031e7a41c15d03feb8cd793c3348ea3b310512d7767a9abfbd7a928a85e977173.data b/test/test-repo/blocks/122031e7/122031e7a41c15d03feb8cd793c3348ea3b310512d7767a9abfbd7a928a85e977173.data similarity index 100% rename from test/example-repo/blocks/122031e7/122031e7a41c15d03feb8cd793c3348ea3b310512d7767a9abfbd7a928a85e977173.data rename to test/test-repo/blocks/122031e7/122031e7a41c15d03feb8cd793c3348ea3b310512d7767a9abfbd7a928a85e977173.data diff --git a/test/example-repo/blocks/12203628/12203628a4a19525dd84bbbffe132ec0b0d3be3528fbbcc814feb7f2fbf71ca06162.data b/test/test-repo/blocks/12203628/12203628a4a19525dd84bbbffe132ec0b0d3be3528fbbcc814feb7f2fbf71ca06162.data similarity index 100% rename from test/example-repo/blocks/12203628/12203628a4a19525dd84bbbffe132ec0b0d3be3528fbbcc814feb7f2fbf71ca06162.data rename to test/test-repo/blocks/12203628/12203628a4a19525dd84bbbffe132ec0b0d3be3528fbbcc814feb7f2fbf71ca06162.data diff --git a/test/example-repo/blocks/12204a5a/12204a5a95586f52e25811cf214677160e64383755a8c5163ba3c053c3b65777ed16.data b/test/test-repo/blocks/12204a5a/12204a5a95586f52e25811cf214677160e64383755a8c5163ba3c053c3b65777ed16.data similarity index 100% rename from test/example-repo/blocks/12204a5a/12204a5a95586f52e25811cf214677160e64383755a8c5163ba3c053c3b65777ed16.data rename to test/test-repo/blocks/12204a5a/12204a5a95586f52e25811cf214677160e64383755a8c5163ba3c053c3b65777ed16.data diff --git a/test/example-repo/blocks/1220503a/1220503aec3bbd533b39fd56f5148750aa787e820fe98dbf68131bf70e796693d373.data b/test/test-repo/blocks/1220503a/1220503aec3bbd533b39fd56f5148750aa787e820fe98dbf68131bf70e796693d373.data similarity index 100% rename from test/example-repo/blocks/1220503a/1220503aec3bbd533b39fd56f5148750aa787e820fe98dbf68131bf70e796693d373.data rename to test/test-repo/blocks/1220503a/1220503aec3bbd533b39fd56f5148750aa787e820fe98dbf68131bf70e796693d373.data diff --git a/test/example-repo/blocks/12205200/12205200cc6b6f79e1588480d9f9016ccadfda3d8bc7d2f8cdf302aa51dae8dae9da.data b/test/test-repo/blocks/12205200/12205200cc6b6f79e1588480d9f9016ccadfda3d8bc7d2f8cdf302aa51dae8dae9da.data similarity index 100% rename from test/example-repo/blocks/12205200/12205200cc6b6f79e1588480d9f9016ccadfda3d8bc7d2f8cdf302aa51dae8dae9da.data rename to test/test-repo/blocks/12205200/12205200cc6b6f79e1588480d9f9016ccadfda3d8bc7d2f8cdf302aa51dae8dae9da.data diff --git a/test/example-repo/blocks/122052c6/122052c63c7775396b3f82c639977a7223c2d96a9f70b5fd8b1d513f8c5b69dcaed4.data b/test/test-repo/blocks/122052c6/122052c63c7775396b3f82c639977a7223c2d96a9f70b5fd8b1d513f8c5b69dcaed4.data similarity index 100% rename from test/example-repo/blocks/122052c6/122052c63c7775396b3f82c639977a7223c2d96a9f70b5fd8b1d513f8c5b69dcaed4.data rename to test/test-repo/blocks/122052c6/122052c63c7775396b3f82c639977a7223c2d96a9f70b5fd8b1d513f8c5b69dcaed4.data diff --git a/test/example-repo/blocks/12205994/122059948439065f29619ef41280cbb932be52c56d99c5966b65e0111239f098bbef.data b/test/test-repo/blocks/12205994/122059948439065f29619ef41280cbb932be52c56d99c5966b65e0111239f098bbef.data similarity index 100% rename from test/example-repo/blocks/12205994/122059948439065f29619ef41280cbb932be52c56d99c5966b65e0111239f098bbef.data rename to test/test-repo/blocks/12205994/122059948439065f29619ef41280cbb932be52c56d99c5966b65e0111239f098bbef.data diff --git a/test/example-repo/blocks/12205dff/12205dffb3c88f3de7036dc1840f0086f9fccbf5fa0106df39556b3b411ea4121a6f.data b/test/test-repo/blocks/12205dff/12205dffb3c88f3de7036dc1840f0086f9fccbf5fa0106df39556b3b411ea4121a6f.data similarity index 100% rename from test/example-repo/blocks/12205dff/12205dffb3c88f3de7036dc1840f0086f9fccbf5fa0106df39556b3b411ea4121a6f.data rename to test/test-repo/blocks/12205dff/12205dffb3c88f3de7036dc1840f0086f9fccbf5fa0106df39556b3b411ea4121a6f.data diff --git a/test/example-repo/blocks/12206006/1220600627d80642365010791de1d62481e88d2c710099738ccbd6964075730194ba.data b/test/test-repo/blocks/12206006/1220600627d80642365010791de1d62481e88d2c710099738ccbd6964075730194ba.data similarity index 100% rename from test/example-repo/blocks/12206006/1220600627d80642365010791de1d62481e88d2c710099738ccbd6964075730194ba.data rename to test/test-repo/blocks/12206006/1220600627d80642365010791de1d62481e88d2c710099738ccbd6964075730194ba.data diff --git a/test/example-repo/blocks/122062ce/122062ce1f2c91a13a97b596e873b5a3346a644605d7614dca53cd3a59f7385a8abb.data b/test/test-repo/blocks/122062ce/122062ce1f2c91a13a97b596e873b5a3346a644605d7614dca53cd3a59f7385a8abb.data similarity index 100% rename from test/example-repo/blocks/122062ce/122062ce1f2c91a13a97b596e873b5a3346a644605d7614dca53cd3a59f7385a8abb.data rename to test/test-repo/blocks/122062ce/122062ce1f2c91a13a97b596e873b5a3346a644605d7614dca53cd3a59f7385a8abb.data diff --git a/test/example-repo/blocks/12206781/122067817186b8ff365c758f387e3ae7f28fa9367ee167c312e6d65a2e02e81ab815.data b/test/test-repo/blocks/12206781/122067817186b8ff365c758f387e3ae7f28fa9367ee167c312e6d65a2e02e81ab815.data similarity index 100% rename from test/example-repo/blocks/12206781/122067817186b8ff365c758f387e3ae7f28fa9367ee167c312e6d65a2e02e81ab815.data rename to test/test-repo/blocks/12206781/122067817186b8ff365c758f387e3ae7f28fa9367ee167c312e6d65a2e02e81ab815.data diff --git a/test/example-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data b/test/test-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data similarity index 100% rename from test/example-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data rename to test/test-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data diff --git a/test/example-repo/blocks/1220709b/1220709b2dcc5f6a90ad64d6fe3a5d202a72b057d1c7f2e682d0776a5363e2cca974.data b/test/test-repo/blocks/1220709b/1220709b2dcc5f6a90ad64d6fe3a5d202a72b057d1c7f2e682d0776a5363e2cca974.data similarity index 100% rename from test/example-repo/blocks/1220709b/1220709b2dcc5f6a90ad64d6fe3a5d202a72b057d1c7f2e682d0776a5363e2cca974.data rename to test/test-repo/blocks/1220709b/1220709b2dcc5f6a90ad64d6fe3a5d202a72b057d1c7f2e682d0776a5363e2cca974.data diff --git a/test/example-repo/blocks/12207d5c/12207d5cd815f41ac0566dd956c8541dced761633aca6704ba01a9be55934d95e5f1.data b/test/test-repo/blocks/12207d5c/12207d5cd815f41ac0566dd956c8541dced761633aca6704ba01a9be55934d95e5f1.data similarity index 100% rename from test/example-repo/blocks/12207d5c/12207d5cd815f41ac0566dd956c8541dced761633aca6704ba01a9be55934d95e5f1.data rename to test/test-repo/blocks/12207d5c/12207d5cd815f41ac0566dd956c8541dced761633aca6704ba01a9be55934d95e5f1.data diff --git a/test/example-repo/blocks/12207fb8/12207fb898b5d7be46d85feb75d894e16cfa9a7ae5533f8e997cdab2ebadd7506340.data b/test/test-repo/blocks/12207fb8/12207fb898b5d7be46d85feb75d894e16cfa9a7ae5533f8e997cdab2ebadd7506340.data similarity index 100% rename from test/example-repo/blocks/12207fb8/12207fb898b5d7be46d85feb75d894e16cfa9a7ae5533f8e997cdab2ebadd7506340.data rename to test/test-repo/blocks/12207fb8/12207fb898b5d7be46d85feb75d894e16cfa9a7ae5533f8e997cdab2ebadd7506340.data diff --git a/test/example-repo/blocks/12208b87/12208b872ca4ee517608331696dd6b3e5cf3497a7845ee8f94456ccf4d1d2f6602b5.data b/test/test-repo/blocks/12208b87/12208b872ca4ee517608331696dd6b3e5cf3497a7845ee8f94456ccf4d1d2f6602b5.data similarity index 100% rename from test/example-repo/blocks/12208b87/12208b872ca4ee517608331696dd6b3e5cf3497a7845ee8f94456ccf4d1d2f6602b5.data rename to test/test-repo/blocks/12208b87/12208b872ca4ee517608331696dd6b3e5cf3497a7845ee8f94456ccf4d1d2f6602b5.data diff --git a/test/example-repo/blocks/122090c0/122090c07a7795c1193510a696d1fdfc0f1e4947cff8e422610996e609dbcb976598.data b/test/test-repo/blocks/122090c0/122090c07a7795c1193510a696d1fdfc0f1e4947cff8e422610996e609dbcb976598.data similarity index 100% rename from test/example-repo/blocks/122090c0/122090c07a7795c1193510a696d1fdfc0f1e4947cff8e422610996e609dbcb976598.data rename to test/test-repo/blocks/122090c0/122090c07a7795c1193510a696d1fdfc0f1e4947cff8e422610996e609dbcb976598.data diff --git a/test/example-repo/blocks/1220929a/1220929a303c39da8a0b67c09697462f687a00c638bcb580feae06452e0c1f20b4.data b/test/test-repo/blocks/1220929a/1220929a303c39da8a0b67c09697462f687a00c638bcb580feae06452e0c1f20b4.data similarity index 100% rename from test/example-repo/blocks/1220929a/1220929a303c39da8a0b67c09697462f687a00c638bcb580feae06452e0c1f20b4.data rename to test/test-repo/blocks/1220929a/1220929a303c39da8a0b67c09697462f687a00c638bcb580feae06452e0c1f20b4.data diff --git a/test/example-repo/blocks/1220933b/1220933b41d37fd4508cdff45930dff56baef91c7dc345e73d049ab570abe10dfbb9.data b/test/test-repo/blocks/1220933b/1220933b41d37fd4508cdff45930dff56baef91c7dc345e73d049ab570abe10dfbb9.data similarity index 100% rename from test/example-repo/blocks/1220933b/1220933b41d37fd4508cdff45930dff56baef91c7dc345e73d049ab570abe10dfbb9.data rename to test/test-repo/blocks/1220933b/1220933b41d37fd4508cdff45930dff56baef91c7dc345e73d049ab570abe10dfbb9.data diff --git a/test/example-repo/blocks/12209d6c/12209d6c2be50f706953479ab9df2ce3edca90b68053c00b3004b7f0accbe1e8eedf.data b/test/test-repo/blocks/12209d6c/12209d6c2be50f706953479ab9df2ce3edca90b68053c00b3004b7f0accbe1e8eedf.data similarity index 100% rename from test/example-repo/blocks/12209d6c/12209d6c2be50f706953479ab9df2ce3edca90b68053c00b3004b7f0accbe1e8eedf.data rename to test/test-repo/blocks/12209d6c/12209d6c2be50f706953479ab9df2ce3edca90b68053c00b3004b7f0accbe1e8eedf.data diff --git a/test/example-repo/blocks/1220a52c/1220a52c3602030cb912edfe4de97002fdadf9d45666c3be122a2efb5db93c1d5fa6.data b/test/test-repo/blocks/1220a52c/1220a52c3602030cb912edfe4de97002fdadf9d45666c3be122a2efb5db93c1d5fa6.data similarity index 100% rename from test/example-repo/blocks/1220a52c/1220a52c3602030cb912edfe4de97002fdadf9d45666c3be122a2efb5db93c1d5fa6.data rename to test/test-repo/blocks/1220a52c/1220a52c3602030cb912edfe4de97002fdadf9d45666c3be122a2efb5db93c1d5fa6.data diff --git a/test/example-repo/blocks/1220c0fc/1220c0fc6b49543d7bf04e83d2a5a7cbe72a83e80f9c7bca1abcaa42298a57a33ff5.data b/test/test-repo/blocks/1220c0fc/1220c0fc6b49543d7bf04e83d2a5a7cbe72a83e80f9c7bca1abcaa42298a57a33ff5.data similarity index 100% rename from test/example-repo/blocks/1220c0fc/1220c0fc6b49543d7bf04e83d2a5a7cbe72a83e80f9c7bca1abcaa42298a57a33ff5.data rename to test/test-repo/blocks/1220c0fc/1220c0fc6b49543d7bf04e83d2a5a7cbe72a83e80f9c7bca1abcaa42298a57a33ff5.data diff --git a/test/example-repo/blocks/1220d15b/1220d15b2cf7ecc6eccbf7e68c2e4290e8710bd97eafcce5380a127dcc5ea32a887f.data b/test/test-repo/blocks/1220d15b/1220d15b2cf7ecc6eccbf7e68c2e4290e8710bd97eafcce5380a127dcc5ea32a887f.data similarity index 100% rename from test/example-repo/blocks/1220d15b/1220d15b2cf7ecc6eccbf7e68c2e4290e8710bd97eafcce5380a127dcc5ea32a887f.data rename to test/test-repo/blocks/1220d15b/1220d15b2cf7ecc6eccbf7e68c2e4290e8710bd97eafcce5380a127dcc5ea32a887f.data diff --git a/test/example-repo/blocks/1220e3b0/1220e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.data b/test/test-repo/blocks/1220e3b0/1220e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.data similarity index 100% rename from test/example-repo/blocks/1220e3b0/1220e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.data rename to test/test-repo/blocks/1220e3b0/1220e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.data diff --git a/test/example-repo/blocks/1220e586/1220e586199640e1a4c63fa38c5434b9e72dc99d23a391d3db5e1e42d00527241671.data b/test/test-repo/blocks/1220e586/1220e586199640e1a4c63fa38c5434b9e72dc99d23a391d3db5e1e42d00527241671.data similarity index 100% rename from test/example-repo/blocks/1220e586/1220e586199640e1a4c63fa38c5434b9e72dc99d23a391d3db5e1e42d00527241671.data rename to test/test-repo/blocks/1220e586/1220e586199640e1a4c63fa38c5434b9e72dc99d23a391d3db5e1e42d00527241671.data diff --git a/test/example-repo/blocks/1220e5f8/1220e5f87b2b69cb032267bbc72a598eb49e3772f8949dacec55d099e3e17ac851c3.data b/test/test-repo/blocks/1220e5f8/1220e5f87b2b69cb032267bbc72a598eb49e3772f8949dacec55d099e3e17ac851c3.data similarity index 100% rename from test/example-repo/blocks/1220e5f8/1220e5f87b2b69cb032267bbc72a598eb49e3772f8949dacec55d099e3e17ac851c3.data rename to test/test-repo/blocks/1220e5f8/1220e5f87b2b69cb032267bbc72a598eb49e3772f8949dacec55d099e3e17ac851c3.data diff --git a/test/example-repo/blocks/1220e605/1220e605408ac3f78113ac9a7fd486441317afc9f967abe2aa05e7c641f7bbe98a37.data b/test/test-repo/blocks/1220e605/1220e605408ac3f78113ac9a7fd486441317afc9f967abe2aa05e7c641f7bbe98a37.data similarity index 100% rename from test/example-repo/blocks/1220e605/1220e605408ac3f78113ac9a7fd486441317afc9f967abe2aa05e7c641f7bbe98a37.data rename to test/test-repo/blocks/1220e605/1220e605408ac3f78113ac9a7fd486441317afc9f967abe2aa05e7c641f7bbe98a37.data diff --git a/test/example-repo/blocks/1220e6a0/1220e6a045864ff8569e43e4866c0af807def07b0db2f018808620eb30b980e94011.data b/test/test-repo/blocks/1220e6a0/1220e6a045864ff8569e43e4866c0af807def07b0db2f018808620eb30b980e94011.data similarity index 100% rename from test/example-repo/blocks/1220e6a0/1220e6a045864ff8569e43e4866c0af807def07b0db2f018808620eb30b980e94011.data rename to test/test-repo/blocks/1220e6a0/1220e6a045864ff8569e43e4866c0af807def07b0db2f018808620eb30b980e94011.data diff --git a/test/example-repo/blocks/1220ec5b/1220ec5b533a3218991f4377b8b8c2538b95dd29d31eac6433af0fb6fcd83dd80778.data b/test/test-repo/blocks/1220ec5b/1220ec5b533a3218991f4377b8b8c2538b95dd29d31eac6433af0fb6fcd83dd80778.data similarity index 100% rename from test/example-repo/blocks/1220ec5b/1220ec5b533a3218991f4377b8b8c2538b95dd29d31eac6433af0fb6fcd83dd80778.data rename to test/test-repo/blocks/1220ec5b/1220ec5b533a3218991f4377b8b8c2538b95dd29d31eac6433af0fb6fcd83dd80778.data diff --git a/test/example-repo/config b/test/test-repo/config similarity index 100% rename from test/example-repo/config rename to test/test-repo/config diff --git a/test/example-repo/datastore/000002.ldb b/test/test-repo/datastore/000002.ldb similarity index 100% rename from test/example-repo/datastore/000002.ldb rename to test/test-repo/datastore/000002.ldb diff --git a/test/example-repo/datastore/000005.ldb b/test/test-repo/datastore/000005.ldb similarity index 100% rename from test/example-repo/datastore/000005.ldb rename to test/test-repo/datastore/000005.ldb diff --git a/test/example-repo/datastore/CURRENT b/test/test-repo/datastore/CURRENT similarity index 100% rename from test/example-repo/datastore/CURRENT rename to test/test-repo/datastore/CURRENT diff --git a/test/example-repo/datastore/LOCK b/test/test-repo/datastore/LOCK similarity index 100% rename from test/example-repo/datastore/LOCK rename to test/test-repo/datastore/LOCK diff --git a/test/example-repo/datastore/LOG b/test/test-repo/datastore/LOG similarity index 100% rename from test/example-repo/datastore/LOG rename to test/test-repo/datastore/LOG diff --git a/test/example-repo/datastore/LOG.old b/test/test-repo/datastore/LOG.old similarity index 100% rename from test/example-repo/datastore/LOG.old rename to test/test-repo/datastore/LOG.old diff --git a/test/example-repo/datastore/MANIFEST-000007 b/test/test-repo/datastore/MANIFEST-000007 similarity index 100% rename from test/example-repo/datastore/MANIFEST-000007 rename to test/test-repo/datastore/MANIFEST-000007 diff --git a/test/example-repo/version b/test/test-repo/version similarity index 100% rename from test/example-repo/version rename to test/test-repo/version From 5b5a124a27562d636f50abf4c1ef3cb144c8b43b Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 8 Apr 2016 16:02:10 +0200 Subject: [PATCH 2/4] docs(readme): Update and cleanup --- README.md | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 2219821..e3b9163 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ IPFS Blocks JavaScript Implementation ===================================== -[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) +[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) [![Travis CI](https://travis-ci.org/ipfs/js-ipfs-blocks.svg?branch=master)](https://travis-ci.org/ipfs/js-ipfs-blocks) @@ -40,8 +40,8 @@ A Block is a data structure available on this module. ## Use in Node.js -```JavaScript -var ipfsBlocks = require('ipfs-blocks') +```js +const ipfsBlocks = require('ipfs-blocks') ``` ## Use in a browser with browserify, webpack or any other bundler @@ -65,7 +65,7 @@ Loading this module through a script tag will make the `Unixfs` obj available in # Usage -```javascript +```js // then, to access each of the components ipfsBlocks.BlockService ipfsBlocks.Block @@ -75,25 +75,34 @@ ipfsBlocks.Block Create a new block -```JavaScript -var block = new blocks.Block('some data') -console.log(block.data) +```js +const block = new blocks.Block('some data') +console.log(block.data) // It will print 'some data' + console.log(block.key) // It will print the sha256 multihash of 'some data' ``` +A block can also have it's own extension, which by default is `data`. + +```js +const block = new blocks.Block('data', 'ipld') +console.log(block.extension) +// => ipld +``` + #### BlockService Create a new block service -```JavaScript -var bs = new ipfsBlocks.BlockService( [, ]) +```js +const bs = new ipfsBlocks.BlockService( [, ]) ``` -##### addBlock +##### `addBlock` -```JavaScript +```js bs.addBlock(block, function (err) { if (!err) { // block successfuly added @@ -101,9 +110,9 @@ bs.addBlock(block, function (err) { }) ``` -##### addBlocks +##### `addBlocks` -```JavaScript +```js bs.addBlocks(blockArray, function (err) { if (!err) { // blocks successfuly added @@ -111,9 +120,9 @@ bs.addBlocks(blockArray, function (err) { }) ``` -##### getBlock +##### `getBlock` -```JavaScript +```js bs.getBlock(multihash, function (err, block) { if (!err) { // block successfuly retrieved @@ -122,9 +131,9 @@ bs.getBlock(multihash, function (err, block) { ``` -##### getBlocks +##### `getBlocks` -```JavaScript +```js bs.getBlocks(multihashArray, function (err, block) { if (!err) { // block successfuly retrieved @@ -132,9 +141,9 @@ bs.getBlocks(multihashArray, function (err, block) { }) ``` -##### deleteBlock +##### `deleteBlock` -```JavaScript +```js bs.deleteBlock(multihash, function (err) { if (!err) { // block successfuly deleted @@ -142,9 +151,9 @@ bs.deleteBlock(multihash, function (err) { }) ``` -##### deleteBlocks +##### `deleteBlocks` -```JavaScript +```js bs.deleteBlocks(multihashArray, function (err) { if (!err) { // blocks successfuly deleted From 7c9b9170ecb2868d2198f3485fc17b20e162fa3f Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Mon, 11 Apr 2016 10:20:01 +0200 Subject: [PATCH 3/4] fix: Latest dep and linting --- package.json | 2 +- test/block.spec.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 1248abf..62d3ebb 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "dignified.js": "^1.0.0", "fs-blob-store": "^5.2.1", "idb-plus-blob-store": "^1.0.0", - "ipfs-repo": "^0.3.0", + "ipfs-repo": "^0.6.1", "lodash": "^4.8.2", "ncp": "^2.0.0", "pre-commit": "^1.1.2", diff --git a/test/block.spec.js b/test/block.spec.js index ead7ab3..4e23505 100644 --- a/test/block.spec.js +++ b/test/block.spec.js @@ -13,7 +13,7 @@ describe('block', () => { }) it('fail to create an empty block', () => { - expect(() => new Block).to.throw() + expect(() => new Block()).to.throw() }) it('2 different blocks have different hashes', () => { From addf54b10a89ae81cb214d07a10807aa31478482 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Mon, 11 Apr 2016 11:12:53 +0200 Subject: [PATCH 4/4] fix: Specify types instead of extensions --- src/block.js | 17 +++++++++++++++-- test/block.spec.js | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/block.js b/src/block.js index 56e2b34..c3ce0f6 100644 --- a/src/block.js +++ b/src/block.js @@ -2,7 +2,7 @@ const util = require('./util') // Immutable block of data -function Block (data, extension) { +function Block (data, type) { if (!data) { throw new Error('Block must be constructed with data') } @@ -18,7 +18,20 @@ function Block (data, extension) { } this.key = util.hash(this.data) - this.extension = extension || 'data' + this.type = type || 'protobuf' } +Object.defineProperty(Block.prototype, 'extension', { + get () { + switch (this.type) { + case 'protobuf': + return 'data' + case 'ipld': + return 'ipld' + default: + return this.type + } + } +}) + module.exports = Block diff --git a/test/block.spec.js b/test/block.spec.js index 4e23505..84bde71 100644 --- a/test/block.spec.js +++ b/test/block.spec.js @@ -31,4 +31,23 @@ describe('block', () => { expect(key.equals(block.key)).to.equal(false) }) + + it('has the right extension to type mapping', () => { + const b1 = new Block('hello', 'protobuf') + const b2 = new Block('hello') + const b3 = new Block('hello', 'ipld') + const b4 = new Block('hello', 'woot') + + expect(b1.type).to.be.eql('protobuf') + expect(b1.extension).to.be.eql('data') + + expect(b2.type).to.be.eql('protobuf') + expect(b2.extension).to.be.eql('data') + + expect(b3.type).to.be.eql('ipld') + expect(b3.extension).to.be.eql('ipld') + + expect(b4.type).to.be.eql('woot') + expect(b4.extension).to.be.eql('woot') + }) })