diff --git a/README.md b/README.md index f1be9e6..53925bb 100644 --- a/README.md +++ b/README.md @@ -69,12 +69,14 @@ const BlockService = require('ipfs-block-service') const Block = require('ipld-block') const multihashing = require('multihashing-async') const IPFSRepo = require('ipfs-repo') // storage repo +const uint8ArrayEquals = require('uint8arrays/equals') +const uint8ArrayFromString = require('uint8arrays/from-string') // setup a repo const repo = new IPFSRepo('example') // create a block -const data = new Buffer('hello world') +const data = uint8ArrayFromString('hello world') const multihash = await multihashing(data, 'sha2-256') const cid = new CID(multihash) @@ -87,7 +89,7 @@ const service = new BlockService(repo) await service.put(block) const result = await service.get(cid) -console.log(block.data.toString() === result.data.toString()) +console.log(uint8ArrayEquals(block.data, result.data)) // => true ``` diff --git a/package.json b/package.json index 82735f9..bb2ca5b 100644 --- a/package.json +++ b/package.json @@ -31,24 +31,23 @@ "homepage": "https://github.com/ipfs/js-ipfs-block-service#readme", "devDependencies": { "abort-controller": "^3.0.0", - "aegir": "^21.8.1", - "chai": "^4.2.0", - "chai-as-promised": "^7.1.1", - "cids": "^0.8.0", - "dirty-chai": "^2.0.1", + "aegir": "^22.0.0", + "cids": "^1.0.0", "fs-extra": "^9.0.0", - "ipfs-repo": "^2.1.0", - "ipld-block": "^0.9.1", + "ipfs-repo": "^6.0.0", + "ipld-block": "^0.10.0", + "it-drain": "^1.0.1", "lodash": "^4.17.11", - "multihashing-async": "^0.8.1" + "multihashing-async": "^2.0.1", + "uint8arrays": "^1.0.0" }, "engines": { - "node": ">=6.0.0", + "node": ">=12.0.0", "npm": ">=3.0.0" }, "dependencies": { "err-code": "^2.0.0", - "streaming-iterables": "^4.1.0" + "streaming-iterables": "^5.0.2" }, "contributors": [ "David Dias ", diff --git a/test/aborting-requests.spec.js b/test/aborting-requests.spec.js index 40b3d68..d0bfce8 100644 --- a/test/aborting-requests.spec.js +++ b/test/aborting-requests.spec.js @@ -1,10 +1,7 @@ /* eslint-env mocha */ 'use strict' -const chai = require('chai') -chai.use(require('dirty-chai')) -chai.use(require('chai-as-promised')) -const expect = chai.expect +const { expect } = require('aegir/utils/chai') const { collect } = require('streaming-iterables') const AbortController = require('abort-controller') diff --git a/test/block-service-test.js b/test/block-service-test.js index 29e7156..92f984a 100644 --- a/test/block-service-test.js +++ b/test/block-service-test.js @@ -1,15 +1,15 @@ /* eslint-env mocha */ 'use strict' -const chai = require('chai') -chai.use(require('dirty-chai')) -const expect = chai.expect +const { expect } = require('aegir/utils/chai') const Block = require('ipld-block') const _ = require('lodash') const { collect } = require('streaming-iterables') const CID = require('cids') const multihashing = require('multihashing-async') +const uint8ArrayFromString = require('uint8arrays/from-string') +const drain = require('it-drain') const BlockService = require('../src') @@ -22,10 +22,10 @@ module.exports = (repo) => { bs = new BlockService(repo) const data = [ - Buffer.from('1'), - Buffer.from('2'), - Buffer.from('3'), - Buffer.from('A random data block') + uint8ArrayFromString('1'), + uint8ArrayFromString('2'), + uint8ArrayFromString('3'), + uint8ArrayFromString('A random data block') ] testBlocks = await Promise.all(data.map(async (d) => { @@ -53,8 +53,16 @@ module.exports = (repo) => { } }) - it('store many blocks', () => { - return bs.putMany(testBlocks) + it('store many blocks', async () => { + await drain(bs.putMany(testBlocks)) + + expect( + await Promise.all( + testBlocks.map(b => bs.get(b.cid)) + ) + ).to.deep.equal( + testBlocks + ) }) it('get many blocks through .get', async () => { @@ -69,7 +77,7 @@ module.exports = (repo) => { }) it('delete a block', async () => { - const data = Buffer.from('Will not live that much') + const data = uint8ArrayFromString('Will not live that much') const hash = await multihashing(data, 'sha2-256') const b = new Block(data, new CID(hash)) @@ -81,7 +89,7 @@ module.exports = (repo) => { }) it('does not delete a block it does not have', async () => { - const data = Buffer.from('Will not live that much ' + Date.now()) + const data = uint8ArrayFromString('Will not live that much ' + Date.now()) const cid = new CID(await multihashing(data, 'sha2-256')) await bs.delete(cid) @@ -92,33 +100,29 @@ module.exports = (repo) => { }) it('deletes lots of blocks', async () => { - const data = Buffer.from('Will not live that much') + const data = uint8ArrayFromString('Will not live that much') const hash = await multihashing(data, 'sha2-256') const b = new Block(data, new CID(hash)) await bs.put(b) - await bs.deleteMany([b.cid]) + await drain(bs.deleteMany([b.cid])) const res = await bs._repo.blocks.has(b.cid) - expect(res).to.be.eql(false) + expect(res).to.be.false() }) it('does not delete a blocks it does not have', async () => { - const data = Buffer.from('Will not live that much ' + Date.now()) + const data = uint8ArrayFromString('Will not live that much ' + Date.now()) const cid = new CID(await multihashing(data, 'sha2-256')) - await bs.deleteMany([cid]) - .then( - () => expect.fail('Should have thrown'), - (err) => expect(err).to.have.property('code', 'ERR_BLOCK_NOT_FOUND') - ) + await expect(drain(bs.deleteMany([cid]))).to.eventually.be.rejected().with.property('code', 'ERR_BLOCK_NOT_FOUND') }) it('stores and gets lots of blocks', async function () { - this.timeout(8 * 1000) + this.timeout(20 * 1000) const data = _.range(1000).map((i) => { - return Buffer.from(`hello-${i}-${Math.random()}`) + return uint8ArrayFromString(`hello-${i}-${Math.random()}`) }) const blocks = await Promise.all(data.map(async (d) => { @@ -126,7 +130,7 @@ module.exports = (repo) => { return new Block(d, new CID(hash)) })) - await bs.putMany(blocks) + await drain(bs.putMany(blocks)) const res = await Promise.all(blocks.map(b => bs.get(b.cid))) expect(res).to.be.eql(blocks) @@ -155,13 +159,13 @@ module.exports = (repo) => { // returns a block with a value equal to its key const bitswap = { get (cid) { - return new Block(Buffer.from('secret'), cid) + return new Block(uint8ArrayFromString('secret'), cid) } } bs.setExchange(bitswap) - const data = Buffer.from('secret') + const data = uint8ArrayFromString('secret') const hash = await multihashing(data, 'sha2-256') const block = await bs.get(new CID(hash)) @@ -178,7 +182,7 @@ module.exports = (repo) => { } bs.setExchange(bitswap) - const data = Buffer.from('secret sauce') + const data = uint8ArrayFromString('secret sauce') const hash = await multihashing(data, 'sha2-256') await bs.put(new Block(data, new CID(hash)))