From cd4481b723b79a18df7f7e36e22f9badf006c46d Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Sun, 12 May 2019 08:13:14 +0100 Subject: [PATCH 1/5] chore: update cids dependency BREAKING CHANGE: v1 CIDs created by this module now default to base32 encoding when stringified refs: https://github.com/ipfs/js-ipfs/issues/1995 License: MIT Signed-off-by: Alan Shaw --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c29e8e3..a8120c2 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.0", "debug": "^4.1.1", "file-type": "^8.0.0", "filesize": "^3.6.1", From 46f8a4a2b5568dc9fed3f926e92b6d3f6265cdfa Mon Sep 17 00:00:00 2001 From: achingbrain Date: Sun, 19 May 2019 22:54:06 +0100 Subject: [PATCH 2/5] chore: update ipld formats Not a direct dependency of this module but ipld-dag-pb changed the case of some property names that are used by this module. --- src/resolver.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/resolver.js b/src/resolver.js index a638048..af9efec 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -80,7 +80,7 @@ const cid = promisify((ipfs, path, callback) => { const nextFileName = item try { - for (let link of dagNode.links) { + for (let link of dagNode.Links) { if (link.name === nextFileName) { // found multihash/cid of requested named-file try { @@ -129,7 +129,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) From 2286158e7b478ac5cae4e3d86ad342f6b44c9e65 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 20 May 2019 22:18:19 +0100 Subject: [PATCH 3/5] fix: resolver for new dag-pb format --- src/dir-view/index.js | 4 ++-- src/resolver.js | 20 ++++++-------------- 2 files changed, 8 insertions(+), 16 deletions(-) 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/resolver.js b/src/resolver.js index af9efec..b62dbd8 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -19,13 +19,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 +38,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)) }) }) @@ -81,16 +81,8 @@ const cid = promisify((ipfs, path, callback) => { 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)) - } + if (link.Name === nextFileName) { + cidOfNextFile = link.Hash break } } From 4dac07681d661e18a38d0f64e4d1e4c11aeb986e Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 21 May 2019 11:38:49 +0100 Subject: [PATCH 4/5] fix: tests License: MIT Signed-off-by: Alan Shaw --- src/index.js | 9 ++++++++- src/resolver.js | 3 +-- test/index.spec.js | 6 +++--- test/resolver.spec.js | 10 +++++----- 4 files changed, 17 insertions(+), 11 deletions(-) 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 b62dbd8..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') 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' }) }) }) From fff65b8d909b13a16d98d6c271c43c0991e5e969 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 21 May 2019 11:45:16 +0100 Subject: [PATCH 5/5] chore: update cids dep --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f1e4acc..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.7.0", + "cids": "~0.7.1", "debug": "^4.1.1", "file-type": "^8.0.0", "filesize": "^3.6.1",