diff --git a/package.json b/package.json index 9679034..73c2d9a 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "homepage": "https://github.com/ipfs/js-ipfs-http-response#readme", "dependencies": { "async": "^2.6.1", - "cids": "~0.5.7", + "cids": "~0.7.1", "debug": "^4.1.1", "file-type": "^8.0.0", "filesize": "^3.6.1", diff --git a/src/dir-view/index.js b/src/dir-view/index.js index 60e1118..6ee0204 100644 --- a/src/dir-view/index.js +++ b/src/dir-view/index.js @@ -18,8 +18,8 @@ function buildFilesList (path, links) { const rows = links.map((link) => { let row = [ `
 
`, - `${link.name}`, - filesize(link.size) + `${link.Name}`, + filesize(link.Tsize) ] row = row.map((cell) => `${cell}`).join('') diff --git a/src/index.js b/src/index.js index e3d96cb..a36a941 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,9 @@ const stream = require('stream') const toBlob = require('stream-to-blob') +const debug = require('debug') +const log = debug('ipfs:http:response') + const resolver = require('./resolver') const pathUtils = require('./utils/path') const detectContentType = require('./utils/content-type') @@ -34,9 +37,10 @@ const response = (ipfsNode, ipfsPath) => { } // redirect to dir entry point (index) - resolve(Response.redirect(pathUtils.joinURLParts(path, content[0].name))) + resolve(Response.redirect(pathUtils.joinURLParts(path, content[0].Name))) }) .catch((error) => { + log(error) resolve(new Response(errorString, header(500, error.toString()))) }) break @@ -48,6 +52,7 @@ const response = (ipfsNode, ipfsPath) => { resolve(new Response(errorString, header(400, errorString))) break default: + log(error) resolve(new Response(errorString, header(500, errorString))) } }) @@ -68,6 +73,7 @@ const response = (ipfsNode, ipfsPath) => { readableStream.once('error', (error) => { if (error) { + log(error) resolve(new Response(error.toString(), header(500, 'Error fetching the file'))) } }) @@ -106,6 +112,7 @@ const response = (ipfsNode, ipfsPath) => { }) }) .catch((error) => { + log(error) resolve(handleResolveError(ipfsNode, ipfsPath, error)) }) }) diff --git a/src/resolver.js b/src/resolver.js index a638048..f578f35 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -6,8 +6,7 @@ const reduce = require('async/reduce') const CID = require('cids') const Unixfs = require('ipfs-unixfs') const debug = require('debug') -const log = debug('jsipfs:http:response:resolver') -log.error = debug('jsipfs:http:response:resolver:error') +const log = debug('ipfs:http:response:resolver') const dirView = require('./dir-view') const pathUtil = require('./utils/path') @@ -19,13 +18,13 @@ function getIndexFiles (links) { 'index.shtml' ] // directory - let indexes = links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1) + let indexes = links.filter((link) => INDEX_HTML_FILES.indexOf(link.Name) !== -1) if (indexes.length) { return indexes } // hamt-sharded-directory uses a 2 char prefix return links.filter((link) => { - return link.name.length > 2 && INDEX_HTML_FILES.indexOf(link.name.substring(2)) !== -1 + return link.Name.length > 2 && INDEX_HTML_FILES.indexOf(link.Name.substring(2)) !== -1 }) } @@ -38,13 +37,13 @@ const directory = promisify((ipfs, path, cid, callback) => { } // Test if it is a Website - const indexFiles = getIndexFiles(dagNode.links) + const indexFiles = getIndexFiles(dagNode.Links) if (indexFiles.length) { return callback(null, indexFiles) } - return callback(null, dirView.render(path, dagNode.links)) + return callback(null, dirView.render(path, dagNode.Links)) }) }) @@ -80,17 +79,9 @@ const cid = promisify((ipfs, path, callback) => { const nextFileName = item try { - for (let link of dagNode.links) { - if (link.name === nextFileName) { - // found multihash/cid of requested named-file - try { - // assume a Buffer with a valid CID - // (cid is allowed instead of multihash since https://github.com/ipld/js-ipld-dag-pb/pull/80) - cidOfNextFile = new CID(link.cid) - } catch (err) { - // fallback to multihash - cidOfNextFile = new CID(mh.toB58String(link.multihash)) - } + for (let link of dagNode.Links) { + if (link.Name === nextFileName) { + cidOfNextFile = link.Hash break } } @@ -129,7 +120,7 @@ const cid = promisify((ipfs, path, callback) => { } try { - let dagDataObj = Unixfs.unmarshal(dagResult.value.data) + let dagDataObj = Unixfs.unmarshal(dagResult.value.Data) // There are at least two types of directories: // - "directory" // - "hamt-sharded-directory" (example: QmT5NvUtoM5nWFfrQdVrFtvGfKFmG7AHE8P34isapyhCxX) diff --git a/test/index.spec.js b/test/index.spec.js index d880d5e..8b20bd9 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -66,7 +66,7 @@ describe('resolve file (CIDv1)', function () { let ipfsd = null const file = { - cid: 'zb2rhdTDKmCQD2a9x2TfLR61M3s7RmESzwV5mqgnakXQbm5gp', + cid: 'bafkreidffqfydlguosmmyebv5rp72m45tbpbq6segnkosa45kjfnduix6u', data: loadFixture('test/fixtures/testfile.txt') } @@ -181,7 +181,7 @@ describe('resolve directory (CIDv1)', function () { let ipfsd = null const directory = { - cid: 'zdj7WggpWuCD8yN57uSxoVJPZr371E75q8m4FmZoCvhBJzGvP', + cid: 'bafybeifhimn7nu6dgmdvj6o63zegwro3yznnpfqib6kkjnagc54h46ox5q', files: { 'pp.txt': Buffer.from(loadFixture('test/fixtures/test-folder/pp.txt')), 'holmes.txt': loadFixture('test/fixtures/test-folder/holmes.txt') @@ -302,7 +302,7 @@ describe('resolve web page (CIDv1)', function () { let ipfsd = null const webpage = { - cid: 'zdj7WYcfiUZa2wBeD9G2Jg9jqHx3Wh8nRsBNdVSWwsZ7XE62V', + cid: 'bafybeibpkvlqjkwl73yam6ffsbrlgbwiffnehajc6qvnrhai5bve6jnawi', files: { 'pp.txt': loadFixture('test/fixtures/test-site/pp.txt'), 'holmes.txt': loadFixture('test/fixtures/test-site/holmes.txt'), diff --git a/test/resolver.spec.js b/test/resolver.spec.js index 2567a86..6246e3a 100644 --- a/test/resolver.spec.js +++ b/test/resolver.spec.js @@ -70,7 +70,7 @@ describe('resolve file (CIDv1)', function () { let ipfsd = null const file = { - cid: 'zb2rhdTDKmCQD2a9x2TfLR61M3s7RmESzwV5mqgnakXQbm5gp', + cid: 'bafkreidffqfydlguosmmyebv5rp72m45tbpbq6segnkosa45kjfnduix6u', data: loadFixture('test/fixtures/testfile.txt') } @@ -177,7 +177,7 @@ describe('resolve directory (CIDv1)', function () { let ipfsd = null const directory = { - cid: 'zdj7WggpWuCD8yN57uSxoVJPZr371E75q8m4FmZoCvhBJzGvP', + cid: 'bafybeifhimn7nu6dgmdvj6o63zegwro3yznnpfqib6kkjnagc54h46ox5q', files: { 'pp.txt': loadFixture('test/fixtures/test-folder/pp.txt'), 'holmes.txt': loadFixture('test/fixtures/test-folder/holmes.txt') @@ -290,7 +290,7 @@ describe('resolve web page (CIDv0)', function () { expect(res).to.exist() expect(res[0]).to.deep.include({ - name: 'index.html' + Name: 'index.html' }) }) }) @@ -300,7 +300,7 @@ describe('resolve web page (CIDv1)', function () { let ipfsd = null const webpage = { - cid: 'zdj7WYcfiUZa2wBeD9G2Jg9jqHx3Wh8nRsBNdVSWwsZ7XE62V', + cid: 'bafybeibpkvlqjkwl73yam6ffsbrlgbwiffnehajc6qvnrhai5bve6jnawi', files: { 'pp.txt': loadFixture('test/fixtures/test-site/pp.txt'), 'holmes.txt': loadFixture('test/fixtures/test-site/holmes.txt'), @@ -352,7 +352,7 @@ describe('resolve web page (CIDv1)', function () { expect(res).to.exist() expect(res[0]).to.deep.include({ - name: 'index.html' + Name: 'index.html' }) }) })