This repository was archived by the owner on Sep 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Add support for CIDv1 and Base32 #9
Merged
+510
−84
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c733266
feat: support cidv1b32 in resolver
lidel e320884
fix: tests with files.add and cidv1
lidel 116255e
fix: update tests with valid CIDv1
lidel d673593
style: remove unused console.log
lidel 826f032
fix: update js-cid to v0.5.5
lidel 17cf177
fix(ci): add test:node
lidel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,13 +10,14 @@ const loadFixture = require('aegir/fixtures') | |
const ipfs = require('ipfs') | ||
const DaemonFactory = require('ipfsd-ctl') | ||
const getStream = require('get-stream') | ||
const CID = require('cids') | ||
|
||
const { getResponse } = require('../src') | ||
const makeWebResponseEnv = require('./utils/web-response-env') | ||
|
||
const df = DaemonFactory.create({ type: 'proc', exec: ipfs }) | ||
|
||
describe('resolve file', function () { | ||
describe('resolve file (CIDv0)', function () { | ||
let ipfs = null | ||
let ipfsd = null | ||
|
||
|
@@ -34,19 +35,20 @@ describe('resolve file', function () { | |
ipfsd = _ipfsd | ||
ipfs = ipfsd.api | ||
|
||
ipfs.files.add(file.data, (err, filesAdded) => { | ||
ipfs.files.add(file.data, {cidVersion: 0}, (err, filesAdded) => { | ||
expect(err).to.not.exist() | ||
expect(filesAdded).to.have.length(1) | ||
|
||
const retrievedFile = filesAdded[0] | ||
expect(retrievedFile.hash).to.equal(file.cid) | ||
expect(new CID(retrievedFile.hash)).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) | ||
|
||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
it('should resolve a multihash', async () => { | ||
it('should resolve a CIDv0', async () => { | ||
const res = await getResponse(ipfs, `/ipfs/${file.cid}`) | ||
|
||
expect(res).to.exist() | ||
|
@@ -59,7 +61,50 @@ describe('resolve file', function () { | |
}) | ||
}) | ||
|
||
describe('resolve directory', function () { | ||
describe('resolve file (CIDv1)', function () { | ||
let ipfs = null | ||
let ipfsd = null | ||
|
||
const file = { | ||
cid: 'zb2rhdTDKmCQD2a9x2TfLR61M3s7RmESzwV5mqgnakXQbm5gp', | ||
data: loadFixture('test/fixtures/testfile.txt') | ||
} | ||
|
||
before(function (done) { | ||
this.timeout(20 * 1000) | ||
Object.assign(global, makeWebResponseEnv()) | ||
|
||
df.spawn({ initOptions: { bits: 512 } }, (err, _ipfsd) => { | ||
expect(err).to.not.exist() | ||
ipfsd = _ipfsd | ||
ipfs = ipfsd.api | ||
|
||
ipfs.files.add(file.data, {cidVersion: 1}, (err, filesAdded) => { | ||
expect(err).to.not.exist() | ||
expect(filesAdded).to.have.length(1) | ||
const retrievedFile = filesAdded[0] | ||
expect(new CID(retrievedFile.hash)).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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we receive the file size with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope :) there is a bug and size is |
||
|
||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
it('should resolve a CIDv1', async () => { | ||
const res = await getResponse(ipfs, `/ipfs/${file.cid}`) | ||
|
||
expect(res).to.exist() | ||
expect(res.status).to.equal(200) | ||
|
||
const contents = await getStream(res.body) | ||
const expectedContents = loadFixture('test/fixtures/testfile.txt').toString() | ||
|
||
expect(contents).to.equal(expectedContents) | ||
}) | ||
}) | ||
|
||
describe('resolve directory (CIDv0)', function () { | ||
let ipfs = null | ||
let ipfsd = null | ||
|
||
|
@@ -90,12 +135,16 @@ describe('resolve directory', function () { | |
content('holmes.txt') | ||
] | ||
|
||
ipfs.files.add(dirs, (err, res) => { | ||
ipfs.files.add(dirs, {cidVersion: 0}, (err, res) => { | ||
expect(err).to.not.exist() | ||
const root = res[res.length - 1] | ||
|
||
expect(root.path).to.equal('test-folder') | ||
expect(root.hash).to.equal(directory.cid) | ||
expect(new CID(root.hash)).to.deep.equal(new CID(directory.cid)) | ||
|
||
expect(res[0].size, 'ipfs.files.add 1st result size should not be smaller than 1st input buffer').greaterThan(dirs[0].content.length) | ||
expect(res[1].size, 'ipfs.files.add 2nd result size should not be smaller than 2nd input buffer').greaterThan(dirs[1].content.length) | ||
|
||
done() | ||
}) | ||
}) | ||
|
@@ -116,9 +165,87 @@ describe('resolve directory', function () { | |
|
||
expect(contents).to.equal(expectedContents) | ||
}) | ||
|
||
it('should return the holmes.txt file', async () => { | ||
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() | ||
|
||
expect(contents).to.equal(expectedContents) | ||
}) | ||
}) | ||
|
||
describe('resolve web page', function () { | ||
describe('resolve directory (CIDv1)', function () { | ||
let ipfs = null | ||
let ipfsd = null | ||
|
||
const directory = { | ||
cid: 'zdj7WggpWuCD8yN57uSxoVJPZr371E75q8m4FmZoCvhBJzGvP', | ||
files: { | ||
'pp.txt': Buffer.from(loadFixture('test/fixtures/test-folder/pp.txt')), | ||
'holmes.txt': loadFixture('test/fixtures/test-folder/holmes.txt') | ||
} | ||
} | ||
|
||
before(function (done) { | ||
this.timeout(20 * 1000) | ||
Object.assign(global, makeWebResponseEnv()) | ||
|
||
df.spawn({ initOptions: { bits: 512 } }, (err, _ipfsd) => { | ||
expect(err).to.not.exist() | ||
ipfsd = _ipfsd | ||
ipfs = ipfsd.api | ||
|
||
const content = (name) => ({ | ||
path: `test-folder/${name}`, | ||
content: directory.files[name] | ||
}) | ||
|
||
const dirs = [ | ||
content('pp.txt'), | ||
content('holmes.txt') | ||
] | ||
|
||
ipfs.files.add(dirs, {cidVersion: 1}, (err, res) => { | ||
expect(err).to.not.exist() | ||
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) | ||
// expect(res[1].size, 'ipfs.files.add 2nd result size should not be smaller than 2nd input buffer').greaterThan(dirs[1].content.length) | ||
expect(new CID(root.hash)).to.deep.equal(new CID(directory.cid)) | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
it('should return the list of files of a directory', async () => { | ||
const res = await getResponse(ipfs, `/ipfs/${directory.cid}`, directory.cid) | ||
|
||
expect(res.status).to.equal(200) | ||
expect(res.body).to.match(/<html>/) | ||
}) | ||
|
||
it('should return the pp.txt file', async () => { | ||
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() | ||
|
||
expect(contents).to.equal(expectedContents) | ||
}) | ||
|
||
it('should return the holmes.txt file', async () => { | ||
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() | ||
|
||
expect(contents).to.equal(expectedContents) | ||
}) | ||
}) | ||
|
||
describe('resolve web page (CIDv0)', function () { | ||
let ipfs = null | ||
let ipfsd = null | ||
|
||
|
@@ -151,12 +278,63 @@ describe('resolve web page', function () { | |
content('index.html') | ||
] | ||
|
||
ipfs.files.add(dirs, (err, res) => { | ||
ipfs.files.add(dirs, {cidVersion: 0}, (err, res) => { | ||
expect(err).to.not.exist() | ||
const root = res[res.length - 1] | ||
|
||
expect(root.path).to.equal('test-site') | ||
expect(root.hash).to.equal(webpage.cid) | ||
expect(new CID(root.hash)).to.deep.equal(new CID(webpage.cid)) | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
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) | ||
|
||
expect(res.status).to.equal(302) | ||
expect(res.headers.get('Location')).to.equal(`/ipfs/${webpage.cid}/index.html`) | ||
}) | ||
}) | ||
|
||
describe('resolve web page (CIDv1)', function () { | ||
let ipfs = null | ||
let ipfsd = null | ||
|
||
const webpage = { | ||
cid: 'zdj7WYcfiUZa2wBeD9G2Jg9jqHx3Wh8nRsBNdVSWwsZ7XE62V', | ||
files: { | ||
'pp.txt': loadFixture('test/fixtures/test-site/pp.txt'), | ||
'holmes.txt': loadFixture('test/fixtures/test-site/holmes.txt'), | ||
'index.html': loadFixture('test/fixtures/test-site/index.html') | ||
} | ||
} | ||
|
||
before(function (done) { | ||
this.timeout(20 * 1000) | ||
Object.assign(global, makeWebResponseEnv()) | ||
|
||
df.spawn({ initOptions: { bits: 512 } }, (err, _ipfsd) => { | ||
expect(err).to.not.exist() | ||
ipfsd = _ipfsd | ||
ipfs = ipfsd.api | ||
|
||
const content = (name) => ({ | ||
path: `test-site/${name}`, | ||
content: webpage.files[name] | ||
}) | ||
|
||
const dirs = [ | ||
content('pp.txt'), | ||
content('holmes.txt'), | ||
content('index.html') | ||
] | ||
|
||
ipfs.files.add(dirs, {cidVersion: 1}, (err, res) => { | ||
expect(err).to.not.exist() | ||
const root = res[res.length - 1] | ||
expect(root.path).to.equal('test-site') | ||
expect(new CID(root.hash)).to.deep.equal(new CID(webpage.cid)) | ||
done() | ||
}) | ||
}) | ||
|
@@ -170,6 +348,7 @@ describe('resolve web page', function () { | |
}) | ||
}) | ||
|
||
// TODO: move mime-types to separate test file | ||
describe('mime-types', () => { | ||
let ipfs = null | ||
let ipfsd = null | ||
|
@@ -207,12 +386,12 @@ describe('mime-types', () => { | |
content('index.html') | ||
] | ||
|
||
ipfs.files.add(dirs, (err, res) => { | ||
ipfs.files.add(dirs, {cidVersion: 0}, (err, res) => { | ||
expect(err).to.not.exist() | ||
const root = res[res.length - 1] | ||
|
||
expect(root.path).to.equal('test-mime-types') | ||
expect(root.hash).to.equal(webpage.cid) | ||
expect(new CID(root.hash)).to.deep.equal(new CID(webpage.cid)) | ||
done() | ||
}) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Backward compatibility: js-ipfs uses
fileName
field, so we are keeping it here for now.When
ipfs-http-response
is released with these changes, will PR js-ipfs to switch fromfileName
tocid