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

Commit 710a96d

Browse files
authored
fix: replace node buffers with uint8arrays (#55)
* fix: replace node buffers with uint8arrays BREAKING CHANGES: - All deps of this module use Uint8Arrays instead of node Buffers * chore: remove browser build steps
1 parent 71dcbc6 commit 710a96d

File tree

5 files changed

+75
-76
lines changed

5 files changed

+75
-76
lines changed

.travis.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ stages:
77
- cov
88

99
node_js:
10-
- '10'
11-
- '12'
10+
- 'lts/*'
11+
- 'node'
1212

1313
os:
1414
- linux
@@ -21,7 +21,6 @@ jobs:
2121
include:
2222
- stage: check
2323
script:
24-
- npx aegir build --bundlesize
2524
- npx aegir dep-check
2625
- npm run lint
2726

package.json

+10-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"lint": "aegir lint",
99
"release": "aegir release --target node",
1010
"release-minor": "aegir release --type minor --target node",
11-
"build": "aegir build",
1211
"test": "aegir test -t node",
1312
"test:node": "aegir test -t node"
1413
},
@@ -32,26 +31,25 @@
3231
},
3332
"homepage": "https://github.com./ipfs/js-ipfs-http-response#readme",
3433
"dependencies": {
35-
"cids": "~0.8.1",
3634
"debug": "^4.1.1",
37-
"file-type": "^8.0.0",
38-
"filesize": "^3.6.1",
35+
"file-type": "^14.7.1",
36+
"filesize": "^6.1.0",
3937
"it-buffer": "^0.1.1",
4038
"it-concat": "^1.0.0",
4139
"it-reader": "^2.1.0",
4240
"it-to-stream": "^0.1.1",
4341
"mime-types": "^2.1.27",
44-
"multihashes": "~0.4.19",
42+
"multihashes": "^3.0.1",
4543
"p-try-each": "^1.0.1"
4644
},
4745
"devDependencies": {
48-
"aegir": "^22.0.0",
49-
"chai": "^4.2.0",
50-
"dirty-chai": "^2.0.1",
51-
"get-stream": "^3.0.0",
52-
"ipfs": "^0.47.0",
53-
"ipfsd-ctl": "^1.0.2",
54-
"it-all": "^1.0.1"
46+
"aegir": "^25.1.0",
47+
"cids": "^1.0.0",
48+
"get-stream": "^6.0.0",
49+
"ipfs": "^0.49.0",
50+
"ipfsd-ctl": "^6.0.0",
51+
"it-all": "^1.0.1",
52+
"uint8arrays": "^1.1.0"
5553
},
5654
"contributors": [
5755
"Vasco Santos <[email protected]>",

src/utils/content-type.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict'
22

3-
const fileType = require('file-type')
3+
const { fromBuffer: fileType } = require('file-type')
44
const mime = require('mime-types')
55
const Reader = require('it-reader')
66

7+
const minimumBytes = 4100
8+
79
const detectContentType = async (path, source) => {
810
let fileSignature
911

@@ -12,11 +14,11 @@ const detectContentType = async (path, source) => {
1214
if (!path.endsWith('.svg')) {
1315
try {
1416
const reader = Reader(source)
15-
const { value, done } = await reader.next(fileType.minimumBytes)
17+
const { value, done } = await reader.next(minimumBytes)
1618

1719
if (done) return { source: reader }
1820

19-
fileSignature = fileType(value.slice())
21+
fileSignature = await fileType(value.slice())
2022

2123
source = (async function * () { // eslint-disable-line require-await
2224
yield value

test/index.spec.js

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

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

96
const loadFixture = require('aegir/fixtures')
10-
const Ctl = require('ipfsd-ctl')
7+
const { createFactory } = require('ipfsd-ctl')
118
const getStream = require('get-stream')
129
const CID = require('cids')
1310
const all = require('it-all')
11+
const uint8ArrayToString = require('uint8arrays/to-string')
1412

1513
const { getResponse } = require('../src')
1614
const makeWebResponseEnv = require('./utils/web-response-env')
1715

18-
const factory = Ctl.createFactory({
16+
const factory = createFactory({
1917
test: true,
2018
type: 'proc',
21-
ipfsModule: {
22-
ref: require('ipfs'),
23-
path: require.resolve('ipfs')
24-
}
19+
ipfsModule: require('ipfs')
2520
})
2621

2722
describe('resolve file (CIDv0)', function () {
2823
let ipfs = null
29-
let ipfsd = null
3024

3125
const file = {
3226
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
@@ -37,26 +31,24 @@ describe('resolve file (CIDv0)', function () {
3731
this.timeout(20 * 1000)
3832
Object.assign(global, makeWebResponseEnv())
3933

40-
ipfsd = await factory.spawn()
34+
const ipfsd = await factory.spawn()
4135
ipfs = ipfsd.api
4236

43-
const filesAdded = await all(ipfs.add(file.data, { cidVersion: 0 }))
44-
45-
expect(filesAdded).to.have.length(1)
46-
47-
const retrievedFile = filesAdded[0]
37+
const retrievedFile = await ipfs.add(file.data, { cidVersion: 0 })
4838
expect(retrievedFile.cid).to.deep.equal(new CID(file.cid))
4939
expect(retrievedFile.size, 'ipfs.add result size should not be smaller than input buffer').greaterThan(file.data.length)
5040
})
5141

42+
after(() => factory.clean())
43+
5244
it('should resolve a CIDv0', async () => {
5345
const res = await getResponse(ipfs, `/ipfs/${file.cid}`)
5446

5547
expect(res).to.exist()
5648
expect(res.status).to.equal(200)
5749

5850
const contents = await getStream(res.body)
59-
const expectedContents = loadFixture('test/fixtures/testfile.txt').toString()
51+
const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/testfile.txt'))
6052

6153
expect(contents).to.equal(expectedContents)
6254
})
@@ -78,23 +70,21 @@ describe('resolve file (CIDv1)', function () {
7870
ipfsd = await factory.spawn()
7971
ipfs = ipfsd.api
8072

81-
const filesAdded = await all(ipfs.add(file.data, { cidVersion: 1 }))
82-
83-
expect(filesAdded).to.have.length(1)
84-
85-
const retrievedFile = filesAdded[0]
73+
const retrievedFile = await ipfs.add(file.data, { cidVersion: 1 })
8674
expect(retrievedFile.cid).to.deep.equal(new CID(file.cid))
87-
// expect(retrievedFile.size, 'ipfs.add result size should not be smaller than input buffer').greaterThan(file.data.length)
75+
expect(retrievedFile.size, 'ipfs.add result size should equal input buffer').to.equal(file.data.length)
8876
})
8977

78+
after(() => factory.clean())
79+
9080
it('should resolve a CIDv1', async () => {
9181
const res = await getResponse(ipfs, `/ipfs/${file.cid}`)
9282

9383
expect(res).to.exist()
9484
expect(res.status).to.equal(200)
9585

9686
const contents = await getStream(res.body)
97-
const expectedContents = loadFixture('test/fixtures/testfile.txt').toString()
87+
const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/testfile.txt'))
9888

9989
expect(contents).to.equal(expectedContents)
10090
})
@@ -129,7 +119,7 @@ describe('resolve directory (CIDv0)', function () {
129119
content('holmes.txt')
130120
]
131121

132-
const res = await all(ipfs.add(dirs, { cidVersion: 0 }))
122+
const res = await all(ipfs.addAll(dirs, { cidVersion: 0 }))
133123
const root = res[res.length - 1]
134124

135125
expect(root.path).to.equal('test-folder')
@@ -139,6 +129,8 @@ describe('resolve directory (CIDv0)', function () {
139129
expect(res[1].size, 'ipfs.add 2nd result size should not be smaller than 2nd input buffer').greaterThan(dirs[1].content.length)
140130
})
141131

132+
after(() => factory.clean())
133+
142134
it('should return the list of files of a directory', async () => {
143135
const res = await getResponse(ipfs, `/ipfs/${directory.cid}`, directory.cid)
144136

@@ -150,7 +142,7 @@ describe('resolve directory (CIDv0)', function () {
150142
const res = await getResponse(ipfs, `/ipfs/${directory.cid}/pp.txt`, directory.cid)
151143

152144
const contents = await getStream(res.body)
153-
const expectedContents = loadFixture('test/fixtures/test-folder/pp.txt').toString()
145+
const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/test-folder/pp.txt'))
154146

155147
expect(contents).to.equal(expectedContents)
156148
})
@@ -159,7 +151,7 @@ describe('resolve directory (CIDv0)', function () {
159151
const res = await getResponse(ipfs, `/ipfs/${directory.cid}/holmes.txt`, directory.cid)
160152

161153
const contents = await getStream(res.body)
162-
const expectedContents = loadFixture('test/fixtures/test-folder/holmes.txt').toString()
154+
const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/test-folder/holmes.txt'))
163155

164156
expect(contents).to.equal(expectedContents)
165157
})
@@ -172,7 +164,7 @@ describe('resolve directory (CIDv1)', function () {
172164
const directory = {
173165
cid: 'bafybeifhimn7nu6dgmdvj6o63zegwro3yznnpfqib6kkjnagc54h46ox5q',
174166
files: {
175-
'pp.txt': Buffer.from(loadFixture('test/fixtures/test-folder/pp.txt')),
167+
'pp.txt': loadFixture('test/fixtures/test-folder/pp.txt'),
176168
'holmes.txt': loadFixture('test/fixtures/test-folder/holmes.txt')
177169
}
178170
}
@@ -194,14 +186,16 @@ describe('resolve directory (CIDv1)', function () {
194186
content('holmes.txt')
195187
]
196188

197-
const res = await all(ipfs.add(dirs, { cidVersion: 1 }))
189+
const res = await all(ipfs.addAll(dirs, { cidVersion: 1 }))
198190
const root = res[res.length - 1]
199191
expect(root.path).to.equal('test-folder')
200192
// expect(res[0].size, 'ipfs.files.add 1st result size should not be smaller than 1st input buffer').greaterThan(dirs[0].content.length)
201193
// expect(res[1].size, 'ipfs.files.add 2nd result size should not be smaller than 2nd input buffer').greaterThan(dirs[1].content.length)
202194
expect(root.cid).to.deep.equal(new CID(directory.cid))
203195
})
204196

197+
after(() => factory.clean())
198+
205199
it('should return the list of files of a directory', async () => {
206200
const res = await getResponse(ipfs, `/ipfs/${directory.cid}`, directory.cid)
207201

@@ -213,7 +207,7 @@ describe('resolve directory (CIDv1)', function () {
213207
const res = await getResponse(ipfs, `/ipfs/${directory.cid}/pp.txt`, directory.cid)
214208

215209
const contents = await getStream(res.body)
216-
const expectedContents = loadFixture('test/fixtures/test-folder/pp.txt').toString()
210+
const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/test-folder/pp.txt'))
217211

218212
expect(contents).to.equal(expectedContents)
219213
})
@@ -222,7 +216,7 @@ describe('resolve directory (CIDv1)', function () {
222216
const res = await getResponse(ipfs, `/ipfs/${directory.cid}/holmes.txt`, directory.cid)
223217

224218
const contents = await getStream(res.body)
225-
const expectedContents = loadFixture('test/fixtures/test-folder/holmes.txt').toString()
219+
const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/test-folder/holmes.txt'))
226220

227221
expect(contents).to.equal(expectedContents)
228222
})
@@ -259,13 +253,15 @@ describe('resolve web page (CIDv0)', function () {
259253
content('index.html')
260254
]
261255

262-
const res = await all(ipfs.add(dirs, { cidVersion: 0 }))
256+
const res = await all(ipfs.addAll(dirs, { cidVersion: 0 }))
263257
const root = res[res.length - 1]
264258

265259
expect(root.path).to.equal('test-site')
266260
expect(root.cid).to.deep.equal(new CID(webpage.cid))
267261
})
268262

263+
after(() => factory.clean())
264+
269265
it('should return the entry point of a web page when a trying to fetch a directory containing a web page', async () => {
270266
const res = await getResponse(ipfs, `/ipfs/${webpage.cid}`, webpage.cid)
271267

@@ -305,13 +301,15 @@ describe('resolve web page (CIDv1)', function () {
305301
content('index.html')
306302
]
307303

308-
const res = await all(ipfs.add(dirs, { cidVersion: 1 }))
304+
const res = await all(ipfs.addAll(dirs, { cidVersion: 1 }))
309305
const root = res[res.length - 1]
310306

311307
expect(root.path).to.equal('test-site')
312308
expect(root.cid).to.deep.equal(new CID(webpage.cid))
313309
})
314310

311+
after(() => factory.clean())
312+
315313
it('should return the entry point of a web page when a trying to fetch a directory containing a web page', async () => {
316314
const res = await getResponse(ipfs, `/ipfs/${webpage.cid}`, webpage.cid)
317315

@@ -356,13 +354,15 @@ describe('mime-types', () => {
356354
content('index.html')
357355
]
358356

359-
const res = await all(ipfs.add(dirs, { cidVersion: 0 }))
357+
const res = await all(ipfs.addAll(dirs, { cidVersion: 0 }))
360358
const root = res[res.length - 1]
361359

362360
expect(root.path).to.equal('test-mime-types')
363361
expect(root.cid).to.deep.equal(new CID(webpage.cid))
364362
})
365363

364+
after(() => factory.clean())
365+
366366
it('should return the correct mime-type for pp.txt', async () => {
367367
const res = await getResponse(ipfs, `/ipfs/${webpage.cid}/pp.txt`, webpage.cid)
368368

0 commit comments

Comments
 (0)