Skip to content

Commit 8a5aed2

Browse files
authored
fix: replace node buffers with uint8arrays (#69)
Follow up to #66 that removes node Buffers from the tests and uses version of protons that returns uint8arrays. BREAKING CHANGES: - All use of node Buffers have been replaced with Uint8Arrays - CIDs exported by this module have breaking API changes, see: multiformats/js-cid#117
1 parent 32e5165 commit 8a5aed2

32 files changed

+1083
-251
lines changed

package-lock.json

+8-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ipfs-unixfs-exporter/package.json

+8-11
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,26 @@
3737
"devDependencies": {
3838
"abort-controller": "^3.0.0",
3939
"aegir": "^25.0.0",
40-
"buffer": "^5.6.0",
41-
"chai": "^4.2.0",
42-
"chai-as-promised": "^7.1.1",
4340
"detect-node": "^2.0.4",
44-
"dirty-chai": "^2.0.1",
4541
"ipfs-unixfs-importer": "^3.0.1",
46-
"ipld": "^0.26.1",
47-
"ipld-dag-pb": "^0.19.0",
48-
"ipld-in-memory": "^5.0.0",
42+
"ipld": "^0.27.0",
43+
"ipld-dag-pb": "^0.20.0",
44+
"ipld-in-memory": "^6.0.0",
4945
"it-all": "^1.0.1",
5046
"it-buffer-stream": "^1.0.2",
5147
"it-first": "^1.0.1",
52-
"multicodec": "^1.0.0",
48+
"multicodec": "^2.0.0",
5349
"nyc": "^15.0.0",
54-
"sinon": "^9.0.1"
50+
"sinon": "^9.0.1",
51+
"uint8arrays": "^1.0.0"
5552
},
5653
"dependencies": {
57-
"cids": "^0.8.0",
54+
"cids": "^1.0.0",
5855
"err-code": "^2.0.0",
5956
"hamt-sharding": "^1.0.0",
6057
"ipfs-unixfs": "^2.0.1",
6158
"ipfs-utils": "^2.3.1",
6259
"it-last": "^1.0.1",
63-
"multihashing-async": "^1.0.0"
60+
"multihashing-async": "^2.0.0"
6461
}
6562
}

packages/ipfs-unixfs-exporter/src/utils/find-cid-in-shard.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
const Bucket = require('hamt-sharding/src/bucket')
44
const multihashing = require('multihashing-async')
5-
const TextEncoder = require('ipfs-utils/src/text-encoder')
6-
const UTF8_ENCODER = new TextEncoder('utf8')
5+
const uint8ArrayFromString = require('uint8arrays/from-string')
76

87
// FIXME: this is copy/pasted from ipfs-unixfs-importer/src/dir-sharded.js
98
const hashFn = async function (value) {
10-
const buf = UTF8_ENCODER.encode(value)
9+
const buf = uint8ArrayFromString(value)
1110
const hash = await multihashing(buf, 'murmur3-128')
1211

1312
// Multihashing inserts preamble of 2 bytes. Remove it.

packages/ipfs-unixfs-exporter/test/exporter-sharded.spec.js

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

4-
const { Buffer } = require('buffer')
5-
const chai = require('chai')
6-
chai.use(require('dirty-chai'))
7-
const expect = chai.expect
4+
const { expect } = require('aegir/utils/chai')
85
const IPLD = require('ipld')
96
const inMemory = require('ipld-in-memory')
107
const UnixFS = require('ipfs-unixfs')
@@ -20,6 +17,7 @@ const {
2017
DAGNode
2118
} = require('ipld-dag-pb')
2219
const blockApi = require('./helpers/block')
20+
const uint8ArrayConcat = require('uint8arrays/concat')
2321

2422
const SHARD_SPLIT_THRESHOLD = 10
2523

@@ -36,7 +34,7 @@ describe('exporter sharded', function () {
3634
const createShardWithFileNames = (numFiles, fileName) => {
3735
const files = new Array(numFiles).fill(0).map((_, index) => ({
3836
path: fileName(index),
39-
content: Buffer.from([0, 1, 2, 3, 4, index])
37+
content: Uint8Array.from([0, 1, 2, 3, 4, index])
4038
}))
4139

4240
return createShardWithFiles(files)
@@ -59,7 +57,7 @@ describe('exporter sharded', function () {
5957

6058
for (let i = 0; i < (SHARD_SPLIT_THRESHOLD + 1); i++) {
6159
files[`file-${Math.random()}.txt`] = {
62-
content: Buffer.concat(await all(randomBytes(100)))
60+
content: uint8ArrayConcat(await all(randomBytes(100)))
6361
}
6462
}
6563

@@ -92,7 +90,7 @@ describe('exporter sharded', function () {
9290

9391
for (let i = 0; i < dirFiles.length; i++) {
9492
const dirFile = dirFiles[i]
95-
const data = Buffer.concat(await all(dirFile.content()))
93+
const data = uint8ArrayConcat(await all(dirFile.content()))
9694

9795
// validate the CID
9896
expect(files[dirFile.name].cid.equals(dirFile.cid)).to.be.true()

packages/ipfs-unixfs-exporter/test/exporter-subtree.spec.js

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

4-
const { Buffer } = require('buffer')
5-
const chai = require('chai')
6-
chai.use(require('dirty-chai'))
7-
const expect = chai.expect
4+
const { expect } = require('aegir/utils/chai')
85
const IPLD = require('ipld')
96
const inMemory = require('ipld-in-memory')
107
const importer = require('ipfs-unixfs-importer')
@@ -13,6 +10,7 @@ const all = require('it-all')
1310
const last = require('it-last')
1411
const blockApi = require('./helpers/block')
1512
const randomBytes = require('it-buffer-stream')
13+
const uint8ArrayConcat = require('uint8arrays/concat')
1614

1715
const ONE_MEG = Math.pow(1024, 2)
1816

@@ -28,7 +26,7 @@ describe('exporter subtree', () => {
2826
})
2927

3028
it('exports a file 2 levels down', async () => {
31-
const content = Buffer.concat(await all(randomBytes(ONE_MEG)))
29+
const content = uint8ArrayConcat(await all(randomBytes(ONE_MEG)))
3230

3331
const imported = await last(importer([{
3432
path: './200Bytes.txt',
@@ -44,12 +42,12 @@ describe('exporter subtree', () => {
4442
expect(exported.name).to.equal('200Bytes.txt')
4543
expect(exported.path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/200Bytes.txt`)
4644

47-
const data = Buffer.concat(await all(exported.content()))
45+
const data = uint8ArrayConcat(await all(exported.content()))
4846
expect(data).to.deep.equal(content)
4947
})
5048

5149
it('exports a directory 1 level down', async () => {
52-
const content = Buffer.concat(await all(randomBytes(ONE_MEG)))
50+
const content = uint8ArrayConcat(await all(randomBytes(ONE_MEG)))
5351
const imported = await last(importer([{
5452
path: './200Bytes.txt',
5553
content: randomBytes(ONE_MEG)
@@ -70,7 +68,7 @@ describe('exporter subtree', () => {
7068
expect(files[1].name).to.equal('level-2')
7169
expect(files[1].path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/level-2`)
7270

73-
const data = Buffer.concat(await all(files[0].content()))
71+
const data = uint8ArrayConcat(await all(files[0].content()))
7472
expect(data).to.deep.equal(content)
7573
})
7674

@@ -88,7 +86,7 @@ describe('exporter subtree', () => {
8886
})
8987

9088
it('exports starting from non-protobuf node', async () => {
91-
const content = Buffer.concat(await all(randomBytes(ONE_MEG)))
89+
const content = uint8ArrayConcat(await all(randomBytes(ONE_MEG)))
9290

9391
const imported = await last(importer([{
9492
path: './level-1/200Bytes.txt',
@@ -108,12 +106,12 @@ describe('exporter subtree', () => {
108106
expect(exported.name).to.equal('200Bytes.txt')
109107
expect(exported.path).to.equal(`${cborNodeCid.toBaseEncodedString()}/a/file/level-1/200Bytes.txt`)
110108

111-
const data = Buffer.concat(await all(exported.content()))
109+
const data = uint8ArrayConcat(await all(exported.content()))
112110
expect(data).to.deep.equal(content)
113111
})
114112

115113
it('uses .path to export all components of a path', async () => {
116-
const content = Buffer.concat(await all(randomBytes(ONE_MEG)))
114+
const content = uint8ArrayConcat(await all(randomBytes(ONE_MEG)))
117115

118116
const imported = await last(importer([{
119117
path: './200Bytes.txt',

0 commit comments

Comments
 (0)