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

chore: update ipld formats #25

Merged
merged 6 commits into from
May 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
4 changes: 2 additions & 2 deletions src/dir-view/index.js
Original file line number Diff line number Diff line change
@@ -18,8 +18,8 @@ function buildFilesList (path, links) {
const rows = links.map((link) => {
let row = [
`<div class="ipfs-icon ipfs-_blank">&nbsp;</div>`,
`<a href="${path}${path.endsWith('/') ? '' : '/'}${link.name}">${link.name}</a>`,
filesize(link.size)
`<a href="${path}${path.endsWith('/') ? '' : '/'}${link.Name}">${link.Name}</a>`,
filesize(link.Tsize)
]

row = row.map((cell) => `<td>${cell}</td>`).join('')
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -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))
})
})
27 changes: 9 additions & 18 deletions src/resolver.js
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 3 additions & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -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'),
10 changes: 5 additions & 5 deletions test/resolver.spec.js
Original file line number Diff line number Diff line change
@@ -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'
})
})
})