Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
fix: replace node buffers with uint8arrays (#105)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

- All uses of Buffers have been replaced with Uint8Arrays
- The ipfs-repo this module uses only returns Uint8Arrays
  • Loading branch information
achingbrain authored Aug 5, 2020
1 parent 6077fb7 commit 804c5cf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 42 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
```

Expand Down
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <daviddias.p@gmail.com>",
Expand Down
5 changes: 1 addition & 4 deletions test/aborting-requests.spec.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
56 changes: 30 additions & 26 deletions test/block-service-test.js
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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) => {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -92,41 +100,37 @@ 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) => {
const hash = await multihashing(d, 'sha2-256')
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)
Expand Down Expand Up @@ -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))
Expand All @@ -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)))
Expand Down

0 comments on commit 804c5cf

Please sign in to comment.