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

Commit 804c5cf

Browse files
authored
fix: replace node buffers with uint8arrays (#105)
BREAKING CHANGE: - All uses of Buffers have been replaced with Uint8Arrays - The ipfs-repo this module uses only returns Uint8Arrays
1 parent 6077fb7 commit 804c5cf

File tree

4 files changed

+44
-42
lines changed

4 files changed

+44
-42
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ const BlockService = require('ipfs-block-service')
6969
const Block = require('ipld-block')
7070
const multihashing = require('multihashing-async')
7171
const IPFSRepo = require('ipfs-repo') // storage repo
72+
const uint8ArrayEquals = require('uint8arrays/equals')
73+
const uint8ArrayFromString = require('uint8arrays/from-string')
7274

7375
// setup a repo
7476
const repo = new IPFSRepo('example')
7577

7678
// create a block
77-
const data = new Buffer('hello world')
79+
const data = uint8ArrayFromString('hello world')
7880
const multihash = await multihashing(data, 'sha2-256')
7981

8082
const cid = new CID(multihash)
@@ -87,7 +89,7 @@ const service = new BlockService(repo)
8789
await service.put(block)
8890

8991
const result = await service.get(cid)
90-
console.log(block.data.toString() === result.data.toString())
92+
console.log(uint8ArrayEquals(block.data, result.data))
9193
// => true
9294
```
9395

package.json

+9-10
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,23 @@
3131
"homepage": "https://github.com/ipfs/js-ipfs-block-service#readme",
3232
"devDependencies": {
3333
"abort-controller": "^3.0.0",
34-
"aegir": "^21.8.1",
35-
"chai": "^4.2.0",
36-
"chai-as-promised": "^7.1.1",
37-
"cids": "^0.8.0",
38-
"dirty-chai": "^2.0.1",
34+
"aegir": "^22.0.0",
35+
"cids": "^1.0.0",
3936
"fs-extra": "^9.0.0",
40-
"ipfs-repo": "^2.1.0",
41-
"ipld-block": "^0.9.1",
37+
"ipfs-repo": "^6.0.0",
38+
"ipld-block": "^0.10.0",
39+
"it-drain": "^1.0.1",
4240
"lodash": "^4.17.11",
43-
"multihashing-async": "^0.8.1"
41+
"multihashing-async": "^2.0.1",
42+
"uint8arrays": "^1.0.0"
4443
},
4544
"engines": {
46-
"node": ">=6.0.0",
45+
"node": ">=12.0.0",
4746
"npm": ">=3.0.0"
4847
},
4948
"dependencies": {
5049
"err-code": "^2.0.0",
51-
"streaming-iterables": "^4.1.0"
50+
"streaming-iterables": "^5.0.2"
5251
},
5352
"contributors": [
5453
"David Dias <daviddias.p@gmail.com>",

test/aborting-requests.spec.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const chai = require('chai')
5-
chai.use(require('dirty-chai'))
6-
chai.use(require('chai-as-promised'))
7-
const expect = chai.expect
4+
const { expect } = require('aegir/utils/chai')
85

96
const { collect } = require('streaming-iterables')
107
const AbortController = require('abort-controller')

test/block-service-test.js

+30-26
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const chai = require('chai')
5-
chai.use(require('dirty-chai'))
6-
const expect = chai.expect
4+
const { expect } = require('aegir/utils/chai')
75

86
const Block = require('ipld-block')
97
const _ = require('lodash')
108
const { collect } = require('streaming-iterables')
119
const CID = require('cids')
1210
const multihashing = require('multihashing-async')
11+
const uint8ArrayFromString = require('uint8arrays/from-string')
12+
const drain = require('it-drain')
1313

1414
const BlockService = require('../src')
1515

@@ -22,10 +22,10 @@ module.exports = (repo) => {
2222
bs = new BlockService(repo)
2323

2424
const data = [
25-
Buffer.from('1'),
26-
Buffer.from('2'),
27-
Buffer.from('3'),
28-
Buffer.from('A random data block')
25+
uint8ArrayFromString('1'),
26+
uint8ArrayFromString('2'),
27+
uint8ArrayFromString('3'),
28+
uint8ArrayFromString('A random data block')
2929
]
3030

3131
testBlocks = await Promise.all(data.map(async (d) => {
@@ -53,8 +53,16 @@ module.exports = (repo) => {
5353
}
5454
})
5555

56-
it('store many blocks', () => {
57-
return bs.putMany(testBlocks)
56+
it('store many blocks', async () => {
57+
await drain(bs.putMany(testBlocks))
58+
59+
expect(
60+
await Promise.all(
61+
testBlocks.map(b => bs.get(b.cid))
62+
)
63+
).to.deep.equal(
64+
testBlocks
65+
)
5866
})
5967

6068
it('get many blocks through .get', async () => {
@@ -69,7 +77,7 @@ module.exports = (repo) => {
6977
})
7078

7179
it('delete a block', async () => {
72-
const data = Buffer.from('Will not live that much')
80+
const data = uint8ArrayFromString('Will not live that much')
7381

7482
const hash = await multihashing(data, 'sha2-256')
7583
const b = new Block(data, new CID(hash))
@@ -81,7 +89,7 @@ module.exports = (repo) => {
8189
})
8290

8391
it('does not delete a block it does not have', async () => {
84-
const data = Buffer.from('Will not live that much ' + Date.now())
92+
const data = uint8ArrayFromString('Will not live that much ' + Date.now())
8593
const cid = new CID(await multihashing(data, 'sha2-256'))
8694

8795
await bs.delete(cid)
@@ -92,41 +100,37 @@ module.exports = (repo) => {
92100
})
93101

94102
it('deletes lots of blocks', async () => {
95-
const data = Buffer.from('Will not live that much')
103+
const data = uint8ArrayFromString('Will not live that much')
96104

97105
const hash = await multihashing(data, 'sha2-256')
98106
const b = new Block(data, new CID(hash))
99107

100108
await bs.put(b)
101-
await bs.deleteMany([b.cid])
109+
await drain(bs.deleteMany([b.cid]))
102110
const res = await bs._repo.blocks.has(b.cid)
103-
expect(res).to.be.eql(false)
111+
expect(res).to.be.false()
104112
})
105113

106114
it('does not delete a blocks it does not have', async () => {
107-
const data = Buffer.from('Will not live that much ' + Date.now())
115+
const data = uint8ArrayFromString('Will not live that much ' + Date.now())
108116
const cid = new CID(await multihashing(data, 'sha2-256'))
109117

110-
await bs.deleteMany([cid])
111-
.then(
112-
() => expect.fail('Should have thrown'),
113-
(err) => expect(err).to.have.property('code', 'ERR_BLOCK_NOT_FOUND')
114-
)
118+
await expect(drain(bs.deleteMany([cid]))).to.eventually.be.rejected().with.property('code', 'ERR_BLOCK_NOT_FOUND')
115119
})
116120

117121
it('stores and gets lots of blocks', async function () {
118-
this.timeout(8 * 1000)
122+
this.timeout(20 * 1000)
119123

120124
const data = _.range(1000).map((i) => {
121-
return Buffer.from(`hello-${i}-${Math.random()}`)
125+
return uint8ArrayFromString(`hello-${i}-${Math.random()}`)
122126
})
123127

124128
const blocks = await Promise.all(data.map(async (d) => {
125129
const hash = await multihashing(d, 'sha2-256')
126130
return new Block(d, new CID(hash))
127131
}))
128132

129-
await bs.putMany(blocks)
133+
await drain(bs.putMany(blocks))
130134

131135
const res = await Promise.all(blocks.map(b => bs.get(b.cid)))
132136
expect(res).to.be.eql(blocks)
@@ -155,13 +159,13 @@ module.exports = (repo) => {
155159
// returns a block with a value equal to its key
156160
const bitswap = {
157161
get (cid) {
158-
return new Block(Buffer.from('secret'), cid)
162+
return new Block(uint8ArrayFromString('secret'), cid)
159163
}
160164
}
161165

162166
bs.setExchange(bitswap)
163167

164-
const data = Buffer.from('secret')
168+
const data = uint8ArrayFromString('secret')
165169

166170
const hash = await multihashing(data, 'sha2-256')
167171
const block = await bs.get(new CID(hash))
@@ -178,7 +182,7 @@ module.exports = (repo) => {
178182
}
179183
bs.setExchange(bitswap)
180184

181-
const data = Buffer.from('secret sauce')
185+
const data = uint8ArrayFromString('secret sauce')
182186

183187
const hash = await multihashing(data, 'sha2-256')
184188
await bs.put(new Block(data, new CID(hash)))

0 commit comments

Comments
 (0)