Skip to content

Commit 2cd4483

Browse files
committed
fix: replace node buffers with uint8arrays
Follow up to #66 that removes node Buffers from the tests and uses version of protons that returns uint8arrays.
1 parent 32e5165 commit 2cd4483

25 files changed

+221
-176
lines changed

packages/ipfs-unixfs-exporter/package-lock.json

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

packages/ipfs-unixfs-exporter/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@
3737
"devDependencies": {
3838
"abort-controller": "^3.0.0",
3939
"aegir": "^25.0.0",
40-
"buffer": "^5.6.0",
4140
"chai": "^4.2.0",
4241
"chai-as-promised": "^7.1.1",
4342
"detect-node": "^2.0.4",
4443
"dirty-chai": "^2.0.1",
4544
"ipfs-unixfs-importer": "^3.0.1",
4645
"ipld": "^0.26.1",
47-
"ipld-dag-pb": "^0.19.0",
46+
"ipld-dag-pb": "ipld/js-ipld-dag-pb#fix/replace-node-buffers-with-unit8arrays",
4847
"ipld-in-memory": "^5.0.0",
4948
"it-all": "^1.0.1",
5049
"it-buffer-stream": "^1.0.2",
5150
"it-first": "^1.0.1",
5251
"multicodec": "^1.0.0",
5352
"nyc": "^15.0.0",
54-
"sinon": "^9.0.1"
53+
"sinon": "^9.0.1",
54+
"uint8arrays": "^1.0.0"
5555
},
5656
"dependencies": {
5757
"cids": "^0.8.0",
@@ -60,6 +60,6 @@
6060
"ipfs-unixfs": "^2.0.1",
6161
"ipfs-utils": "^2.3.1",
6262
"it-last": "^1.0.1",
63-
"multihashing-async": "^1.0.0"
63+
"multihashing-async": "^2.0.0"
6464
}
6565
}

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

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

4-
const { Buffer } = require('buffer')
54
const chai = require('chai')
65
chai.use(require('dirty-chai'))
76
const expect = chai.expect
@@ -20,6 +19,7 @@ const {
2019
DAGNode
2120
} = require('ipld-dag-pb')
2221
const blockApi = require('./helpers/block')
22+
const uint8ArrayConcat = require('uint8arrays/concat')
2323

2424
const SHARD_SPLIT_THRESHOLD = 10
2525

@@ -36,7 +36,7 @@ describe('exporter sharded', function () {
3636
const createShardWithFileNames = (numFiles, fileName) => {
3737
const files = new Array(numFiles).fill(0).map((_, index) => ({
3838
path: fileName(index),
39-
content: Buffer.from([0, 1, 2, 3, 4, index])
39+
content: Uint8Array.from([0, 1, 2, 3, 4, index])
4040
}))
4141

4242
return createShardWithFiles(files)
@@ -59,7 +59,7 @@ describe('exporter sharded', function () {
5959

6060
for (let i = 0; i < (SHARD_SPLIT_THRESHOLD + 1); i++) {
6161
files[`file-${Math.random()}.txt`] = {
62-
content: Buffer.concat(await all(randomBytes(100)))
62+
content: uint8ArrayConcat(await all(randomBytes(100)))
6363
}
6464
}
6565

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

9393
for (let i = 0; i < dirFiles.length; i++) {
9494
const dirFile = dirFiles[i]
95-
const data = Buffer.concat(await all(dirFile.content()))
95+
const data = uint8ArrayConcat(await all(dirFile.content()))
9696

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

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

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

4-
const { Buffer } = require('buffer')
54
const chai = require('chai')
65
chai.use(require('dirty-chai'))
76
const expect = chai.expect
@@ -13,6 +12,7 @@ const all = require('it-all')
1312
const last = require('it-last')
1413
const blockApi = require('./helpers/block')
1514
const randomBytes = require('it-buffer-stream')
15+
const uint8ArrayConcat = require('uint8arrays/concat')
1616

1717
const ONE_MEG = Math.pow(1024, 2)
1818

@@ -28,7 +28,7 @@ describe('exporter subtree', () => {
2828
})
2929

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

3333
const imported = await last(importer([{
3434
path: './200Bytes.txt',
@@ -44,12 +44,12 @@ describe('exporter subtree', () => {
4444
expect(exported.name).to.equal('200Bytes.txt')
4545
expect(exported.path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/200Bytes.txt`)
4646

47-
const data = Buffer.concat(await all(exported.content()))
47+
const data = uint8ArrayConcat(await all(exported.content()))
4848
expect(data).to.deep.equal(content)
4949
})
5050

5151
it('exports a directory 1 level down', async () => {
52-
const content = Buffer.concat(await all(randomBytes(ONE_MEG)))
52+
const content = uint8ArrayConcat(await all(randomBytes(ONE_MEG)))
5353
const imported = await last(importer([{
5454
path: './200Bytes.txt',
5555
content: randomBytes(ONE_MEG)
@@ -70,7 +70,7 @@ describe('exporter subtree', () => {
7070
expect(files[1].name).to.equal('level-2')
7171
expect(files[1].path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/level-2`)
7272

73-
const data = Buffer.concat(await all(files[0].content()))
73+
const data = uint8ArrayConcat(await all(files[0].content()))
7474
expect(data).to.deep.equal(content)
7575
})
7676

@@ -88,7 +88,7 @@ describe('exporter subtree', () => {
8888
})
8989

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

9393
const imported = await last(importer([{
9494
path: './level-1/200Bytes.txt',
@@ -108,12 +108,12 @@ describe('exporter subtree', () => {
108108
expect(exported.name).to.equal('200Bytes.txt')
109109
expect(exported.path).to.equal(`${cborNodeCid.toBaseEncodedString()}/a/file/level-1/200Bytes.txt`)
110110

111-
const data = Buffer.concat(await all(exported.content()))
111+
const data = uint8ArrayConcat(await all(exported.content()))
112112
expect(data).to.deep.equal(content)
113113
})
114114

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

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

0 commit comments

Comments
 (0)