This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add HTTP Gateway to the js-ipfs daemon License: MIT Signed-off-by: Yahya <ya7yaz@gmail.com> * apply remaining CR * chore: update deps * test: fix failing gateway tests with this one simple trick! (#1006) * fix failing tests with this one simple trick! * adding files to make tests self-contained * changes requested by VictorBjelkholm #968 * chore: fix circle CI chrome build (#1008) (#1009) Thanks to mkg20001
- Loading branch information
Showing
51 changed files
with
738 additions
and
23 deletions.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'use strict' | ||
|
||
const filesize = require('filesize') | ||
|
||
const mainStyle = require('./style') | ||
const pathUtil = require('../utils/path') | ||
|
||
function getParentDirectoryURL (originalParts) { | ||
const parts = originalParts.slice() | ||
|
||
if (parts.length > 1) { | ||
parts.pop() | ||
} | ||
|
||
return [ '', 'ipfs' ].concat(parts).join('/') | ||
} | ||
|
||
function buildFilesList (path, links) { | ||
const rows = links.map((link) => { | ||
let row = [ | ||
`<div class="ipfs-icon ipfs-_blank"> </div>`, | ||
`<a href="${pathUtil.joinURLParts(path, link.name)}">${link.name}</a>`, | ||
filesize(link.size) | ||
] | ||
|
||
row = row.map((cell) => `<td>${cell}</td>`).join('') | ||
|
||
return `<tr>${row}</tr>` | ||
}) | ||
|
||
return rows.join('') | ||
} | ||
|
||
function buildTable (path, links) { | ||
const parts = pathUtil.splitPath(path) | ||
const parentDirectoryURL = getParentDirectoryURL(parts) | ||
|
||
return ` | ||
<table class="table table-striped"> | ||
<tbody> | ||
<tr> | ||
<td class="narrow"> | ||
<div class="ipfs-icon ipfs-_blank"> </div> | ||
</td> | ||
<td class="padding"> | ||
<a href="${parentDirectoryURL}">..</a> | ||
</td> | ||
<td></td> | ||
</tr> | ||
${buildFilesList(path, links)} | ||
</tbody> | ||
</table> | ||
` | ||
} | ||
|
||
function render (path, links) { | ||
return ` | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>${path}</title> | ||
<style>${mainStyle}</style> | ||
</head> | ||
<body> | ||
<div id="header" class="row"> | ||
<div class="col-xs-2"> | ||
<div id="logo" class="ipfs-logo"></div> | ||
</div> | ||
</div> | ||
<br> | ||
<div class="col-xs-12"> | ||
<div class="panel panel-default"> | ||
<div class="panel-heading"> | ||
<strong>Index of ${path}</strong> | ||
</div> | ||
${buildTable(path, links)} | ||
</div> | ||
</div> | ||
</body> | ||
</html> | ||
` | ||
} | ||
|
||
exports = module.exports | ||
exports.render = render |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
'use strict' | ||
|
||
const mh = require('multihashes') | ||
const promisify = require('promisify-es6') | ||
const reduce = require('async/reduce') | ||
const CID = require('cids') | ||
const Unixfs = require('ipfs-unixfs') | ||
const debug = require('debug') | ||
const log = debug('jsipfs:http-gateway:resolver') | ||
log.error = debug('jsipfs:http-gateway:resolver:error') | ||
|
||
const dirView = require('./dir-view') | ||
const pathUtil = require('./utils/path') | ||
|
||
function getIndexFiles (links) { | ||
const INDEX_HTML_FILES = [ | ||
'index.html', | ||
'index.htm', | ||
'index.shtml' | ||
] | ||
|
||
return links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1) | ||
} | ||
|
||
const resolveDirectory = promisify((ipfs, path, multihash, callback) => { | ||
mh.validate(mh.fromB58String(multihash)) | ||
|
||
ipfs.object.get(multihash, { enc: 'base58' }, (err, dagNode) => { | ||
if (err) { return callback(err) } | ||
|
||
const indexFiles = getIndexFiles(dagNode.links) | ||
|
||
if (indexFiles.length > 0) { | ||
return callback(null, indexFiles) | ||
} | ||
|
||
return callback(null, dirView.render(path, dagNode.links)) | ||
}) | ||
}) | ||
|
||
const resolveMultihash = promisify((ipfs, path, callback) => { | ||
const parts = pathUtil.splitPath(path) | ||
let firstMultihash = parts.shift() | ||
let currentCid | ||
|
||
reduce(parts, firstMultihash, (memo, item, next) => { | ||
try { | ||
currentCid = new CID(mh.fromB58String(memo)) | ||
} catch (err) { | ||
return next(err) | ||
} | ||
|
||
log('memo: ', memo) | ||
log('item: ', item) | ||
|
||
ipfs.dag.get(currentCid, (err, result) => { | ||
if (err) { return next(err) } | ||
|
||
let dagNode = result.value | ||
// find multihash of requested named-file in current dagNode's links | ||
let multihashOfNextFile | ||
let nextFileName = item | ||
|
||
const links = dagNode.links | ||
|
||
for (let link of links) { | ||
if (link.name === nextFileName) { | ||
// found multihash of requested named-file | ||
multihashOfNextFile = mh.toB58String(link.multihash) | ||
log('found multihash: ', multihashOfNextFile) | ||
break | ||
} | ||
} | ||
|
||
if (!multihashOfNextFile) { | ||
return next(new Error(`no link named "${nextFileName}" under ${memo}`)) | ||
} | ||
|
||
next(null, multihashOfNextFile) | ||
}) | ||
}, (err, result) => { | ||
if (err) { return callback(err) } | ||
|
||
let cid | ||
try { | ||
cid = new CID(mh.fromB58String(result)) | ||
} catch (err) { | ||
return callback(err) | ||
} | ||
|
||
ipfs.dag.get(cid, (err, dagResult) => { | ||
if (err) return callback(err) | ||
|
||
let dagDataObj = Unixfs.unmarshal(dagResult.value.data) | ||
if (dagDataObj.type === 'directory') { | ||
let isDirErr = new Error('This dag node is a directory') | ||
// add memo (last multihash) as a fileName so it can be used by resolveDirectory | ||
isDirErr.fileName = result | ||
return callback(isDirErr) | ||
} | ||
|
||
callback(null, { multihash: result }) | ||
}) | ||
}) | ||
}) | ||
|
||
module.exports = { | ||
resolveDirectory: resolveDirectory, | ||
resolveMultihash: resolveMultihash | ||
} |
Oops, something went wrong.