diff --git a/.travis.yml b/.travis.yml index acc302d..508af5a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,8 +7,8 @@ stages: - cov node_js: - - '10' - - '12' + - 'lts/*' + - 'node' os: - linux @@ -21,7 +21,6 @@ jobs: include: - stage: check script: - - npx aegir build --bundlesize - npx aegir dep-check - npm run lint diff --git a/package.json b/package.json index 561860d..8da0d13 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ "lint": "aegir lint", "release": "aegir release --target node", "release-minor": "aegir release --type minor --target node", - "build": "aegir build", "test": "aegir test -t node", "test:node": "aegir test -t node" }, @@ -32,26 +31,25 @@ }, "homepage": "https://github.com/ipfs/js-ipfs-http-response#readme", "dependencies": { - "cids": "~0.8.1", "debug": "^4.1.1", - "file-type": "^8.0.0", - "filesize": "^3.6.1", + "file-type": "^14.7.1", + "filesize": "^6.1.0", "it-buffer": "^0.1.1", "it-concat": "^1.0.0", "it-reader": "^2.1.0", "it-to-stream": "^0.1.1", "mime-types": "^2.1.27", - "multihashes": "~0.4.19", + "multihashes": "^3.0.1", "p-try-each": "^1.0.1" }, "devDependencies": { - "aegir": "^22.0.0", - "chai": "^4.2.0", - "dirty-chai": "^2.0.1", - "get-stream": "^3.0.0", - "ipfs": "^0.47.0", - "ipfsd-ctl": "^1.0.2", - "it-all": "^1.0.1" + "aegir": "^25.1.0", + "cids": "^1.0.0", + "get-stream": "^6.0.0", + "ipfs": "^0.49.0", + "ipfsd-ctl": "^6.0.0", + "it-all": "^1.0.1", + "uint8arrays": "^1.1.0" }, "contributors": [ "Vasco Santos ", diff --git a/src/utils/content-type.js b/src/utils/content-type.js index 5459655..a09fa81 100644 --- a/src/utils/content-type.js +++ b/src/utils/content-type.js @@ -1,9 +1,11 @@ 'use strict' -const fileType = require('file-type') +const { fromBuffer: fileType } = require('file-type') const mime = require('mime-types') const Reader = require('it-reader') +const minimumBytes = 4100 + const detectContentType = async (path, source) => { let fileSignature @@ -12,11 +14,11 @@ const detectContentType = async (path, source) => { if (!path.endsWith('.svg')) { try { const reader = Reader(source) - const { value, done } = await reader.next(fileType.minimumBytes) + const { value, done } = await reader.next(minimumBytes) if (done) return { source: reader } - fileSignature = fileType(value.slice()) + fileSignature = await fileType(value.slice()) source = (async function * () { // eslint-disable-line require-await yield value diff --git a/test/index.spec.js b/test/index.spec.js index 0d270d4..d00ef52 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -1,32 +1,26 @@ /* eslint-env mocha */ 'use strict' -const chai = require('chai') -const dirtyChai = require('dirty-chai') -const expect = chai.expect -chai.use(dirtyChai) +const { expect } = require('aegir/utils/chai') const loadFixture = require('aegir/fixtures') -const Ctl = require('ipfsd-ctl') +const { createFactory } = require('ipfsd-ctl') const getStream = require('get-stream') const CID = require('cids') const all = require('it-all') +const uint8ArrayToString = require('uint8arrays/to-string') const { getResponse } = require('../src') const makeWebResponseEnv = require('./utils/web-response-env') -const factory = Ctl.createFactory({ +const factory = createFactory({ test: true, type: 'proc', - ipfsModule: { - ref: require('ipfs'), - path: require.resolve('ipfs') - } + ipfsModule: require('ipfs') }) describe('resolve file (CIDv0)', function () { let ipfs = null - let ipfsd = null const file = { cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', @@ -37,18 +31,16 @@ describe('resolve file (CIDv0)', function () { this.timeout(20 * 1000) Object.assign(global, makeWebResponseEnv()) - ipfsd = await factory.spawn() + const ipfsd = await factory.spawn() ipfs = ipfsd.api - const filesAdded = await all(ipfs.add(file.data, { cidVersion: 0 })) - - expect(filesAdded).to.have.length(1) - - const retrievedFile = filesAdded[0] + const retrievedFile = await ipfs.add(file.data, { cidVersion: 0 }) expect(retrievedFile.cid).to.deep.equal(new CID(file.cid)) expect(retrievedFile.size, 'ipfs.add result size should not be smaller than input buffer').greaterThan(file.data.length) }) + after(() => factory.clean()) + it('should resolve a CIDv0', async () => { const res = await getResponse(ipfs, `/ipfs/${file.cid}`) @@ -56,7 +48,7 @@ describe('resolve file (CIDv0)', function () { expect(res.status).to.equal(200) const contents = await getStream(res.body) - const expectedContents = loadFixture('test/fixtures/testfile.txt').toString() + const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/testfile.txt')) expect(contents).to.equal(expectedContents) }) @@ -78,15 +70,13 @@ describe('resolve file (CIDv1)', function () { ipfsd = await factory.spawn() ipfs = ipfsd.api - const filesAdded = await all(ipfs.add(file.data, { cidVersion: 1 })) - - expect(filesAdded).to.have.length(1) - - const retrievedFile = filesAdded[0] + const retrievedFile = await ipfs.add(file.data, { cidVersion: 1 }) expect(retrievedFile.cid).to.deep.equal(new CID(file.cid)) - // expect(retrievedFile.size, 'ipfs.add result size should not be smaller than input buffer').greaterThan(file.data.length) + expect(retrievedFile.size, 'ipfs.add result size should equal input buffer').to.equal(file.data.length) }) + after(() => factory.clean()) + it('should resolve a CIDv1', async () => { const res = await getResponse(ipfs, `/ipfs/${file.cid}`) @@ -94,7 +84,7 @@ describe('resolve file (CIDv1)', function () { expect(res.status).to.equal(200) const contents = await getStream(res.body) - const expectedContents = loadFixture('test/fixtures/testfile.txt').toString() + const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/testfile.txt')) expect(contents).to.equal(expectedContents) }) @@ -129,7 +119,7 @@ describe('resolve directory (CIDv0)', function () { content('holmes.txt') ] - const res = await all(ipfs.add(dirs, { cidVersion: 0 })) + const res = await all(ipfs.addAll(dirs, { cidVersion: 0 })) const root = res[res.length - 1] expect(root.path).to.equal('test-folder') @@ -139,6 +129,8 @@ describe('resolve directory (CIDv0)', function () { expect(res[1].size, 'ipfs.add 2nd result size should not be smaller than 2nd input buffer').greaterThan(dirs[1].content.length) }) + after(() => factory.clean()) + it('should return the list of files of a directory', async () => { const res = await getResponse(ipfs, `/ipfs/${directory.cid}`, directory.cid) @@ -150,7 +142,7 @@ describe('resolve directory (CIDv0)', function () { const res = await getResponse(ipfs, `/ipfs/${directory.cid}/pp.txt`, directory.cid) const contents = await getStream(res.body) - const expectedContents = loadFixture('test/fixtures/test-folder/pp.txt').toString() + const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/test-folder/pp.txt')) expect(contents).to.equal(expectedContents) }) @@ -159,7 +151,7 @@ describe('resolve directory (CIDv0)', function () { const res = await getResponse(ipfs, `/ipfs/${directory.cid}/holmes.txt`, directory.cid) const contents = await getStream(res.body) - const expectedContents = loadFixture('test/fixtures/test-folder/holmes.txt').toString() + const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/test-folder/holmes.txt')) expect(contents).to.equal(expectedContents) }) @@ -172,7 +164,7 @@ describe('resolve directory (CIDv1)', function () { const directory = { cid: 'bafybeifhimn7nu6dgmdvj6o63zegwro3yznnpfqib6kkjnagc54h46ox5q', files: { - 'pp.txt': Buffer.from(loadFixture('test/fixtures/test-folder/pp.txt')), + 'pp.txt': loadFixture('test/fixtures/test-folder/pp.txt'), 'holmes.txt': loadFixture('test/fixtures/test-folder/holmes.txt') } } @@ -194,7 +186,7 @@ describe('resolve directory (CIDv1)', function () { content('holmes.txt') ] - const res = await all(ipfs.add(dirs, { cidVersion: 1 })) + const res = await all(ipfs.addAll(dirs, { cidVersion: 1 })) const root = res[res.length - 1] expect(root.path).to.equal('test-folder') // expect(res[0].size, 'ipfs.files.add 1st result size should not be smaller than 1st input buffer').greaterThan(dirs[0].content.length) @@ -202,6 +194,8 @@ describe('resolve directory (CIDv1)', function () { expect(root.cid).to.deep.equal(new CID(directory.cid)) }) + after(() => factory.clean()) + it('should return the list of files of a directory', async () => { const res = await getResponse(ipfs, `/ipfs/${directory.cid}`, directory.cid) @@ -213,7 +207,7 @@ describe('resolve directory (CIDv1)', function () { const res = await getResponse(ipfs, `/ipfs/${directory.cid}/pp.txt`, directory.cid) const contents = await getStream(res.body) - const expectedContents = loadFixture('test/fixtures/test-folder/pp.txt').toString() + const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/test-folder/pp.txt')) expect(contents).to.equal(expectedContents) }) @@ -222,7 +216,7 @@ describe('resolve directory (CIDv1)', function () { const res = await getResponse(ipfs, `/ipfs/${directory.cid}/holmes.txt`, directory.cid) const contents = await getStream(res.body) - const expectedContents = loadFixture('test/fixtures/test-folder/holmes.txt').toString() + const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/test-folder/holmes.txt')) expect(contents).to.equal(expectedContents) }) @@ -259,13 +253,15 @@ describe('resolve web page (CIDv0)', function () { content('index.html') ] - const res = await all(ipfs.add(dirs, { cidVersion: 0 })) + const res = await all(ipfs.addAll(dirs, { cidVersion: 0 })) const root = res[res.length - 1] expect(root.path).to.equal('test-site') expect(root.cid).to.deep.equal(new CID(webpage.cid)) }) + after(() => factory.clean()) + it('should return the entry point of a web page when a trying to fetch a directory containing a web page', async () => { const res = await getResponse(ipfs, `/ipfs/${webpage.cid}`, webpage.cid) @@ -305,13 +301,15 @@ describe('resolve web page (CIDv1)', function () { content('index.html') ] - const res = await all(ipfs.add(dirs, { cidVersion: 1 })) + const res = await all(ipfs.addAll(dirs, { cidVersion: 1 })) const root = res[res.length - 1] expect(root.path).to.equal('test-site') expect(root.cid).to.deep.equal(new CID(webpage.cid)) }) + after(() => factory.clean()) + it('should return the entry point of a web page when a trying to fetch a directory containing a web page', async () => { const res = await getResponse(ipfs, `/ipfs/${webpage.cid}`, webpage.cid) @@ -356,13 +354,15 @@ describe('mime-types', () => { content('index.html') ] - const res = await all(ipfs.add(dirs, { cidVersion: 0 })) + const res = await all(ipfs.addAll(dirs, { cidVersion: 0 })) const root = res[res.length - 1] expect(root.path).to.equal('test-mime-types') expect(root.cid).to.deep.equal(new CID(webpage.cid)) }) + after(() => factory.clean()) + it('should return the correct mime-type for pp.txt', async () => { const res = await getResponse(ipfs, `/ipfs/${webpage.cid}/pp.txt`, webpage.cid) diff --git a/test/resolver.spec.js b/test/resolver.spec.js index 96220cc..cfb71d1 100644 --- a/test/resolver.spec.js +++ b/test/resolver.spec.js @@ -1,26 +1,20 @@ /* eslint-env mocha */ 'use strict' -const chai = require('chai') -const dirtyChai = require('dirty-chai') -const expect = chai.expect -chai.use(dirtyChai) +const { expect } = require('aegir/utils/chai') const loadFixture = require('aegir/fixtures') -const Ctl = require('ipfsd-ctl') +const { createFactory } = require('ipfsd-ctl') const CID = require('cids') const mh = require('multihashes') const all = require('it-all') const ipfsResolver = require('../src/resolver') -const factory = Ctl.createFactory({ +const factory = createFactory({ test: true, type: 'proc', - ipfsModule: { - ref: require('ipfs'), - path: require.resolve('ipfs') - } + ipfsModule: require('ipfs') }) describe('resolve file (CIDv0)', function () { @@ -38,13 +32,12 @@ describe('resolve file (CIDv0)', function () { ipfsd = await factory.spawn() ipfs = ipfsd.api - const filesAdded = await all(ipfs.add(file.data, { cidVersion: 0 })) - expect(filesAdded).to.have.length(1) - - const retrievedFile = filesAdded[0] + const retrievedFile = await ipfs.add(file.data, { cidVersion: 0 }) expect(retrievedFile.cid).to.deep.equal(new CID(file.cid)) }) + after(() => factory.clean()) + it('should resolve a multihash', async () => { const res = await ipfsResolver.multihash(ipfs, `/ipfs/${file.cid}`) @@ -81,14 +74,13 @@ describe('resolve file (CIDv1)', function () { ipfsd = await factory.spawn() ipfs = ipfsd.api - const filesAdded = await all(ipfs.add(file.data, { cidVersion: 1 })) - expect(filesAdded).to.have.length(1) - // console.log('ipfs.files.add result', filesAdded) - const retrievedFile = filesAdded[0] + const retrievedFile = await ipfs.add(file.data, { cidVersion: 1 }) expect(retrievedFile.cid).to.deep.equal(new CID(file.cid)) - // expect(retrievedFile.size, 'ipfs.files.add result size should not be smaller than input buffer').greaterThan(file.data.length) + expect(retrievedFile.size, 'ipfs.files.add result size should not be smaller than input buffer').equal(file.data.length) }) + after(() => factory.clean()) + it('should resolve a multihash', async () => { const res = await ipfsResolver.multihash(ipfs, `/ipfs/${file.cid}`) @@ -138,13 +130,15 @@ describe('resolve directory (CIDv0)', function () { content('holmes.txt') ] - const res = await all(ipfs.add(dirs, { cidVersion: 0 })) + const res = await all(ipfs.addAll(dirs, { cidVersion: 0 })) const root = res[res.length - 1] expect(root.path).to.equal('test-folder') expect(root.cid).to.deep.equal(new CID(directory.cid)) }) + after(() => factory.clean()) + it('should throw an error when trying to fetch a directory', async () => { try { const res = await ipfsResolver.cid(ipfs, `/ipfs/${directory.cid}`) @@ -191,7 +185,7 @@ describe('resolve directory (CIDv1)', function () { content('holmes.txt') ] - const res = await all(ipfs.add(dirs, { cidVersion: 1 })) + const res = await all(ipfs.addAll(dirs, { cidVersion: 1 })) const root = res[res.length - 1] // console.log('ipfs.files.add result', res) expect(root.path).to.equal('test-folder') @@ -200,6 +194,8 @@ describe('resolve directory (CIDv1)', function () { expect(root.cid).to.deep.equal(new CID(directory.cid)) }) + after(() => factory.clean()) + it('should throw an error when trying to fetch a directory', async () => { try { const res = await ipfsResolver.cid(ipfs, `/ipfs/${directory.cid}`) @@ -249,13 +245,15 @@ describe('resolve web page (CIDv0)', function () { content('index.html') ] - const res = await all(ipfs.add(dirs, { cidVersion: 0 })) + const res = await all(ipfs.addAll(dirs, { cidVersion: 0 })) const root = res[res.length - 1] expect(root.path).to.equal('test-site') expect(root.cid).to.deep.equal(new CID(webpage.cid)) }) + after(() => factory.clean()) + it('should throw an error when trying to fetch a directory containing a web page', async () => { try { const res = await ipfsResolver.cid(ipfs, `/ipfs/${webpage.cid}`) @@ -306,13 +304,15 @@ describe('resolve web page (CIDv1)', function () { content('index.html') ] - const res = await all(ipfs.add(dirs, { cidVersion: 1 })) + const res = await all(ipfs.addAll(dirs, { cidVersion: 1 })) // console.log(res) const root = res[res.length - 1] expect(root.path).to.equal('test-site') expect(root.cid).to.deep.equal(new CID(webpage.cid)) }) + after(() => factory.clean()) + it('should throw an error when trying to fetch a directory containing a web page', async () => { try { const res = await ipfsResolver.cid(ipfs, `/ipfs/${webpage.cid}`)